race condition 父先行

本文介绍了一个简单的父子进程通信示例程序,使用了SIGUSR1和SIGUSR2信号进行同步,并展示了如何通过信号处理函数实现进程间的等待通知机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
* 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.

转载于:https://www.cnblogs.com/linuxkernel/archive/2009/08/10/1854978.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值