forked from nyg0813/sysprog_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsh_st_server.c
58 lines (50 loc) · 1.21 KB
/
sh_st_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
46
47
48
49
50
51
52
53
54
55
56
57
#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 9999
#define MEM_SIZE 1024
int main( void)
{
int shm_id;
int num, index;
char buf1[MEM_SIZE];
char buf2[MEM_SIZE];
int *buf;
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;
}
buf = (int*)shm_addr;
buf[255]=1;
printf("Shmem is set, write the number that you want to insert \n");
fgets(buf1, MEM_SIZE ,stdin);
num = atoi(buf1);
while(1)
{
printf("Type the index number that you want to insert (0~250)\n");
fgets(buf2,MEM_SIZE,stdin);
index = atoi(buf2);
if(index<0||index>250)
printf("Wrond index (0~250)\n");
else
{
buf[252] = index;
buf[253] = num;
buf[254] = 1;
break;
}
}
buf[index] = num;
return 0;
}