您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
- 例程说明:
- 执行server 两个client
- 在server 储存 内容
- 两个client 进行读取内容
- 三个进行程使用信号量进行同步
- server.c
- -----------------------------------------------------
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/sem.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #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 <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <stdlib.h>
- #include <sys/shm.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #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 <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <stdlib.h>
- #include <sys/shm.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- 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 <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #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}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。