- 论坛徽章:
- 0
|
本帖最后由 cryingbaby304 于 2011-09-06 08:37 编辑
执行下诉代码,父进程counter = 2后 就死锁了,而使用apue的程序清单15-3中用管道实现的另一套TELL,WAIT函数就正常执行,实在搞不清为什么,求解答,谢谢了!
如果需要可以贴出另外的一套TELL,WAIT函数
- #include "apue.h"
- #include <fcntl.h>
- #include <sys/mman.h>
- #include "list10_17.c"
- #define NLOOPS 1000
- #define SIZE sizeof(long) /* size of shared memory area */
- static int
- update(long *ptr)
- {
- return((*ptr)++); /* return value before increment */
- }
- int
- main(void)
- {
- int fd, i, counter;
- pid_t pid;
- void *area;
- if ((fd = open("/dev/zero", O_RDWR)) < 0)
- err_sys("open error");
- if ((area = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
- fd, 0)) == MAP_FAILED)
- err_sys("mmap error");
- close(fd); /* can close /dev/zero now that it's mapped */
- TELL_WAIT();
- if ((pid = fork()) < 0) {
- err_sys("fork error");
- } else if (pid > 0) { /* parent */
- for (i = 0; i < NLOOPS; i += 2) {
- if ((counter = update((long *)area)) != i)
- err_quit("parent: expected %d, got %d", i, counter);
- printf("%d\n",counter);//the added new line
- TELL_CHILD(pid);
- WAIT_CHILD();
- }
- } else { /* child */
- for (i = 1; i < NLOOPS + 1; i += 2) {
- WAIT_PARENT();
- if ((counter = update((long *)area)) != i)
- err_quit("child: expected %d, got %d", i, counter);
- TELL_PARENT(getppid());
- }
- }
- exit(0);
- }
复制代码
- //list10_17.c
- #include "apue.h"
- static volatile sig_atomic_t sigflag;
- static sigset_t newmask,oldmask,zeromask;
- static void sig_usr(int signo)
- {
- 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);
- if(sigprocmask(SIG_BLOCK,&newmask,&oldmask) < 0)
- err_sys("SIG_BLOCK error");
- }
- void TELL_PARENT(pid_t pid)
- {
- kill(pid,SIGUSR2);
- }
- void WAIT_PARENT(void)
- {
- while(sigflag == 0)
- sigsuspend(&zeromask);
- sigflag = 0;
- if(sigprocmask(SIG_SETMASK,&oldmask,NULL) < 0)
- err_sys("SIG_SETMASK error");
- }
- void TELL_CHILD(pid_t pid)
- {
- kill(pid,SIGUSR1);
- }
- void WAIT_CHILD(void)
- {
- while(sigflag == 0)
- sigsuspend(&zeromask);
- sigflag = 0;
- if(sigprocmask(SIG_SETMASK,&oldmask,NULL) < 0)
- err_sys("SIG_SETMASK error");
- }
复制代码 |
|