pintos-pro2-project 2-User Program

printf(\ ExitStatus(-1); }

if(pfn[No]==NULL) {

printf(\ ExitStatus(-1); }

pfn[No](f); }

void IWrite(struct intr_frame *f) //三个参数

{

int *esp=(int *)f->esp; if(!is_user_vaddr(esp+7)) ExitStatus(-1);

int fd=*(esp+2); //文件句柄

char *buffer=(char *)*(esp+6); //要输出人缓冲 unsigned size=*(esp+3); //输出内容大小。

if(fd==STDOUT_FILENO) //标准输出设备 {

putbuf (buffer, size); f->eax=0; }

else //文件 {

struct thread *cur=thread_current();

struct file_node *fn=GetFile(cur,fd); //获取文件指针 if(fn==NULL) {

f->eax=0; return; }

f->eax=file_write(fn->f,buffer,size);//写文件

} }

void IExit(struct intr_frame *f) //一个参数 正常退出时使用

{

if(!is_user_vaddr(((int *)f->esp)+2)) ExitStatus(-1);

struct thread *cur=thread_current(); cur->ret=*((int *)f->esp+1); f->eax=0;

thread_exit(); }

void ExitStatus(int status) //非正常退出时使用

{

struct thread *cur=thread_current(); cur->ret=status; thread_exit(); }

void ICreate(struct intr_frame *f) //两个参数

{

if(!is_user_vaddr(((int *)f->esp)+6)) ExitStatus(-1);

if((const char *)*((unsigned int *)f->esp+4)==NULL) {

f->eax=-1;

ExitStatus(-1); } bool ret=filesys_create((const char *)*((unsigned *)f->esp+4),*((unsigned int *)f->esp+5)); f->eax=ret; }

int

void IOpen(struct intr_frame *f)

{

if(!is_user_vaddr(((int *)f->esp)+2)) ExitStatus(-1);

struct thread *cur=thread_current();

const char *FileName=(char *)*((int *)f->esp+1); if(FileName==NULL) {

f->eax=-1;

ExitStatus(-1); }

struct file_node *fn=(struct file_node *)malloc(sizeof(struct file_node));

fn->f=filesys_open(FileName);

if(fn->f==NULL|| cur->FileNum>=MaxFiles)// fn->fd=-1; else

fn->fd=++cur->maxfd;

f->eax=fn->fd; if(fn->fd==-1) free(fn);

else {

cur->FileNum++;

list_push_back(&cur->file_list,&fn->elem); } }

void IClose(struct intr_frame *f)

{

if(!is_user_vaddr(((int *)f->esp)+2)) ExitStatus(-1);

struct thread *cur=thread_current(); int fd=*((int *)f->esp+1);

f->eax=CloseFile(cur,fd,false); }

int CloseFile(struct thread *t,int fd,int bAll)

{

struct list_elem *e,*p;

if(bAll) {

while(!list_empty(&t->file_list)) {

struct file_node *fn = list_entry (list_pop_front(&t->file_list), struct file_node, elem);

file_close(fn->f); free(fn); }

t->FileNum=0; return 0; }

for (e = list_begin (&t->file_list); e != list_end (&t->file_list);) {

struct file_node *fn = list_entry (e, struct file_node, elem); if(fn->fd==fd) {

list_remove(e); if(fd==t->maxfd) t->maxfd--; t->FileNum--;

file_close(fn->f); free(fn);

return 0; } } }

void IRead(struct intr_frame *f)

{

int *esp=(int *)f->esp; if(!is_user_vaddr(esp+7)) ExitStatus(-1); int fd=*(esp+2);

char *buffer=(char *)*(esp+6); unsigned size=*(esp+3);

if(buffer==NULL||!is_user_vaddr(buffer+size)) {

f->eax=-1;

ExitStatus(-1); }

struct thread *cur=thread_current(); struct file_node *fn=NULL; unsigned int i;

if(fd==STDIN_FILENO) //从标准输入设备读 {

for(i=0;i

buffer[i]=input_getc();

}

else //从文件读 {

fn=GetFile(cur,fd); //获取文件指针 if(fn==NULL) {

f->eax=-1; return; }

f->eax=file_read(fn->f,buffer,size); } }

struct file_node *GetFile(struct thread *t,int fd) //依据文件句柄从进程打开文件表中找到文件指针

{

struct list_elem *e;

for (e = list_begin (&t->file_list); e != list_end (&t->file_list);e=list_next (e)) {

struct file_node *fn = list_entry (e, struct file_node, elem); if(fn->fd==fd) return fn; }

return NULL; }

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4