944,198 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1262
  • C RSS
Aug 1st, 2006
0

Trouble with Pointers

Expand Post »
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
knorden is offline Offline
3 posts
since Aug 2006
Aug 1st, 2006
0

Re: Trouble with Pointers

int main(int argc, char *argv[0])

That looks strange.
Reputation Points: 197
Solved Threads: 12
Junior Poster
Grunt is offline Offline
147 posts
since Jul 2006
Aug 1st, 2006
0

Re: Trouble with Pointers

Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
knorden is offline Offline
3 posts
since Aug 2006
Aug 1st, 2006
0

Re: Trouble with Pointers

Quote 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[])
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Aug 1st, 2006
0

Re: Trouble with Pointers

I tried running the same code but didnt got any segmentation fault.
Just changed the prototype of main and try it
Reputation Points: 16
Solved Threads: 3
Junior Poster in Training
dilip.mathews is offline Offline
89 posts
since Jun 2006
Aug 1st, 2006
0

Re: Trouble with Pointers

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.
Reputation Points: 16
Solved Threads: 3
Junior Poster in Training
dilip.mathews is offline Offline
89 posts
since Jun 2006
Aug 1st, 2006
0

Re: Trouble with Pointers

Quote 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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
knorden is offline Offline
3 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: serial I/O, check for input
Next Thread in C Forum Timeline: output N bytes of data





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


Follow us on Twitter


© 2011 DaniWeb® LLC