-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2.c
More file actions
100 lines (82 loc) · 2.39 KB
/
task2.c
File metadata and controls
100 lines (82 loc) · 2.39 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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#define SEM_KEY 9979
#define SHM_KEY 9979
int create_semaphore(key_t key) {
/* Testen, ob das Semaphor bereits existiert */
int semid = semget(key, 0, IPC_PRIVATE);
if (semid < 0) {
/* Semaphor doesnt exists */
semid = semget(key, 1, IPC_CREAT | IPC_EXCL | 0644);
if (semid < 0) {
perror("semget");
return -1;
}
/* Init Semaphor with 1 */
if (semctl(semid, 0, SETVAL, (int)1) == -1) return -1;
}
return semid;
}
int main(void) {
// n -> Process counter, m = loops each process
const int n = 1000, m = 10000;
int pid, shmid, semid, RESULT_FIFO;
char *fifo = "/tmp/RESULT_FIFO";
// get SEM
semid = create_semaphore(SEM_KEY);
if (semid < 0) {
perror("semget");
exit(EXIT_FAILURE);
}
// Get SHM
shmid = shmget(SHM_KEY, 0, 0660);
if (shmid < 0) {
perror("shmget");
exit(EXIT_FAILURE);
}
int *data = (int *)shmat(shmid, NULL, 0); // SHM_WRONLY
if (*data == -1) {
perror("shmat");
exit(EXIT_FAILURE);
}
// Creates n Processes
for (int i = 1; i <= n; i++) {
// Create fork
pid = fork();
if (pid == 0) {
/* Structs for semaphor */
struct sembuf enter, leave;
enter.sem_num = leave.sem_num = 0;
enter.sem_flg = leave.sem_flg = SEM_UNDO;
enter.sem_op = -1; /* DOWN-Operation */
leave.sem_op = 1; /* UP-Operation */
for (int i = 1; i <= m; i++) {
/* Join critical area */
semop(semid, &enter, 1);
// Inc val in SHM
*data = *data + 1;
/* Leave critical area */
semop(semid, &leave, 1);
}
exit(EXIT_SUCCESS);
}
}
// Wait for Child
for (int i = 1; i <= n; i++) waitpid(pid, NULL, 0);
// Read from SHM > stdout
fprintf(stdout, "Data: %d\n", *data);
// Write to RESULT_FIFO
RESULT_FIFO = open(fifo, O_WRONLY);
if (RESULT_FIFO != -1) write(RESULT_FIFO, data, sizeof(data));
semctl(semid, 0, IPC_RMID, 0);
close(RESULT_FIFO);
return EXIT_SUCCESS;
}