-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.c
61 lines (58 loc) · 1.19 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
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/shm.h>
#include<sys/sem.h>
#include<unistd.h>
#define N_MAX 3
int main(){
int* N;
int shmid;
int new = 1;
int semid;
struct sembuf sem;
int msqid;
char pathname[] = "file.c";
key_t key;
int i, len;
struct msgbuf{
long mtype;
int result;
} buf;
struct mymsgbuf{
long mtype;
int a;
int b;
pid_t pid;
} mybuf;
(key = ftok(pathname, 0));
msqid = msgget(key, 0666 | IPC_CREAT);
pid_t pid;
sem.sem_op = N_MAX;
sem.sem_flg = 0;
sem.sem_num = 0;
semop(semid, &sem, 1);
while(1){
sem.sem_op = -1;
sem.sem_flg = 0;
sem.sem_num = 0;
semop(semid, &sem, 1);
msgrcv(msqid, &mybuf, sizeof(struct mymsgbuf) - sizeof(long), 1, 0);
pid = fork();
if(pid == 0){
sleep(10);
buf.result = mybuf.a * mybuf.b;
buf.mtype = mybuf.pid;
msgsnd(msqid, &buf, sizeof(struct msgbuf) - sizeof(long), 0);
sem.sem_op = 1;
sem.sem_flg = 0;
sem.sem_num = 0;
semop(semid, &sem, 1);
exit(-1);
}
}
return 0;
}