现在的位置: 首页 > 综合 > 正文

race condition 父先行

2012年03月16日 ⁄ 综合 ⁄ 共 2127字 ⁄ 字号 评论关闭
/*
* file8_13.c
*
* Created on: 2009-8-10
* Author: lengyuex
*/
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 4096
void err_sys (const char *,...);
void TELL_WAIT(void);
void TELL_CHILD(pid_t);
void WAIT_PARENT(void);
static void charatatime(char *);
static void err_doit(int, int, const char *, va_list);
int main(void)
{
pid_t pid;
TELL_WAIT();
if ((pid=fork()) err_sys("fork error");
else if (pid==0)
{
WAIT_PARENT();
charatatime("output from child \n");
}
else
{
charatatime("output from parent \n");
TELL_CHILD(pid);
}
exit (0);
}
static void charatatime(char *str)
{
char *ptr;
int c;
setbuf(stdout,NULL);
for (ptr=str;(c=*ptr++)!=0;)
{
putc(c,stdout);
}
}
static void
err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
char buf[MAXLINE];

vsnprintf(buf, MAXLINE, fmt, ap);
if (errnoflag)
snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
strerror(error));
strcat(buf, "\n");
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(NULL); /* flushes all stdio output streams */
}
void
err_sys(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
exit(1);
}
static volatile sig_atomic_t sigflag; /* set nonzero by sig handler */
static sigset_t newmask, oldmask, zeromask;
static void
sig_usr(int signo) /* one signal handler for SIGUSR1 and SIGUSR2 */
{
sigflag = 1;
}
void
TELL_WAIT(void)
{
if (signal(SIGUSR1, sig_usr) == SIG_ERR)
err_sys("signal(SIGUSR1) error");
if (signal(SIGUSR2, sig_usr) == SIG_ERR)
err_sys("signal(SIGUSR2) error");
sigemptyset(&zeromask);
sigemptyset(&newmask);
sigaddset(&newmask, SIGUSR1);
sigaddset(&newmask, SIGUSR2);

/*
* Block SIGUSR1 and SIGUSR2, and save current signal mask.
*/
if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) err_sys("SIG_BLOCK error");
}
void
TELL_CHILD(pid_t pid)
{
kill(pid, SIGUSR1); /* tell child we're done */
}
void
WAIT_PARENT(void)
{
while (sigflag == 0)
sigsuspend(&zeromask); /* and wait for parent */
sigflag = 0;

/*
* Reset signal mask to original value.
*/
if (sigprocmask(SIG_SETMASK, &oldmask, NULL) err_sys("SIG_SETMASK error");
}

#./a.out
    output from parent
    output from child

#./a.out;a.out;a.out;

output from parent
output from child
output from parent
output from child
output from parent
$ output from child

因为parent exit 后,下一个a.out excuted.但是有可能这时候上一个child还未执行完。这里上一个child跟下一个parent  interfere.

解决方法让parent 等一会child.

抱歉!评论已关闭.