-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwriteshm.c
More file actions
61 lines (38 loc) · 1 KB
/
Copy pathwriteshm.c
File metadata and controls
61 lines (38 loc) · 1 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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <semaphore.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
int main(int argc, char **argv){
const int sharedMemorySize = 128; //--change size
const char* name = "MySharedMemory";
//shm file descriptor
int shm_fd;
void *ptr;
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
ftruncate(shm_fd, sharedMemorySize);
ptr = mmap(NULL, sharedMemorySize, PROT_WRITE, MAP_SHARED, shm_fd, 0);
char semViewName[64];
sem_t *semView;
sprintf(semViewName, "/semView%d", getpid());
semView = sem_open(semViewName, O_CREAT | O_EXCL, 0777, 0);
if(semView == SEM_FAILED){
perror("ERROR OPEN SEMAPHORE");
return 1;
}
char *msg = "MENSAJE\nMENSAJE\nMENSAJE\n";
sprintf(ptr, msg);
ptr += strlen(msg);
sem_post(semView);
printf("so far so good, my pid is: %i\n", getpid());
sleep(20);
printf("bye!\n");
shm_unlink("MySharedMemory");
sem_close(semView);
sem_unlink(semViewName);
}
//-lpthread -lrt