Commit | Line | Data |
---|---|---|
1652ed72 JB |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <unistd.h> | |
4 | #include <string.h> | |
5 | #include <sys/mman.h> | |
6 | #include <sys/stat.h> | |
7 | #include <sys/types.h> | |
8 | #include <fcntl.h> | |
9 | ||
10 | #define nbchar 4096 | |
11 | ||
12 | int main() | |
13 | { | |
14 | int fd, res; | |
15 | char *partage = NULL; | |
16 | char *message = ""; | |
17 | fd = shm_open("partage.mem", O_RDWR | O_CREAT, 0600); /* ouverture du segment partagé */ | |
18 | if (fd == -1) { /* et création du nom (du fichier) */ | |
19 | perror("shm_open"); | |
20 | exit(1); | |
21 | } | |
22 | res = ftruncate(fd, nbchar); /* choix de la taille du segment */ | |
23 | if (res == -1) { | |
24 | perror("ftruncate"); | |
25 | exit(1); | |
26 | } | |
27 | partage = (char *)mmap(NULL, nbchar, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
28 | if (partage == MAP_FAILED) { | |
29 | perror("mmap"); | |
30 | exit(1); | |
31 | } | |
32 | strncpy(partage, message, nbchar); | |
33 | while (strcmp(partage, message) == 0) { | |
34 | sleep(1); | |
35 | } | |
36 | fprintf(stdout, "la réponse est %s\n", partage); | |
37 | res = munmap(partage, nbchar); | |
38 | if (res == -1) { | |
39 | perror("munmap"); | |
40 | exit(1); | |
41 | } | |
42 | close(fd); /* fermeture du fichier */ | |
43 | res = shm_unlink("partage.mem"); /* suppression du nom */ | |
44 | if (res == -1) { | |
45 | perror("shm_unlink"); | |
46 | exit(1); | |
47 | } | |
48 | return 0; | |
49 | } |