forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
16 changed files
with
424 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include "types.h" | ||
#include "param.h" | ||
#include "x86.h" | ||
#include "mmu.h" | ||
#include "proc.h" | ||
#include "defs.h" | ||
#include "fd.h" | ||
|
||
struct fd fds[NFD]; | ||
|
||
/* | ||
* allocate a file descriptor number for curproc. | ||
*/ | ||
int | ||
fd_ualloc() | ||
{ | ||
int fd; | ||
struct proc *p = curproc[cpu()]; | ||
for(fd = 0; fd < NOFILE; fd++) | ||
if(p->fds[fd] == 0) | ||
return fd; | ||
return -1; | ||
} | ||
|
||
struct fd * | ||
fd_alloc() | ||
{ | ||
int i; | ||
|
||
for(i = 0; i < NFD; i++){ | ||
if(fds[i].type == FD_CLOSED){ | ||
fds[i].type = FD_NONE; | ||
fds[i].count = 1; | ||
return fds + i; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
void | ||
fd_close(struct fd *fd) | ||
{ | ||
if(fd->type == FD_CLOSED || fd->count <= 0) | ||
panic("fd_close"); | ||
fd->count -= 1; | ||
if(fd->count == 0){ | ||
if(fd->type == FD_PIPE) | ||
pipe_close(fd->pipe, fd->writeable); | ||
fd->type = FD_CLOSED; | ||
} | ||
} | ||
|
||
/* | ||
* addr is a kernel address, pointing into some process's p->mem. | ||
*/ | ||
int | ||
fd_write(struct fd *fd, char *addr, int n) | ||
{ | ||
if(fd->writeable == 0) | ||
return -1; | ||
if(fd->type == FD_PIPE){ | ||
return pipe_write(fd->pipe, addr, n); | ||
} else { | ||
panic("fd_write"); | ||
return -1; | ||
} | ||
} | ||
|
||
int | ||
fd_read(struct fd *fd, char *addr, int n) | ||
{ | ||
if(fd->readable == 0) | ||
return -1; | ||
if(fd->type == FD_PIPE){ | ||
return pipe_read(fd->pipe, addr, n); | ||
} else { | ||
panic("fd_read"); | ||
return -1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
struct fd { | ||
enum { FD_CLOSED, FD_NONE, FD_PIPE } type; | ||
int count; // reference count | ||
char readable; | ||
char writeable; | ||
struct pipe *pipe; | ||
}; | ||
|
||
extern struct fd fds[NFD]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#include "types.h" | ||
#include "param.h" | ||
#include "x86.h" | ||
#include "mmu.h" | ||
#include "proc.h" | ||
#include "defs.h" | ||
#include "fd.h" | ||
|
||
#define PIPESIZE 512 | ||
|
||
struct pipe { | ||
int readopen; // read fd is still open | ||
int writeopen; // write fd is still open | ||
int writep; // next index to write | ||
int readp; // next index to read | ||
char data[PIPESIZE]; | ||
}; | ||
|
||
int | ||
pipe_alloc(struct fd **fd1, struct fd **fd2) | ||
{ | ||
*fd1 = *fd2 = 0; | ||
struct pipe *p = 0; | ||
|
||
if((*fd1 = fd_alloc()) == 0) | ||
goto oops; | ||
if((*fd2 = fd_alloc()) == 0) | ||
goto oops; | ||
if((p = (struct pipe *) kalloc(PAGE)) == 0) | ||
goto oops; | ||
(*fd1)->type = FD_PIPE; | ||
(*fd1)->readable = 1; | ||
(*fd1)->writeable = 0; | ||
(*fd1)->pipe = p; | ||
(*fd2)->type = FD_PIPE; | ||
(*fd2)->readable = 0; | ||
(*fd2)->writeable = 1; | ||
(*fd2)->pipe = p; | ||
return 0; | ||
oops: | ||
if(p) | ||
kfree((char *) p, PAGE); | ||
if(*fd1){ | ||
(*fd1)->type = FD_NONE; | ||
fd_close(*fd1); | ||
} | ||
if(*fd2){ | ||
(*fd2)->type = FD_NONE; | ||
fd_close(*fd2); | ||
} | ||
return -1; | ||
} | ||
|
||
void | ||
pipe_close(struct pipe *p, int writeable) | ||
{ | ||
if(writeable) | ||
p->writeopen = 0; | ||
else | ||
p->readopen = 0; | ||
if(p->readopen == 0 && p->writeopen == 0) | ||
kfree((char *) p, PAGE); | ||
} | ||
|
||
int | ||
pipe_write(struct pipe *p, char *addr, int n) | ||
{ | ||
int i; | ||
|
||
for(i = 0; i < n; i++){ | ||
while(((p->writep + 1) % PIPESIZE) == p->readp){ | ||
if(p->readopen == 0) | ||
return -1; | ||
sleep(&p->writep); | ||
} | ||
p->data[p->writep] = addr[i]; | ||
p->writep = (p->writep + 1) % PIPESIZE; | ||
} | ||
return i; | ||
} | ||
|
||
int | ||
pipe_read(struct pipe *p, char *addr, int n) | ||
{ | ||
int i; | ||
|
||
while(p->readp == p->writep){ | ||
if(p->writeopen == 0) | ||
return 0; | ||
sleep(&p->readp); | ||
} | ||
|
||
for(i = 0; i < n; i++){ | ||
if(p->readp == p->writep) | ||
break; | ||
addr[i] = p->data[p->readp]; | ||
p->readp = (p->readp + 1) % PIPESIZE; | ||
} | ||
return i; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.