-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.c
65 lines (60 loc) · 1.78 KB
/
control.c
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
#include <stdio.h>
#include <strings.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#define SEMKEY 42069
#define MEMKEY 69420
// union semun{
// int val;
// struct semid_ds *buf;
// unsigned short *array;
// struct seminfo *__buf;
// };
int main(int argc, char *argv[])
{
int semd, memd, file;
char input[3];
if (argc > 1)
{
if (strcmp(argv[1], "remove") == 0)
{
file = open("telephone.txt", O_RDONLY, 0644);
semd = semget(SEMKEY, 0, 0);
memd = shmget(MEMKEY, 0, 0);
semctl(semd, IPC_RMID, 0);
shmctl(memd, IPC_RMID, 0);
struct stat filestat;
stat("telephone.txt", &filestat);
char *fileContent = malloc(filestat.st_size);
read(file, fileContent, filestat.st_size);
printf("The complete story: \n%s\n", fileContent);
close(file);
}
else if (strcmp(argv[1], "create") == 0)
{
semd = semget(SEMKEY, 1, IPC_CREAT | IPC_EXCL | 0640);
memd = shmget(MEMKEY, sizeof(int), IPC_CREAT | IPC_EXCL | 0640);
if (semd == -1){
printf("Error: %s. There may already be an open semaphore session here.\n", strerror(errno));
}else{
union semun us;
us.val = 1;
semctl(semd, 0, SETVAL, us);
file = open("telephone.txt", O_CREAT | O_WRONLY | O_TRUNC, 0644);
close(file);
}
}else{
printf("Use args 'create' or 'remove' to use this.");
}
}else{
printf("Use args 'create' or 'remove' to use this.");
}
return 0;
}