Linux ipc------ 信号量+共享内存编程
2014-08-29 22:44
344 查看
此程序实现没有亲缘关系的两个进程间通过共享内存进行数据通信。
同时,使用信号量保证两个进程的读写同步:发送方在写共享内存时,接收方不能读数据;接收方在读数据时,发送方不能写数据。
1、fork创建子进程
2、使用二元信号量,同步读写端
fork_shm.c
send_recv.c
fork_send.c
fork_recv.c
同时,使用信号量保证两个进程的读写同步:发送方在写共享内存时,接收方不能读数据;接收方在读数据时,发送方不能写数据。
1、fork创建子进程
2、使用二元信号量,同步读写端
fork_shm.c
#include<stdio.h> #include<sys/types.h> #include<unistd.h> #include"send_recv.h" int main(void) { printf("fork test!\n"); pid_t pid; if((pid=fork())==-1) printf("fork error"); else if(pid==0) { printf("in the child process\n"); printf("the father process's ppid is %d\n",getppid()); fork_recv(); } else { //sleep(1); printf("in the parent process\n"); printf("the son process's pid is %d\n",getpid()); fork_send(); } return 0; }
send_recv.c
#include"fork_recv.c" #include"fork_send.c" int fork_recv(); int fork_send();
fork_send.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/shm.h> #include <sys/ipc.h> #include <sys/sem.h> #include <string.h> int fork_send() { int running=1; int shid; int semid; int value; void *sharem=NULL; struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_flg = SEM_UNDO; if((semid=semget((key_t)123456,1,0666|IPC_CREAT))==-1) { perror("semget"); exit(EXIT_FAILURE); } if (semctl(semid, 0, SETVAL, 0) == -1) { printf("sem init error"); if(semctl(semid,0,IPC_RMID,0)!=0) { perror("semctl"); exit(EXIT_FAILURE); } exit(EXIT_FAILURE); } shid=shmget((key_t)654321,(size_t)2048,0600|IPC_CREAT); if(shid==-1) { perror("shmget"); exit(EXIT_FAILURE); } sharem=shmat(shid,NULL,0); if(sharem==NULL) { perror("shmat"); exit(EXIT_FAILURE); } while(running) { if((value=semctl( semid, 0, GETVAL ))==0) { printf("write data operate\n"); printf("please input something:\n"); scanf("%s",sharem); sem_b.sem_op = 1; if (semop(semid, &sem_b, 1) == -1) { fprintf(stderr, "semaphore_p failed\n"); exit(EXIT_FAILURE); } } if(strcmp(sharem,"end")==0) running--; } shmdt(sharem); return 0; }
fork_recv.c
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <string.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/sem.h> int fork_recv() { int running=1; char *shm_p=NULL; int shmid; int semid; int value; struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_flg = SEM_UNDO; if((semid=semget((key_t)123456,1,0666|IPC_CREAT))==-1) { perror("semget"); exit(EXIT_FAILURE); } shmid=shmget((key_t)654321,(size_t)2048,0600|IPC_CREAT); if(shmid==-1) { perror("shmget"); exit(EXIT_FAILURE); } shm_p=shmat(shmid,NULL,0); if(shm_p==NULL) { perror("shmat"); exit(EXIT_FAILURE); } while(running) { if((value=semctl( semid, 0, GETVAL ))==1) { printf("read data operate\n"); sem_b.sem_op = -1; if (semop(semid, &sem_b, 1) == -1) { fprintf(stderr, "semaphore_p failed\n"); exit(EXIT_FAILURE); } printf("%s\n",shm_p); } if(strcmp(shm_p,"end")==0) running--; } shmdt(shm_p); if(shmctl(shmid,IPC_RMID,0)!=0) { perror("shmctl"); exit(EXIT_FAILURE); } if(semctl(semid,0,IPC_RMID,0)!=0) { perror("semctl"); exit(EXIT_FAILURE); } return 0; }
相关文章推荐
- <转>linux操作系统编程——共享内存读写(采用信号量进行同步互斥)
- 进程间通信与同步---共享内存和信号量编程简介
- <转>linux操作系统编程——共享内存读写(采用信号量进行同步互斥)
- 多进程编程之进程间通信-共享内存,信号量和套接字
- 信号量编程心得 共享内存增加、减小心得 2010.9.10
- linux高级编程基础系列:System V进程间通信(信号量、共享内存)
- linux下C语言编程7-信号量与共享内存
- linux基础编程:进程通信之System V IPC:消息队列,信号量,共享内存
- linux操作系统编程——共享内存读写(采用信号量进行同步互斥)
- linux基础编程:进程通信之System V IPC:消息队列,信号量,共享内存
- unix/linux下的共享内存、信号量、队列信息管理
- 进程间通信(IPC)——信号量、共享内存、消息队列
- Linux进程间共享临界区“信号量”编程
- UNIX/LINUX编程学习之进程通信--共享内存
- 进程间通信--信号量+共享内存
- UNIX环境高级编程学习之第十五章进程间通信 - 系统V 共享内存使用(使用信号灯保护共享内存)
- vxworks中任务间的通信支持信号量、消息队列、管道、信号、事件、共享内存等
- vxworks中任务间的通信支持信号量、消息队列、管道、信号、事件、共享内存等
- solaris 信号量,共享内存可编辑参数含义(这些参数在solaris10下已过时)
- 清除共享内存信号量