Monday, January 5, 2026

OS WEEK 4


Simulate the following CPU scheduling algorithms FCFS b) SJF c) Priority d) Round Robin


 

FCFS:

// C program for implementation of FCFS  

// scheduling 

#include<stdio.h> 

// Function to find the waiting time for all  

// processes 

void findWaitingTime(int processes[], int n,  

                          int bt[], int wt[]) 

    // waiting time for first process is 0 

    wt[0] = 0; 

  

    // calculating waiting time 

    for (int  i = 1; i < n ; i++ ) 

        wt[i] =  bt[i-1] + wt[i-1] ; 

  

// Function to calculate turn around time 

void findTurnAroundTime( int processes[], int n,  

                  int bt[], int wt[], int tat[]) 

    // calculating turnaround time by adding 

    // bt[i] + wt[i] 

    for (int  i = 0; i < n ; i++) 

        tat[i] = bt[i] + wt[i]; 

  

//Function to calculate average time 

void findavgTime( int processes[], int n, int bt[]) 

    int wt[n], tat[n], total_wt = 0, total_tat = 0; 

  

    //Function to find waiting time of all processes 

    findWaitingTime(processes, n, bt, wt); 

  

    //Function to find turn around time for all processes 

    findTurnAroundTime(processes, n, bt, wt, tat); 

  

    //Display processes along with all details 

    printf("Processes   Burst time   Waiting time   Turn around time\n"); 

  

    // Calculate total waiting time and total turn  

    // around time 

    for (int  i=0; i<n; i++) 

    { 

        total_wt = total_wt + wt[i]; 

        total_tat = total_tat + tat[i]; 

        printf("   %d ",(i+1));

        printf("       %d ", bt[i] );

        printf("       %d",wt[i] );

        printf("       %d\n",tat[i] ); 

    } 

    float s=(float)total_wt / (float)n;

    float t=(float)total_tat / (float)n;

    printf("Average waiting time = %f",s);

    printf("\n");

    printf("Average turn around time = %f ",t); 

  

// Driver code 

int main() 

    //process id's 

    int processes[] = { 1, 2, 3}; 

    int n = sizeof processes / sizeof processes[0]; 

  

    //Burst time of all processes 

    int  burst_time[] = {10, 5, 8}; 

  

    findavgTime(processes, n,  burst_time); 

    return 0; 


OUTPUT:

DO IT ON YOUR OWN


SJF:


#include <stdio.h>

int main()

{

    // Matrix for storing Process Id, Burst

    // Time, Average Waiting Time & Average

    // Turn Around Time.

    int A[100][4];

    int i, j, n, total = 0, index, temp;

    float avg_wt, avg_tat;

    printf("Enter number of process: ");

    scanf("%d", &n);

    printf("Enter Burst Time:\n");

    // User Input Burst Time and alloting Process Id.

    for (i = 0; i < n; i++) {

        printf("P%d: ", i + 1);

        scanf("%d", &A[i][1]);

        A[i][0] = i + 1;

    }

    // Sorting process according to their Burst Time.

    for (i = 0; i < n; i++) {

        index = i;

        for (j = i + 1; j < n; j++)

            if (A[j][1] < A[index][1])

                index = j;

        temp = A[i][1];

        A[i][1] = A[index][1];

        A[index][1] = temp;


        temp = A[i][0];

        A[i][0] = A[index][0];

        A[index][0] = temp;

    }

    A[0][2] = 0;

    // Calculation of Waiting Times

    for (i = 1; i < n; i++) {

        A[i][2] = 0;

        for (j = 0; j < i; j++)

            A[i][2] += A[j][1];

        total += A[i][2];

    }

    avg_wt = (float)total / n;

    total = 0;

    printf("P     BT     WT     TAT\n");

    // Calculation of Turn Around Time and printing the

    // data.

    for (i = 0; i < n; i++) {

        A[i][3] = A[i][1] + A[i][2];

        total += A[i][3];

        printf("P%d     %d     %d      %d\n", A[i][0],

               A[i][1], A[i][2], A[i][3]);

    }

    avg_tat = (float)total / n;

    printf("Average Waiting Time= %f", avg_wt);

    printf("\nAverage Turnaround Time= %f", avg_tat);

}

   


OUTPUT:

DO IT ON YOUR OWN



Priority:

/*

 * C program to implement priority scheduling

 */

 

#include <stdio.h>

 

//Function to swap two variables

void swap(int *a,int *b)

{

    int temp=*a;

    *a=*b;

    *b=temp;

}

int main()

{

    int n;

    printf("Enter Number of Processes: ");

    scanf("%d",&n);

 

    // b is array for burst time, p for priority and index for process id

    int b[n],p[n],index[n];

    for(int i=0;i<n;i++)

    {

        printf("Enter Burst Time and Priority Value for Process %d: ",i+1);

        scanf("%d %d",&b[i],&p[i]);

        index[i]=i+1;

    }

    for(int i=0;i<n;i++)

    {

        int a=p[i],m=i;

 

        //Finding out highest priority element and placing it at its desired position

        for(int j=i;j<n;j++)

        {

            if(p[j] > a)

            {

                a=p[j];

                m=j;

            }

        }

 

        //Swapping processes

        swap(&p[i], &p[m]);

        swap(&b[i], &b[m]);

        swap(&index[i],&index[m]);

    }

 

    // T stores the starting time of process

    int t=0;

 

    //Printing scheduled process

    printf("Order of process Execution is\n");

    for(int i=0;i<n;i++)

    {

        printf("P%d is executed from %d to %d\n",index[i],t,t+b[i]);

        t+=b[i];

    }

    printf("\n");

    printf("Process Id     Burst Time   Wait Time    TurnAround Time\n");

    int wait_time=0;

    for(int i=0;i<n;i++)

    {

        printf("P%d          %d          %d          %d\n",index[i],b[i],wait_time,wait_time + b[i]);

        wait_time += b[i];

    }

    return 0;

}


OUTPUT:

DO IT ON YOUR OWN


Round Robin:


#include<stdio.h>
 
int main()
{
 
  int cnt,j,n,t,remain,flag=0,tq;
  int wt=0,tat=0,at[10],bt[10],rt[10];
  printf("Enter Total Process:\t ");
  scanf("%d",&n);
  remain=n;
  for(cnt=0;cnt<n;cnt++)
  {
    printf("Enter Arrival Time and Burst Time for Process Process Number %d :",cnt+1);
    scanf("%d",&at[cnt]);
    scanf("%d",&bt[cnt]);
    rt[cnt]=bt[cnt];
  }
  printf("Enter Time Quantum:\t");
  scanf("%d",&tq);
  printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
  for(t=0,cnt=0;remain!=0;)
  {
    if(rt[cnt]<=tq && rt[cnt]>0)
    {
      t+=rt[cnt];
      rt[cnt]=0;
      flag=1;
    }
    else if(rt[cnt]>0)
    {
      rt[cnt]-=tq;
      t+=tq;
    }
    if(rt[cnt]==0 && flag==1)
    {
      remain--;
      printf("P[%d]\t|\t%d\t|\t%d\n",cnt+1,t-at[cnt],t-at[cnt]-bt[cnt]);
      wt+=t-at[cnt]-bt[cnt];
      tat+=t-at[cnt];
      flag=0;
    }
    if(cnt==n-1)
      cnt=0;
    else if(at[cnt+1]<=t)
      cnt++;
    else
      cnt=0;
  }
  printf("\nAverage Waiting Time= %f\n",wt*1.0/n);
  printf("Avg Turnaround Time = %f",tat*1.0/n);
  
  return 0;
}




OUTPUT:

DO IT ON YOUR OWN

**************************************



FCFS:

#include <stdio.h>


int main() {

    int n, i;

    int bt[10], wt[10], tat[10];


    printf("Enter number of processes: ");

    scanf("%d", &n);


    printf("Enter burst time for each process:\n");

    for(i = 0; i < n; i++) {

        scanf("%d", &bt[i]);

    }


    wt[0] = 0;   // first process waiting time


    for(i = 1; i < n; i++) {

        wt[i] = wt[i-1] + bt[i-1];

    }


    for(i = 0; i < n; i++) {

        tat[i] = wt[i] + bt[i];

    }


    printf("\nProcess\tBT\tWT\tTAT\n");

    for(i = 0; i < n; i++) {

        printf("P%d\t%d\t%d\t%d\n", i+1, bt[i], wt[i], tat[i]);

    }


    return 0;

}


OUTPUT:

FCFS (First Come First Serve)

🔹 Sample Input

Number of processes = 3 Burst times: P1 = 5 P2 = 3 P3 = 2

🔹 Execution Order

Processes execute in arrival order.

🔹 Gantt Chart

| P1 | P2 | P3 | 0 5 8 10

🔹 Waiting Time

  • P1 = 0

  • P2 = 5

  • P3 = 8

🔹 Turnaround Time

  • P1 = 5

  • P2 = 8

  • P3 = 10



SJF:

#include <stdio.h>

int main() {
    int n, i, j;
    int bt[10], wt[10], tat[10], temp;

    printf("Enter number of processes: ");
    scanf("%d", &n);

    printf("Enter burst times:\n");
    for(i = 0; i < n; i++) {
        scanf("%d", &bt[i]);
    }

    // Sort burst times
    for(i = 0; i < n-1; i++) {
        for(j = i+1; j < n; j++) {
            if(bt[i] > bt[j]) {
                temp = bt[i];
                bt[i] = bt[j];
                bt[j] = temp;
            }
        }
    }

    wt[0] = 0;

    for(i = 1; i < n; i++) {
        wt[i] = wt[i-1] + bt[i-1];
    }

    for(i = 0; i < n; i++) {
        tat[i] = wt[i] + bt[i];
    }

    printf("\nProcess\tBT\tWT\tTAT\n");
    for(i = 0; i < n; i++) {
        printf("P%d\t%d\t%d\t%d\n", i+1, bt[i], wt[i], tat[i]);
    }

    return 0;
}

OUTPUT:

SJF (Shortest Job First – Non-Preemptive)

🔹 Sample Input

Number of processes = 3 Burst times: P1 = 5 P2 = 3 P3 = 2

🔹 Execution Order

Shortest burst time first → P3 → P2 → P1

🔹 Gantt Chart

| P3 | P2 | P1 | 0 2 5 10

🔹 Waiting Time

  • P3 = 0

  • P2 = 2

  • P1 = 5

🔹 Turnaround Time

  • P3 = 2

  • P2 = 5

  • P1 = 10



Priority Scheduling (Non-Preemptive)
#include <stdio.h>

int main() {
    int n, i, j;
    int bt[10], pr[10], wt[10], tat[10], temp;

    printf("Enter number of processes: ");
    scanf("%d", &n);

    printf("Enter burst time and priority:\n");
    for(i = 0; i < n; i++) {
        scanf("%d %d", &bt[i], &pr[i]);
    }

    // Sort based on priority (lower value = higher priority)
    for(i = 0; i < n-1; i++) {
        for(j = i+1; j < n; j++) {
            if(pr[i] > pr[j]) {
                temp = pr[i]; pr[i] = pr[j]; pr[j] = temp;
                temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;
            }
        }
    }

    wt[0] = 0;

    for(i = 1; i < n; i++) {
        wt[i] = wt[i-1] + bt[i-1];
    }

    for(i = 0; i < n; i++) {
        tat[i] = wt[i] + bt[i];
    }

    printf("\nProcess\tBT\tPriority\tWT\tTAT\n");
    for(i = 0; i < n; i++) {
        printf("P%d\t%d\t%d\t\t%d\t%d\n", i+1, bt[i], pr[i], wt[i], tat[i]);
    }

    return 0;
}


OUTPUT:

Priority Scheduling (Lower number = Higher priority)

🔹 Sample Input

Process Burst Time Priority P1 4 2 P2 3 1 P3 2 3

🔹 Execution Order

Priority order → P2 → P1 → P3

🔹 Gantt Chart

| P2 | P1 | P3 | 0 3 7 9

🔹 Waiting Time

  • P2 = 0

  • P1 = 3

  • P3 = 7

🔹 Turnaround Time

  • P2 = 3

  • P1 = 7

  • P3 = 9



Round Robin Scheduling

#include <stdio.h>

int main() {
    int n, tq;
    int bt[10], rem_bt[10];
    int wt[10] = {0}, tat[10];
    int i, time = 0, done;

    printf("Enter number of processes: ");
    scanf("%d", &n);

    printf("Enter burst times:\n");
    for(i = 0; i < n; i++) {
        scanf("%d", &bt[i]);
        rem_bt[i] = bt[i];
    }

    printf("Enter time quantum: ");
    scanf("%d", &tq);

    do {
        done = 1;
        for(i = 0; i < n; i++) {
            if(rem_bt[i] > 0) {
                done = 0;
                if(rem_bt[i] > tq) {
                    time += tq;
                    rem_bt[i] -= tq;
                } else {
                    time += rem_bt[i];
                    wt[i] = time - bt[i];
                    rem_bt[i] = 0;
                }
            }
        }
    } while(!done);

    for(i = 0; i < n; i++) {
        tat[i] = wt[i] + bt[i];
    }

    printf("\nProcess\tBT\tWT\tTAT\n");
    for(i = 0; i < n; i++) {
        printf("P%d\t%d\t%d\t%d\n", i+1, bt[i], wt[i], tat[i]);
    }

    return 0;
}

OUTPUT:

Round Robin Scheduling

🔹 Sample Input

Number of processes = 3 Burst times: P1 = 5 P2 = 3 P3 = 2 Time Quantum = 2

🔹 Execution Order (Time Slice = 2)

StepProcessTime
1P10–2
2P22–4
3P34–6
4P16–8
5P28–9
6P19–10

🔹 Gantt Chart

| P1 | P2 | P3 | P1 | P2 | P1 | 0 2 4 6 8 9 10

🔹 Waiting Time

  • P1 = 5

  • P2 = 6

  • P3 = 4

🔹 Turnaround Time

  • P1 = 10

  • P2 = 9

  • P3 = 6



____________________________________________

No comments:

Post a Comment

OS UNIT-II

  UNIT-II   Processes: Process Concept, Process scheduling, Operations on processes, Inter-process communication. Threads and Concurrenc...