Advertisement
claukiller

Untitled

May 17th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. //In OperatingSystem.h
  2. #define MEMEJ1 "MemConfig1stEx"
  3. #define MEMEJ2 "MemConfig2ndEx"
  4. enum SystemCallIdentifiers { SYSCALL_PRINTPID=2, SYSCALL_END=3, SYSCALL_YIELD=4, SYSCALL_PRINTEXECPID=5,
  5. SYSCALL_SLEEP=7, SYSCALL_PRINTSTATUS=100};
  6.  
  7. typedef struct {
  8. int occupied;
  9. int initAddress; // Lowest physical address of the partition
  10. int size; // Size of the partition in memory positions
  11. int PID; // PID of the process using the partition, if occupied
  12. int times; //Number of times the partition is used
  13. } PARTITIONDATA;
  14.  
  15. //IN OperatingSystem.c
  16. void OperatingSystem_HandleSystemCall() {
  17.  
  18. int sytemCallID;
  19.  
  20. // Register A contains the identifier of the issued system call
  21. sytemCallID=registerA_CPU;
  22.  
  23. switch (sytemCallID) {
  24.  
  25. case SYSCALL_PRINTEXECPID:
  26. printf("Process [%d] has the processor assigned\n",executingProcessID);
  27. break;
  28.  
  29. case SYSCALL_END:
  30. ComputerSystem_DebugMessage(SYSPROC,"sdRsRdRs","[",Clock_GetTime(),"] Process [",executingProcessID,"] has requested to terminate\n");
  31. OperatingSystem_TerminateProcess();
  32. break;
  33.  
  34. case SYSCALL_YIELD: if(numberOfReadyToRunProcesses[processTable[executingProcessID].queueID] > 0
  35. && processTable[readyToRunQueue[processTable[executingProcessID].queueID][0]].priority == processTable[executingProcessID].priority)
  36. {
  37. int newprocess = OperatingSystem_ShortTermScheduler();
  38. ComputerSystem_DebugMessage(SHORTTERMSCHEDULE,"sdsGdsGds","[",Clock_GetTime(),"] Process [",executingProcessID,"] transfers control of the processor to process [",newprocess,"]\n");
  39. OperatingSystem_PreemptRunningProcess();
  40. OperatingSystem_Dispatch(newprocess);
  41. OperatingSystem_PrintStatus();
  42. }
  43. break;
  44. //We check there are programs in the given process queue
  45. //In case there are at least one, check if the priority of both is the same
  46. //If both conditions fulfills:
  47. // - Select the new process to be executed
  48. // - Prints the message
  49. // - Move the running process to the ready state
  50. // - Move the new process to the running state
  51. //Otherwise nothing happens
  52. case SYSCALL_SLEEP:
  53. processTable[executingProcessID].whenToWakeUp=abs(registerAccumulator_CPU+clockInterrupts+1);
  54. OperatingSystem_MoveToTheBlockedState();
  55. OperatingSystem_PrintStatus();
  56. break;
  57. case SYSCALL_PRINTSTATUS:
  58. OperatingSystem_PrintStatus();
  59. ComputerSystem_DebugMessage(INTERRUPT,"Ms","Nueva syscall\n");
  60. break;
  61. default:
  62. ComputerSystem_DebugMessage(INTERRUPT,"sdsRdsRds","[",Clock_GetTime(),"] Process [",executingProcessID,
  63. "] has made an invalid system call (",sytemCallID,") and is being terminated\n");
  64. OperatingSystem_TerminateProcess();
  65. break;
  66. }
  67. }
  68.  
  69. void OperatingSystem_CreateDaemons() {
  70.  
  71. USER_PROGRAMS_DATA systemIdleProcess;
  72.  
  73. systemIdleProcess.executableName="SystemIdleProcess";
  74. sipID=OperatingSystem_CreateProcess(systemIdleProcess,DAEMONSQUEUE);
  75.  
  76. USER_PROGRAMS_DATA secondDaemon;
  77. secondDaemon.executableName="SecondDaemon";
  78. sipID=OperatingSystem_CreateProcess(secondDaemon,DAEMONSQUEUE);
  79. }
  80.  
  81. int OperatingSystem_ObtainMainMemory(int processSize, int PID) {
  82. int i=0;
  83. int part=-1;
  84. int empty=INT_MAX;
  85. int highest=0;
  86.  
  87. ComputerSystem_DebugMessage(SYSMEM,"sdsGdsGds","[",Clock_GetTime(),"] Process [",PID,"] requests [", processSize,"] memory positions \n");
  88. if(processSize>maxPartitionSize)
  89. return TOOBIGPROCESS;
  90. for (i=0; i<numberOfPartitions; i++){
  91. if(partitionsTable[i].size>=processSize && !partitionsTable[i].occupied && partitionsTable[i].size-processSize<empty){
  92. if(partitionsTable[i].initAddress>highest){
  93. empty=partitionsTable[i].size-processSize;
  94. highest=partitionsTable[i].initAddress;
  95. part=i;
  96. }
  97. }
  98. }
  99. if(part>=0){
  100. OperatingSystem_ShowPartitionTable("before allocating memory");
  101. ComputerSystem_DebugMessage(SYSMEM,"sdsGdsGdsGdsGds","[",Clock_GetTime(),"] Partition [",part,": ",partitionsTable[part].initAddress, "->",partitionsTable[part].size,"] has been assigned to process [",PID,"]\n");
  102. partitionsTable[part].PID=PID;
  103. partitionsTable[part].occupied=1;
  104. OperatingSystem_ShowPartitionTable("after allocating memory");
  105. return part;
  106. }
  107. else
  108. return MEMORYFULL;
  109. }
  110.  
  111.  
  112. int OperatingSystem_LongTermScheduler() {
  113.  
  114. int PID;
  115. int i=0;
  116. int created=0;
  117. while (OperatingSystem_GetNewProgram()==1) {
  118. i=Heap_poll(arrivalTimeQueue,QUEUE_ARRIVAL,numberOfProgramsInArrivalTimeQueue--);
  119. PID=OperatingSystem_CreateProcess(*userProgramsList[i],USERPROCESSQUEUE);
  120. if (PID>=0){
  121. numberOfNotTerminatedProcesses++;
  122. ComputerSystem_DebugMessage(INIT,"GsGdGsGdGsGsGs","[",Clock_GetTime(),"] Process [",PID,"] created from program [",
  123. userProgramsList[i]->executableName,"]\n");
  124. created++;
  125. partitionsTable[processTable[PID].partition].times=partitionsTable[processTable[PID].partition].times+1;
  126. }
  127. else
  128. OperatingSystem_PrintError(PID, userProgramsList[i]->executableName);
  129. }
  130.  
  131. if (created>0)
  132. OperatingSystem_PrintStatus();
  133. // Return the number of succesfully created processes
  134. return i;
  135. }
  136.  
  137. void OperatingSystem_ShowPartitionTable(char * message){
  138. ComputerSystem_DebugMessage(SYSMEM,"sdsss","[",Clock_GetTime(),"] Main memory state (",message,"):\n");
  139. int i=0;
  140. for(i=0; i<numberOfPartitions; i++){
  141. if(partitionsTable[i].occupied==1)
  142. ComputerSystem_DebugMessage(SYSMEM,"sGdsGdsGdsGdsGds","\t[",i,"] [",partitionsTable[i].times," times] [",
  143. partitionsTable[i].initAddress,"->",partitionsTable[i].size,"] [",partitionsTable[i].PID,"]\n");
  144. else
  145. ComputerSystem_DebugMessage(SYSMEM,"sGdsGdsGdsGdsGss","\t[",i,"] [",partitionsTable[i].times," times] [",
  146. partitionsTable[i].initAddress,"->",partitionsTable[i].size,"] [","AVAILABLE","]\n");
  147. //ComputerSystem_DebugMessage(SYSMEM,"sGdsGdsGdsGss","\t[",i,"] [",partitionsTable[i].initAddress,"->",
  148. //partitionsTable[i].size,"] [","AVAILABLE","]\n");
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement