2

I have a school project which requires me to simulate first come first serve using these variables:

Users Input:

Number of Process: 3

Process 1 Arrives at 0 time and requires 5 'resources'

3
1,5,0
2,5,4
3,1,8

However, i can't seem to get past the first 5 'resources'. I'm trying to figure out how to increase PID and repeat but keep time increasing for all these resources. I've created this same program but it only allows for this specific input and I'm trying to make it more versatile so i can choose any number of processes and resources(unit) needed.

#include <stdio.h>

main() {
    int n;
    printf("Enter the Amount of processes: ");
    scanf("%d",&n);
    //Variables
    int process[n], unit[n], at[n];
    int i,time,PID = 1;
    int awt, atat,sum,counter;
    int x = n;

    //Takes and stores the users input into process unit and at
    for(i=0;i<n;i++)
    {
        scanf("%d,%d,%d", &process[i], &unit[i], &at[i]);

    }
    sum = sum_array(unit,n);
    printf("%d\n", sum);
    printf("FCFS\n");
    printf("Time PID");


    for(counter = 0; counter < x; counter++, PID++){
        FCFS(time,n,unit,PID);
    }

}
    int sum_array(int at[], int num_elements){
        int x, sum = 0;
        for(x=0; x<num_elements;x++){
            sum = sum + at[x];
        }
        return(sum);
    }
    int FCFS(int time,int n,int unit[], int PID){

        for(time = 0, n = 0 ; unit[n] >0  ;time++, unit[n]--){
            printf("\n%d     ", time);
            printf("%d", PID);

        }

        return;
    }

Sample Output:

FCFS
TIME PID
0     1
1     1
2     1
3     1
4     1
5     2
6     2
7     2
8     2
9     2
10    3
2
  • 1
    I don't get your problem, but think it is a bit strange to pass n to FCFS and then set it to zero every time. Essentially you are always using unit[0] in that function. Commented Aug 28, 2017 at 12:29
  • What is the sample input suppsed to mean? Commented Aug 28, 2017 at 12:31

2 Answers 2

1

Your problems are mostly related to the FCFS function and the loop where you call it.

Try the following:

  • Initialize time = 0 in the main function
  • Pass counter instead of n to FCFS in the loop
  • Return the updated time from FCFS
  • Don't reset the time and n parameter inside FCFS

Call to FCFS inside for loop:

time = FCFS(time, counter, unit, PID);

Updated FCFS code:

int FCFS(int time,int n,int unit[], int PID)
{
    for( ; unit[n] >0  ;time++, unit[n]--)
    {
        printf("\n%d     ", time);
        printf("%d", PID);
    }
    return time;
}

Other than that, there are a number of issues with your code, but it wouldn't really fit into this Q/A to mention them all, so I stick with the necessary things to get your code running for valid example input.

Sign up to request clarification or add additional context in comments.

2 Comments

In turn with the question. In order to loop through a function multiple times, one must assign it to a variable and use that?
@Dreeww I'm not completely sure what you mean, but I'm sure the answer is No. Calling a function in a loop is possible without assigning it anywhere and there are many ways to update variables on each function call (generally, including inside a loop)
0

Since this is a homework question, I would encourage you to solve it on your own. Since you have put in some effort on solving this, I am posting the answer below as a spoiler (Note indentation does not work in spoilers). However before seeing the answer here are few suggestions to fix your program:

  • As mentioned above, passing n does absolutely nothing. Please use a different variable inside the FCFS function.
  • No need to increment and pass the PID. Since you are putting it in an array, try to get the value from the array.
  • Instead of n pass counter to the function so that you can index the two arrays.
  • The for loop inside FCFS makes no sense. It should be for(i=0; i<unit[counter]; i++). time can just be incremented inside the loop.
  • time needs to be returned to increment properly

And my code:

int time = 0;
int cur_index = 0;
while (cur_index < n) {
    int pid = -1;
    if (at[cur_index] <= time) {
        pid = process[cur_index];
    } else {
        printf("%d %d\n", time, pid);
        time++;
        continue;
    }

    if (pid != -1) {
        int r = 0;
        for (r = 0; r < unit[cur_index]; r++) {
            printf("%d %d\n", time, pid);
            time++;
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.