-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE6_T2b.c
More file actions
119 lines (97 loc) · 2.67 KB
/
Copy pathE6_T2b.c
File metadata and controls
119 lines (97 loc) · 2.67 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/sem.h>
union senum // Declaration of union because otherwise compile Error
{
int val;
struct semid_ds *buf;
ushort *array;
};
int main(void)
{
int pid[99];
int shmId;
key_t key = 1000;
size_t size = sizeof(int);
shmId = shmget(key, size, IPC_CREAT | 0666); // Gets a semaphore set. The value returned is its id, for use with other calls.
if(shmId == -1)
{
perror("shmget failed");
exit(1);
}
int semId;
semId = semget(key, sizeof(int*), IPC_CREAT | 0666);
if(semId == -1)
{
perror("semget failed");
exit(1);
}
union senum arg;
arg.val = 1; // Set union member val = 1
if(semctl(semId, 0, SETVAL, arg) == -1)
{
perror("semctl failed");
exit(1);
}
struct sembuf sb = {0, -1, 0}; // Defines an operation to be performed by the semop() call
for(int i = 0; i < 100; i++)
{
pid[i] = fork();
if(pid[i] < 0)
{
printf("fork() failed\n");
exit(1);
}
else if(pid[i] == 0)
{
int* data = shmat(shmId, (void*)0,0);
if(semop(semId,&sb,1) == -1) // Performs a semaphore operation (i.e. incrementing, decrementing, etc.) on the selected members of a
semaphore set
{
perror("semop failed");
exit(1);
}
for(int useless = 0; useless < 100; useless++)
{
*data = *data + 1;
}
sb.sem_op = 1;
if(semop(semId, &sb, 1) == -1)
{
perror("semop failed");
exit(1);
}
exit(0);
}
wait(NULL);
}
int* data = shmat(shmId, (void*)0,0);
FILE* fifo = fopen("RESULT_FIFO", "w");
if(fifo == NULL)
{
perror("fopen failed");
exit(1);
}
fprintf(fifo, "%d", *data);
printf("Data in shared memory: %d\n", *data);
if(shmdt(data) == -1)
{
perror("shmdt(data) failed");
exit(1);
}
if(fclose(fifo) == -1)
{
perror("fclose failed");
exit(1);
}
return EXIT_SUCCESS;
}