diff --git a/src/threads/thread.c b/src/threads/thread.c index 1b8407b1d072f7eafb977925b2a347b115bc655e..1a760bd251b316d46625a7580c6d04bf33eab992 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -321,6 +321,10 @@ thread_exit (void) when it calls thread_schedule_tail(). */ intr_disable (); list_remove (&thread_current()->allelem); + thread_current()->process_exit = true; + if(thread_current() != initial_thread){ + sema_up(&(thread_current()->exit_semaphore)); + } thread_current ()->status = THREAD_DYING; schedule (); NOT_REACHED (); @@ -496,9 +500,11 @@ init_thread (struct thread *t, const char *name, int priority) //t->is_kernel = is_kernel; - old_level = intr_disable (); + //old_level = intr_disable (); list_push_back (&all_list, &t->allelem); - intr_set_level (old_level); + //intr_set_level (old_level); + /* child process list init*/ + list_init(&(t->child_list)); } diff --git a/src/threads/thread.h b/src/threads/thread.h index e814f6b029d3a8eeabeb42a9f45be0b9306c54dd..aba13418b0117bf437dbf0fc9d800d672c4d2bbe 100644 --- a/src/threads/thread.h +++ b/src/threads/thread.h @@ -4,6 +4,8 @@ #include <debug.h> #include <list.h> #include <stdint.h> +#include "threads/synch.h" +#include "lib/kernel/list.h" /* States in a thread's life cycle. */ enum thread_status @@ -81,9 +83,9 @@ typedef int tid_t; ready state is on the run queue, whereas only a thread in the blocked state is on a semaphore wait list. */ struct thread -{ + { /* Owned by thread.c. */ - struct list_elem childelem; + struct list_elem childelem; /* List element for child_list */ tid_t tid; /* Thread identifier. */ enum thread_status status; /* Thread state. */ char name[16]; /* Name (for debugging purposes). */ @@ -93,28 +95,29 @@ struct thread /* Shared between thread.c and synch.c. */ struct list_elem elem; /* List element. */ - + #ifdef USERPROG /* Owned by userprog/process.c. */ uint32_t *pagedir; /* Page directory. */ uint8_t *current_esp; /* The current value of the user program’s stack pointer. - A page fault might occur in the kernel, so we might - need to store esp on transition to kernel mode*/ + A page fault might occur in the kernel, so we might + need to store esp on transition to kernel mode*/ #endif /* Owned by thread.c. */ unsigned magic; /* Detects stack overflow. */ /* VALUE */ - struct file **file_descriptor; - int next_fd; - bool load_success; - bool process_exit; - int process_exit_status; - struct semaphore load_semaphore; - struct semaphore exit_semaphore; - struct thread *parent_thread; - struct list child_list; -}; + struct file **file_descriptor; + int next_fd; + bool load_success; + bool process_exit; + int process_exit_status; + struct semaphore load_semaphore; + struct semaphore exit_semaphore; + struct thread *parent_thread; + struct list child_list; + + }; /* If false (default), use round-robin scheduler. If true, use multi-level feedback queue scheduler. diff --git a/src/userprog/process.c b/src/userprog/process.c index 4cbf0d6cb876caec341ecc27dc893e08b5edcb76..c2f424eb5d2bef8179077d0be6d308c270a12e65 100644 --- a/src/userprog/process.c +++ b/src/userprog/process.c @@ -46,7 +46,7 @@ process_execute(const char *file_name) /* Parse first argument as program name */ strlcpy(program, file_name, file_name_length); strtok_r(program, " ", &ptr); - printf("\nProgram name: %s\n\n", program) ; + printf("\nProgram name: %s\n", program) ; /* Create a new thread to execute FILE_NAME. */ tid = thread_create(file_name, PRI_DEFAULT, start_process, file_copy); @@ -137,7 +137,7 @@ start_process(void *file_name_) char *file_name = file_name_; struct intr_frame if_; bool success; - char *parse[250]; + char *parse[255]; char *token; char *ptr; int count = 0; @@ -159,6 +159,10 @@ start_process(void *file_name_) success = load(parse[0], &if_.eip, &if_.esp); + /* load finished sema up */ + thread_current()->load_success=success; + + /* push arguments */ argument_pushing(&parse, count, &if_.esp); sema_up(&(thread_current()->load_semaphore)); @@ -532,6 +536,9 @@ argument_pushing(char **parse, int count, void **esp) int parse0_address; // First Argument's Adress int address[count]; // Argument adress + if(parse==NULL) + return; + /*Push arguments to stack one by one*/ for (i = count - 1; i > -1; i--) { @@ -543,11 +550,9 @@ argument_pushing(char **parse, int count, void **esp) } /*Store address of argument*/ address[i] = *(unsigned int *)esp; - printf("\nAdress of %d 's argument: %d\n", i + 1, address[i]); + printf("\nAddress of %d 's argument: %d\n", i + 1, address[i]); } - printf("\nNumber of arguments pushed onto stack: %d\n", length); - /* Word Allignment*/ for (i = 0; i < 4 - (length % 4); i++) { @@ -556,28 +561,28 @@ argument_pushing(char **parse, int count, void **esp) } /* Last argument needs to be NULL*/ - *esp -= 4; - **(char ***)esp = 0; + *esp=*esp-4; + **(char* **)esp = 0; /*Push argument adress - Use counter to set **esp */ for (i = count - 1; i >= 0; i--) { - *esp -= 4; + *esp=*esp-4; **(char* **)esp = (char *)address[i]; } - /* Set **argv*/ + /* Set **argv - Main argv*/ parse0_address = *(unsigned int *)esp; - *esp -= 4; + *esp=*esp-4; **(char* **)esp = (char *)parse0_address; - /* Set counter*/ - *esp -= 4; - *(int *)*esp = count; + /* Set counter - Main argc*/ + *esp=*esp-4; + **(int **)esp=count; /*Fake Adress - Set ret*/ - *esp -= 4; - *(int *)*esp = 0; + *esp=*esp-4; + **(int **)esp=0; } /* Create a minimal stack by mapping a zeroed page at the top of diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index 01b74aa46b511befc920115d350c5e596b7e2599..830d961f605d1e42198ba57c54ad603c2d72455b 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -32,39 +32,38 @@ syscall_handler (struct intr_frame *f UNUSED) syscall_number = *(int *)esp; switch (syscall_number) { - case SYS_HALT: // 0 - /*Terminate PintOS*/ - syscall_halt(); - break; + case SYS_HALT: // 0 + /*Terminate PintOS*/ + syscall_halt(); + break; case SYS_EXIT: //1 - get_argument(esp,argument,1); - syscall_exit(argument[0]); - break; + get_argument(esp,argument,1); + syscall_exit(argument[0]); + break; case SYS_WAIT: // 2 - get_argument(esp,argument,1); - f->eax = syscall_wait(argument[0]); - break; + get_argument(esp,argument,1); + f->eax = syscall_wait(argument[0]); + break; case SYS_CREATE: // 3 - get_argument(esp,argument,2); - check_address((void *)argument[0]); - f->eax = syscall_create((const char *)argument[0],(unsigned)argument[1]); - break; + get_argument(esp,argument,2); + check_address((void *)argument[0]); + f->eax = syscall_create((const char *)argument[0],(unsigned)argument[1]); + break; case SYS_REMOVE: // 4 - get_argument(esp,argument,1); - check_address((void *)argument[0]); - f->eax=syscall_remove((const char *)argument[0]); - break; + get_argument(esp,argument,1); + check_address((void *)argument[0]); + f->eax=syscall_remove((const char *)argument[0]); + break; case SYS_WRITE: //5 - get_argument(esp,argument,3); - check_address((void *)argument[1]); - f->eax = syscall_write(argument[0],(void *)argument[1],(unsigned)argument[2]); - break; - /* Unimplemented system calls */ + get_argument(esp,argument,3); + check_address((void *)argument[1]); + f->eax = syscall_write(argument[0],(void *)argument[1],(unsigned)argument[2]); + break; + /* Unimplemented system calls */ default: printf("ERROR: system call ( %d ) has not implemented!\n", syscall_number); - /* Terminate. */ - syscall_exit(-1); + syscall_exit(-1) break; } } diff --git a/src/userprog/syscall.h b/src/userprog/syscall.h index 341169bd8b80edcbcce2148715e4dcf43614c3b9..0c8c352582ee9d7efbbc5d72ad8b6968d1a682f7 100644 --- a/src/userprog/syscall.h +++ b/src/userprog/syscall.h @@ -4,14 +4,10 @@ #include <stdbool.h> #include <stddef.h> -#include "threads/thread.h" -#include <stdbool.h> -#include <stddef.h> - void syscall_init (void); void check_address(void *addr); -void get_argument(void *esp, int *arg, int count); +void get_argument(void *esp, int *argument, int count); void syscall_halt(void); void syscall_exit(int status);