-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipe.c
103 lines (92 loc) · 1.97 KB
/
pipe.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "pipe.h"
#include "types.h"
#include "param.h"
#include "mmu.h"
#include "proc.h"
#include "fs.h"
#include "file.h"
#include "kalloc.h"
extern struct proc *curproc; //当前进程
int pipealloc(struct file **f0, struct file **f1)
{
struct pipe *p;
p = 0;
*f0 = *f1 = 0;
if ((*f0 = filealloc()) == 0 || (*f1 = filealloc()) == 0)
goto bad;
if ((p = (struct pipe*)kalloc()) == 0)
goto bad;
p->readopen = 1;
p->writeopen = 1;
p->nwrite = 0;
p->nread = 0;
(*f0)->type = FD_PIPE;
(*f0)->readable = 1;
(*f0)->writeable = 0;
(*f0)->pipe = p;
(*f1)->type = FD_PIPE;
(*f1)->readable = 1;
(*f1)->writeable = 0;
(*f1)->pipe = p;
return 0;
bad:
if (p)
kfree((char*)p);
if (*f0)
fileclose(*f0);
if (*f1)
fileclose(*f1);
return -1;
}
void pipeclose(struct pipe *p, int write)
{
if (write)
{
p->writeopen = 0;
wakeup(&p->nread);
}
else
{
p->readopen = 0;
wakeup(&p->nwrite);
}
if (p->readopen == 0 && p->writeopen == 0)
{
kfree((char*)p);
}
}
int pipewrite(struct pipe *p, char *addr, int n)
{
int i;
for (i = 0; i < n; i++)
{
while (p->nwrite == p->nread + PIPESIZE)
{
if (p->readopen == 0 || curproc->killed)
return -1;
wakeup(&p->nread); //唤醒读进程
sleep(&p->nwrite); //写进程睡眠
}
p->data[p->nwrite++ % PIPESIZE] = addr[i];
}
wakeup(&p->read);
return n;
}
int piperead(struct pipe *p, char *addr, int n)
{
int i;
while (p->nread == p->nwrite && p->writeopen)
{
if (curproc->killed)
return -1;
sleep(&p->nread);
}
for (i = 0; i < n; i++)
{
if (p->nread == p->nwrite)
break;
addr[i] = p->data[p->nread++ % PIPESIZE];
}
wakeup(&p->nwrite); //唤醒写管道的进程
return i;
}