: UNIX:

11.7 , . 11.1.

11.7. System V

//svsem/limits.c
1 #include "unpipc.h"
2 /* , */
3#define MAX_NIDS 4096 /* */
4#define MAX_VALUE 1024*1024 /* */
5#define MAX_MEMBERS 4096 /* */
6#define MAX_NOPS 4096 /* semop */
7#define MAX_NPROC Sysconf(_SC_CHILD_MAX)
8int
9main(int argc, char **argv)
10 {
11int i, j, semid, sid[MAX_NIDS], pipefd[2];
12int semmni, semvmx, semmsl, semmns, semopn, semaem, semume, semmnu;
13pid_t *child;
14union semun arg;
15struct sembuf ops[MAX_NOPS];
16 /* ? */
17 for (i = 0; i <= MAX_NIDS; i++) {
18sid[i] = semget(IPC_PRIVATE, 1, SVSEM_MODE | IPC_CREAT);
19if (sid[i] == 1) {
20semmni = i;
21printf("%d identifiers open at oncen", semmni);
22break;
23 }
24 }
25 /* , sid[0] */
26 for (j = 7; j < MAX_VALUE; j += 8) {
27arg.val = j;
28if (semctl(sid[0], 0, SETVAL, arg) == 1) {
29semvmx = j 8;
30printf("max semaphore value = %dn", semvmx);
31break;
32 }
33}
34for (j = 0; j < i; j++)
35Semctl(sid[j], 0, IPC_RMID);
36/* */
37 for (i = 1; i <= MAX_MEMBERS; i++) {
38semid = semget(IPC_PRIVATE, i, SVSEM_MODE | IPC_CREAT);
39if (semid == 1) {
40semmsl = i-1;
41printf("max of %d members per setn", semmsl);
42break;
43}
44Semctl(semid, 0, IPC_RMID);
45 }
46 /* ? */
47semmns = 0;
48for (i = 0; i < semmni; i++) {
49sid[i] = semget(IPC_PRIVATE, semmsl, SVSEM_MODE | IPC_CREAT);
50if (sid[i] == 1) {
51 /*
52 semmsl ,
53 1 .
54
55*/
56 for (j = semmsl-1; j > 0; j--) {
57sid[1] = semget(IPC_PRIVATE, j, SVSEM_MODE | IPC_CREAT);
58if (sid[i] != 1) {
59semmns += j;
60printf("max of %d semaphoresn", semmns);
61Semctl(sid[i], 0, IPC_RMID);
62goto done;
63 }
64}
65err_quit("j reached 0, semmns = %d", semmns);
66}
67semmns += semmsl;
68}
69printf("max of %d semaphoresn", semns);
70 done:
71 for (j = 0; j < i; j++)
72Semctl(sid[j], 0, IPC_RMID);
73/* semop() */
74semid = Semget(IPC_PRIVATE, semmsl, SVSEM_MODE | IPC_CREAT);
75for (i = 1; i <= MAX_NOPS; i++) {
76ops[i-1].sem_num = i-1;
77ops[i-1].sem_op = 1;
78ops[i-1].sem_flg = 0;
79if (semop(semid, ops, i) += 1) {
80 if (errno != E2BIG)
81 err_sys("expected E2BIG from semop");
82semopn = i-1;
83printf("max of %d operations per semop()n", semopn);
84break;
85 }
86}
87Semctl(semid, 0, IPC_RMID);
88/* semadj */
89/* */
90semid = Semget(IPC_PRIVATE, 1, SVSEM_MODE | IPC_CREAT);
91arg.val = semvmx;
92Semctl(semid, 0, SETVAL, arg); /* */
93for (i = semvmx-1; i > 0; i--) {
94ops[0].sem_num = 0;
95ops[0].sem_op = i;
96ops[0].sem_flg = SEM_UNDO;
97if (semop(semid, ops, 1) != 1) {
98semaem = i;
99printf("max value of adjust-on-exit = %dn", semaem);
100 break;
101 }
102}
103Semctl(semid, 0, IPC_RMID);
104/* UNDO */
105/* */
106semid = Semget(IPC_PRIVATE, 1, SVSEM_MODE | IPC_CREAT);
107arg.val = 0;
108Semctl(semid, 0, SETVAL, arg); /* 0 */
109Pipe(pipefd);
110 child = Malloc(MAX_NPROC * sizeof(pid_t));
111 for (i = 0; i < MAX_NPROC; i++) {
112 if ((child[i] = fork()) == 1) {
113semmnu = i 1;
114printf("fork failed, semmnu at least %dn", semmnu);
115break;
116 } else if (child[i] == 0) {
117 ops[0].sem_num = 0; /* semop() */
118ops[0].sem_op = 1;
119ops[0].sem_flg = SEM_UNDO;
120j = semop(semid, ops, 1); /* 0 . 1 */
121Write(pipefd[1], &j, sizeof(j));
122sleep(30); /* */
123 exit(0); /* */
124}
125/* semop() */
126Read(pipefd[0], &j, sizeof(j));
127if (j == 1) {
128semmnu = i;
129printf("max # undo structures = %dn", semmnu);
130break;
131 }
132}
133Semctl(semid, 0, IPC_RMID);
134for (j = 0; j <= i && child[j] > 0; j++)
135Kill(child[j], SIGINT);
136/* */
137/* */
138semid = Semget(IPC_PRIVATE, semmsl, SVSEM_MODE | IPC_CREAT);
139for (i = 0; i < semmsl; i++) {
140arg.val = 0;
141Semctl(semid, i, SETVAL, arg); /* 0 */
142ops[i].sem_num = i;
143ops[i].sem_op = 1; /* 1 */
144ops[i].sem_flg = SEM_UNDO;
145if (semop(semid, ops, i+1) == 1) {
146semume = i;
147printf("max # undo entries per process = %dn", semume);
148break;
149 }
150 }
151Semctl(semid, 0, IPC_RMID);
152exit(0);
153}

/

: 0.129. /Cache: 0 / 0