linux的进程通信:信号量实例(C语言)
linux的进程通信:信号量实例(C语言)2012-01-06 06:25:49标签:信号量 进程 通信 linux C原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://hector.blog.51cto.com/4229131/758930 这篇发的很纠结,这不是我原创的代码,是同学写的,我只
·
2012-01-06 06:25:49
原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://hector.blog.51cto.com/4229131/758930
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- union semun
- {
- int val;
- struct semid_ds *buf;
- unsigned short int *array;
- struct seminfo *__buf;
- };
- int main(void)
- {
- char* buf_child[]={"this", "is", "the", "child", "process"};
- char* buf_father[]={"father", "say", "hello", "to", "child"};
- int i = 0, semid, fd;
- pid_t pid;
- struct sembuf sb; //信号量操作
- union semun sem;
- semid = semget(1000, 2, 0666 | IPC_CREAT); //申请信号量组,包含2个信号量
- sem.val = 0;
- semctl(semid, 0, SETVAL, sem); //初始化0号信号量为0
- sem.val = 1;
- semctl(semid, 1, SETVAL, sem); //初始化1号信号量为1
- fd=open("tmp",O_CREAT|O_TRUNC|O_WRONLY,0666);
- pid = fork();
- switch (pid) {
- case -1:
- perror("fork fail");
- break;
- case 0: /* child consume */
- srand((unsigned int)getpid());
- while (i < 5) {
- sb.sem_num = 1; //将1号信号量
- sb.sem_op = -1; //减1
- sb.sem_flg = sb.sem_flg & ~IPC_NOWAIT;
- semop(semid, &sb, 1);
- write(fd,buf_child[i], strlen(buf_child[i]));
- sleep(rand());
- write(fd,&" ", 1);
- i++;
- sb.sem_num = 0; //将0号信号量
- sb.sem_op = 1; //加1
- sb.sem_flg = sb.sem_flg & ~IPC_NOWAIT;
- semop(semid, &sb, 1); //操作信号量
- }
- break;
- default:/* parent production */
- srand((unsigned int)getpid());
- while (i < 5) {
- sb.sem_num = 0; //将0号信号量
- sb.sem_op = -1; //减1
- sb.sem_flg = sb.sem_flg & ~IPC_NOWAIT;
- semop(semid, &sb, 1); //操作信号量
- write(fd,buf_father[i], strlen(buf_father[i]));
- sleep(rand());
- write(fd,&" ", 1);
- i++;
- sb.sem_num = 1;
- sb.sem_op = 1;
- sb.sem_flg = sb.sem_flg & ~IPC_NOWAIT;
- semop(semid, &sb, 1);
- }
- break;
- }
- return 0;
- }
更多推荐
已为社区贡献2条内容
所有评论(0)