diff --git a/defs.h b/defs.h index 7af5421c04a7fb7a55b4d36278e2fe23ffde7678..b81ef31bc1597d1e7f17e278f59199b6ed41dd1a 100644 --- a/defs.h +++ b/defs.h @@ -28,7 +28,7 @@ int exec(char*, char**); // file.c struct file* filealloc(void); void fileclose(struct file*); -void fileincref(struct file*); +struct file* filedup(struct file*); void fileinit(void); int fileread(struct file*, char*, int n); int filestat(struct file*, struct stat*); diff --git a/file.c b/file.c index 297fd1c46dec2b0964323f338c51fd4b72e7528e..9bd6ecec77452e1500c6a1a57828fc11a0c81cc2 100644 --- a/file.c +++ b/file.c @@ -41,14 +41,15 @@ filealloc(void) } // Increment ref count for file f. -void -fileincref(struct file *f) +struct file* +filedup(struct file *f) { acquire(&file_table_lock); if(f->ref < 1 || f->type == FD_CLOSED) - panic("fileincref"); + panic("filedup"); f->ref++; release(&file_table_lock); + return f; } // Read from file f. Addr is kernel address. diff --git a/proc.c b/proc.c index fb79444f6d1d06b70b5a60d4a72f0b7268863890..776c8b41fe85ef59e6f94a09db7d4b1a568cbbe6 100644 --- a/proc.c +++ b/proc.c @@ -129,10 +129,9 @@ copyproc(struct proc *p) } memmove(np->mem, p->mem, np->sz); - for(i = 0; i < NOFILE; i++){ - if((np->ofile[i] = p->ofile[i]) != 0) - fileincref(np->ofile[i]); - } + for(i = 0; i < NOFILE; i++) + if(p->ofile[i]) + np->ofile[i] = filedup(p->ofile[i]); np->cwd = idup(p->cwd); } diff --git a/sysfile.c b/sysfile.c index 1182e17d1970ca023d6a5514d4d03f35d64b487f..712a2200b7bcfe7327faec8a80fb045ff8afb0bd 100644 --- a/sysfile.c +++ b/sysfile.c @@ -83,7 +83,7 @@ sys_dup(void) return -1; if((fd=fdalloc(f)) < 0) return -1; - fileincref(f); + filedup(f); return fd; }