-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.c
63 lines (45 loc) · 1.11 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
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <signal.h>
#include <unistd.h>
#define BUFF 128
#define RCVTYPE 16
#define SNDTYPE 17
int msgid;
typedef struct msgp {
long mtype;
int terme1;
int terme2;
}msgp;
void interup() {
printf("Fin du server d'addition");
msgctl(msgid, IPC_RMID, NULL);
exit(0);
}
int main(int argc, char* argv[])
{
key_t cle;
msgp unMessage;
if ( ( cle = ftok("/home/azman/ed34/Server",17 ) ) == -1)
perror("ftok"), exit(1);
printf("\n cle : %d\n", cle);
if ( ( msgid = msgget(cle, IPC_CREAT|IPC_EXCL|S_IRUSR|S_IWUSR|S_IROTH|S_IWOTH )) == -1 )
perror("msgget"), exit(1);
printf("\n msgid: %d\n", msgid);
signal(SIGINT, interup);
for (;;) {
msgrcv(msgid, &unMessage, sizeof(msgp), RCVTYPE, MSG_NOERROR);
printf("\nServer: Terme1: %d, Terme2: %d\n", unMessage.terme1, unMessage.terme2);
unMessage.terme1 = unMessage.terme1 + unMessage.terme2;
unMessage.mtype = SNDTYPE;
msgsnd(msgid, &unMessage, sizeof(unMessage),0);
sleep(1);
}
exit(EXIT_SUCCESS);
}