unix 共享储存及信号量例程
例程说明:执行server 两个client 在server 储存 内容两个client 进行读取内容三个进行程使用信号量进行同步 server.c ----------------------------------------------------- #include #include #include #include #include #include #include #include #include "sem_shm.h" int main(int argc,char** argv) { int ret; int semque_id; //打开一个信号集 semque_id = open_semque(); //初始化信号量 set_sem(semque_id, 1); ret = semctl(semque_id, 0, GETVAL); printf("信号集中第一个信号量的值为:%d\n",ret); fputs("设置共享内存\n",stdout); //打开一个共享储存 int id_shm = open_shm(); int count = 3; while(count--) { ret = semctl(semque_id, 0, GETVAL); printf("信号集中第一个信号量的值为:%d\n",ret); v_sem(semque_id); fputs("执行V操作\n",stdout); ret = semctl(semque_id, 0, GETVAL); printf("信号集中第一个信号量的值为:%d\n",ret); fputs("输入发送内容\n",stdout); char buf[100]; memset(buf,100,0); scanf("%s",buf); //获取共享储存第一个有效地址 char * addr = (char *)shmat(id_shm, 0, 0); if((void*)addr == (void*)-1) { perror("shmat\n"); exit(1); } memset(addr,100,0); strncpy(addr,buf,100); fputs("内容发送完成\n",stdout); printf("\n"); fputs("执行P操作\n",stdout); p_sem(semque_id); sleep(1); } fputs("是否删除共享内存退出 y or n\n",stdout); if(getchar()=='y') { shmctl(id_shm, IPC_RMID, NULL); } return 0; } ----------------------------------------------------- client.c ----------------------------------------------------- #include #include #include #include #include #include #include #include #include "sem_shm.h" int main(int argc,char** argv) { int ret; int semque_id = open_semque(); int shm_id = open_shm(); ret = semctl(semque_id, 0, GETVAL); printf("信号集中第一个信号量的值为:%d\n",ret); while(1) { fputs("执行操作V\n",stdout); v_sem(semque_id); ret = semctl(semque_id, 0, GETVAL); printf("信号集中第一个信号量的值为:%d\n",ret); fputs("读取服务器内容\n",stdout); char * addr = (char *)shmat(shm_id, 0, SHM_RDONLY); if((void*)addr == (void*)-1) { perror("shmat\n"); exit(1); } fflush(stdout); fputs(addr,stdout); printf("\n"); fputs("执行操作P\n",stdout); p_sem(semque_id); ret = semctl(semque_id, 0, GETVAL); printf("信号集中第一个信号量的值为:%d\n",ret); sleep(1); } //semctl(semque_id, 0, IPC_RMID); return 0; } ----------------------------------------------------- sem_shm.c ----------------------------------------------------- #include "sem_shm.h" #include #include #include #include #include #include #include #include int open_semque(void) { key_t key = ftok("./",100); if(key == -1) { perror("ftok\n"); exit(1); } int semque_id = semget(key, 1,IPC_CREAT|0666); if(semque_id == -1) { perror("msgget\n"); exit(1); } return semque_id; } void set_sem(int semque_id, int val) { union semun sem_union; sem_union.val = val; if(semctl(semque_id, 0, SETVAL, sem_union) == -1) { perror("semctl_set\n"); exit(1); } } int open_shm() { key_t key = ftok("../",100); if(key == -1) { perror("ftok\n"); exit(1); } //0666表示user group other 三种用户都没有执行权限 int id_shm = shmget(key, SHM_SIZE, IPC_CREAT|0666); if(-1 == id_shm) { perror("shmget\n"); exit(1); } return id_shm; } void p_sem(int semque_id) { struct sembuf arry[1]; arry[0].sem_num = 0; arry[0].sem_op = 1; //arry[0].sem_flg = SEM_UNDO; int ret = semop(semque_id, arry, 1); if(ret) { perror("semop\n"); exit(1); } } void v_sem(int semque_id) { struct sembuf arry[1]; arry[0].sem_num = 0; arry[0].sem_op = -1; //arry[0].sem_flg = IPC_NOWAIT; //arry[0].sem_flg = SEM_UNDO; int ret = semop(semque_id, arry, 1); if(ret) { perror("semop\n"); exit(1); } } ----------------------------------------------------- sem_shm.h ----------------------------------------------------- #ifndef SEM_SHM_H #define SEM_SHM_H #include #include #include #define SHM_SIZE 100 int open_semque(void); void set_sem(int, int); void v_sem(int); void p_sem(int); int open_shm(); union semun { int val; struct semid_ds buf; unsigned short *array; }; #endif // end SEM_SHM_H ----------------------------------------------------- makefile ----------------------------------------------------- CC=gcc Target0=server Target1=client warn=-Wall #################### .PHONY: all clean # command: make all or make clean clean: rm -f $(obj) main server client *~ *gch *.o ################### all: $(Target0) $(Target1) $(Target0):server.o sem_shm.o $(CC) -o $@ $^ ${warn} server.o:server.c $(CC) -c $< ${warn} $(Target1):client.o sem_shm.o $(CC) -o $@ $^ ${warn} client.o:client.c $(CC) -c $< ${warn} sem_shm.o:sem_shm.c $(CC) -c $< ${warn}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~