-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipc.c
More file actions
66 lines (52 loc) · 1.7 KB
/
ipc.c
File metadata and controls
66 lines (52 loc) · 1.7 KB
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
#include <unistd.h>
#include <stdio.h>
#include <asm/errno.h>
#include "ipc.h"
#include "process.h"
#include <sys/ioctl.h>
#include <errno.h>
#define RESULT_SUCCESS 0
#define RESULT_FAIL -1
int send(void *self, local_id dst, const Message *msg) {
Process *p = ((Process *) self);
size_t size = sizeof(MessageHeader) + msg->s_header.s_payload_len;
write(w_ends[p->id][dst], msg, size);
return RESULT_SUCCESS;
}
int send_multicast(void *self, const Message *msg) {
Process *p = ((Process *) self);
for (int i = 0; i < p->processes_count; i++) {
if (i != p->id) {
if (send(self, i, msg) < 0)
return RESULT_FAIL;
}
}
return RESULT_SUCCESS;
}
int receive(void *self, local_id from, Message *msg) {
Process *p = ((Process *) self);
size_t header_size = sizeof(MessageHeader);
if (read(r_ends[from][p->id], &(msg->s_header), header_size) != header_size) {
return RESULT_FAIL;
}
size_t msg_len = msg->s_header.s_payload_len;
if ((read(r_ends[from][p->id], msg->s_payload, msg_len)) != msg_len) {
return RESULT_FAIL;
}
return RESULT_SUCCESS;
}
int receive_any(void *self, Message *msg) {
Process *p = ((Process *) self);
MessageType expected_type = msg->s_header.s_type;
int expected_msg_count = p->processes_count - 1;
if (! p->is_parent)
expected_msg_count--;
for (int received = 0; received < expected_msg_count;){
for (int i = 1; i < p->processes_count; i++) {
if (i != p->id)
if (receive(self, i, msg) == RESULT_SUCCESS && msg->s_header.s_type == expected_type)
received++;
}
}
return RESULT_SUCCESS;
}