You are on page 1of 41

PANDIT DEENDAYAL ENERGY

UNIVERSITY
B.TECH – ICT Div 4 H8
Laboratory Manual

Name:SHIVANI ODEDRA
Roll no.: 21BIT267
Subject: Operating Systems Laboratory
Semester: 5

Submitted to
Shivangi
Mehta
Department of
Information and Communication Technology
Engineering

School of Energy Technology,


Pandit Deendayal Energy University
PANDIT DEENDAYAL ENERGY
UNIVERSITY

Information and Communication


Engineering Department

Certificat
e

This is to certify that

Mr./Ms. SHIVANI ODEDRA Roll no. 21BIT267

rd
Exam No. of 3 Year B.TECH Degree in Information and

Communication Technology has satisfactorily completed his/her term work in Operating

Systems Laboratory subject during the semester from to at School of

Technology, PDEU.

Date of Submission:

Signature:

Faculty In-charge Head of Department


Operating Systems 21BIT267

2|Page
Operating Systems 21BIT267

Index

Page
Sr. s Date of
Experiment Title Marks Sign.
No Completion
.
F T
ro o
m
1 Linux Commands. 5 10

2 Shell Scripting 11 1
4
3 To write program to 15 2
perform FCFS and SJF 4
scheduling algorithms.
4 To write program to perform 25 3
Priority and Round-Robbin 5
scheduling algorithms.

3|Page
Operating Systems 21BIT267

4|Page
Operating Systems 21BIT267

Experiment-1

Aim: Linux Commands


1. Enlist different operating systems that are available.
1. MS-DOS
2. Microsoft Windows Operating System
3. LINUX Operating System
4. Solaris Operating System
5. Symbian Operating System
6. Android Mobile Operating System
7. iOS Mobile Operating System
8. macOS
9. Unix
10. Fedora Linux

2. Open source vs License Version, which one is better.

As a key decision-maker, you’ll have to do your research before choosing the


best software for your business.
At YourShortlist, our goal is to make technology procurement simple,
transpar- ent, and cost-effective by matching businesses with the right software
providers. We’re all about helping our members understand, choose, and
implement the so- lutions that will facilitate successful growth. We manage a
diverse database of over 700 software partners providing solutions in ERP,
CRM, cyber security, cloud migration, HR, and more.
With the help of Your Shortlist, IT buyers across the globe have made better-
informed decisions on new software, infrastructure, and managed services to
im- prove their business.
Our non-chargeable service includes:
• Independent buying advice on the best software solutions and providers on the
market
• A bespoke shortlist of specially selected partners experienced in implementing
software for your industry
• Partner background information
• Free call-backs from partners with no obligation to purchase

Practicality

5|Page
Operating Systems 21BIT267

As open-source software tends to accommodate the needs of developers rather


than the majority of layperson users, the convenience and practicality of open
source are frequently criticised.
Often, there are no user guides or manuals – as they are not a legal requirement
– and when they are written, they tend to be written strictly for other developers.
In other words, they’re not written with the less technically experienced users in
mind.
Expert usability testing has enabled licensed software to be more practical for a
wider audience. User manuals are usually on hand for instant reference and
swift training, and support services ensure that issues are solved quickly.

Linux Commands.

Linux Commands.
1) pwd- shows your
current location

2) mkdir- creates a
new, empty directory
whose name is defined by
path.

6|Page
Operating Systems 21BIT267

3) rmdir-
removes
the

directory, specified by the Directory parameter, from the system.

4) ls- used
to list
files.

5)

7|Page
Operating Systems 21BIT267

cd- used to change the working directory of the working drive or another

lettered drive.
6) touch- used to update the access date and/or modification date of
a computer file or directory.

8|Page
Operating Systems 21BIT267

7) cat- used for


piping a file to any
program
that expects
binary data or plain text on the input stream.

8) rm- removes the entries for a specified file, group of files, or certain select
9) cp-
used to
copy
files or
groups
of files
or

directories.

10) mv- used to move files and directories from one directory to another or to

rename a file or directory.

9|Page
Operating Systems 21BIT267

11) head- allows you to see the initial lines of a file in standard

output without opening a file.

12) tail- prints data from the end of a specified file or files to standard output.

10 | P a g e
Operating Systems 21BIT267

13) tac- The


'tac'

command is the reverse of the 'cat' command. It is also known as 'cat'


backward. It will display the file content in reverse order.

11 | P a g e
Operating Systems 21BIT267

14) ifconfig- to assign an address to a network interface and to configure or


display the current network interface configuration information.

15) wc- used to find out number of lines, word count, byte and
characters count in the files specified in the file arguments.

12 | P a g e
Operating Systems 21BIT267

Aim: Shell Experiment-2


Scripting

Theory program will be saved using .sh extension echo is printf of shell scripting. read is
scanf of shell scripting.
Given that: Write a script called which outputs the following:
· Your username.
· The time and date.
· Who is logged on.
· Also output a line of asterisks after every execution.
· Touch command used to make a new file.
· chmod is a command gives the permission of read, write or execute.
· chmod +rwx filename.sh

1. Display Username, Date and who is logged in:

13 | P a g e
Operating Systems 21BIT267

2. Compare two numbers and find greater number:

3. Find the type of triangle by reading the lengths of its sides:

14 | P a g e
Operating Systems 21BIT267

4. Find factorial of a number:

15 | P a g e
Operating Systems 21BIT267

5. Generate a Fibonacci series:

6. Find whether a number is a palindrome or not:

16 | P a g e
Operating Systems 21BIT267

Experiment-3
Aim: To write program to perform FCFS and SJF scheduling algorithms.
FCFS:
Code:
#include <stdio.h>

struct Process
{
int pid; // Process ID
int arrivalTime; // Arrival Time
int burstTime; // Burst Time
int startTime; // Start Time
int endTime; // End Time
int turnaroundTime; // Turnaround
Time int waitingTime; // Waiting
Time
};

void sortProcessesByArrivalTime(struct Process processes[], int n)


{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (processes[j].arrivalTime > processes[j + 1].arrivalTime)
{
struct Process temp = processes[j];
processes[j] = processes[j + 1];
processes[j + 1] = temp;
}
}

17 | P a g e
Operating Systems 21BIT267

18 | P a g e
Operating Systems 21BIT267

void calculateTimes(struct Process processes[], int n)


{
int currentTime = 0; // Current time
for (int i = 0; i < n; i++)
{
// Set the start time for the current process
processes[i].startTime = (currentTime > processes[i].arrivalTime) ? currentTime :
processes[i].arrivalTime;

// Update current time


currentTime = processes[i].startTime + processes[i].burstTime;

// Set the end time for the current process


processes[i].endTime = currentTime;

// Calculate turnaround time and waiting time


processes[i].turnaroundTime = processes[i].endTime - processes[i].arrivalTime;
processes[i].waitingTime = processes[i].turnaroundTime - processes[i].burstTime;
}
}

void displayResults(struct Process processes[], int n)


{
float avgTurnaroundTime = 0, avgWaitingTime = 0;

printf("Process\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time\


n");

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


19 | P a g e
Operating Systems 21BIT267

{
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
processes[i].pid, processes[i].arrivalTime, processes[i].burstTime,
processes[i].endTime, processes[i].turnaroundTime, processes[i].waitingTime);

avgTurnaroundTime +=
processes[i].turnaroundTime; avgWaitingTime +=
processes[i].waitingTime;
}

avgTurnaroundTime /=
n; avgWaitingTime /= n;

printf("\nAverage Turnaround Time: %.2f\n",


avgTurnaroundTime); printf("Average Waiting Time: %.2f\n",
avgWaitingTime);
}

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

struct Process processes[n];


for (int i = 0; i < n; i++)
{
processes[i].pid = i + 1;
printf("Enter arrival time and burst time for process %d: ", i + 1);

scanf("%d %d", &processes[i].arrivalTime, &processes[i].burstTime);


20 | P a g e
Operating Systems 21BIT267

sortProcessesByArrivalTime(processes, n);
calculateTimes(processes, n); displayResults(processes, n);

return 0;
}

Output:

2) SJF:
Code:
#include <stdio.h>
#include <stdbool.h>

struct Process
{
int id;

21 | P a g e
Operating Systems 21BIT267

int arrivalTime;
int burstTime;
int completionTime;

int remainingTime; // Remaining burst time for preemptive SJF


int turnaroundTime; int waitingTime;
};

void nonPreemptiveSJF(struct Process processes[], int n)


{
int currentTime = 0;
int totalWaitingTime = 0;
int totalTurnaroundTime = 0;

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


{
int shortestIndex = -1;
int shortestBurstTime = 9999;

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


{
if (processes[j].arrivalTime <= currentTime && !processes[j].completionTime)
{
if (processes[j].burstTime < shortestBurstTime)
{
shortestIndex = j;
shortestBurstTime = processes[j].burstTime;
}

}
22 | P a g e
Operating Systems 21BIT267

if (shortestIndex == -1)
{
currentTime++;
i--;
}
else
{
processes[shortestIndex].completionTime = currentTime +
processes[shortestIndex].burstTime;
processes[shortestIndex].turnaroundTime = processes[shortestIndex].completionTime
- processes[shortestIndex].arrivalTime;
processes[shortestIndex].waitingTime = processes[shortestIndex].turnaroundTime -
processes[shortestIndex].burstTime;
currentTime = processes[shortestIndex].completionTime;
totalWaitingTime += processes[shortestIndex].waitingTime;
totalTurnaroundTime +=
processes[shortestIndex].turnaroundTime;
}
}

printf("Non-preemptive SJF Scheduling:\n");


printf("Process\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time\
n");
for (int i = 0; i < n; ++i)
{
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
processes[i].id, processes[i].arrivalTime, processes[i].burstTime,
processes[i].completionTime,

processes[i].turnaroundTime,
processes[i].waitingTime);
23 | P a g e
Operating Systems 21BIT267

printf("Average Turnaround Time: %.2f\n", (float)totalTurnaroundTime /


n); printf("Average Waiting Time: %.2f\n", (float)totalWaitingTime / n);
}

void preemptiveSJF(struct Process processes[], int n)

{
int currentTime = 0;
int totalWaitingTime = 0;
int totalTurnaroundTime = 0;
int completedProcesses = 0;

while (completedProcesses < n)


{
int shortestIndex = -1;
int shortestBurstTime = 9999;

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


{
if (processes[i].arrivalTime <= currentTime && processes[i].remainingTime > 0)
{
if (processes[i].remainingTime < shortestBurstTime)
{
shortestIndex = i;

24 | P a g e
Operating Systems 21BIT267

shortestBurstTime = processes[i].remainingTime;

}
}
}

if (shortestIndex == -1)
{
currentTime++;
}

ELSE
{
processes[shortestIndex].remainingTime--;

currentTime++;
if (processes[shortestIndex].remainingTime == 0)
{
processes[shortestIndex].completionTime = currentTime;
processes[shortestIndex].turnaroundTime =
processes[shortestIndex].completionTime - processes[shortestIndex].arrivalTime;
processes[shortestIndex].waitingTime = processes[shortestIndex].turnaroundTime -
processes[shortestIndex].burstTime;
totalWaitingTime += processes[shortestIndex].waitingTime;
totalTurnaroundTime +=
processes[shortestIndex].turnaroundTime; completedProcesses++;
}
}
}
printf("Preemptive SJF Scheduling:\n");

25 | P a g e
Operating Systems 21BIT267

printf("Process\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time\


n");

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


{
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
processes[i].id, processes[i].arrivalTime, processes[i].burstTime,
processes[i].completionTime,

processes[i].turnaroundTime,
processes[i].waitingTime);
}
printf("Average Turnaround Time: %.2f\n", (float)totalTurnaroundTime /
n); printf("Average Waiting Time: %.2f\n", (float)totalWaitingTime / n);
}
int main()

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

struct Process processes[n];


for (int i = 0; i < n; ++i)
{
processes[i].id = i + 1;
printf("Enter arrival time and burst time for process %d (format: arrival burst): ", i + 1);
scanf("%d %d", &processes[i].arrivalTime, &processes[i].burstTime);
processes[i].completionTime = 0;
processes[i].turnaroundTime = 0;
processes[i].waitingTime = 0;
26 | P a g e
Operating Systems 21BIT267

processes[i].remainingTime = processes[i].burstTime; // Initialize remainingTime


}

int choice;
printf("Select scheduling type:\n");
printf("1. Non-preemptive SJF\n");
printf("2. Preemptive SJF\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
nonPreemptiveSJF(processes, n);
break;
case 2:
preemptiveSJF(processes,

n); break;
default:
printf("Invalid choice!\n");
break;
}
return 0;
}

27 | P a g e
Operating Systems 21BIT267

Output:

EXPERIMENT - 4
AIM: To write program to perform Priority and Round-Robbin scheduling
algorithms.
CODE:

PRIORITY CODE:
#include <iostream>
#include <algorithm>
using namespace std;

struct Process
{
int pid; // Process ID
int at; // Arrival time
int bt; // CPU Burst time required
int priority; // Priority of this process
int ct; // Completion time
int wt; // Waiting time
int tat; // Turnaround
time
int remaining_bt; // Remaining burst time
};
28 | P a g e
Operating Systems 21BIT267

// Function to sort the Process according to priority (higher priority first)


bool comparison(Process a, Process b)
{
if (a.at == 0 && b.at == 0)
return (a.priority > b.priority);
if (a.at == 0)
return true;

if (b.at == 0)

return false;
return (a.priority > b.priority);
}

// Function for Non-Preemptive Priority Scheduling


void nonPreemptivePriorityScheduling(Process proc[], int n)
{
sort(proc, proc + n, comparison);

int total_wt = 0, total_tat = 0;

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


{
if (i == 0 || proc[i].at > proc[i - 1].ct)
proc[i].ct = proc[i].at + proc[i].bt;
else
proc[i].ct = proc[i - 1].ct + proc[i].bt;

29 | P a g e
Operating Systems 21BIT267

proc[i].tat = proc[i].ct - proc[i].at;


proc[i].wt = proc[i].tat - proc[i].bt;

total_wt += proc[i].wt;
total_tat += proc[i].tat;
}

// Display processes along with all details


cout << "\nNon-Preemptive Priority Scheduling\n";
cout << "Processes "

<< " Arrival time "


<< " Burst time "
<< " Priority "
<< " Completion time "
<< " Waiting time "
<< " Turnaround time\n";

// Print details for each process


for (int i = 0; i < n; i++)
{
cout << " " << proc[i].pid << "\t\t" << proc[i].at << "\t\t"
<< proc[i].bt << "\t " << proc[i].priority << "\t\t" << proc[i].ct
<< "\t\t " << proc[i].wt << "\t\t " << proc[i].tat << endl;
}

cout << "\nAverage waiting time = " << (float)total_wt / (float)n;


cout << "\nAverage turnaround time = " << (float)total_tat /
(float)n;
30 | P a g e
Operating Systems 21BIT267

// Function for Preemptive Priority Scheduling


void preemptivePriorityScheduling(Process proc[], int n)
{
int currentTime =
0; int completed =
0;

int completedProcesses[n] = {0}; // Array to track completed processes

while (completed < n)


{
int highestPriority =

INT_MIN; int selectedProcess


= -1;

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


{
if (proc[i].at <= currentTime && !completedProcesses[i] && proc[i].priority >
highestPriority)
{
highestPriority = proc[i].priority;
selectedProcess = i;
}
}

if (selectedProcess == -1)
31 | P a g e
Operating Systems 21BIT267

{
currentTime++;
}
else
{
proc[selectedProcess].remaining_bt--;
currentTime++;

if (proc[selectedProcess].remaining_bt == 0)
{
completedProcesses[selectedProcess] = 1;
completed++;
proc[selectedProcess].ct = currentTime;
proc[selectedProcess].tat = proc[selectedProcess].ct - proc[selectedProcess].at;
proc[selectedProcess].wt = proc[selectedProcess].tat - proc[selectedProcess].bt;

}
}
}

// Display processes along with all details


int total_wt = 0, total_tat = 0;

cout << "\nPreemptive Priority Scheduling\n";


cout << "Processes "
<< " Arrival time "
<< " Burst time "
<< " Priority "
<< " Completion time "
<< " Waiting time "
32 | P a g e
Operating Systems 21BIT267

<< " Turnaround time\n";

// Print details for each process


for (int i = 0; i < n; i++)
{
total_wt += proc[i].wt;
total_tat +=

proc[i].tat;
cout << " " << proc[i].pid << "\t\t" << proc[i].at << "\t\t"
<< proc[i].bt << "\t " << proc[i].priority << "\t\t" << proc[i].ct
<< "\t\t " << proc[i].wt << "\t\t " << proc[i].tat << endl;
}

cout << "\nAverage waiting time = " << (float)total_wt / (float)n;


cout << "\nAverage turnaround time = " << (float)total_tat /
(float)n;
}

int main()
{
int n;
cout << "Enter the number of processes: ";
cin >> n;

Process proc[n];
for (int i = 0; i < n; i++)
{
cout << "Enter Process " << i + 1 << " details (at bt priority): ";
cin >> proc[i].at >> proc[i].bt >> proc[i].priority;

33 | P a g e
Operating Systems 21BIT267

proc[i].pid = i + 1; // Assign a process ID


proc[i].ct = 0; // Initialize completion time to 0
proc[i].wt = 0; // Initialize waiting time to 0
proc[i].tat = 0; // Initialize turnaround time to 0
proc[i].remaining_bt = proc[i].bt; // Initialize remaining burst time
}

int choice;
cout << "\nSelect Scheduling Method:\n";

cout << "1. Non-Preemptive Priority Scheduling\n";


cout << "2. Preemptive Priority Scheduling\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice)
{
case 1:

nonPreemptivePriorityScheduling(proc,

n); break;
case 2:
preemptivePriorityScheduling(proc, n);
break;
default:
cout << "Invalid choice!";
}
34 | P a g e
Operating Systems 21BIT267

return 0;
}

Output:
1) Non- preemptive:

35 | P a g e
Operating Systems 21BIT267

2) Preemptive:

ROUND ROBIN CODE:

#include <iostream>

using namespace std;

void queueUpdation(int queue[], int timer, int arrival[], int n, int maxProccessIndex)
{
int zeroIndex;
for (int i = 0; i < n; i++)
{
if (queue[i] == 0)
{
zeroIndex = i;
break;
}
}
queue[zeroIndex] = maxProccessIndex + 1;
}

void queueMaintainence(int queue[], int n)


{
for (int i = 0; (i < n - 1) && (queue[i + 1] != 0); i++)
{
int temp = queue[i];
queue[i] = queue[i + 1];
queue[i + 1] = temp;
}
}

36 | P a g e
Operating Systems 21BIT267

void checkNewArrival(int timer, int arrival[], int n, int maxProccessIndex, int queue[])
{
if (timer <= arrival[n - 1])
{
bool newArrival = false;
for (int j = (maxProccessIndex + 1); j < n; j++)
{
if (arrival[j] <= timer)
{
if (maxProccessIndex < j)
{
maxProccessIndex = j;
newArrival = true;
}
}
}
if (newArrival)
queueUpdation(queue, timer, arrival, n, maxProccessIndex);
}
}

int main()
{
int n, tq, timer = 0, maxProccessIndex = 0;
float avgWait = 0, avgTT = 0;
cout << "\nEnter the time quanta : ";
cin >> tq;
cout << "\nEnter the number of processes : ";
cin >> n;
int arrival[n], burst[n], wait[n], turn[n], queue[n], temp_burst[n];
bool complete[n];

cout << "\nEnter the arrival time of the processes : ";


for (int i = 0; i < n; i++)
cin >> arrival[i];

cout << "\nEnter the burst time of the processes : ";


for (int i = 0; i < n; i++)
{
cin >> burst[i];
temp_burst[i] = burst[i];
}

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


{
complete[i] =

37 | P a g e
Operating Systems 21BIT267

false; queue[i] = 0;
}
while (timer < arrival[0])
timer++;
queue[0] = 1;

while (true)
{
bool flag = true;
for (int i = 0; i < n; i++)
{
if (temp_burst[i] != 0)
{
flag = false;
break;
}
}
if (flag)
break;

for (int i = 0; (i < n) && (queue[i] != 0); i++)


{
int ctr = 0;
while ((ctr < tq) && (temp_burst[queue[0] - 1] > 0))
{
temp_burst[queue[0] - 1] -= 1;
timer += 1;
ctr++;

checkNewArrival(timer, arrival, n, maxProccessIndex, queue);


}

if ((temp_burst[queue[0] - 1] == 0) && (complete[queue[0] - 1] == false))


{
turn[queue[0] - 1] = timer;
complete[queue[0] - 1] = true;
}

bool idle = true;


if (queue[n - 1] == 0)
{
for (int i = 0; i < n && queue[i] != 0; i++)
{
if (complete[queue[i] - 1] == false)
{
idle = false;
}

38 | P a g e
Operating Systems 21BIT267

}
}
else
idle = false;

if (idle)
{
timer++;
checkNewArrival(timer, arrival, n, maxProccessIndex, queue);
}

queueMaintainence(queue, n);
}
}

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


{
turn[i] = turn[i] - arrival[i];
wait[i] = turn[i] - burst[i];
}

cout << "\nProgram No.\tArrival Time\tBurst Time\tWait Time\tTurnAround Time"


<< endl;
for (int i = 0; i < n; i++)
{
cout << i + 1 << "\t\t" << arrival[i] << "\t\t"
<< burst[i] << "\t\t" << wait[i] << "\t\t" << turn[i] << endl;
}
for (int i = 0; i < n; i++)
{
avgWait +=
wait[i]; avgTT +=
turn[i];
}
cout << "\nAverage wait time : " << (avgWait / n)
<< "\nAverage Turn Around Time : " << (avgTT / n);

return 0;
}
Output:

39 | P a g e
Operating Systems 21BIT267

40 | P a g e

You might also like