-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreadshm.c
More file actions
69 lines (40 loc) · 909 Bytes
/
Copy pathreadshm.c
File metadata and controls
69 lines (40 loc) · 909 Bytes
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
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <semaphore.h>
int main(int argc, char *argv[]){
if(argc != 2){
printf("Usage: ./view PID\n");
return 1;
}
view(argv[1]);
return 0;
}
void view(char *PID){
char semViewName[64];
sem_t *semView, *semEnd;
sprintf(semViewName, "semView%s", PID);
semView = sem_open(semViewName, 0);
if(semView == SEM_FAILED){
perror("ERROR OPEN SEMAPHORE");
return;
}
const int sharedMemorySize = 128; // --change size
const char* name = "MySharedMemory";
int shm_fd;
void* ptr;
shm_fd = shm_open(name, O_RDONLY, 0666);
ptr = mmap(NULL, sharedMemorySize, PROT_READ, MAP_SHARED, shm_fd, 0);
char * c = (char*)ptr;
int i = sharedMemorySize;
sem_wait(semView);
while( i!= 0){
printf("%c", *c);
c++;
i--;
}
sem_close(semView);
sem_unlink(semViewName);
}