Trouble with Pointers

Reply

Join Date: Aug 2006
Posts: 3
Reputation: knorden is an unknown quantity at this point 
Solved Threads: 0
knorden knorden is offline Offline
Newbie Poster

Trouble with Pointers

 
0
  #1
Aug 1st, 2006
Hi all. I am new to C, at least to the concept of pointers. I am trying to write a process scheduling simulator for my operating systems class (at the last minute of course). Anyway, I wrote a program just using arrays and got it to compile but then of course I got a Segmentation Fault so I am trying to start fresh using pointers. Right now, all I want to do is take the input file and add the pieces of input into structs (this part works) but then when I add a for loop to go through the structs again and increment values, etc. I get another segmentation fault. Here is the code:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int p, clock;
  5. int addition, numJobs;
  6. int i, j, availableMem;
  7.  
  8. // Enum, structure declarations
  9. enum state {
  10. NOTARRIVED,
  11. NOTINSYSTEM,
  12. READYTORUN,
  13. SLEEPINGIO,
  14. RUNNING,
  15. EXITED
  16. } currentState;
  17.  
  18. /*******************************************************
  19. For each process:
  20. unique PID, arrival time, service time, priority rating,
  21. memory allocation, no start, time in IO state,
  22. time in Ready to Run state, time in Running state,
  23. timeslice
  24. *******************************************************/
  25. typedef struct
  26. {
  27. int pid;
  28. int arrivalTime;
  29. int serviceTime;
  30. int priorityRating;
  31. int memoryAllocation;
  32. int noStart;
  33. int timeInIO;
  34. int timeInReadytoRun;
  35. int timeslice;
  36. int memTaken;
  37. enum state currentState;
  38. } process;
  39.  
  40. typedef struct
  41. {
  42. process elems[1000];
  43. int back; // Initialized to -1
  44. int front; // Initialized to 0
  45. } queue;
  46.  
  47. /* Input processes from data file into array of structs:
  48.   Process ID : Arrival Time : Service Time : Priority : Memory Requirement in MB */
  49. process *proc[20];
  50.  
  51. /* Priority Queues: 0, 1, 2, 3, 4, I/O queue, Not In System queue. */
  52. queue queue0, queue1, queue2, queue3, queue4, queueIO, queueNoMem;
  53.  
  54. /* Begin Main */
  55. int main(int argc, char *argv[0])
  56. {
  57. char line[80];
  58. FILE *inputFile, *outputFile;
  59.  
  60. inputFile = fopen("input_file.txt", "r");
  61.  
  62. if (inputFile == NULL)
  63. {
  64. fprintf(stderr, "Can't open input file!\n");
  65. exit(1);
  66. }
  67.  
  68. outputFile = fopen("output_file.txt", "w");
  69.  
  70. if (outputFile == NULL)
  71. {
  72. fprintf(stderr, "Can't open output file!\n");
  73. exit(1);
  74. }
  75.  
  76. p = 0;
  77. clock = 0;
  78.  
  79. fprintf(outputFile, "Input File: \n");
  80.  
  81. while(fgets(line, 80, inputFile))
  82. {
  83. int uniquePID, arriveTime, serviceTime, priority, memAlloc;
  84.  
  85. sscanf(line, "%d : %d : %d : %d : %d", &uniquePID, &arriveTime, &serviceTime, &priority, &memAlloc);
  86.  
  87. proc[p] = (process *)malloc(sizeof(process));
  88.  
  89. proc[p]->pid = uniquePID;
  90. proc[p]->arrivalTime = arriveTime;
  91. proc[p]->serviceTime = serviceTime;
  92. proc[p]->priorityRating = priority;
  93. proc[p]->memoryAllocation = memAlloc;
  94. proc[p]->noStart = 0;
  95. proc[p]->timeInIO = 0;
  96. proc[p]->timeInReadytoRun = 0;
  97. proc[p]->timeslice = 0;
  98. proc[p]->currentState = NOTARRIVED;
  99.  
  100. // LATER -> free(proc[p]);
  101.  
  102. addition = proc[p]->memoryAllocation%4;
  103. proc[p]->memTaken = proc[p]->memoryAllocation + addition;
  104.  
  105. fprintf(outputFile, "PID = %d Arrival Time = %d Service Time = %d Priority = %d Memory Needed = %d \n",
  106. proc[p]->pid, proc[p]->arrivalTime, proc[p]->serviceTime, proc[p]->priorityRating, proc[p]->memoryAllocation);
  107.  
  108. p++;
  109. }
  110.  
  111. // Set total number of jobs
  112. numJobs = p;
  113.  
  114. fprintf(outputFile, "Number of jobs: %d \n", numJobs);
  115.  
  116. // Set memory size for entire simulation
  117. availableMem = 32;
  118.  
  119. fprintf(outputFile, "Available Memory: %d \n", availableMem);
  120.  
  121. // THIS IS WHERE I THINK THE SEGMENTATION FAULT OCCURS
  122. // WHEN I TRY TO ACCESS THE PIECES IN THE STRUCTS
  123. // Add new incoming jobs to the "Ready to Run" state
  124. for(i = 1; i <= numJobs; i++)
  125. {
  126. if(availableMem > proc[i]->memTaken)
  127. {
  128. // Allocate necessary memory for this process
  129. availableMem = availableMem - proc[i]->memTaken;
  130.  
  131. // Set the state to Ready to Run
  132. proc[i]->currentState = READYTORUN;
  133. }
  134. else
  135. {
  136. // Keep this job out of the system until
  137. // more memory is available
  138. proc[i]->currentState = NOTINSYSTEM;
  139. proc[i]->noStart++;
  140. }
  141. }
  142. }

Any advice, tips would be greatly appreciated! Thanks so much!
Last edited by knorden; Aug 1st, 2006 at 4:38 am. Reason: Posted it by accident before I was finished...
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Trouble with Pointers

 
0
  #2
Aug 1st, 2006
int main(int argc, char *argv[0])

That looks strange.
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 3
Reputation: knorden is an unknown quantity at this point 
Solved Threads: 0
knorden knorden is offline Offline
Newbie Poster

Re: Trouble with Pointers

 
0
  #3
Aug 1st, 2006
Originally Posted by Grunt
int main(int argc, char *argv[0])

That looks strange.
You're right, I got that from a really old simple program. Although, I don't think that is the reason for the problem because I do get the data from the input file. I just do not have the right syntax - or idea - about how to loop through the structs and increment values in them.

Thank you for the quick reply!
Last edited by knorden; Aug 1st, 2006 at 5:14 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Trouble with Pointers

 
0
  #4
Aug 1st, 2006
Originally Posted by Grunt
int main(int argc, char *argv[0])

That looks strange.
Yes, it should be
  1. int main(int argc, char *argv[])
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: Trouble with Pointers

 
0
  #5
Aug 1st, 2006
I tried running the same code but didnt got any segmentation fault.
Just changed the prototype of main and try it
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: Trouble with Pointers

 
0
  #6
Aug 1st, 2006
Sorry Knorden ... Me too got it

Solution is simple.....

Instead of using for(i = 1; i < = numJobs; i++) use
for(i = 1; i < numJobs; i++)

The array was going out of bound.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 3
Reputation: knorden is an unknown quantity at this point 
Solved Threads: 0
knorden knorden is offline Offline
Newbie Poster

Re: Trouble with Pointers

 
0
  #7
Aug 1st, 2006
Originally Posted by dilip.mathews
Sorry Knorden ... Me too got it

Solution is simple.....

Instead of using for(i = 1; i < = numJobs; i++) use
for(i = 1; i < numJobs; i++)

The array was going out of bound.
Wow I definitely should've caught onto that. Thanks!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC