void load (char *filename, int priority){ PCB *pcb; int SYS_CHECK_ERR = 0; int SYS_LOAD_ERR = 0; int PCB_CREATE_ERR = 0; int pclass = 0; int state = 0; int len_p; int offset_p; int memory_size; unsigned char load_address; unsigned char execution_address; if( name == NULL ) { // No name argument printf("No name supplied.\n"); return PCB_CREATE_ERR; } else if( strlen(name) > 8 ) { // Name too long printf("Specified process name too long.\n"); return PCB_CREATE_ERR; } else if ( !(priority >= -128 && priority <= 127) ) { printf("Invalid priority.\n"); return PCB_CREATE_ERR; } else { SYS_CHECK_ERR = sys_check_program("\0", filename, *len_p, *offset_p); if( !SYS_CHECK_ERR == 0 ) { printf("Program either doesn't exist or is invalid.\n"); return SYS_CHECK_ERR; } else { memory_size = len_p; load_address = (unsigned char*)sys_alloc_mem(memory_size); execution_address = load_address + (unsigned char*)offset_p // We've gotten what we needed and now we can apply it to a PCB. pcb = PCB_setup(filename, priority, pclass); // Place it in appropriate queue PCB_CREATE_ERR = PCB_insert(PRIORITY_QUEUES[state], temp, state); pcb->loadaddr = load_address; pcb->execaddr = execution_address context cp* = (context *) pcb->stack_top; } } }
// loads a program into memory void loadProgram(int argc, char *argv[]){ //name,fileName,priority,path // sets up variables static int count; MEMDSC *tempMem; unsigned char temptop; char *dir, *name, *filename; int size,offset,priority; tcontext *tempContext; unsigned int *tempCS,*tempIP; STACKDSC *temp; // initializes values int err = 0; PCB *newPCB = allocate_PCB(); tempMem=newPCB->memdsc; dir = (char*) sys_alloc_mem(30 * sizeof(char)); name = (char*) sys_alloc_mem(30 * sizeof(char)); filename = (char*) sys_alloc_mem(30 * sizeof(char)); strcpy(dir,argv[4]); strcpy(name,argv[1]); strcpy(filename,argv[2]); priority = atoi(argv[3]); err = sys_check_program(dir,filename,&size,&offset); if((argc==5)||(127<=priority<=-128)&&( err==0)){ //checks for validity /* if( count == ZERO ){ //If first process allocate queue rQueue = (ROOT*) sys_alloc_mem(sizeof(ROOT)); wsQueue = (ROOT*) sys_alloc_mem(sizeof(ROOT)); } */ setup_PCB(newPCB,name,APPLICATION,SUSPENDED_READY,priority); // sets up the adressess newPCB->memdsc->loadADDR= sys_alloc_mem(size); newPCB->memdsc->execADDR=newPCB->memdsc->loadADDR + offset;// is this the correct address? //make sure all registers are properly set newPCB -> stackdsc-> top = newPCB -> stackdsc-> base + STACKSIZE - sizeof(tcontext); tempContext = (tcontext *) (newPCB -> stackdsc-> top); tempContext ->ES = _ES; tempContext ->DS = _DS; tempContext ->CS = FP_SEG(newPCB->memdsc->execADDR); tempContext ->IP = FP_OFF(newPCB->memdsc->execADDR); tempContext ->FLAGS = 0x200; // load the program into memory err = sys_load_program(newPCB->memdsc->loadADDR,size,dir,filename); insert_PCB(newPCB); // put pcb into a queue count++;//Update the number of times the function has run. } else{ printf("Wrong or invalid arguments entered."); } }