2
3#include<sys/sem.h>
4#include<sys/ipc.h>
5
6#define SEGSIZE 1024
7#define READTIME 1
8
9union semum
10{
11int val
12struct semid_ds *buf
13unsigned short *array
14}arg
15
16/* 创建信号量 */
17int sem_creat(key_t key)
18{
19union semun sem
20int semid
21sem.val = 0
22semid = semget(key, 1, IPC_CREAT | 0666)
23
24if (semid == -1)
25{
26printf("Create semaphore error\n")
27exit(-1)
28}
29
30semctl(semid, 0, SETVAL, sem)
31
32return semid
33}
34
35/* 删除信号量*/
36int del_sem(int semid)
37{
38union semun sem
39sem.val = 0
40semctl(semid, 0, IPC_RMID, sem)
41}
42
43/* 信号量的P操作,使得信号量的值加1 */
44int p(int semid)
45{
46struct sembuf sops = {0,
47 +1,
48 IPC_NOWAIT
49 }
50
51return (semop(semid, &sops, 1))
52}
53
54/* 信号量的v操作,使得信号量的值减1 */
55int v(int semid)
56{
57struct sembuf sops = {0,
58 -1,
59 IPC_NOWAIT
60 }
61
62return (semop(semid, &sops, 1))
63}
64
65/* server主程序 */
66int main(int argc, char **argv)
67{
68key_tkey
69int shmid, semid
70char *shm
71char msg[7] = "-data-"
72char i
73struct semid_ds buf
74
75key = ftok("/", 0)
76shmid = shmget(key, SEGSIZE, IPC_CREAT|0604)
77
78if shmid == -1)
79{
80printf(" create shared memory error\n")
81return -1
82}
83
84shm = (char *)shmat(shmid, 0, 0)
85if (-1 == (int)shm)
86{
87printf(" attach shared memory error\n")
88return -1
89}
90
91semid = sem_creat(key)
92
93for (i = 0i <= 3i++)
94{
95sleep(1)
96p(semid)
97sleep(READTIME)
98msg[5] = '0' + i
99memcpy(shm,msg,sizeof(msg))
100sleep(58)
101v(semid)
102}
103
104shmdt(shm)
105
106shmctl(shmid,IPC_RMID,&buf)
107
108del_sem(semid)
109
110return 0
111
112}
113
114
115
116
117
118
119
120
121
完整程序要花不少时间调试的,我这里给你一个实现PV操作的程序吧,5个人抢3个位子,这个你们上课应该会讲的,通过几个pv操作就可以实现,只要把我这里写的pv操作的代码用上就可以了。程序是在linux系统下调试的。pv操作分别对应下面的up和down(我们老师习惯用这种称呼,于是就在代码中也这么用了)#include <stdio.h>#include <signal.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/mman.h>#include <sys/times.h>#include <fcntl.h>#include <stdlib.h>#include <sys/sem.h> //include header file for semaphore #define FILE_SIZE 11#define NO_PROC 10 int DelayCount = 0int readerID = 0int writerID = 0char* shared_bufferint Delay100ms = 0int* shared_buffer2////////Variables for semaphoreoperations/////int semid// the followings are not necessary to definehere, but just for conveniencekey_t semkeystruct sembuf operation[1]short sarray[2]int ret_val/////////////////////////////////////////////// void init_sem(){ int*rc //getkey semkey=ftok("/dev/null",12)if( semkey == (key_t) -1 ) { printf("main:ftok() failed\n") exit(-1)} printf("semkeygenerated\n") //getsemaphore set semid= semget( semkey, 2, 0666 | IPC_CREAT | IPC_EXCL )if( semid == -1 ) { printf("main: semget()failed\n")exit(-1)} //setthe two semaphores to 1sarray[0]=1sarray[1]=1 ret_val=semctl(semid,1, SETALL, sarray)if(ret_val==-1) { printf("main:semctlerror\n") exit(-1)} printf("ddddd\n") //setreader count rc=(int*)(&shared_buffer[FILE_SIZE])//printf("%d\n",*shared_buffer2)*rc=0printf("RC:%d\n",*rc)} void down(int i) //i=0, 1{ operation[0].sem_num=ioperation[0].sem_op=-1operation[0].sem_flg=0ret_val=semop(semid,operation,1)} void up(int i){ operation[0].sem_num=ioperation[0].sem_op=1operation[0].sem_flg=0ret_val=semop(semid,operation,1)}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)