| | |
Trouble with Pointers
![]() |
•
•
Join Date: Aug 2006
Posts: 3
Reputation:
Solved Threads: 0
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:
Any advice, tips would be greatly appreciated! Thanks so much!
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int p, clock; int addition, numJobs; int i, j, availableMem; // Enum, structure declarations enum state { NOTARRIVED, NOTINSYSTEM, READYTORUN, SLEEPINGIO, RUNNING, EXITED } currentState; /******************************************************* For each process: unique PID, arrival time, service time, priority rating, memory allocation, no start, time in IO state, time in Ready to Run state, time in Running state, timeslice *******************************************************/ typedef struct { int pid; int arrivalTime; int serviceTime; int priorityRating; int memoryAllocation; int noStart; int timeInIO; int timeInReadytoRun; int timeslice; int memTaken; enum state currentState; } process; typedef struct { process elems[1000]; int back; // Initialized to -1 int front; // Initialized to 0 } queue; /* Input processes from data file into array of structs: Process ID : Arrival Time : Service Time : Priority : Memory Requirement in MB */ process *proc[20]; /* Priority Queues: 0, 1, 2, 3, 4, I/O queue, Not In System queue. */ queue queue0, queue1, queue2, queue3, queue4, queueIO, queueNoMem; /* Begin Main */ int main(int argc, char *argv[0]) { char line[80]; FILE *inputFile, *outputFile; inputFile = fopen("input_file.txt", "r"); if (inputFile == NULL) { fprintf(stderr, "Can't open input file!\n"); exit(1); } outputFile = fopen("output_file.txt", "w"); if (outputFile == NULL) { fprintf(stderr, "Can't open output file!\n"); exit(1); } p = 0; clock = 0; fprintf(outputFile, "Input File: \n"); while(fgets(line, 80, inputFile)) { int uniquePID, arriveTime, serviceTime, priority, memAlloc; sscanf(line, "%d : %d : %d : %d : %d", &uniquePID, &arriveTime, &serviceTime, &priority, &memAlloc); proc[p] = (process *)malloc(sizeof(process)); proc[p]->pid = uniquePID; proc[p]->arrivalTime = arriveTime; proc[p]->serviceTime = serviceTime; proc[p]->priorityRating = priority; proc[p]->memoryAllocation = memAlloc; proc[p]->noStart = 0; proc[p]->timeInIO = 0; proc[p]->timeInReadytoRun = 0; proc[p]->timeslice = 0; proc[p]->currentState = NOTARRIVED; // LATER -> free(proc[p]); addition = proc[p]->memoryAllocation%4; proc[p]->memTaken = proc[p]->memoryAllocation + addition; fprintf(outputFile, "PID = %d Arrival Time = %d Service Time = %d Priority = %d Memory Needed = %d \n", proc[p]->pid, proc[p]->arrivalTime, proc[p]->serviceTime, proc[p]->priorityRating, proc[p]->memoryAllocation); p++; } // Set total number of jobs numJobs = p; fprintf(outputFile, "Number of jobs: %d \n", numJobs); // Set memory size for entire simulation availableMem = 32; fprintf(outputFile, "Available Memory: %d \n", availableMem); // THIS IS WHERE I THINK THE SEGMENTATION FAULT OCCURS // WHEN I TRY TO ACCESS THE PIECES IN THE STRUCTS // Add new incoming jobs to the "Ready to Run" state for(i = 1; i <= numJobs; i++) { if(availableMem > proc[i]->memTaken) { // Allocate necessary memory for this process availableMem = availableMem - proc[i]->memTaken; // Set the state to Ready to Run proc[i]->currentState = READYTORUN; } else { // Keep this job out of the system until // more memory is available proc[i]->currentState = NOTINSYSTEM; proc[i]->noStart++; } } }
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...
•
•
Join Date: Aug 2006
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Grunt
int main(int argc, char *argv[0])
That looks strange.
Thank you for the quick reply!
Last edited by knorden; Aug 1st, 2006 at 5:14 am.
•
•
•
•
Originally Posted by Grunt
int main(int argc, char *argv[0])
That looks strange.
C Syntax (Toggle Plain Text)
int main(int argc, char *argv[])
バルサミコ酢やっぱいらへんで
![]() |
Similar Threads
- The Problem with Pointers (C++)
- Trouble with Pointers and Arrays (C)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
- Sorting arrays of pointers with function? (C)
- Function with pointer? (C++)
Other Threads in the C Forum
- Previous Thread: serial I/O, check for input
- Next Thread: output N bytes of data
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open openwebfoundation owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






