-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsgget_check.c
More file actions
59 lines (47 loc) · 1.25 KB
/
msgget_check.c
File metadata and controls
59 lines (47 loc) · 1.25 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
/**
* Investigate kernel-side implementation of Xget sysv ipc calls.
*/
#include <unistd.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/msg.h>
#include <sys/stat.h>
#define KEY_FILE_1 "/tmp/ftok_check.sysv"
#define KEY_FILE_2 "/tmp/ftok_check_2.sysv"
#define PROJ 1
static key_t get_tok(char *f) {
struct stat sb;
int fd;
if (stat(f, &sb) == -1) {
fd = open(f, O_CREAT | O_RDWR, S_IRWXU);
close(fd);
}
return ftok(f, PROJ);
}
int main(int argc, char **argv) {
int id, id2, i;
key_t tok1, tok2;
tok1 = get_tok(KEY_FILE_1);
tok2 = get_tok(KEY_FILE_2);
printf("TOK:\t%lx\n", tok1);
puts("Creating objects and closing them with the same KEY");
for (i = 0; i < 10; i++) {
if ((id = msgget(tok1, S_IRWXU | O_RDONLY | O_CREAT)) == -1)
exit(EXIT_FAILURE);
printf("ID:\t%08x\n", id);
msgctl(id, IPC_RMID, NULL);
}
puts("Creating two objects with different keys");
if ((id = msgget(tok1, S_IRWXU | O_RDONLY | O_CREAT)) == -1)
exit(EXIT_FAILURE);
if ((id2 = msgget(tok2, S_IRWXU | O_RDONLY | O_CREAT)) == -1)
exit(EXIT_FAILURE);
printf("ID:\t%08x\n", id);
printf("ID:\t%08x\n", id2);
msgctl(id, IPC_RMID, NULL);
msgctl(id2, IPC_RMID, NULL);
exit(EXIT_SUCCESS);
}