Skip to content
Snippets Groups Projects
Commit 1be77d96 authored by Isak Hall's avatar Isak Hall
Browse files

lab3 finally running benim happy

parent 7c2c7b92
No related branches found
No related tags found
No related merge requests found
...@@ -116,17 +116,13 @@ struct child_parent{ ...@@ -116,17 +116,13 @@ struct child_parent{
int exit_status; int exit_status;
int yani_count; int yani_count;
struct list_elem list_elem; struct list_elem list_elem;
struct thread *child; struct thread *child;
struct thread *parent; struct thread *parent;
bool loaded;
tid_t child_tid; tid_t child_tid;
struct lock lock;
struct lock exit_lock;
char *exec_file_name; char *exec_file_name;
//struct semaphore yäni_sema; struct semaphore s;
struct semaphore exit_s;
}; };
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "threads/init.h" #include "threads/init.h"
#include "threads/interrupt.h" #include "threads/interrupt.h"
#include "threads/palloc.h" #include "threads/palloc.h"
#include "threads/malloc.h"
#include "threads/thread.h" #include "threads/thread.h"
#include "threads/vaddr.h" #include "threads/vaddr.h"
#include <stdlib.h> #include <stdlib.h>
...@@ -51,37 +52,30 @@ tid_t process_execute(const char *file_name){ ...@@ -51,37 +52,30 @@ tid_t process_execute(const char *file_name){
return TID_ERROR; return TID_ERROR;
strlcpy (fn_copy, file_name, PGSIZE); strlcpy (fn_copy, file_name, PGSIZE);
//Vi vill initiera vårat reletionship med barnet struct child_parent *relation = (struct child_parent*)malloc(sizeof(struct child_parent));//declare child parent relationship
struct child_parent *relation = malloc(sizeof(struct child_parent));
lock_init(&relation->lock); sema_init(&relation->s, 0);//semaphore that locks before list insertion.
lock_init(&relation->exit_lock); sema_init(&relation->exit_s,0);//semaphore used in process_exit.
relation->exec_file_name = fn_copy; relation->exec_file_name = fn_copy;//file name thats used in execution.
relation->exit_status =-1; relation->exit_status =-1;//used for indication of when we should exit
relation->yani_count = 2; relation->yani_count = 2;//alive count with scuffed name, used to check if the relation is still alive.
thread_current()->child_parent=relation;
relation->parent = thread_current();//set parent to current thread.
//lägga till reltionen till våran lista
relation->parent = thread_current();
/* Create a new thread to execute FILE_NAME. */ /* Create a new thread to execute FILE_NAME. */
tid = thread_create (file_name, PRI_DEFAULT, start_process, relation); tid = thread_create (file_name, PRI_DEFAULT, start_process, relation);
// Använda oss av semaphorer för att se till så att threadsen inte låser sig
lock_acquire(&relation->lock);
if (tid == TID_ERROR){ if (tid == TID_ERROR){
free(relation); free(relation);
palloc_free_page (fn_copy); palloc_free_page (fn_copy);
return tid; return tid;
} }
sema_down(&relation->s);//sema down before critical section.
/* if(!relation -> loaded){
tid = TID_ERROR;
}*/
list_push_back(&thread_current()->barn_list, relation); list_push_back(&thread_current()->barn_list, &relation->list_elem);//insert relation.
return tid; return tid;
} }
...@@ -92,11 +86,13 @@ static void ...@@ -92,11 +86,13 @@ static void
start_process (void *x) start_process (void *x)
{ {
struct child_parent *relation = (struct parent_child*)x; struct child_parent *relation = (struct parent_child*)x;//initiate relation in start process
relation->child= thread_current(); struct thread *child = thread_current();//current thread set to child.
char *file_name = relation -> exec_file_name; relation->child= child;//set the child in relation
char *file_name = relation -> exec_file_name;
struct intr_frame if_; struct intr_frame if_;
bool success; bool success;
child->child_parent=relation;
/* Initialize interrupt frame and load executable. */ /* Initialize interrupt frame and load executable. */
...@@ -104,7 +100,7 @@ start_process (void *x) ...@@ -104,7 +100,7 @@ start_process (void *x)
if_.gs = if_.fs = if_.es = if_.ds = if_.ss = SEL_UDSEG; if_.gs = if_.fs = if_.es = if_.ds = if_.ss = SEL_UDSEG;
if_.cs = SEL_UCSEG; if_.cs = SEL_UCSEG;
if_.eflags = FLAG_IF | FLAG_MBS; if_.eflags = FLAG_IF | FLAG_MBS;
success = load (file_name, &if_.eip, &if_.esp); success = load (file_name, &if_.eip, &if_.esp); //load file_name.
...@@ -116,10 +112,11 @@ start_process (void *x) ...@@ -116,10 +112,11 @@ start_process (void *x)
if (!success) { if (!success) {
//current_thread->child_parent->exit_status = -1; //current_thread->child_parent->exit_status = -1;
sema_up(&relation->s);
thread_exit(); thread_exit();
} }
lock_release(&relation->lock); sema_up(&relation->s); //critical section is done, sema_up to continue.
asm volatile ("movl %0, %%esp; jmp intr_exit" : : "g" (&if_) : "memory"); asm volatile ("movl %0, %%esp; jmp intr_exit" : : "g" (&if_) : "memory");
NOT_REACHED (); NOT_REACHED ();
...@@ -147,35 +144,24 @@ process_exit (void) ...@@ -147,35 +144,24 @@ process_exit (void)
{ {
struct thread *cur = thread_current(); struct thread *cur = thread_current();
struct child_parent *relation = cur->child_parent; struct child_parent *relation = cur->child_parent;
struct list *list = &cur->barn_list; struct list *list = &cur->barn_list;//children list
relation->yani_count--;//when we are in exit it is time do remove relation
/*if (relation != NULL)
{
sema_down(&relation->yani_sema);
//check if parent bond is dead
//relation->yani_count--;
if (relation->yani_count == 0)
{
free(relation);
}
else{
sema_up(&relation->yani_sema);
}
}*/
while(!list_empty(list)){ while(!list_empty(list)){
struct list_elem *e = list_pop_front(list); struct list_elem *e = list_pop_front(list);//remove first element from list
relation = list_entry(e,struct child_parent, list_elem); relation = list_entry(e,struct child_parent, list_elem); //set relation.
lock_acquire(&relation->exit_lock); sema_down(&relation->exit_s);//enter critical section where we change variables inside of the relation
relation->yani_count--; relation->yani_count--;
relation->exit_status=cur->status;
if (relation->yani_count == 0){ if (relation->yani_count == 0){
free(relation); free(relation);//free the relation that we malloced earlier.
} }
else{ else{
lock_release(&relation->exit_lock); sema_up(&relation->exit_s);//critical section done.
} }
relation->exit_status=cur->status; }
if(relation->yani_count==0){
free(relation);
} }
uint32_t *pd; uint32_t *pd;
......
...@@ -84,7 +84,7 @@ syscall_handler (struct intr_frame *f UNUSED) ...@@ -84,7 +84,7 @@ syscall_handler (struct intr_frame *f UNUSED)
} }
case(SYS_EXEC):{ case(SYS_EXEC):{
const char *cmd_line = *((char**)(f->esp + 4)); const char *cmd_line = *(char**)(f->esp + 4);
f->eax = exec(cmd_line); f->eax = exec(cmd_line);
break; break;
} }
...@@ -97,7 +97,7 @@ void halt (void){ ...@@ -97,7 +97,7 @@ void halt (void){
void exit (int status){ void exit (int status){
/* Exit status */ /* Exit status */
//thread_current()->child_parent->exit_status = status; thread_current()->child_parent->exit_status = status;
/* For make check */ /* For make check */
//printf("%s: exit(%d)\n", thread_current()->name, status); //printf("%s: exit(%d)\n", thread_current()->name, status);
...@@ -211,11 +211,11 @@ tid_t exec(const char *cmd_line){ ...@@ -211,11 +211,11 @@ tid_t exec(const char *cmd_line){
} }
bool validate(int fd){ bool validate(int fd){
if (fd >= 2 && fd <=130) if (fd < 0 || fd >= 130)
{ {
return true; return false;
} }
else{ else{
return false; return true;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment