| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 510 人关注过本帖
标题:高手清指点,如何用mutex解决这个程序的同步问题
只看楼主 加入收藏
phynico
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-4-10
收藏
 问题点数:0 回复次数:2 
高手清指点,如何用mutex解决这个程序的同步问题

#include <types.h>
#include <lib.h>
#include <test.h>
#include <thread.h>
#include <synch.h>

#define NADDERS 10 /* the number of adder threads */
#define NADDS 10000 /* the number of overall increments to perform */

/*
* **********************************************************************
* Declare the counter variable that all the adder() threads increment
*
* Declaring it "volatile" instructs the compiler to always (re)read the
* variable from memory and not optimise by removing memory references
* and re-using the content of a register.
*/
volatile unsigned long int counter;


/*
* Declare an array of adder counters to count per-thread
* increments. These are used for printing statistics.
*/
unsigned long int adder_counters[NADDERS];


/* We use a semaphore to wait for adder() threads to finish */
struct semaphore *finished;


/*
* **********************************************************************
* ADD YOUR OWN VARIABLES HERE AS NEEDED
* **********************************************************************
*/

/*
* adder()
*
* Each adder thread simply keeps incrementing the counter until we
* hit the max value.
*
* **********************************************************************
* YOU NEED TO INSERT SYNCHRONISATION PRIMITIVES APPROPRIATELY
* TO ENSURE COUNTING IS CORRECTLY PERFORMED.
* **********************************************************************
*
* You should not re-write the existing code.
*
* * Only the correct number of increments are performed
* * Ensure x+1 == x+1
* * Ensure that the statistics kept match the number of increments
* * performed.
*
*
*/

void adder(void * unusedpointer, unsigned long addernumber)
{
unsigned long int a,b;
int flag = 1;

/*
* Avoid unused variable warnings.
*/
(void) unusedpointer; /* remove this line if variable is used */

while (flag) {
/* loop doing increments until we achieve the overall number
of increments */

a = counter;

if (a < NADDS) {

counter = counter + 1;

b = counter;


/* count the number of increments we perform for statistics */
adder_counters[addernumber]++;

/* check we are getting sane results */
if (a + 1 != b) {
kprintf("In thread %ld, %ld + 1 == %ld?\n", addernumber, a, b) ;

}
}
else {
flag = 0;
}
}

/* signal the main thread we have finished and then exit */
V(finished);

thread_exit();
}


/*
* math()
*
* This function:
*
* * Initialises the counter variables
* * Creates a semaphore to wait for adder threads to complete
* * Starts the define number of adder threads
* * waits, prints statistics, cleans up, and exits
*/
int maths (int nargs,
char ** args)
{
int index, error;
unsigned long int sum;

/*
* Avoid unused variable warnings.
*/

(void) nargs;
(void) args;

/* create a semaphore to allow main thread to wait on workers */

finished = sem_create("finished", 0);

if (finished == NULL) {
panic("maths: sem create failed");
}

/*
* **********************************************************************
* INSERT ANY INITIALISATION CODE YOU REQUIRE HERE
* **********************************************************************
*/


/*
* Start NADDERS adder() threads.
*/


kprintf("Starting %d adder threads\n", NADDERS);

for (index = 0; index < NADDERS; index++) {

error = thread_fork("adder thread",
NULL,
index,
adder,
NULL
);

/*
* panic() on error.
*/

if (error) {

panic("adder: thread_fork failed: %s\n",
strerror(error)
);
}
}

/* Wait until the adder threads complete */

for (index = 0; index < NADDERS; index++) {
P(finished);
}

kprintf("Adder threads performed %ld adds\n", counter);

/* Print out some statistics */
sum = 0;
for (index = 0; index < NADDERS; index++) {
sum += adder_counters[index];
kprintf("Adder %d performed %ld increments.\n",
index, adder_counters[index]);
}
kprintf("The adders performed %ld increments overall\n", sum);

/*
* **********************************************************************
* INSERT ANY CLEANUP CODE YOU REQUIRE HERE
* **********************************************************************
*/


/* clean up the semaphore we allocated earlier */
sem_destroy(finished);
return 0;
}

搜索更多相关主题的帖子: mutex 
2007-04-10 18:53
alading664
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-1-25
收藏
得分:0 

互斥信号量,可以采用UC/OS的框架……时间来不及了

2007-04-13 00:09
moonwalker
Rank: 1
等 级:新手上路
威 望:1
帖 子:909
专家分:2
注 册:2007-3-2
收藏
得分:0 
不懂!

“视频教程网”免费提供教学资源
C不限制你的自由!
条件是自己承担滥用自由的恶果!
2007-04-13 00:21
快速回复:高手清指点,如何用mutex解决这个程序的同步问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011849 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved