1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| #include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/sem.h> #include <stdlib.h> #include <time.h>
#define MAXN 10
#define ALL 20
typedef struct buff { int arr[MAXN]; int finish; int curIdx; } Buff;
union semun{ int val; struct semid_ds *buf; unsigned short *arry; struct seminfo *__buf; };
int initSem(int semid, int value);
int P(int semid);
int V(int semid);
int getSem(int semid);
int deleteSem(int semid);
int main() { int shmid = shmget((key_t)9527, sizeof(Buff), IPC_CREAT | 0644); if (shmid == -1) { fprintf(stderr, "create shared memory error!\n"); return -1; } Buff* shm = (Buff*)shmat(shmid, 0, 0); shm -> finish = 0, shm -> curIdx = -1;
if ((void*)shm == (void*)-1) { fprintf(stderr, "shmat error!\n"); return -1; }
int semid_empty = semget((key_t)99999, 1, IPC_CREAT | 0644); int semid_full = semget((key_t)66666, 1, IPC_CREAT | 0644); int semid_mutex = semget((key_t)12345, 1, IPC_CREAT | 0644);
if (initSem(semid_empty, MAXN) == -1 || initSem(semid_full, 0) == -1 || initSem(semid_mutex, 1) == -1) { fprintf(stderr, "init sem error!\n"); return -1; }
int cnt = 0;
while (cnt < ALL) { P(semid_empty); P(semid_mutex);
srand((unsigned int)time(NULL)); ++ (shm -> curIdx); shm -> arr[shm -> curIdx] = cnt; ++ cnt; printf("produce cur-val = %d, curIdx = %d, empty size = %d\n", cnt, shm->curIdx, getSem(semid_empty));
V(semid_mutex); V(semid_full); }
while (!(shm -> finish)) {}
if (shmdt(shm) == -1) { fprintf(stderr, "shmdt error!\n"); return -1; }
if (shmctl(shmid, IPC_RMID, 0) == -1) { fprintf(stderr, "shmctl error!\n"); return -1; }
if (deleteSem(semid_full) == -1 || deleteSem(semid_empty) == -1 || deleteSem(semid_mutex) == -1) { fprintf(stderr, "deleteSem error!\n"); return -1; }
printf("write done!\n"); return 0; }
int initSem(int semid, int value) { union semun sem; sem.val = value;
if (semctl(semid, 0, SETVAL, sem) == -1 ) return -1; return 0; }
int P(int semid) { struct sembuf sem; sem.sem_num = 0; sem.sem_op = -1; sem.sem_flg = SEM_UNDO;
if (semop(semid, &sem, 1) == -1) return -1; return 0; }
int V(int semid) { struct sembuf sem; sem.sem_num = 0; sem.sem_op = 1; sem.sem_flg = SEM_UNDO;
if (semop(semid, &sem, 1) == -1) return -1; return 0; }
int getSem(int semid) { int ret = semctl(semid, 0, GETVAL); return ret; }
int deleteSem(int semid) { union semun sem; if (semctl(semid, 0, IPC_RMID, sem) == -1) return -1; return 0; }
|