21 semaphore 3
来源:互联网 发布:windows不激活 编辑:程序博客网 时间:2024/05/18 19:35
1. 示例,多进程间信号灯通信:
需要先执行Client程序,再执行Server。
Server端程序
#include "stdio.h"#include "stdlib.h"#include "pthread.h"//#include "semaphore.h"#include "sys/ipc.h"#include "sys/sem.h"#include "unistd.h"//sem_t sem;union semun { int val; /* Value for SETVAL*/struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ unsigned short *array; /* Array for GETALL, SETALL */ struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */};int semid;union semun mysemun;struct sembuf mysembuf;int main()//main thread code{ int i; int key; key=ftok("./a.c",'a'); if(key < 0) {printf("creat key failure\n");return -1; } printf("creat key sucess \n"); semid=semget(key,3,IPC_CREAT | 0777);//IPC_EXCL if(semid < 0) {printf("creat semaphore failure\n");return -2; } printf("creat semaphore sucess,semid=%d\n",semid); system("ipcs -s"); //init sem //mysemun.val=0; //semctl(semid,0,SETVAL,mysemun); mysembuf.sem_num=0; mysembuf.sem_flg=0; for(i=0;i<10;i++)//first {usleep(100);printf("this is main fun i=%d\n",i); } //V mysembuf.sem_op=1; semop(semid,&mysembuf,1); while(1); return 0;}
执行结果:
alex@alex-VirtualBox:~/Share/process/twenty-one$ ./servercreat key sucesscreat semaphore sucess,semid=65537------ Semaphore Arrays --------key semid owner perms nsems0x00000000 32768 alex 777 30x6101657b 65537 alex 777 3this is main fun i=0this is main fun i=1this is main fun i=2this is main fun i=3this is main fun i=4this is main fun i=5this is main fun i=6this is main fun i=7this is main fun i=8this is main fun i=9
Client端程序
#include "stdio.h"#include "stdlib.h"#include "pthread.h"//#include "semaphore.h"#include "sys/ipc.h"#include "sys/sem.h"#include "unistd.h"//sem_t sem;union semun { int val; /* Value for SETVAL*/struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ unsigned short *array; /* Array for GETALL, SETALL */ struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */};int semid;union semun mysemun;struct sembuf mysembuf;int main()//main thread code{ int i; int key; key=ftok("./a.c",'a'); if(key < 0) {printf("creat key failure\n");return -1; } printf("creat key sucess \n"); semid=semget(key,3,IPC_CREAT | 0777);//IPC_EXCL if(semid < 0) {printf("creat semaphore failure\n");return -2; } printf("creat semaphore sucess,semid=%d\n",semid); system("ipcs -s"); //init sem mysemun.val=0; semctl(semid,0,SETVAL,mysemun); mysembuf.sem_num=0; mysembuf.sem_flg=0; //P wait mysembuf.sem_op=-1; semop(semid,&mysembuf,1); for(i=0;i<10;i++)//second {usleep(100);printf("this is main fun i=%d\n",i); } //V //mysembuf.sem_op=1; //semop(semid,&mysembuf,1); while(1); return 0;}
执行结果:
alex@alex-VirtualBox:~/Share/process/twenty-one$ ./clientcreat key sucesscreat semaphore sucess,semid=65537------ Semaphore Arrays --------key semid owner perms nsems0x00000000 32768 alex 777 30x6101657b 65537 alex 777 3this is main fun i=0this is main fun i=1this is main fun i=2this is main fun i=3this is main fun i=4this is main fun i=5this is main fun i=6this is main fun i=7this is main fun i=8this is main fun i=9
0 0
- 21 semaphore 3
- 21-Semaphore应用
- Semaphore
- semaphore
- semaphore
- Semaphore
- Semaphore
- semaphore
- Semaphore
- semaphore
- Semaphore
- Semaphore
- Semaphore
- semaphore
- Semaphore
- Semaphore
- semaphore
- Semaphore
- Android--多线程之Handler
- P1125 笨小猴
- SRM 531 SutTheBumbers
- Spring的三大核心接口——BeanFactory、ApplicationContext、WebApplicationContext
- PHP安装 (windows)
- 21 semaphore 3
- JVM内存模型
- KiCad设计PCB-4-使用KiCad制作第一个元器件的封装
- 浅谈谷歌全新设计理念Material design
- 配置Spring后Tomcat启动错误——已解决
- SRM 514 MagicalGirlLevelThreeDivTwo
- Cookie和Session的作用,区别和各自的应用范围,cookie、Session工作原理
- PAT甲级1056
- 数列求和 (sdut oj)