-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE6_T1a.c
More file actions
61 lines (52 loc) · 1.12 KB
/
Copy pathE6_T1a.c
File metadata and controls
61 lines (52 loc) · 1.12 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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(void)
{
int shmId; // id of the shared segment
key_t key = 1000; // key
size_t size = sizeof(int); // size of memory
char buf[4096];
shmId = shmget(key, size, IPC_CREAT | 0666); // gets segment -> Creates new segment with read, write rights
if(shmId == -1)
{
perror("shmget failed");
exit(1);
}
else
{
int* data = shmat(shmId, (void*)0,0); // Maps a shared memory segment onto process’s address space.
*data = 0; // set data to 0
if(shmdt(data) == -1) // Detaches the given segment
{
perror("shmdt(data) failed");
exit(1);
}
if(mkfifo("RESULT_FIFO", 0666) == -1)
{
perror("mkfifo failed");
exit(1);
}
FILE* myFifo = fopen("RESULT_FIFO", "r");
if(myFifo == NULL)
{
perror("fopen failed");
exit(1);
}
while(fscanf(myFifo, "%s", buf) > 0)
{
printf("%s\n", buf);
}
if(fclose(myFifo) == -1)
{
perror("fclose failed");
exit(1);
}
}
return EXIT_SUCCESS;
}