KOS() { /* Semaphores */ writeOK = make_kt_sem(0); writers = make_kt_sem(1); readers = make_kt_sem(1); nElem = make_kt_sem(0); consoleWait = make_kt_sem(0); /* Generics */ sys_stop_read = 0; current_pid = 0; console_size = 256; buffer_head, buffer_tail; // Zero out memory bzero(main_memory, MemorySize); bzero(memory_space_array, 8); /* Initializers */ currentProcess = (PCB *) malloc(sizeof(PCB)); initialize_console_buffer(&buffer_head, &buffer_tail); readyQ = new_dllist(); found_node = make_jrb(); pid_tree = make_jrb(); kt_fork(initialize_user_process, (void *)kos_argv); kt_fork(console_buf_read, (void *)kos_argv[0]); kt_joinall(); start_timer(10); scheduler(); }
KOS() { writeok = make_kt_sem(0); writers = make_kt_sem(1); readers = make_kt_sem(1); nelem = make_kt_sem(0); consoleWait = make_kt_sem(0); sysStopRead = 0; consoleSize = BUFSIZE; bzero(main_memory, MemorySize); current = (PCB *) malloc(sizeof(PCB)); initialize_console_buffer(&consoleBufferHead, &consoleBufferTail); queue = new_dllist(); kt_fork(initialize_user_process, NULL); kt_fork(console_buf_read, NULL); kt_joinall(); scheduler(); }
//static char *Argv[5] = {"argtest","Rex", "my", "man",NULL}; KOS() { //exitSignal = 0; // printf("Starting kos\n"); bzero(main_memory, MemorySize); //make a PCB curProc = (PCB *) malloc(sizeof(PCB)); //make a dllist(readyq) which holds user processes to be run readyq = new_dllist(); //circular console size, head, and tail cbSize = 256; //Step 16 cbHead = 0; cbTail = 0; //step 14: should be initialized to 1 writers = make_kt_sem(1); readers = make_kt_sem(1); writeok = make_kt_sem(0); //step 17: consoleWait= make_kt_sem(0); nelem = make_kt_sem(0); nslots = make_kt_sem(256); //printf("Before Forking\n"); //Step 10: pids curpid = 0; rbtree = make_jrb(); foundPid = make_jrb(); //has to be its own mini tree //Lab4 globalFD = 0; //Step 12: Memory splitting 8 bzero(mem8,8); //fork to the file with args provided, just loading it kt_fork(initialize_user_process, kos_argv); //Step 4: fork to the file with args provided, now reading it, start from the very beginning kos_argv[0] kt_fork(console_buffer, kos_argv[0]); //now join all the threads kt_joinall(); //call the scheduler // start_timer(10); //scheduler(); scheduler(); }
void *initialize_user_process(void *arg) { // printf("Enters initalize_user_process\n"); int i; char **filename = (char **)arg; int argc = 0; while (filename[argc]!=NULL) { printf("This is filename[%i]: %s\n", argc, filename[argc]); argc++; } // printf("This is argc in init: %i\n", argc); //Step 19: putting in argc //k = WordToMachine(argc); //memcpy(main_memory+MemorySize-40+12, &k, 4); //L3 Step 18: start first process, is this init? init=(PCB *)malloc(sizeof(PCB)); init->registers = (int *)malloc(NumTotalRegs*sizeof(int)); for (i=0; i < NumTotalRegs; i++) { init->registers[i] = 0; } init->pid = (int *)0; // printf("This is init's pid: %i\n", (int)init->pid); //L3 Step 19: init->waiter_sem = make_kt_sem(0); init->waiters = new_dllist(); init->children = make_jrb(); //Allocate a new PCB PCB *temp=(PCB *)malloc(sizeof(PCB)); temp->registers = (int *)malloc(NumTotalRegs*sizeof(int)); temp->user_base = 0; //printf("Initial user_base: %i\n", temp->user_base); //Changed Step 12: temp->user_limit = MemorySize-2048; temp->user_limit = MemorySize/8; //printf("Initial user_limt: %i\n", temp->user_limit); //L3 Step 18: temp->parent = init; //L3 Step 19: temp->waiter_sem = make_kt_sem(0); temp->waiters = new_dllist(); //L3 Step 21: make rb tree for children temp->children = make_jrb(); //Changed at Step 12: User_Base = temp->user_base; User_Base = memory8(); User_Limit = temp->user_limit; //printf("This is User_Base in initialize: %i\n", User_Base); //printf("This is User_Limit in initialize: %i\n", User_Limit); //set the regs of the //printf("Setting all the registers to 0 in initalize_user_process\n"); for (i=0; i < NumTotalRegs; i++) temp->registers[i] = 0; //printf("Setting pid in init\n"); temp->pid = (int *)get_new_pid(); printf("First Pid: %i and its parent's should be 0 init: %i\n", temp->pid, temp->parent->pid ); /* set up the program counters and the stack register */ temp->registers[PCReg] = 0; temp->registers[NextPCReg] = 4; //insert the first process as init's child; WOW you can use this function! Jval tempN = new_jval_v((void*)temp); //can only insert Jvals jrb_insert_int(init->children, (int)temp->pid, tempN); //JRB tree, int ikey, Jval val //JRB ptr; // jrb_traverse(ptr, init->children) //{ //printf("This is child pid %i of init %i\n", ptr->key, (int)init->pid); //} //perform_execve(job, fn, argv) // where job is the new PCB, fn is the name of your initial executable (at this point, mine is a.out), //and argv is the argv of this initial job. //returns-> 0 success, errno if error //PrintStack(temp->registers[StackReg], temp->user_base); int errno = perform_execve(temp, filename[0],filename); // printf("This is perform_execve: %i\n", errno); //returns to initialize_user_process(), it either exits because there was an error, or it puts the new job onto the ready queue and calls kt_exit() PrintStack(temp->registers[StackReg], temp->user_base); if (errno!=0) { printf("Perform_execve returned unsucessful\n"); kt_exit(); } //printf("Placing jval into queue\n"); Jval value = new_jval_v((void *)temp); //value.v = temp; //printf("This is the value being placed into the readyq in the initalize_user_process: %s\n",value.v ); dll_append(readyq, value); // printf("Program %s loaded\n", kos_argv[0]); kt_exit(); }
void *initialize_user_process(void *arg){ PCB *user_process; Jval jval_pcb; Jval user_process_child_node; char **filename; int base; int arg_count; int errno; int i; arg_count = 0; errno = 0; i = 0; /* Unmarshalling */ filename = (char **) arg; /* Create the global init process */ init = (PCB *)malloc(sizeof(PCB)); init->registers = (int *)malloc(sizeof(int) * NumTotalRegs); // Initialize init's registers */ for (i = 0; i < NumTotalRegs; i++) { init->registers[i] = 0; } // Initialize init's other variables init->pid = (int *)0; init->waiter = make_kt_sem(0); init->waiters = new_dllist(); init->children = make_jrb(); /* Allocate new PCB and initialize registers */ user_process = (PCB *)malloc(sizeof(PCB)); user_process->registers = (int *)malloc(NumTotalRegs * sizeof(int)); for (i = 0; i < NumTotalRegs; i++) { user_process->registers[i] = 0; } // Obtain pid and partiton memory for new process user_process->pid = (int *)get_new_pid(); base = partition_memory(user_process->pid); // Initialize first process' other variables user_process->waiter = make_kt_sem(0); user_process->waiters = new_dllist(); user_process->children = make_jrb(); // Set base and limit for new process user_process->base = 0; user_process->limit = MemorySize/8; // Set User global vars User_Base = base; User_Limit = user_process->limit; // Set process' PCRegs user_process->registers[PCReg] = 0; user_process->registers[NextPCReg] = 4; // Set first process' parent as init and add to init's children user_process->parent = (struct PCB *)init; user_process_child_node.v = user_process; jrb_insert_int(init->children, (int)user_process->pid, user_process_child_node); // Get number of args and turn off exec flag. while (filename[arg_count] != NULL) { arg_count++; } totalArgs = arg_count; is_exec = 0; /* Call perform_execve */ errno = perform_execve(user_process, filename[0], filename); // If errors, returns proper errno if (errno != 1) { syscall_return(user_process, errno); } else { // Puts new job onto readyQ and calls kt_exit() jval_pcb.v = user_process; dll_append(readyQ, jval_pcb); kt_exit(); } }