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:

#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!

4
Contributors
6
Replies
7
Views
11 Years
Discussion Span
Last Post by knorden
0

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!

0

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

That looks strange.

Yes, it should be

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

I tried running the same code but didnt got any segmentation fault.
Just changed the prototype of main and try it

0

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.

0

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!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.