-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathview.c
More file actions
76 lines (46 loc) · 1.14 KB
/
Copy pathview.c
File metadata and controls
76 lines (46 loc) · 1.14 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#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){
//semaphore
char semViewName[64];
sem_t *semView;
sprintf(semViewName, "semView%s", PID);
semView = sem_open(semViewName, 0);
if(semView == SEM_FAILED){
perror("ERROR OPEN SEMAPHORE");
return;
}
//shm for message size
const int sharedMemorySize = sizeof(int);
const char* name = "MySharedMemory";
int shm_fd;
int* ptr;
shm_fd = shm_open(name, O_RDONLY, 0666);
ptr = (int*) mmap(NULL, sharedMemorySize, PROT_READ, MAP_SHARED, shm_fd, 0);
sem_wait(semView);
int msgSize = *ptr;
//printf("%i\n",num);
//
//
const int sharedMemorySize2 = msgSize;
const char* name2 = "MySharedMemory2";
int shm_fd2;
char* ptr2;
shm_fd2 = shm_open(name2, O_RDONLY, 0666);
ptr2 = (char*) mmap(NULL, sharedMemorySize2, PROT_READ, MAP_SHARED, shm_fd2, 0);
fwrite(ptr2, sharedMemorySize2, 1, stdout);
sem_close(semView);
sem_unlink(semViewName);
}