forked from rasendubi/akernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_pipe_master.c
36 lines (33 loc) · 948 Bytes
/
user_pipe_master.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
#include <user_pipe_master.h>
#include <alloc.h>
#include <pipe_master.h>
#include <utils.h>
#include <user/syscalls.h>
int pipe_new(const char *name) {
int name_len = strlen(name);
int buf_len = name_len + 1 + 3*sizeof(unsigned);
int pid = getpid();
char *buf = malloc(buf_len);
((unsigned *)buf)[0] = pid;
((unsigned *)buf)[1] = 0;
((unsigned *)buf)[2] = name_len + 1;
memcpy(buf + 3*sizeof(unsigned), name, name_len + 1);
write(MASTER_PIPE, buf, buf_len);
int reply;
read(pid, &reply, sizeof(reply));
return reply;
}
int pipe_open(const char *name) {
int name_len = strlen(name);
int buf_len = name_len + 1 + 3*sizeof(unsigned);
int pid = getpid();
char *buf = malloc(buf_len);
((unsigned *)buf)[0] = pid;
((unsigned *)buf)[1] = 1;
((unsigned *)buf)[2] = name_len + 1;
memcpy(buf + 3*sizeof(unsigned), name, name_len + 1);
write(MASTER_PIPE, buf, buf_len);
int reply;
read(pid, &reply, sizeof(reply));
return reply;
}