0% found this document useful (0 votes)
142 views17 pages

BCS - 303 - Lab Programs

The document discusses implementing different CPU scheduling algorithms in C including FCFS, SJF, Round Robin, and Priority. For each algorithm, it provides the necessary code to simulate the algorithm, calculate waiting times and turnaround times, and output the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views17 pages

BCS - 303 - Lab Programs

The document discusses implementing different CPU scheduling algorithms in C including FCFS, SJF, Round Robin, and Priority. For each algorithm, it provides the necessary code to simulate the algorithm, calculate waiting times and turnaround times, and output the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1.

Develop a c program to implement the Process system calls (fork (), exec(), wait(), create process,
terminate process)

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
printf("I am: %d\n", (int) getpid());

pid_t pid = fork();


printf("fork returned: %d\n", (int) pid);

if (pid < 0) { /* error occurred */


perror("Fork failed");
}
if (pid == 0) { /* child process */
printf("I am the child with pid %d\n", (int) getpid());
printf("Child process is exiting\n");
exit(0);
}
/* parent process */
printf("I am the parent waiting for the child process to
end\n");
wait(NULL);
printf("parent process is exiting\n");
return(0);
}
2. Simulate the following CPU scheduling algorithms to find turnaround time and waiting time a) FCFS
b) SJF c) Round Robin d) Priority.

a) 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;

}
b) SJF

SJF cpu scheduling algorithm program in c

#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);

}
c) Round Robin

Round Robin CPU scheduling Algorithm implementation in C

#include<stdio.h>

int main()

int count,j,n,time,remain,flag=0,time_quantum;

int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];

printf("Enter Total Process:\t ");

scanf("%d",&n);

remain=n;

for(count=0;count<n;count++)

printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);

scanf("%d",&at[count]);

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

rt[count]=bt[count];

printf("Enter Time Quantum:\t");

scanf("%d",&time_quantum);

printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");

for(time=0,count=0;remain!=0;)

if(rt[count]<=time_quantum && rt[count]>0)

time+=rt[count];

rt[count]=0;

flag=1;

}
else if(rt[count]>0)

rt[count]-=time_quantum;

time+=time_quantum;

if(rt[count]==0 && flag==1)

remain--;

printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);

wait_time+=time-at[count]-bt[count];

turnaround_time+=time-at[count];

flag=0;

if(count==n-1)

count=0;

else if(at[count+1]<=time)

count++;

else

count=0;

printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);

printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);

return 0;

}
d) Priority.

Priority CPU scheduling Algorithm implementation in C

#include<stdio.h>

// structure representing a structure

struct priority_scheduling {

// name of the process

char process_name;

// time required for execution

int burst_time;

// waiting time of a process

int waiting_time;

// total time of execution

int turn_around_time;

// priority of the process

int priority;

};

int main() {

// total number of processes

int number_of_process;

// total waiting and turnaround time

int total = 0;
// temporary structure for swapping

struct priority_scheduling temp_process;

// ASCII numbers are used to represent the name of the process

int ASCII_number = 65;

// swapping position

int position;

// average waiting time of the process

float average_waiting_time;

// average turnaround time of the process

float average_turnaround_time;

printf("Enter the total number of Processes: ");

// get the total number of the process as input

scanf("%d", & number_of_process);

// initializing the structure array

struct priority_scheduling process[number_of_process];

printf("\nPlease Enter the Burst Time and Priority of each process:\n");

// get burst time and priority of all process

for (int i = 0; i < number_of_process; i++) {

// assign names consecutively using ASCII number

process[i].process_name = (char) ASCII_number;

printf("\nEnter the details of the process %c \n", process[i].process_name);


printf("Enter the burst time: ");

scanf("%d", & process[i].burst_time);

printf("Enter the priority: ");

scanf("%d", & process[i].priority);

// increment the ASCII number to get the next alphabet

ASCII_number++;

// swap process according to high priority

for (int i = 0; i < number_of_process; i++) {

position = i;

for (int j = i + 1; j < number_of_process; j++) {

// check if priority is higher for swapping

if (process[j].priority > process[position].priority)

position = j;

// swapping of lower priority process with the higher priority process

temp_process = process[i];

process[i] = process[position];

process[position] = temp_process;

// First process will not have to wait and hence has a waiting time of 0

process[0].waiting_time = 0;

for (int i = 1; i < number_of_process; i++) {


process[i].waiting_time = 0;

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

// calculate waiting time

process[i].waiting_time += process[j].burst_time;

// calculate total waiting time

total += process[i].waiting_time;

// calculate average waiting time

average_waiting_time = (float) total / (float) number_of_process;

// assigning total as 0 for next calculations

total = 0;

printf("\n\nProcess_name \t Burst Time \t Waiting Time \t Turnaround Time\n");

printf("------------------------------------------------------------\n");

for (int i = 0; i < number_of_process; i++) {

// calculating the turnaround time of the processes

process[i].turn_around_time = process[i].burst_time + process[i].waiting_time;

// calculating the total turnaround time.

total += process[i].turn_around_time;

// printing all the values

printf("\t %c \t\t %d \t\t %d \t\t %d", process[i].process_name, process[i].burst_time,


process[i].waiting_time, process[i].turn_around_time);

printf("\n-----------------------------------------------------------\n");
}

// calculating the average turn_around time

average_turnaround_time = (float) total / (float) number_of_process;

// average waiting time

printf("\n\n Average Waiting Time : %f", average_waiting_time);

// average turnaround time

printf("\n Average Turnaround Time: %f\n", average_turnaround_time);

return 0;

You might also like