Chinaunix首页 | 论坛 | 博客
  • 博客访问: 968661
  • 博文数量: 200
  • 博客积分: 5011
  • 博客等级: 大校
  • 技术积分: 2479
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-27 15:07
文章分类

全部博文(200)

文章存档

2009年(12)

2008年(190)

我的朋友

分类:

2008-11-26 17:59:51

8.9 race conditions

Race condition发生的情况:程序的输出结果会收到程序内部若干个进程的不同执行顺序的影响时,就会发生race condition

因此,为了合理安排各个进程的执行顺序,我们需要一种同步机制。类似于如下的函数:

1TELL_WAIT()对同步机制的初始化

2WAIT_PARENT()等待父亲完成任务

3. TELL_CHILD(pid) 告诉儿子父亲已经完成了任务

4. WAIT_CHILD() 等待儿子完成任务

5. TELL_PARENT(pid) 告诉父亲,儿子已经完成了任务

那么具体怎么实现这几个函数,就是不同的进程之间IPC的机制决定了。

可以采用的机制包括:

1SIGNAL机制

2PIPE机制

如下是一段采用了上述函数的代码片断:

#include  "apue.h"
 
TELL_WAIT();    /* set things up for TELL_xxx & WAIT_xxx */
 
if ((pid = fork()) < 0) {
    err_sys("fork error");
} else if (pid == 0) {            /* child */
 
    /* child does whatever is necessary ... */
 
    TELL_PARENT(getppid());     /* tell parent we're done */
    WAIT_PARENT();              /* and wait for parent */
 
    /* and the child continues on its way ... */
 
    exit(0);
}
 
/* parent does whatever is necessary ... */
 
TELL_CHILD(pid);            /* tell child we're done */
WAIT_CHILD();               /* and wait for child */
 
/* and the parent continues on its way ... */
 
exit(0);
阅读(1248) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~