943,940 Members | Top Members by Rank

Ad:
  • C Code Snippet
  • Views: 1726
  • C RSS
-1

Linked list coding for CPU scheduling algorithms help with pointers

by on Nov 1st, 2009
  1. struct PCB* handleProcessArrival_PP(struct PCB *processhead,struct PCB *processtail,struct PCB *currProcess,struct PCB *newProcess,int currTime){
  2. if(currProcess==NULL){
  3. newProcess->executionStartTime = currTime;
  4. newProcess->executionEndTime = currTime+newProcess->totalBurstTime;
  5. newProcess->remainingBurstTime = newProcess->totalBurstTime;
  6. if(newProcess->processID==processhead->processID){
  7. processhead= newProcess->next;
  8. printf("processhead in case 1 %d \n",processhead->processID);
  9. }
  10. currProcess=newProcess;
  11. printf("***current process = new process%d \n",currProcess->processID);
  12. contents(processhead);
  13. return currProcess;
  14. //return processhead;
  15.  
  16. }
  17. if (currProcess->processID !=0){
  18. contents(processhead);
  19. if(currProcess->processPriority>newProcess->processPriority){//new process higher priority
  20. printf("***new process in handle %d \n",newProcess->processID);
  21. currProcess->executionStartTime = 0;
  22. currProcess->executionEndTime = 0;
  23. currProcess->remainingBurstTime = currProcess->totalBurstTime-(currProcess->executionStartTime-currTime);
  24. processtail->next=currProcess;
  25. processtail=currProcess;
  26. newProcess->executionStartTime=currTime;
  27. newProcess->executionEndTime = currTime+newProcess->totalBurstTime;
  28. newProcess->remainingBurstTime = newProcess->totalBurstTime;
  29. currProcess=newProcess;
  30. printf("current process replaced with%d \n",newProcess->processID);
  31. printf("processhead in case 2 %d \n",processhead->processID);
  32. return currProcess;
  33. }
  34. if(currProcess->processPriority<newProcess->processPriority){//new process lower priority
  35. contents(processhead);
  36. newProcess->executionStartTime = 0;
  37. newProcess->executionEndTime = 0;
  38. newProcess->remainingBurstTime = newProcess->totalBurstTime;
  39. processtail->next=newProcess;
  40. processtail=newProcess;
  41. printf("new process in handle %d \n",newProcess->processID);
  42. printf("current process continues%d \n",currProcess->processID);
  43. printf("processhead in case 3 %d \n",processhead->processID);
  44. return currProcess;
  45. }
  46.  
  47. }
  48. /* printf("process id = %d \n",currProcess.processID);
  49.   printf("arrival time = %ld \n",currProcess.arrivalTimeStamp);
  50.   printf("total burst time = %ld \n",currProcess.totalBurstTime);
  51.   printf("start time = %ld \n",currProcess.executionStartTime);
  52.   printf("end time = %ld \n",currProcess.executionEndTime);
  53.   printf("remaining burst time = %ld \n",currProcess.remainingBurstTime);
  54.   printf("priority = %d \n",currProcess.processPriority);
  55. */
  56. }
  57. struct PCB* handleProcessCompletion_PP(struct PCB *processhead,int currTime){
  58. if (processhead==NULL){
  59. return NULL;
  60. }else{
  61. contents(processhead);
  62. printf("in else processhead is %d \n",processhead->processID);
  63. struct PCB *processtemp;
  64. processtemp=processhead;
  65. int len=length(processhead);
  66. struct PCB *procArray[len];
  67. int i=0;
  68. printf("test");
  69. while(i<len){
  70. procArray[i] = processtemp;
  71. printf("process put in array %d",procArray[i]->processID);
  72. processtemp=processtemp->next;
  73. i++;
  74. }
  75. int min =0;
  76. for(i=0;i<len;i++){
  77. if(procArray[min]->processPriority > procArray[i]->processPriority){
  78. min = i;
  79. }
  80. }
  81. printf("high priority process %d",procArray[min]->processID);
  82. procArray[min]->executionStartTime = currTime;
  83. procArray[min]->executionEndTime = currTime+procArray[min]->remainingBurstTime;
  84. procArray[min-1]->next=procArray[min+1];
  85. printf("process to run next is %d",procArray[min]->processID);
  86. return procArray[min];
  87. //return processhead;
  88. }
  89. }
C Code Snippet (Toggle Plain Text)
  1. Hi All
  2.  
  3. I am trying to code for CPU scheduling algorithms in C using linked list...In the code snippet below Im maintaining a linked list of the processes
  4. and processhead points to head of the list currProcess is currently running and newprocess is newly arriving..this code is for priority preemptive scheduling
  5.  
  6. The problem is as I modify the processhead to point to a different process in the function the change is effected in processhead inside the handle function however in main the value is the same as it was before the function was called.
  7. I thought the basic purpose of pointers was to not have this problem.I might be wrong in the code as I am new to working with pointers. Please help and yeah this is my assignment and I have written the code myself.
Message:
Previous Thread in C Forum Timeline: kernel modules
Next Thread in C Forum Timeline: Calulator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC