zhangmeng
2024-01-18 8fc23a3bb9f49e88478a2505fa7dee434ec50c16
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <linux/futex.h>
#include <sys/time.h>
#include "usg_common.h"
#include <sys/mman.h>
#include <sys/stat.h>        /* For mode constants */
#include <fcntl.h>           /* For O_* constants */
#include "sem_util.h"
 
 
 
 
 
#include "sem_util.h"
 
int _get(key_t key, unsigned int value) {
// printf("==================_get===============================\n");
  int semid, perms;
 
  perms = S_IRUSR | S_IWUSR;
 
  semid = semget(key, 1, IPC_CREAT | IPC_EXCL | perms);
 
  if (semid != -1) { /* Successfully created the semaphore */
    union semun arg;
    struct sembuf sop;
 
    //logger.info("%ld: created semaphore\n", (long)getpid());
 
    arg.val = 0; /* So initialize it to 0 */
    if (semctl(semid, 0, SETVAL, arg) == -1)
      err_exit(errno, "semctl 1");
    //logger.info("%ld: initialized semaphore\n", (long)getpid());
 
    /* Perform a "no-op" semaphore operation - changes sem_otime
       so other processes can see we've initialized the set. */
 
    sop.sem_num = 0; /* Operate on semaphore 0 */
    sop.sem_op = value;
    sop.sem_flg = 0;
    if (semop(semid, &sop, 1) == -1)
      err_exit(errno, "semop");
    //logger.info("%ld: completed dummy semop()\n", (long)getpid());
 
  } else { /* We didn't create the semaphore set */
 
    if (errno != EEXIST) { /* Unexpected error from semget() */
      err_exit(errno, "semget 1");
 
    } else { /* Someone else already created it */
      const int MAX_TRIES = 10;
      int j;
      union semun arg;
      struct semid_ds ds;
 
      semid = semget(key, 1, perms); /* So just get ID */
      if (semid == -1)
        err_exit(errno, "semget 2");
 
     // logger.info("%ld: got semaphore key\n", (long)getpid());
      /* Wait until another process has called semop() */
 
      arg.buf = &ds;
      for (j = 0; j < MAX_TRIES; j++) {
        //logger.info("Try %d\n", j);
        if (semctl(semid, 0, IPC_STAT, arg) == -1)
          err_exit(errno, "semctl 2");
 
        if (ds.sem_otime != 0) /* Semop() performed? */
          break;               /* Yes, quit loop */
        sleep(1);              /* If not, wait and retry */
      }
 
      if (ds.sem_otime == 0) /* Loop ran to completion! */
        err_exit(errno, "Existing semaphore not initialized");
    }
  }
  return semid;
}
 
/* Reserve semaphore (blocking), return 0 on success, or -1 with 'errno'
   set to EINTR if operation was interrupted by a signal handler */
 
/* Reserve semaphore - decrement it by 1 */
int _dec(int semId) {
  struct sembuf sops;
 
  sops.sem_num = 0;
  sops.sem_op = -1;
  sops.sem_flg = SEM_UNDO;
 
  while (semop(semId, &sops, 1) == -1)
    if (errno != EINTR) {
      // err_msg(errno, "_dec");
      return errno;
    }
 
  return 0;
}
 
int _dec_nowait(int semId) {
  struct sembuf sops;
 
  sops.sem_num = 0;
  sops.sem_op = -1;
  sops.sem_flg = IPC_NOWAIT ;
 
  while (semop(semId, &sops, 1) == -1)
    if (errno != EINTR) {
      // err_msg(errno, "_dec_nowait");
      return errno;
    }
 
  return 0;
}
 
int _dec_timeout(const int semId, const struct timespec *timeout) {
  struct sembuf sops;
 
  sops.sem_num = 0;
  sops.sem_op = -1;
  sops.sem_flg = 0;
 
  while (semtimedop(semId, &sops, 1, timeout) == -1)
    if (errno != EINTR) {
      // err_msg(errno, "_dec_timeout");
      return errno;
    }
 
  return 0;
}
 
 
/**
 * If sem_op equals 0, the value of the semaphore is checked to see whether it
 * currently equals 0. If it does, the operation completes immediately; otherwise,
 * semop() blocks until the semaphore value becomes 0.
 */
int _zero(int semId) {
// logger.debug("%d: _dec\n", semId);
  struct sembuf sops;
 
  sops.sem_num = 0;
  sops.sem_op = 0;
  sops.sem_flg = 0;
 
  while (semop(semId, &sops, 1) == -1)
    if (errno != EINTR) {
      // err_msg(errno, "_zero");
      return errno;
    }
 
  return 0;
}
 
 
int _zero_nowait(int semId) {
  struct sembuf sops;
 
  sops.sem_num = 0;
  sops.sem_op = 0;
  sops.sem_flg = IPC_NOWAIT;
 
  while (semop(semId, &sops, 1) == -1)
    if (errno != EINTR) {
      // err_msg(errno, "_zero_nowait");
      return errno;
    }
 
  return 0;
}
 
int _zero_timeout(const int semId, const struct timespec *timeout) {
  struct sembuf sops;
 
  sops.sem_num = 0;
  sops.sem_op = 0;
  sops.sem_flg = 0;
 
  while (semtimedop(semId, &sops, 1, timeout) == -1)
    if (errno != EINTR) {
      // err_msg(errno, "_zero_timeout");
      return errno;
    }
 
  return 0;
}
 
 
/* Release semaphore - increment it by 1 */
int _inc(int semId) {
  struct sembuf sops;
 
  sops.sem_num = 0;
  sops.sem_op = 1;
  sops.sem_flg = 0;
 
  int rv = semop(semId, &sops, 1);
  if (rv == -1) {
    // err_msg(errno, "_inc");
    return errno;
  }
  return 0;
}
 
void _remove(int semid) {
  union semun dummy;
  if (semctl(semid, 0, IPC_RMID, dummy) == -1)
    err_msg(errno, "_remove");
}
 
int _set(int semId, int val) {
  union semun arg;
  arg.val = val;
  if (semctl(semId, 0, SETVAL, arg) == -1) {
    err_msg(errno, "_set");
    return errno;
  }
  return 0;
}
 
 
#define KEY 0x383
 
int main() {
    int semid = _get(KEY, 1) ;
 
    if(_dec(semid) != 0)
        err_exit(errno, "_dec");
 
        printf("(%ld) 进入互斥区\n", (long) getpid());
        sleep(10);
 
    if(_inc(semid) != 0)
        err_exit(errno, "_inc");
}