forked from nyg0813/sysprog_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shmem_client.c
45 lines (39 loc) · 897 Bytes
/
shmem_client.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 <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];
int buf[MEM_SIZE/sizeof(int)];
void *shm_addr;
while ( -1 == ( shm_id = shmget( (key_t)KEY_NUM, MEM_SIZE, 0666)))
;
printf( "ready to attach\n");
if ( ( void *)-1 == ( shm_addr = shmat( shm_id, ( void *)0, 0)))
{
printf( "shmat fail\n");
return -1;
}
strcpy(buf_in, (char*)shm_addr);
while(1)
{
if(!strcmp(buf_in,(char*)shm_addr)&&strncmp((char*)shm_addr,"OK",2)!=0)
{
sprintf((char*)shm_addr, "%s", "OK");
while(!strncmp((char*)shm_addr,"OK",2))
;
printf("server send\n");
printf("%s",(char*)shm_addr);
}
else
{
strcpy(buf_in, (char*)shm_addr);
}
}
return 0;
}