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}
专注于为中小企业提供成都网站设计、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业醴陵免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
附件:http://down.51cto.com/data/2362216
分享文章:unix共享储存及信号量例程
链接URL:http://pwwzsj.com/article/podgcp.html