int sem_init() { int ret, semid; int status = 0; semid = creat_sem(1, &status); if (semid == -1) return -1; if (status == 3) return semid; ret = semctl(semid, 0, SETVAL, 1); if (ret == -1) { perror("semctl"); return -1; } return semid; }
int main (int argc, char ** argv) { long i , k , temp; long * ptz ; int p1 , p2, p3, p4; int ipc_key, ipc_result ; int mutex; // id du semaphore if (argc<2) exit(1) ; ipc_key = atoi(argv[1]) ; ipc_result = shmget(ipc_key, 0 , 0) ; // ouverture mémoire partagée ptz = (long *) shmat(ipc_result, 0, SHMEM_RIGHTS) ; // attachement mémoire partagée // creation du semaphore mutex if((mutex = creat_sem(ipc_key+1,1)) == -1) erreur("erreur creation semaphore"); if ((p1 = fork()) == 0) { for(i=0;i<10;i++) { P(mutex); //__________________________ temp = * ptz; temp++; sleep( rand() % 2 ); // attente aléatoire *ptz = temp; ; V(mutex); //________________________________ //(*ptz) ++ ; } exit(0) ; } if ((p2 = fork()) == 0) { for(i=0;i<10;i++) { P(mutex); //__________________________ temp = * ptz; temp++; sleep( rand() % 2 ); // attente aléatoire *ptz = temp; V(mutex); //________________________________ } exit(0) ; } if ((p3 = fork()) == 0) { for(i=0;i<10;i++) { P(mutex); //__________________________ temp = * ptz; temp++; sleep( rand() % 2 ); // attente aléatoire *ptz = temp; V(mutex); //________________________________ //(*ptz) ++ ; } exit(0) ; } if ((p4 = fork()) == 0) { for(i=0;i<10;i++) { P(mutex); //__________________________ temp = * ptz; temp++; sleep( rand() % 2 ); // attente aléatoire *ptz = temp; ; //(*ptz) ++ ; V(mutex); //________________________________ } exit(0) ; } // while(wait(0) != -1); si le père attend que tous ses fils soient terminés // suppression du semaphore //del_sem(mutex); printf("\n Arret processus pid = %d compteur ( %d ) \n\n", getpid(), *ptz) ; exit(0) ; }
int main() { key_t key; int shmid, semid, msqid; char *shm; char data[] = "this is server"; struct shmid_ds buf1; /*用于删除共享内存*/ struct msqid_ds buf2; /*用于删除消息队列*/ struct msg_form msg; /*消息队列用于通知对方更新了共享内存*/ // 获取key值 if((key = ftok(".", 'z')) < 0) { perror("ftok error"); exit(1); } // 创建共享内存 if((shmid = shmget(key, 1024, IPC_CREAT|0666)) == -1) { perror("Create Shared Memory Error"); exit(1); } // 连接共享内存 shm = (char*)shmat(shmid, 0, 0); if((int)shm == -1) { perror("Attach Shared Memory Error"); exit(1); } // 创建消息队列 if ((msqid = msgget(key, IPC_CREAT|0777)) == -1) { perror("msgget error"); exit(1); } // 创建信号量 semid = creat_sem(key); // 读数据 while(1) { msgrcv(msqid, &msg, 1, 888, 0); /*读取类型为888的消息*/ if(msg.mtext == 'q') /*quit - 跳出循环*/ break; if(msg.mtext == 'r') /*read - 读共享内存*/ { sem_p(semid); printf("%s\n",shm); sem_v(semid); } } // 断开连接 shmdt(shm); /*删除共享内存、消息队列、信号量*/ shmctl(shmid, IPC_RMID, &buf1); msgctl(msqid, IPC_RMID, &buf2); del_sem(semid); return 0; }
int main(void) { int semid, shmid; char buf[STR_LEN] = {0}; void *pshm_addr = NULL; shm_t * pshm = NULL; int i = 0; /*获取信号量标识符*/ semid = creat_sem( ); if(semid == -1) { printf("%s : semid = %d!\n", __func__, semid); return -1; } /*获取共享内存标识符*/ shmid = shmget(ftok(FTOK_FILE,111), sizeof(shm_t), IPC_CREAT|0666); if(shmid == -1) { printf("%s: shmid = %d!\n", __func__, shmid); return -1; } /*当前进程连接该共享内存段*/ pshm_addr = shmat(shmid, 0, 0); if(pshm_addr == (void *)-1) { printf("%s : pshm_addr = (void*)-1!\n",__func__); return -1; } pshm = pshm_addr; printf("read process : semid is %d, shmid is %d!\n",semid, shmid); for(; ;) { /*占用信号量,p操作*/ if(sem_p(semid)) { printf("%s : sem_p failed !\n",__func__); return -1; } printf("enter write process!\n"); printf("enter something, end with end >\n"); fgets(buf, STR_LEN, stdin); pshm->num = i++; strcpy(pshm->buf, buf); printf("leave write process!\n\n"); /*释放信号量,v操作*/ if(sem_v(semid)) { printf("%s : sem_v failed!\n",__func__); return -1; } if(!strncmp(pshm->buf , "end", 3)) break; sleep(2); } /*进程和共享内存脱离*/ if(shmdt(pshm_addr) == -1) { printf("%s : shmdt is failed!\n",__func__); return -1; } printf(" Good Bye! \n"); return 0; }