/* * Create a proc structure. */ static struct proc * proc_create(const char *name) { struct proc *proc; proc = kmalloc(sizeof(*proc)); if (proc == NULL) { return NULL; } proc->p_name = kstrdup(name); if (proc->p_name == NULL) { kfree(proc); return NULL; } threadarray_init(&proc->p_threads); spinlock_init(&proc->p_lock); /* VM fields */ proc->p_addrspace = NULL; /* VFS fields */ proc->p_cwd = NULL; #ifdef UW proc->console = NULL; #endif // UW return proc; }
/* * Create a proc structure. */ static struct proc * proc_create(const char *name) { struct proc *proc; proc = kmalloc(sizeof(*proc)); if (proc == NULL) { return NULL; } proc->p_name = kstrdup(name); if (proc->p_name == NULL) { kfree(proc); return NULL; } threadarray_init(&proc->p_threads); spinlock_init(&proc->p_lock); /* VM fields */ proc->p_addrspace = NULL; /* VFS fields */ proc->p_cwd = NULL; #if OPT_A2 //for some reason we cannot do vfs_open in here. proc->filetable = array_create(); array_setsize(proc->filetable, 128); for(int i = 0; i < 128; i++) { array_set(proc->filetable, i, NULL); } proc->opencloselock = lock_create("opencloselock"); #endif #ifdef UW proc->console = NULL; #endif // UW return proc; }
/* * Create a proc structure. */ static struct proc * proc_create(const char *name) { struct proc *proc; proc = kmalloc(sizeof(*proc)); if (proc == NULL) { return NULL; } proc->p_name = kstrdup(name); if (proc->p_name == NULL) { kfree(proc); return NULL; } threadarray_init(&proc->p_threads); spinlock_init(&proc->p_lock); // added by jon-bassi #if OPT_A2 proc->sem_running = sem_create("sem_running", 1); proc->sem_waiting = sem_create("sem_waiting processes", 0); #endif /* VM fields */ proc->p_addrspace = NULL; /* VFS fields */ proc->p_cwd = NULL; #ifdef UW proc->console = NULL; #endif // UW #if OPT_A2 proc->pid = addProc(proc); #endif return proc; }
/* * Create a proc structure. */ static struct proc * proc_create(const char *name) { struct proc *proc; proc = kmalloc(sizeof(*proc)); if (proc == NULL) { return NULL; } proc->p_name = kstrdup(name); if (proc->p_name == NULL) { kfree(proc); return NULL; } threadarray_init(&proc->p_threads); spinlock_init(&proc->p_lock); #if OPT_A2 int result; proc->proc_f_table=f_table_create(void); result=console_init(proc->proc_f_table); if(result){ //do stuff; //prob kill the process. } //add stuff. #endif /* VM fields */ proc->p_addrspace = NULL; /* VFS fields */ proc->p_cwd = NULL; return proc; }
/* * Create a proc structure. */ static struct proc * proc_create(const char *name) { struct proc *proc; proc = kmalloc(sizeof(*proc)); if (proc == NULL) { return NULL; } proc->p_name = kstrdup(name); if (proc->p_name == NULL) { kfree(proc); return NULL; } threadarray_init(&proc->p_threads); spinlock_init(&proc->p_lock); /* VM fields */ proc->p_addrspace = NULL; /* VFS fields */ proc->p_cwd = NULL; proc->p_filetable = NULL; //if pid table is not initialized yet, initialize it if(init == 0){ pid_init(); //add entry of the first process struct processID *info = (struct processID *)kmalloc(sizeof(struct processID)); info->pid = PID_MIN - 1; info->ppid = 0; info->exited = false; info->exitCode = 0; info->sem_wait = sem_create("wait", 1); info->sem_exit = sem_create("exit", 1); pidTable[PID_MIN - 1] = info; proc->pid = PID_MIN - 1; } // if not first time executing, create new entry for process table if(init == 1){ pid_t count;; for(count = PID_MIN; count < PID_MAX; count++){ if(pidTable[count] == NULL){ break; } } //put entry in table struct processID *info = (struct processID *)kmalloc(sizeof(struct processID)); info->pid = count; info->ppid = 0; info->exited = false; info->exitCode = 0; info->sem_wait = sem_create("wait", 1); info->sem_exit = sem_create("exit", 1); pidTable[count] = info; proc->pid = count; } init = 1; return proc; }
/* * Create a proc structure. */ static struct proc * proc_create(const char *name) { struct proc *proc; proc = kmalloc(sizeof(*proc)); if (proc == NULL) { return NULL; } proc->p_name = kstrdup(name); if (proc->p_name == NULL) { kfree(proc); return NULL; } /* File Descriptor Table */ proc->p_fd_table = fd_table_create(proc); if(proc->p_fd_table == NULL){ kfree(proc->p_name); kfree(proc); return NULL; } // add file descriptors for stdin, stdoud, stderr // we can not do this for the kernel process since VFS is not yet bootstrapped if(kproc!=NULL){ int fdi_0, fdi_1, fdi_2; int temp_res = 0; char* console_0 = NULL; console_0 = kstrdup("con:"); char* console_1 = NULL; console_1 = kstrdup("con:"); char* console_2 = NULL; console_2 = kstrdup("con:"); temp_res = fd_open(proc->p_fd_table, console_0, O_RDONLY, &fdi_0); KASSERT(temp_res == 0); temp_res = fd_open(proc->p_fd_table, console_1, O_WRONLY, &fdi_1); KASSERT(temp_res == 0); temp_res = fd_open(proc->p_fd_table, console_2, O_WRONLY, &fdi_2); KASSERT(temp_res == 0); KASSERT(fdi_2 ==2); } threadarray_init(&proc->p_threads); spinlock_init(&proc->p_lock); /* VM fields */ proc->p_addrspace = NULL; /* VFS fields */ proc->p_cwd = NULL; /* Process ID */ proc->PID = get_new_process_id(); /* Child list */ //proclist_init(&proc->p_childlist); proc->p_childlist = list_create(); proc->p_parent = NULL; proc->p_returnvalue = 0; proc->p_childlist_lock = lock_create(name); proc->p_childlist_lock = lock_create(name); proc->p_exit_sem_child = sem_create("wait_sem_child", 0); proc->p_exit_sem_parent = sem_create("wait_sem_parent", 0); return proc; }
pid_t sys_fork(struct trapframe *tf) { if(procarray_num(procarr) == PID_MAX - PID_MIN) { errno = EMPROC; return 0; } struct proc *newproc = kmalloc(sizeof(*newproc)); // normal field newproc->p_name = kstrdup(curthread->t_proc->p_name); // name newproc->p_cwd = curproc->p_cwd; // vnode // synch field spinlock_init(&newproc->p_lock); newproc->p_cv = cv_create("proc cv"); newproc->p_lk = lock_create("proc lock"); newproc->open_num = 0; // files field newproc->fd_table = kmalloc(sizeof(struct fd *) * MAX_fd_table); for(unsigned i = 0 ; i < MAX_fd_table ; i++) { newproc->fd_table[i] = curproc->fd_table[i]; } // pid creation for(pid_t curid = PID_MIN ; curid <= PID_MAX ; curid++) { // loop through available pid range. if(find_proc(curid) == NULL) { // first available pid found. newproc->p_pid = curid; // set pid. break; // break loop. } } threadarray_init(&newproc->p_threads); spinlock_init(&newproc->p_lock); // copy threads // struct threadarray oldthreads = curproc->p_threads; pid_t result; procarray_add(procarr,newproc,NULL); struct exitc *c = kmalloc(sizeof(struct exitc)); c->exitcode = -1; c->pid = newproc->p_pid; exitcarray_add(codes,c,NULL); // fork thread // struct thread *t = threadarray_get(&oldthreads,0); // copy trapframe to heap. struct trapframe *ctf = kmalloc(sizeof(struct trapframe)); ctf->tf_vaddr = tf->tf_vaddr; ctf->tf_status = tf->tf_status; ctf->tf_cause = tf->tf_cause; ctf->tf_lo = tf->tf_lo; ctf->tf_hi = tf->tf_hi; ctf->tf_ra = tf->tf_ra; ctf->tf_at = tf->tf_at; ctf->tf_v0 = tf->tf_v0; ctf->tf_v1 = tf->tf_v1; ctf->tf_a0 = tf->tf_a0; ctf->tf_a1 = tf->tf_a1; ctf->tf_a2 = tf->tf_a2; ctf->tf_a3 = tf->tf_a3; ctf->tf_t0 = tf->tf_t0; ctf->tf_t1 = tf->tf_t1; ctf->tf_t2 = tf->tf_t2; ctf->tf_t3 = tf->tf_t3; ctf->tf_t4 = tf->tf_t4; ctf->tf_t5 = tf->tf_t5; ctf->tf_t6 = tf->tf_t6; ctf->tf_t7 = tf->tf_t7; ctf->tf_s0 = tf->tf_s0; ctf->tf_s1 = tf->tf_s1; ctf->tf_s2 = tf->tf_s2; ctf->tf_s3 = tf->tf_s3; ctf->tf_s4 = tf->tf_s4; ctf->tf_s5 = tf->tf_s5; ctf->tf_s6 = tf->tf_s6; ctf->tf_s7 = tf->tf_s7; ctf->tf_t8 = tf->tf_t8; ctf->tf_t9 = tf->tf_t9; ctf->tf_k0 = tf->tf_k0; ctf->tf_k1 = tf->tf_k1; ctf->tf_gp = tf->tf_gp; ctf->tf_sp = tf->tf_sp; ctf->tf_s8 = tf->tf_s8; ctf->tf_epc = tf->tf_epc; //int spl = splhigh(); result = thread_fork(curthread->t_proc->p_name, newproc, enter_forked_process, ctf, (unsigned long)curproc->p_addrspace ); // parent return. //splx(spl); return newproc->p_pid; }