-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.c
88 lines (78 loc) · 1.35 KB
/
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "bank.h"
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
static int msgid1;
static int msgid2;
void init()
{
msgid1 = msgget(key1,0666|IPC_CREAT|IPC_EXCL);
if(msgid1 == -1)
{
perror("Create Message_Queue1 Failed.");
exit(-1);
}
msgid2 = msgget(key2,0666|IPC_CREAT|IPC_EXCL);
if(msgid2 == -1)
{
perror("Create Message_Queue2 Failed.");
exit(-1);
}
}
void start()
{
printf("Serever is starting...\n");
sleep(2);
// create a child process
pid_t open_pid = vfork();
if(open_pid == -1)
{
perror("vfork failed");
exit(-1);
}
else if(open_pid == 0)
{
execl("open","open",0);
}
else
{
printf("Waiting for client's choice:\n");
waitpid(open_pid,0,0);
}
printf("Server is now working...\n");
}
void destroy()
{
printf("Server is shutting down...\n");
sleep(2);
if(msgctl(msgid1,IPC_RMID,NULL) == -1)
{
perror("Delete Message_Queue1 Failed.");
exit(-1);
}
if(msgctl(msgid2,IPC_RMID,NULL) == -1)
{
perror("Delete Message_Queue2 Failed.");
exit(-1);
}
printf("Server has been shut down...\n");
}
void sig_exit()
{
exit(0);
}
int main()
{
atexit(destroy);
printf("To exit, press CTRL+C\n");
signal(SIGINT,sig_exit);
//create message queue
init();
//start server
start();
return 0;
}