我对所有这些都是陌生的...我正在尝试编写一个程序,它使用5个不同的进程,计数从1到100。 我必须使用信号量。 (这是用于家庭作业,不允许任何其他操作。)
我为每个信号量设置一个从1到5的值,每次我重新访问一个信号量时,它都会执行以下三件事:
这是我自己可以做的事情:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#define NUM_OF_SEM 5
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
unsigned short int *array; /* array for GETALL, SETALL */
struct seminfo *__buf; /* buffer for IPC_INFO */
};
int semid;
struct sembuf sops[1]; // Define buffer of semaphore operations
union semun semarg; // Define semaphore value holder (could also be array of semaphores)
int main() {
int i, j, k;
semid = semget(IPC_PRIVATE, NUM_OF_SEM, 0060); // Create 5 semaphores
semarg.array = (short*) malloc(NUM_OF_SEM * sizeof(short)); // Allocate array of values
for (i = 0; i < NUM_OF_SEM; i++) {
semarg.array[i] = 4 - i; // Init each cell from top - bottom
}
semctl(semid, i, SETVAL, semarg); // Set value for a set of semaphores
for (i = 0; i < NUM_OF_SEM; i++) {
if (fork() == 0) {
for (j = i + 1; j < 101; j += 5) {
for(k = 0; k < 4; k++) { // Wait 4 times
sops->sem_op = -1; // Dec by 1
sops->sem_flg = 0;
sops->sem_num = i; // Index of the current semaphore
semop(semid, sops, i);
wait(0);
}
printf("%d\n", j);
signal(i);
}
}
}
}
void signal(int this_SemIndex) {
int i = 0;
for (i = 0; i < NUM_OF_SEM; i++) {
if (i != this_SemIndex) {
// Init sops
sops->sem_op = 1; // Inc by 1
sops->sem_flg = 0;
sops->sem_num = i; // Index of the cuurent semaphore
semop(semid, sops, NUM_OF_SEM); // Operate the signal
}
}
}
输出:
1
6
11
16
21
26
31
36
41
46
51
56
61
66
71
76
81
86
91
96
2
7
12
17
22
27
32
37
42
47
52
57
2
62
67
7
72
12
77
17
82
22
87
27
92
32
97
37
42
47
52
57
62
67
72
77
82
87
92
97
3
8
13
18
23
28
33
38
43
48
53
58
63
68
73
78
83
88
93
98
3
8
13
18
23
28
33
38
43
48
53
58
63
68
73
78
83
88
93
98
5
10
15
20
25
30
35
40
45
50
55
60
65
70
75
80
85
90
95
100
5
10
15
20
25
30
35
40
45
50
55
60
65
70
75
80
85
90
95
100
4
9
14
19
24
29
34
39
44
49
5
54
10
59
15
64
20
69
25
74
30
79
35
84
40
89
45
94
50
99
55
60
65
70
75
80
85
4
90
95
9
100
14
19
24
29
34
39
44
49
54
59
64
69
74
79
84
89
94
99
4
9
5
5
10
15
20
25
3
10
14
15
19
20
24
25
29
30
34
35
39
40
44
45
49
50
54
55
59
60
64
65
69
70
74
75
79
80
84
85
89
90
94
95
99
100
30
35
4
9
14
19
24
5
29
10
34
15
39
20
44
25
49
30
54
35
59
40
64
45
69
74
79
84
89
94
99
40
4
9
14
5
19
10
45
50
55
60
65
70
75
80
85
90
95
100
5
10
15
20
25
30
35
40
45
50
55
60
65
8
3
8
13
18
23
5
28
10
50
55
15
15
20
25
24
29
34
39
44
49
54
59
64
69
74
70
79
84
33
38
43
48
53
58
63
68
73
78
83
88
93
98
4
9
14
19
24
29
34
39
44
49
54
59
64
69
74
79
84
89
94
99
60
65
70
75
80
85
90
95
100
20
25
13
4
9
14
19
24
29
30
34
75
80
85
90
95
100
5
10
15
20
25
30
35
40
45
50
55
60
65
89
70
75
5
30
35
39
44
49
40
54
45
59
50
64
55
69
60
74
65
79
70
84
75
89
80
94
85
99
90
95
100
35
5
18
94
99
80
40
45
50
10
23
28
33
38
43
48
53
58
63
68
73
78
5
83
10
88
15
93
20
98
25
30
35
40
45
50
55
60
65
70
75
80
85
90
95
100
4
9
14
19
24
29
34
39
44
49
54
59
64
69
74
79
84
89
94
99
5
10
15
20
25
30
10
15
55
60
65
70
75
80
85
90
95
100
85
90
95
100
15
20
25
30
35
40
45
50
55
60
65
70
75
80
85
90
95
100
5
10
15
20
25
30
35
40
45
50
55
60
65
70
75
80
85
90
95
100
5
10
15
20
25
30
35
40
45
50
55
60
65
70
75
80
85
90
95
100
35
40
45
50
55
60
65
70
75
80
85
90
95
100
20
25
30
35
40
45
50
55
60
65
70
75
80
85
90
95
100