-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
36 lines (25 loc) · 957 Bytes
/
server.cpp
File metadata and controls
36 lines (25 loc) · 957 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
#include <iostream>
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <cstring>
#include <unistd.h>
#include <signal.h>
#include <thread>
using namespace std;
#define MSG_SZ 2048
int shmid;
char* msg;
int main() {
key_t key = ftok("shmfile", 65);
shmid = shmget(key, MSG_SZ, 0666 | IPC_CREAT); //0666: access permission to shared memory segment, IPC_CREAT: create shared memory segment if not exist
msg = (char*)shmat(shmid, (void*)0, 0);
//attach shared memory segment to process
//void *shmat(int shmid ,void *shmaddr ,int shmflg), shmaddr = (void*)0 -> OS should choose an address to attach to shared memory segment
printf("shared memory segment attached at %p\n", (void*)msg);
printf("enter a message: ");
fgets(msg, MSG_SZ, stdin);
printf("data writen into shared memory: %s\n", msg);
shmdt(msg); //detach shared memory from process
// shmctl(shmid, IPC_RMID, NULL);
}