test for closed pipe or killed on every char, not just if pipe full

This commit is contained in:
Robert Morris 2020-10-22 06:36:36 -04:00
parent 4df1a265cb
commit 147855e521

View file

@ -76,26 +76,29 @@ pipeclose(struct pipe *pi, int writable)
int
pipewrite(struct pipe *pi, uint64 addr, int n)
{
int i;
char ch;
int i = 0;
struct proc *pr = myproc();
acquire(&pi->lock);
for(i = 0; i < n; i++){
while(pi->nwrite == pi->nread + PIPESIZE){ //DOC: pipewrite-full
while(i < n){
if(pi->readopen == 0 || pr->killed){
release(&pi->lock);
return -1;
}
if(pi->nwrite == pi->nread + PIPESIZE){ //DOC: pipewrite-full
wakeup(&pi->nread);
sleep(&pi->nwrite, &pi->lock);
}
} else {
char ch;
if(copyin(pr->pagetable, &ch, addr + i, 1) == -1)
break;
pi->data[pi->nwrite++ % PIPESIZE] = ch;
i++;
}
}
wakeup(&pi->nread);
release(&pi->lock);
return i;
}