- 论坛徽章:
- 0
|
原始程序清单:
程序清单10-17
tellwait.c
#include "apue.h"
static volatile sig_atomic_t sigflag;
/* set nonzero by signal handler */
static sigset_t newmask, oldmask, zeromask;
static void
sig_usr(int signo) /* one signal handler for SIGUSR1 and SIGUSR2 */
{
sigflag = 1;
return;
}
void
TELL_WAIT()
{
if (signal(SIGUSR1, sig_usr) == SIG_ERR)
err_sys("signal(SIGINT) error");
if (signal(SIGUSR2, sig_usr) == SIG_ERR)
err_sys("signal(SIGQUIT) 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) < 0)
err_sys("SIG_BLOCK error");
}
void
TELL_PARENT(pid_t pid)
{
kill(pid, SIGUSR2); /* tell parent 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) < 0)
err_sys("SIG_SETMASK error");
}
void
TELL_CHILD(pid_t pid)
{
kill(pid, SIGUSR1); /* tell child we're done */
}
void
WAIT_CHILD(void)
{
while (sigflag == 0)
sigsuspend(&zeromask); /* and wait for child */
sigflag = 0;
/* reset signal mask to original value */
if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
err_sys("SIG_SETMASK error");
}
|
所执行的程序清单:
#include "apue.h"
static void charatatime(char *);
int
main(void)
{
pid_t pid;
TELL_WAIT();
if ((pid = fork()) < 0) {
perror("fork error");
} else if (pid == 0) {
WAIT_PARENT(); /* parent goes first */
charatatime("output from child\n");
TELL_PARENT(getppid());
WAIT_PARENT(); /* parent goes first */
charatatime("11111111111111111111111111111111111111111111111111\n");
TELL_PARENT(getppid());
} else {
charatatime("output from parent\n");
TELL_CHILD(pid);
WAIT_CHILD();
charatatime("22222222222222222222222222222222222222222222222222\n");
TELL_CHILD(pid);
WAIT_CHILD();
}
exit(0);
}
static void
charatatime(char *str)
{
char *ptr;
int c;
setbuf(stdout, NULL); /* set unbuffered */
for (ptr = str; (c = *ptr++) != 0; )
putc(c, stdout);
}
|
程序运行结果:
./tellwait2;./tellwait2;./tellwait2
output from parent
output from child
22222222222222222222222222222222222222222222222222
11111111111111111111111111111111111111111111111111
output from parent
output from child
22222222222222222222222222222222222222222222222222
11111111111111111111111111111111111111111111111111
output from parent
output from child
22222222222222222222222222222222222222222222222222
11111111111111111111111111111111111111111111111111
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
我想不通两个地方
1.为什么要用SIGUSR1 SIGUSR2两个信号? (感觉只用一个信号就行了)
2.为什么要在TELL_WAIT中通过sigprocmask阻塞这两个信号?(感觉不阻塞也行)
所以我将代码改成下面的:
程序清单10-17
tellwait.c(修改)
#include "tellwait.h"
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)
perror("signal(SIGUSR1) error");
}
void
TELL_PARENT(pid_t pid)
{
kill(pid, SIGUSR1); /* tell parent we're done */
}
void
WAIT_PARENT(void)
{
while (sigflag == 0)
sigsuspend(&zeromask); /* and wait for parent */
sigflag = 0;
}
void
TELL_CHILD(pid_t pid)
{
kill(pid, SIGUSR1); /* tell child we're done */
}
void
WAIT_CHILD(void)
{
while (sigflag == 0)
sigsuspend(&zeromask); /* and wait for child */
sigflag = 0;
}
| 所执行的程序清单 与原先的程序清单相同)
#include "tellwait.h"
static void charatatime(char *);
int
main(void)
{
pid_t pid;
TELL_WAIT();
if ((pid = fork()) < 0) {
perror("fork error");
} else if (pid == 0) {
WAIT_PARENT(); /* parent goes first */
charatatime("output from child\n");
TELL_PARENT(getppid());
WAIT_PARENT(); /* parent goes first */
charatatime("11111111111111111111111111111111111111111111111111\n");
TELL_PARENT(getppid());
} else {
charatatime("output from parent\n");
TELL_CHILD(pid);
WAIT_CHILD();
charatatime("22222222222222222222222222222222222222222222222222\n");
TELL_CHILD(pid);
WAIT_CHILD();
}
exit(0);
}
static void
charatatime(char *str)
{
char *ptr;
int c;
setbuf(stdout, NULL); /* set unbuffered */
for (ptr = str; (c = *ptr++) != 0; )
putc(c, stdout);
}
| 执行结果 与原执行结果一样)
程序运行结果:
./a.out;./a.out;./a.out
output from parent
output from child
22222222222222222222222222222222222222222222222222
11111111111111111111111111111111111111111111111111
output from parent
output from child
22222222222222222222222222222222222222222222222222
11111111111111111111111111111111111111111111111111
output from parent
output from child
22222222222222222222222222222222222222222222222222
11111111111111111111111111111111111111111111111111
请教下我修改的程序哪些地方会出错~~~
附件为我修改的程序~
[ 本帖最后由 zxkevin 于 2007-9-24 16:01 编辑 ] |
|