You are on page 1of 4

Operating System

Lab Journal 11

Student Name: AMMARA BABAR


Enrolment No. 01-235191-043
Class and Section 4B

Title: Scheduling II
Objectives: To understand and write simulation programs for scheduling algorithms in LINUX.
Tools Used: Linux Ubuntu Terminal, gedit Text Editor

Task # 1:

Write a C program to implement CPU scheduling algorithm for priority Scheduling.

Structure for Priority Scheduling:


struct Process
{
int id; //user
int burstTime; //user
int waitingTime; //calculate: the time it has to wait for other processes
int turnAroundTime; //calculate: waitingtime + burstTime
int priority;
};
struct Process p[n];

Algorithm:
Step 1: Take number of process from user.
Step 2: Initialize all the structure elements with 0. Receive inputs from the user to fill process id,
burst time and Priority.
Step 3: Sort the array in the ascending order on the basis of priority. (Use bubble sort)
Step 4: Calculate the Turnaround time and Waiting time for the remaining processes.
Step5: Waiting time of one process is the total service time of all the previous processes.
Step6: Total time of process is calculated by adding its Waiting time and its Service time.
Step 7: Calculate the average waiting time and average turnaround time
Step8: Display the result in the following format for each process.
printf("Id \t Service Time \t Waiting Time \t Total Time");
printf(“%d \t %d \t %d \t %d”,p.id, p.serviceTime, p.waitingTime, p.totalTime);

Program:
#include <stdio.h>

struct Process
{
int ID;
int BT;
int WT;
int TURNTIME;
int PRTY;
};
int main()
{
int ttl = 0;
printf("total Process:");
scanf("%d", &ttl);
struct Process p[ttl];
int TtlWaiting = 0, ttlTurn = 0;
float avgWaiting = 0, avgTurn = 0;
int i = 0;
p[i].waitingtime = 0;
while (i < ttl)
{
p[i].id = i + 1;
printf("Process ID: ");
scanf("%d", &p[i].ID);
printf("Process Service Time: ");
scanf("%d", &p[i].BT);
printf("Process Priority: ");
scanf("%d", &p[i].PRTY);
i++;
}
i = 0;
while (i < ttl)
{
for (int j = i + 1; j < ttl; j++)
{
if (p[i].priority > p[j].priority)
{
struct Process temp;
temp = p[j];
p[j] = p[i];
p[i] = temp;

}
}
i++;
}
p[0].turnAroundtime = p[0].BT;
i = 1;
while (i < ttl)
{
p[i].WT = p[i - 1].TURNTIME;
p[i].TURNTIME = p[i].WT + p[i].BT;
i++;
}
i = 0;
while (i < ttl)
{
TtlWaiting = TtlWaiting + p[i].WT;
ttlTurn = ttlTurn + p[i].TURNTIME;
i++;
}
avgWaiting = TtlWaiting / ttl;
avgTurn = ttlTurn / ttl;
printf("\nAverage Waiting Time: %fms\n", avgWaiting);
printf("\nAverage TurnAround Time: %fms\n", avgTurn);
printf("\n");
i = 0;
printf("Id \t Priority \t Service Time \t Waiting Time \t Total Time\n");
while (i < ttl)
{
printf("%d \t\t %d \t\t %d \t\t %d \t\t %d\n", p[i].ID, p[i].PRTY, p[i].BT, p[i].WT,
p[i].TURNTIME);
i++;
}
return 0;
Output:
//Paste snapshot of your output here

Bonus Task: Try writing the simulation of Round Robin Algorithm as explained in the lecture.

Submission Date: Signature Ms. Umarah Qaseem

You might also like