1 Index: src/threads/thread.c
2 diff -u src/threads/thread.c~ src/threads/thread.c
3 --- src/threads/thread.c~
4 +++ src/threads/thread.c
6 #include "threads/synch.h"
8 #include "userprog/process.h"
9 +#include "userprog/syscall.h"
12 /* Random value for struct thread's `magic' member.
13 @@ -251,16 +252,19 @@ thread_tid (void)
17 + struct thread *t = thread_current ();
19 ASSERT (!intr_context ());
27 /* Just set our status to dying and schedule another process.
28 We will be destroyed during the call to schedule_tail(). */
30 - thread_current ()->status = THREAD_DYING;
31 + t->status = THREAD_DYING;
35 @@ -400,6 +404,11 @@ init_thread (struct thread *t, const cha
36 strlcpy (t->name, name, sizeof t->name);
37 t->stack = (uint8_t *) t + PGSIZE;
38 t->priority = priority;
39 + list_init (&t->children);
41 + t->wait_status = NULL;
42 + list_init (&t->fds);
44 t->magic = THREAD_MAGIC;
47 Index: src/threads/thread.h
48 diff -u src/threads/thread.h~ src/threads/thread.h
49 --- src/threads/thread.h~
50 +++ src/threads/thread.h
55 +#include "threads/synch.h"
57 /* States in a thread's life cycle. */
59 @@ -89,6 +90,11 @@ struct thread
60 uint8_t *stack; /* Saved stack pointer. */
61 int priority; /* Priority. */
63 + /* Owned by process.c. */
64 + int exit_code; /* Exit code. */
65 + struct wait_status *wait_status; /* This process's completion status. */
66 + struct list children; /* Completion status of children. */
68 /* Shared between thread.c and synch.c. */
69 struct list_elem elem; /* List element. */
71 @@ -96,11 +102,31 @@ struct thread
72 /* Owned by userprog/process.c. */
73 uint32_t *pagedir; /* Page directory. */
75 + struct file *bin_file; /* Executable. */
77 + /* Owned by syscall.c. */
78 + struct list fds; /* List of file descriptors. */
79 + int next_handle; /* Next handle value. */
81 /* Owned by thread.c. */
82 unsigned magic; /* Detects stack overflow. */
85 +/* Tracks the completion of a process.
86 + Reference held by both the parent, in its `children' list,
87 + and by the child, in its `wait_status' pointer. */
90 + struct list_elem elem; /* `children' list element. */
91 + struct lock lock; /* Protects ref_cnt. */
92 + int ref_cnt; /* 2=child and parent both alive,
93 + 1=either child or parent alive,
94 + 0=child and parent both dead. */
95 + tid_t tid; /* Child thread id. */
96 + int exit_code; /* Child exit code, if dead. */
97 + struct semaphore dead; /* 1=child alive, 0=child dead. */
100 void thread_init (void);
101 void thread_start (void);
102 void thread_tick (void);
103 Index: src/userprog/exception.c
104 diff -u src/userprog/exception.c~ src/userprog/exception.c
105 --- src/userprog/exception.c~
106 +++ src/userprog/exception.c
107 @@ -150,6 +150,14 @@ page_fault (struct intr_frame *f)
108 write = (f->error_code & PF_W) != 0;
109 user = (f->error_code & PF_U) != 0;
111 + /* Handle bad dereferences from system call implementations. */
114 + f->eip = (void (*) (void)) f->eax;
119 /* To implement virtual memory, delete the rest of the function
120 body, and replace it with code that brings in the page to
121 which fault_addr refers. */
122 Index: src/userprog/process.c
123 diff -u src/userprog/process.c~ src/userprog/process.c
124 --- src/userprog/process.c~
125 +++ src/userprog/process.c
127 #include "threads/init.h"
128 #include "threads/interrupt.h"
129 +#include "threads/malloc.h"
130 #include "threads/palloc.h"
131 #include "threads/thread.h"
132 #include "threads/vaddr.h"
134 static thread_func execute_thread NO_RETURN;
135 -static bool load (const char *cmdline, void (**eip) (void), void **esp);
136 +static bool load (const char *cmd_line, void (**eip) (void), void **esp);
138 +/* Data structure shared between process_execute() in the
139 + invoking thread and execute_thread() in the newly invoked
143 + const char *file_name; /* Program to load. */
144 + struct semaphore load_done; /* "Up"ed when loading complete. */
145 + struct wait_status *wait_status; /* Child process. */
146 + bool success; /* Program successfully loaded? */
149 /* Starts a new thread running a user program loaded from
150 FILE_NAME. The new thread may be scheduled (and may even exit)
151 @@ -27,29 +39,37 @@ static bool load (const char *cmdline, v
153 process_execute (const char *file_name)
156 + struct exec_info exec;
157 + char thread_name[16];
161 - /* Make a copy of FILE_NAME.
162 - Otherwise there's a race between the caller and load(). */
163 - fn_copy = palloc_get_page (0);
164 - if (fn_copy == NULL)
166 - strlcpy (fn_copy, file_name, PGSIZE);
167 + /* Initialize exec_info. */
168 + exec.file_name = file_name;
169 + sema_init (&exec.load_done, 0);
171 /* Create a new thread to execute FILE_NAME. */
172 - tid = thread_create (file_name, PRI_DEFAULT, execute_thread, fn_copy);
173 - if (tid == TID_ERROR)
174 - palloc_free_page (fn_copy);
175 + strlcpy (thread_name, file_name, sizeof thread_name);
176 + strtok_r (thread_name, " ", &save_ptr);
177 + tid = thread_create (thread_name, PRI_DEFAULT, execute_thread, &exec);
178 + if (tid != TID_ERROR)
180 + sema_down (&exec.load_done);
182 + list_push_back (&thread_current ()->children, &exec.wait_status->elem);
190 /* A thread function that loads a user process and starts it
193 -execute_thread (void *file_name_)
194 +execute_thread (void *exec_)
196 - char *file_name = file_name_;
197 + struct exec_info *exec = exec_;
198 struct intr_frame if_;
201 @@ -58,10 +78,28 @@ execute_thread (void *file_name_)
202 if_.gs = if_.fs = if_.es = if_.ds = if_.ss = SEL_UDSEG;
204 if_.eflags = FLAG_IF | FLAG_MBS;
205 - success = load (file_name, &if_.eip, &if_.esp);
206 + success = load (exec->file_name, &if_.eip, &if_.esp);
208 + /* Allocate wait_status. */
211 + exec->wait_status = thread_current ()->wait_status
212 + = malloc (sizeof *exec->wait_status);
213 + success = exec->wait_status != NULL;
216 - /* If load failed, quit. */
217 - palloc_free_page (file_name);
218 + /* Initialize wait_status. */
221 + lock_init (&exec->wait_status->lock);
222 + exec->wait_status->ref_cnt = 2;
223 + exec->wait_status->tid = thread_current ()->tid;
224 + sema_init (&exec->wait_status->dead, 0);
227 + /* Notify parent thread and clean up. */
228 + exec->success = success;
229 + sema_up (&exec->load_done);
233 @@ -75,18 +113,47 @@ execute_thread (void *file_name_)
237 +/* Releases one reference to CS and, if it is now unreferenced,
240 +release_child (struct wait_status *cs)
244 + lock_acquire (&cs->lock);
245 + new_ref_cnt = --cs->ref_cnt;
246 + lock_release (&cs->lock);
248 + if (new_ref_cnt == 0)
252 /* Waits for thread TID to die and returns its exit status. If
253 it was terminated by the kernel (i.e. killed due to an
254 exception), returns -1. If TID is invalid or if it was not a
255 child of the calling process, or if process_wait() has already
256 been successfully called for the given TID, returns -1
257 - immediately, without waiting.
259 - This function will be implemented in problem 2-2. For now, it
261 + immediately, without waiting. */
263 -process_wait (tid_t child_tid UNUSED)
264 +process_wait (tid_t child_tid)
266 + struct thread *cur = thread_current ();
267 + struct list_elem *e;
269 + for (e = list_begin (&cur->children); e != list_end (&cur->children);
272 + struct wait_status *cs = list_entry (e, struct wait_status, elem);
273 + if (cs->tid == child_tid)
277 + sema_down (&cs->dead);
278 + exit_code = cs->exit_code;
279 + release_child (cs);
286 @@ -95,8 +162,32 @@ void
289 struct thread *cur = thread_current ();
290 + struct list_elem *e, *next;
293 + printf ("%s: exit(%d)\n", cur->name, cur->exit_code);
295 + /* Close executable (and allow writes). */
296 + file_close (cur->bin_file);
298 + /* Notify parent that we're dead. */
299 + if (cur->wait_status != NULL)
301 + struct wait_status *cs = cur->wait_status;
302 + cs->exit_code = cur->exit_code;
303 + sema_up (&cs->dead);
304 + release_child (cs);
307 + /* Free entries of children list. */
308 + for (e = list_begin (&cur->children); e != list_end (&cur->children);
311 + struct wait_status *cs = list_entry (e, struct wait_status, elem);
312 + next = list_remove (e);
313 + release_child (cs);
316 /* Destroy the current process's page directory and switch back
317 to the kernel-only page directory. */
319 @@ -193,7 +284,7 @@ struct Elf32_Phdr
320 #define PF_W 2 /* Writable. */
321 #define PF_R 4 /* Readable. */
323 -static bool setup_stack (void **esp);
324 +static bool setup_stack (const char *cmd_line, void **esp);
325 static bool validate_segment (const struct Elf32_Phdr *, struct file *);
326 static bool load_segment (struct file *file, off_t ofs, uint8_t *upage,
328 @@ -209,13 +300,15 @@ static bool setup_stack (void **esp);
329 and its initial stack pointer into *ESP.
330 Returns true if successful, false otherwise. */
332 -load (const char *file_name, void (**eip) (void), void **esp)
333 +load (const char *cmd_line, void (**eip) (void), void **esp)
335 struct thread *t = thread_current ();
336 + char file_name[NAME_MAX + 2];
337 struct Elf32_Ehdr ehdr;
338 struct file *file = NULL;
340 bool success = false;
344 /* Allocate and activate page directory. */
345 @@ -224,13 +317,22 @@ load (const char *file_name, void (**eip)
349 + /* Extract file_name from command line. */
350 + while (*cmd_line == ' ')
352 + strlcpy (file_name, cmd_line, sizeof file_name);
353 + cp = strchr (file_name, ' ');
357 /* Open executable file. */
358 - file = filesys_open (file_name);
359 + t->bin_file = file = filesys_open (file_name);
362 printf ("load: %s: open failed\n", file_name);
365 + file_deny_write (file);
367 /* Read and verify executable header. */
368 if (file_read (file, &ehdr, sizeof ehdr) != sizeof ehdr
369 @@ -284,7 +386,7 @@ load (const char *file_name, void (**eip)
373 - if (!setup_stack (esp))
374 + if (!setup_stack (cmd_line, esp))
378 @@ -294,7 +396,6 @@ load (const char *file_name, void (**eip)
381 /* We arrive here whether the load is successful or not. */
386 @@ -393,10 +494,92 @@ load_segment (struct file *file, const s
390 -/* Create a minimal stack by mapping a zeroed page at the top of
391 - user virtual memory. */
392 +/* Reverse the order of the ARGC pointers to char in ARGV. */
394 +reverse (int argc, char **argv)
396 + for (; argc > 1; argc -= 2, argv++)
398 + char *tmp = argv[0];
399 + argv[0] = argv[argc - 1];
400 + argv[argc - 1] = tmp;
404 +/* Pushes the SIZE bytes in BUF onto the stack in KPAGE, whose
405 + page-relative stack pointer is *OFS, and then adjusts *OFS
406 + appropriately. The bytes pushed are rounded to a 32-bit
409 + If successful, returns a pointer to the newly pushed object.
410 + On failure, returns a null pointer. */
412 +push (uint8_t *kpage, size_t *ofs, const void *buf, size_t size)
414 + size_t padsize = ROUND_UP (size, sizeof (uint32_t));
415 + if (*ofs < padsize)
419 + memcpy (kpage + *ofs + (padsize - size), buf, size);
420 + return kpage + *ofs + (padsize - size);
423 +/* Sets up command line arguments in KPAGE, which will be mapped
424 + to UPAGE in user space. The command line arguments are taken
425 + from CMD_LINE, separated by spaces. Sets *ESP to the initial
426 + stack pointer for the process. */
428 +init_cmd_line (uint8_t *kpage, uint8_t *upage, const char *cmd_line,
431 + size_t ofs = PGSIZE;
432 + char *const null = NULL;
433 + char *cmd_line_copy;
434 + char *karg, *saveptr;
438 + /* Push command line string. */
439 + cmd_line_copy = push (kpage, &ofs, cmd_line, strlen (cmd_line) + 1);
440 + if (cmd_line_copy == NULL)
443 + if (push (kpage, &ofs, &null, sizeof null) == NULL)
446 + /* Parse command line into arguments
447 + and push them in reverse order. */
449 + for (karg = strtok_r (cmd_line_copy, " ", &saveptr); karg != NULL;
450 + karg = strtok_r (NULL, " ", &saveptr))
452 + void *uarg = upage + (karg - (char *) kpage);
453 + if (push (kpage, &ofs, &uarg, sizeof uarg) == NULL)
458 + /* Reverse the order of the command line arguments. */
459 + argv = (char **) (upage + ofs);
460 + reverse (argc, (char **) (kpage + ofs));
462 + /* Push argv, argc, "return address". */
463 + if (push (kpage, &ofs, &argv, sizeof argv) == NULL
464 + || push (kpage, &ofs, &argc, sizeof argc) == NULL
465 + || push (kpage, &ofs, &null, sizeof null) == NULL)
468 + /* Set initial stack pointer. */
469 + *esp = upage + ofs;
473 +/* Create a minimal stack for T by mapping a page at the
474 + top of user virtual memory. Fills in the page using CMD_LINE
475 + and sets *ESP to the stack pointer. */
477 -setup_stack (void **esp)
478 +setup_stack (const char *cmd_line, void **esp)
481 bool success = false;
482 @@ -404,9 +587,9 @@ setup_stack (void **esp)
483 kpage = palloc_get_page (PAL_USER | PAL_ZERO);
486 - success = install_page (((uint8_t *) PHYS_BASE) - PGSIZE, kpage, true);
489 + uint8_t *upage = ((uint8_t *) PHYS_BASE) - PGSIZE;
490 + if (install_page (upage, kpage, true))
491 + success = init_cmd_line (kpage, upage, cmd_line, esp);
493 palloc_free_page (kpage);
495 Index: src/userprog/syscall.c
496 diff -u src/userprog/syscall.c~ src/userprog/syscall.c
497 --- src/userprog/syscall.c~
498 +++ src/userprog/syscall.c
500 #include "userprog/syscall.h"
503 #include <syscall-nr.h>
504 +#include "userprog/process.h"
505 +#include "userprog/pagedir.h"
506 +#include "devices/input.h"
507 +#include "filesys/filesys.h"
508 +#include "filesys/file.h"
509 +#include "threads/init.h"
510 #include "threads/interrupt.h"
511 +#include "threads/malloc.h"
512 +#include "threads/palloc.h"
513 #include "threads/thread.h"
514 +#include "threads/vaddr.h"
518 +static int sys_halt (void);
519 +static int sys_exit (int status);
520 +static int sys_exec (const char *ufile);
521 +static int sys_wait (tid_t);
522 +static int sys_create (const char *ufile, unsigned initial_size);
523 +static int sys_remove (const char *ufile);
524 +static int sys_open (const char *ufile);
525 +static int sys_filesize (int handle);
526 +static int sys_read (int handle, void *udst_, unsigned size);
527 +static int sys_write (int handle, void *usrc_, unsigned size);
528 +static int sys_seek (int handle, unsigned position);
529 +static int sys_tell (int handle);
530 +static int sys_close (int handle);
532 static void syscall_handler (struct intr_frame *);
534 +static void copy_in (void *, const void *, size_t);
536 +static struct lock fs_lock;
541 intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall");
542 + lock_init (&fs_lock);
545 +/* System call handler. */
547 +syscall_handler (struct intr_frame *f)
549 + typedef int syscall_function (int, int, int);
551 + /* A system call. */
554 + size_t arg_cnt; /* Number of arguments. */
555 + syscall_function *func; /* Implementation. */
558 + /* Table of system calls. */
559 + static const struct syscall syscall_table[] =
561 + {0, (syscall_function *) sys_halt},
562 + {1, (syscall_function *) sys_exit},
563 + {1, (syscall_function *) sys_exec},
564 + {1, (syscall_function *) sys_wait},
565 + {2, (syscall_function *) sys_create},
566 + {1, (syscall_function *) sys_remove},
567 + {1, (syscall_function *) sys_open},
568 + {1, (syscall_function *) sys_filesize},
569 + {3, (syscall_function *) sys_read},
570 + {3, (syscall_function *) sys_write},
571 + {2, (syscall_function *) sys_seek},
572 + {1, (syscall_function *) sys_tell},
573 + {1, (syscall_function *) sys_close},
576 + const struct syscall *sc;
580 + /* Get the system call. */
581 + copy_in (&call_nr, f->esp, sizeof call_nr);
582 + if (call_nr >= sizeof syscall_table / sizeof *syscall_table)
584 + sc = syscall_table + call_nr;
586 + /* Get the system call arguments. */
587 + ASSERT (sc->arg_cnt <= sizeof args / sizeof *args);
588 + memset (args, 0, sizeof args);
589 + copy_in (args, (uint32_t *) f->esp + 1, sizeof *args * sc->arg_cnt);
591 + /* Execute the system call,
592 + and set the return value. */
593 + f->eax = sc->func (args[0], args[1], args[2]);
596 +/* Returns true if UADDR is a valid, mapped user address,
597 + false otherwise. */
599 +verify_user (const void *uaddr)
601 + return (uaddr < PHYS_BASE
602 + && pagedir_get_page (thread_current ()->pagedir, uaddr) != NULL);
605 +/* Copies a byte from user address USRC to kernel address DST.
606 + USRC must be below PHYS_BASE.
607 + Returns true if successful, false if a segfault occurred. */
609 +get_user (uint8_t *dst, const uint8_t *usrc)
612 + asm ("movl $1f, %%eax; movb %2, %%al; movb %%al, %0; 1:"
613 + : "=m" (*dst), "=&a" (eax) : "m" (*usrc));
617 +/* Writes BYTE to user address UDST.
618 + UDST must be below PHYS_BASE.
619 + Returns true if successful, false if a segfault occurred. */
621 +put_user (uint8_t *udst, uint8_t byte)
624 + asm ("movl $1f, %%eax; movb %b2, %0; 1:"
625 + : "=m" (*udst), "=&a" (eax) : "r" (byte));
629 +/* Copies SIZE bytes from user address USRC to kernel address
631 + Call thread_exit() if any of the user accesses are invalid. */
633 -syscall_handler (struct intr_frame *f UNUSED)
634 +copy_in (void *dst_, const void *usrc_, size_t size)
636 + uint8_t *dst = dst_;
637 + const uint8_t *usrc = usrc_;
639 + for (; size > 0; size--, dst++, usrc++)
640 + if (usrc >= (uint8_t *) PHYS_BASE || !get_user (dst, usrc))
644 +/* Creates a copy of user string US in kernel memory
645 + and returns it as a page that must be freed with
646 + palloc_free_page().
647 + Truncates the string at PGSIZE bytes in size.
648 + Call thread_exit() if any of the user accesses are invalid. */
650 +copy_in_string (const char *us)
655 + ks = palloc_get_page (0);
659 + for (length = 0; length < PGSIZE; length++)
661 + if (us >= (char *) PHYS_BASE || !get_user (ks + length, us++))
664 + if (ks[length] == '\0')
667 + ks[PGSIZE - 1] = '\0';
671 +/* Halt system call. */
678 +/* Exit system call. */
680 +sys_exit (int exit_code)
682 + thread_current ()->exit_code = exit_code;
687 +/* Exec system call. */
689 +sys_exec (const char *ufile)
692 + char *kfile = copy_in_string (ufile);
694 + lock_acquire (&fs_lock);
695 + tid = process_execute (kfile);
696 + lock_release (&fs_lock);
698 + palloc_free_page (kfile);
703 +/* Wait system call. */
705 +sys_wait (tid_t child)
707 + return process_wait (child);
710 +/* Create system call. */
712 +sys_create (const char *ufile, unsigned initial_size)
714 + char *kfile = copy_in_string (ufile);
717 + lock_acquire (&fs_lock);
718 + ok = filesys_create (kfile, initial_size);
719 + lock_release (&fs_lock);
721 + palloc_free_page (kfile);
726 +/* Remove system call. */
728 +sys_remove (const char *ufile)
730 - printf ("system call!\n");
731 + char *kfile = copy_in_string (ufile);
734 + lock_acquire (&fs_lock);
735 + ok = filesys_remove (kfile);
736 + lock_release (&fs_lock);
738 + palloc_free_page (kfile);
743 +/* A file descriptor, for binding a file handle to a file. */
744 +struct file_descriptor
746 + struct list_elem elem; /* List element. */
747 + struct file *file; /* File. */
748 + int handle; /* File handle. */
751 +/* Open system call. */
753 +sys_open (const char *ufile)
755 + char *kfile = copy_in_string (ufile);
756 + struct file_descriptor *fd;
759 + fd = malloc (sizeof *fd);
762 + lock_acquire (&fs_lock);
763 + fd->file = filesys_open (kfile);
764 + if (fd->file != NULL)
766 + struct thread *cur = thread_current ();
767 + handle = fd->handle = cur->next_handle++;
768 + list_push_front (&cur->fds, &fd->elem);
772 + lock_release (&fs_lock);
775 + palloc_free_page (kfile);
779 +/* Returns the file descriptor associated with the given handle.
780 + Terminates the process if HANDLE is not associated with an
782 +static struct file_descriptor *
783 +lookup_fd (int handle)
785 + struct thread *cur = thread_current ();
786 + struct list_elem *e;
788 + for (e = list_begin (&cur->fds); e != list_end (&cur->fds);
791 + struct file_descriptor *fd;
792 + fd = list_entry (e, struct file_descriptor, elem);
793 + if (fd->handle == handle)
800 +/* Filesize system call. */
802 +sys_filesize (int handle)
804 + struct file_descriptor *fd = lookup_fd (handle);
807 + lock_acquire (&fs_lock);
808 + size = file_length (fd->file);
809 + lock_release (&fs_lock);
814 +/* Read system call. */
816 +sys_read (int handle, void *udst_, unsigned size)
818 + uint8_t *udst = udst_;
819 + struct file_descriptor *fd;
820 + int bytes_read = 0;
822 + /* Handle keyboard reads. */
823 + if (handle == STDIN_FILENO)
825 + for (bytes_read = 0; (size_t) bytes_read < size; bytes_read++)
826 + if (udst >= (uint8_t *) PHYS_BASE || !put_user (udst++, input_getc ()))
831 + /* Handle all other reads. */
832 + fd = lookup_fd (handle);
833 + lock_acquire (&fs_lock);
836 + /* How much to read into this page? */
837 + size_t page_left = PGSIZE - pg_ofs (udst);
838 + size_t read_amt = size < page_left ? size : page_left;
841 + /* Check that touching this page is okay. */
842 + if (!verify_user (udst))
844 + lock_release (&fs_lock);
848 + /* Read from file into page. */
849 + retval = file_read (fd->file, udst, read_amt);
852 + if (bytes_read == 0)
856 + bytes_read += retval;
858 + /* If it was a short read we're done. */
859 + if (retval != (off_t) read_amt)
866 + lock_release (&fs_lock);
871 +/* Write system call. */
873 +sys_write (int handle, void *usrc_, unsigned size)
875 + uint8_t *usrc = usrc_;
876 + struct file_descriptor *fd = NULL;
877 + int bytes_written = 0;
879 + /* Lookup up file descriptor. */
880 + if (handle != STDOUT_FILENO)
881 + fd = lookup_fd (handle);
883 + lock_acquire (&fs_lock);
886 + /* How much bytes to write to this page? */
887 + size_t page_left = PGSIZE - pg_ofs (usrc);
888 + size_t write_amt = size < page_left ? size : page_left;
891 + /* Check that we can touch this user page. */
892 + if (!verify_user (usrc))
894 + lock_release (&fs_lock);
898 + /* Do the write. */
899 + if (handle == STDOUT_FILENO)
901 + putbuf (usrc, write_amt);
902 + retval = write_amt;
905 + retval = file_write (fd->file, usrc, write_amt);
908 + if (bytes_written == 0)
909 + bytes_written = -1;
912 + bytes_written += retval;
914 + /* If it was a short write we're done. */
915 + if (retval != (off_t) write_amt)
922 + lock_release (&fs_lock);
924 + return bytes_written;
927 +/* Seek system call. */
929 +sys_seek (int handle, unsigned position)
931 + struct file_descriptor *fd = lookup_fd (handle);
933 + lock_acquire (&fs_lock);
934 + if ((off_t) position >= 0)
935 + file_seek (fd->file, position);
936 + lock_release (&fs_lock);
941 +/* Tell system call. */
943 +sys_tell (int handle)
945 + struct file_descriptor *fd = lookup_fd (handle);
948 + lock_acquire (&fs_lock);
949 + position = file_tell (fd->file);
950 + lock_release (&fs_lock);
955 +/* Close system call. */
957 +sys_close (int handle)
959 + struct file_descriptor *fd = lookup_fd (handle);
960 + lock_acquire (&fs_lock);
961 + file_close (fd->file);
962 + lock_release (&fs_lock);
963 + list_remove (&fd->elem);
968 +/* On thread exit, close all open files. */
972 + struct thread *cur = thread_current ();
973 + struct list_elem *e, *next;
975 + for (e = list_begin (&cur->fds); e != list_end (&cur->fds); e = next)
977 + struct file_descriptor *fd;
978 + fd = list_entry (e, struct file_descriptor, elem);
979 + next = list_next (e);
980 + lock_acquire (&fs_lock);
981 + file_close (fd->file);
982 + lock_release (&fs_lock);
986 Index: src/userprog/syscall.h
987 diff -u src/userprog/syscall.h~ src/userprog/syscall.h
988 --- src/userprog/syscall.h~
989 +++ src/userprog/syscall.h
991 #define USERPROG_SYSCALL_H
993 void syscall_init (void);
994 +void syscall_exit (void);
996 #endif /* userprog/syscall.h */