Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //In OperatingSystem.h
- #define MEMEJ1 "MemConfig1stEx"
- #define MEMEJ2 "MemConfig2ndEx"
- enum SystemCallIdentifiers { SYSCALL_PRINTPID=2, SYSCALL_END=3, SYSCALL_YIELD=4, SYSCALL_PRINTEXECPID=5,
- SYSCALL_SLEEP=7, SYSCALL_PRINTSTATUS=100};
- typedef struct {
- int occupied;
- int initAddress; // Lowest physical address of the partition
- int size; // Size of the partition in memory positions
- int PID; // PID of the process using the partition, if occupied
- int times; //Number of times the partition is used
- } PARTITIONDATA;
- //IN OperatingSystem.c
- void OperatingSystem_HandleSystemCall() {
- int sytemCallID;
- // Register A contains the identifier of the issued system call
- sytemCallID=registerA_CPU;
- switch (sytemCallID) {
- case SYSCALL_PRINTEXECPID:
- printf("Process [%d] has the processor assigned\n",executingProcessID);
- break;
- case SYSCALL_END:
- ComputerSystem_DebugMessage(SYSPROC,"sdRsRdRs","[",Clock_GetTime(),"] Process [",executingProcessID,"] has requested to terminate\n");
- OperatingSystem_TerminateProcess();
- break;
- case SYSCALL_YIELD: if(numberOfReadyToRunProcesses[processTable[executingProcessID].queueID] > 0
- && processTable[readyToRunQueue[processTable[executingProcessID].queueID][0]].priority == processTable[executingProcessID].priority)
- {
- int newprocess = OperatingSystem_ShortTermScheduler();
- ComputerSystem_DebugMessage(SHORTTERMSCHEDULE,"sdsGdsGds","[",Clock_GetTime(),"] Process [",executingProcessID,"] transfers control of the processor to process [",newprocess,"]\n");
- OperatingSystem_PreemptRunningProcess();
- OperatingSystem_Dispatch(newprocess);
- OperatingSystem_PrintStatus();
- }
- break;
- //We check there are programs in the given process queue
- //In case there are at least one, check if the priority of both is the same
- //If both conditions fulfills:
- // - Select the new process to be executed
- // - Prints the message
- // - Move the running process to the ready state
- // - Move the new process to the running state
- //Otherwise nothing happens
- case SYSCALL_SLEEP:
- processTable[executingProcessID].whenToWakeUp=abs(registerAccumulator_CPU+clockInterrupts+1);
- OperatingSystem_MoveToTheBlockedState();
- OperatingSystem_PrintStatus();
- break;
- case SYSCALL_PRINTSTATUS:
- OperatingSystem_PrintStatus();
- ComputerSystem_DebugMessage(INTERRUPT,"Ms","Nueva syscall\n");
- break;
- default:
- ComputerSystem_DebugMessage(INTERRUPT,"sdsRdsRds","[",Clock_GetTime(),"] Process [",executingProcessID,
- "] has made an invalid system call (",sytemCallID,") and is being terminated\n");
- OperatingSystem_TerminateProcess();
- break;
- }
- }
- void OperatingSystem_CreateDaemons() {
- USER_PROGRAMS_DATA systemIdleProcess;
- systemIdleProcess.executableName="SystemIdleProcess";
- sipID=OperatingSystem_CreateProcess(systemIdleProcess,DAEMONSQUEUE);
- USER_PROGRAMS_DATA secondDaemon;
- secondDaemon.executableName="SecondDaemon";
- sipID=OperatingSystem_CreateProcess(secondDaemon,DAEMONSQUEUE);
- }
- int OperatingSystem_ObtainMainMemory(int processSize, int PID) {
- int i=0;
- int part=-1;
- int empty=INT_MAX;
- int highest=0;
- ComputerSystem_DebugMessage(SYSMEM,"sdsGdsGds","[",Clock_GetTime(),"] Process [",PID,"] requests [", processSize,"] memory positions \n");
- if(processSize>maxPartitionSize)
- return TOOBIGPROCESS;
- for (i=0; i<numberOfPartitions; i++){
- if(partitionsTable[i].size>=processSize && !partitionsTable[i].occupied && partitionsTable[i].size-processSize<empty){
- if(partitionsTable[i].initAddress>highest){
- empty=partitionsTable[i].size-processSize;
- highest=partitionsTable[i].initAddress;
- part=i;
- }
- }
- }
- if(part>=0){
- OperatingSystem_ShowPartitionTable("before allocating memory");
- ComputerSystem_DebugMessage(SYSMEM,"sdsGdsGdsGdsGds","[",Clock_GetTime(),"] Partition [",part,": ",partitionsTable[part].initAddress, "->",partitionsTable[part].size,"] has been assigned to process [",PID,"]\n");
- partitionsTable[part].PID=PID;
- partitionsTable[part].occupied=1;
- OperatingSystem_ShowPartitionTable("after allocating memory");
- return part;
- }
- else
- return MEMORYFULL;
- }
- int OperatingSystem_LongTermScheduler() {
- int PID;
- int i=0;
- int created=0;
- while (OperatingSystem_GetNewProgram()==1) {
- i=Heap_poll(arrivalTimeQueue,QUEUE_ARRIVAL,numberOfProgramsInArrivalTimeQueue--);
- PID=OperatingSystem_CreateProcess(*userProgramsList[i],USERPROCESSQUEUE);
- if (PID>=0){
- numberOfNotTerminatedProcesses++;
- ComputerSystem_DebugMessage(INIT,"GsGdGsGdGsGsGs","[",Clock_GetTime(),"] Process [",PID,"] created from program [",
- userProgramsList[i]->executableName,"]\n");
- created++;
- partitionsTable[processTable[PID].partition].times=partitionsTable[processTable[PID].partition].times+1;
- }
- else
- OperatingSystem_PrintError(PID, userProgramsList[i]->executableName);
- }
- if (created>0)
- OperatingSystem_PrintStatus();
- // Return the number of succesfully created processes
- return i;
- }
- void OperatingSystem_ShowPartitionTable(char * message){
- ComputerSystem_DebugMessage(SYSMEM,"sdsss","[",Clock_GetTime(),"] Main memory state (",message,"):\n");
- int i=0;
- for(i=0; i<numberOfPartitions; i++){
- if(partitionsTable[i].occupied==1)
- ComputerSystem_DebugMessage(SYSMEM,"sGdsGdsGdsGdsGds","\t[",i,"] [",partitionsTable[i].times," times] [",
- partitionsTable[i].initAddress,"->",partitionsTable[i].size,"] [",partitionsTable[i].PID,"]\n");
- else
- ComputerSystem_DebugMessage(SYSMEM,"sGdsGdsGdsGdsGss","\t[",i,"] [",partitionsTable[i].times," times] [",
- partitionsTable[i].initAddress,"->",partitionsTable[i].size,"] [","AVAILABLE","]\n");
- //ComputerSystem_DebugMessage(SYSMEM,"sGdsGdsGdsGss","\t[",i,"] [",partitionsTable[i].initAddress,"->",
- //partitionsTable[i].size,"] [","AVAILABLE","]\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement