forked from nyg0813/sysprog_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shmem_server.c
45 lines (40 loc) · 853 Bytes
/
shmem_server.c
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
#include <stdio.h> // printf()
#include <unistd.h> // sleep()
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define KEY_NUM 8000
#define MEM_SIZE 1024
int main( void)
{
int shm_id;
char buf_in[MEM_SIZE];
void *shm_addr;
int count;
if ( -1 == ( shm_id = shmget( (key_t)KEY_NUM, MEM_SIZE, IPC_CREAT|0666)))
{
printf( "shmget fail\n");
return -1;
}
if ( ( void *)-1 == ( shm_addr = shmat( shm_id, ( void *)0, 0)))
{
printf( "shmat fail\n");
return -1;
}
while(1)
{
if(!strncmp((char*)shm_addr,"OK",2))
{
printf("write message to client\n");
fgets(buf_in, MEM_SIZE, stdin);
sprintf((char*)shm_addr, "%s", buf_in);
}
else
{
while(!strcmp((char*)shm_addr,buf_in))
;
}
}
return 0;
}