0% found this document useful (0 votes)
7 views

OS Lab

Os lab docx

Uploaded by

aviaryapanwar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

OS Lab

Os lab docx

Uploaded by

aviaryapanwar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Page |1

Experiment 1:

Perform comparative analysis of different operating systems.

UNIX LINUX WINDOWS


Developed in 1960s by Bell Developed in 1991, when Developed in 1983 by
Labs, the Massachusetts University of Helsinki Microsoft founders Bill
Institute of Technology student Linus Torvalds Gates and Paul Allen. Its
(MIT), and General Electric developed a UNIX-based codename at the time was
(GE) combined forces in an operating system for his Interface Manager.
attempt to build the personal computer.
Multiplexed Information and
Computing Service
(Multics), an interactive
time-sharing system.
UNIX architecture consists Linux architecture consists ofWindows architecture
of three main components: four main components: consists of six main
kernel, shell, and kernel, shell, applications, components: kernel,
applications and commands. and hardware. hardware abstraction layer
(HAL), device drivers,
system libraries, user
interface (UI), and
applications.
Applications of UNIX Web servers are a core Windows is known for
include supporting data function of Linux. The reason compatibility with various
center infrastructure and Linux dominates the web software programs, including
applications, virtual server server space is its low productivity tools and
deployments, distributed installation cost. entertainment software, such
scientific computing, web as games.
servers, and databases.
UNIX variants can provide Linux is the operating system Windows user interface helps
cloud support and feature of choice for supercomputers, organize and manage files.
compatibility with modern with most of the prominent This makes accessing data
cloud infrastructure. The global supercomputers simple. Windows includes
operating system is also running on a version of built-in, user-friendly support
helpful for enhancing cloud Linux. for internet access. Windows
security is perhaps the most widely
used operating system for
multimedia applications.
UNIX is a proprietary Linux is free of cost for all Windows is a commercial
operating system, and not all users and is an open-source operating system that users
versions are freely system. Corporate support need to purchase before use.
distributed. However, can cost a nominal fee based
specific UNIX versions are on the use case and distro.
free for development use.
UNIX typically uses a Linux also employs a Windows uses a hybrid
monolithic kernel monolithic kernel, providing kernel combining aspects of
architecture where core high performance and direct monolithic and microkernel

AMISH SONI 2200910100019


Page |2

functionalities run in kernel hardware access. However, it designs, offering stability,


space. It offers efficient supports loadable kernel performance, and flexibility.
system calls but can be less modules for added
modular. functionality.
UNIX systems commonly Linux supports various file Windows uses NTFS (New
use file systems like UFS systems such as Ext3, Ext4, Technology File System) as
(Unix File System), ZFS XFS, and Btrfs, each offering the primary file system,
(Zettabyte File System), or different features like supporting features like file
HFS (Hierarchical File journaling, encryption, and compression, encryption, and
System) with features like advanced metadata handling. access control lists (ACLs).
snapshots and journaling. FAT32 and exFAT are also
supported for compatibility.
UNIX often has limited Linux boasts extensive Windows offers broad
hardware support, especially hardware support, thanks to hardware compatibility, with
on non-standard its open-source nature and drivers available for most
architectures, as it is community-driven devices, ensuring a plug-and-
typically optimized for development, making it play experience for users.
specific hardware platforms. compatible with a wide range
of devices and peripherals.
UNIX systems are generally Linux is known for its Windows, being the most
considered secure due to security, with frequent widely used desktop OS, is a
their access control updates, community audits, common target for malware.
mechanisms, file and robust permission models However, recent versions
permissions, and limited like SELinux and AppArmor, have improved security with
attack surface. However, making it a popular choice features like Windows
vulnerabilities can still exist. for secure environments. Defender and Secure Boot.
UNIX systems offer high Linux provides extensive Windows offers limited
customization through shell customization options with customization compared to
scripting, system various desktop environments UNIX and Linux, with
configuration files, and the (e.g., GNOME, KDE) and options mainly focused on
ability to tailor the package managers (e.g., GUI settings, themes, and
environment to specific user APT, YUM), allowing users system preferences.
needs, ideal for advanced to personalize their
users and administrators. experience easily.
UNIX is prevalent in server Linux has gained popularity Windows dominates the
environments, mainframes, across servers, desktops, desktop market, with a
and high-performance embedded systems, and cloud significant share in personal
computing due to its infrastructure, becoming a computing, gaming, and
stability, scalability, and dominant force in the IT corporate environments,
historical legacy. It has a landscape, especially in open- supported by a vast
niche market share compared source and enterprise ecosystem of software and
to Linux and Windows. environments. hardware vendors.
UNIX systems may have Linux benefits from Windows enjoys
commercial support available extensive community comprehensive official
for certain distributions, support, online forums, support from Microsoft,
offering extended documentation, and vendor- including regular updates,
maintenance, security backed support options, patches, troubleshooting
updates, and technical ensuring users can find help resources, and enterprise-
assistance, often tailored for and solutions easily across a grade support contracts for
enterprise needs. wide range of issues. businesses.

AMISH SONI 2200910100019


Page |3

UNIX systems provide Linux offers a robust Windows provides a wide


comprehensive development development environment array of development tools,
tools, compilers, libraries, with compilers (GCC, including Visual
and scripting languages like Clang), integrated Studio, .NET Framework,
C, C++, Python, Perl, and development environments PowerShell, and support for
Shell scripts, empowering (IDEs) like Visual Studio popular programming
developers with a rich Code, and extensive libraries languages like C#, Java,
ecosystem for software and frameworks for JavaScript, and Python,
development and system application development, catering to developers across
programming. system administration, and various domains and
DevOps practices. platforms.
UNIX systems manage Linux distributions follow a Windows updates are pushed
updates through package similar approach with by Microsoft regularly, with
managers (e.g., apt, yum) package managers (e.g., apt, scheduled patches, feature
and source code repositories, yum, pacman), official upgrades, and security
allowing users to control repositories, and rolling updates delivered through
updates and dependencies, release models, ensuring Windows Update, aiming to
ensuring system stability and timely updates, security improve performance,
security. patches, and software reliability, and security.
enhancements across the
system.

AMISH SONI 2200910100019


Page |4

Experiment 2:

Implement First Come First Serve (FCFS) CPU Scheduling algorithm.

Program Code:

#include <stdio.h>

int main() {

int n, bt[20], at[20], st[20], wt[20], ft[20], tat[20];

float twt = 0, ttat = 0, awt, atat;

printf("Enter number of processes: ");

scanf("%d", &n);

printf("\n");

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

printf("Enter burst time for process %d: ", i + 1);

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

at[i] = 0;

if (i == 0) {

st[i] = 0;

wt[i] = 0;

ft[i] = bt[i];

tat[i] = ft[i] - at[i];

} else {

st[i] = ft[i - 1];

ft[i] = st[i] + bt[i];

tat[i] = ft[i] - at[i];

AMISH SONI 2200910100019


Page |5

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

twt += wt[i];

ttat += tat[i];

printf("\n");

awt = twt / n;

atat = ttat / n;

printf("============================================================
=====================\n");

printf("Process Burst-Time Arrival-Time Start-Time Wait-Time Finish-Time Turn-


Around-Time\n");

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

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

printf("=============================================================
====================\n");

printf("\n");

printf("Average waiting time: %f\n", awt);

printf("Average turn-around time: %f", atat);

return 0;

AMISH SONI 2200910100019


Page |6

Input/Output:

Enter number of processes: 3

Enter burst time for process 1: 24

Enter burst time for process 2: 3

Enter burst time for process 3: 3

==================================================================

Process Burst-Time Arrival-Time Start-Time Wait-Time Finish-Time Turn-Around-Time

P1 24 0 0 0 24 24

P2 3 0 24 24 27 27

P3 3 0 27 27 30 30

==================================================================

Average waiting time: 17.000000

Average turn-around time: 27.000000

AMISH SONI 2200910100019


Page |7

Experiment 3:

Implement Shortest Job First (SJF) CPU Scheduling algorithm.

Program Code:

#include <stdio.h>

#include <limits.h>

struct Process

int processId, arrivalTime, burstTime, startTime, finishTime, waitTime, turnAroundTime,


completed;

};

int main()

int noOfProcess, time = 0, smallest, totalWaitTime = 0, totalTurnAroundTime = 0,


completed = 0;

float averageWaitTime, averageTurnAroundTime;

printf("Enter number of processes: ");

scanf("%d", &noOfProcess);

printf("\n");

struct Process processes[noOfProcess + 1];

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

AMISH SONI 2200910100019


Page |8

processes[i].processId = i + 1;

processes[i].completed = 0;

printf("Enter arrival time for process %d: ", processes[i].processId);

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

printf("Enter burst time for process %d: ", processes[i].processId);

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

printf("\n");

processes[noOfProcess].burstTime = INT_MAX;

for (time = 0; completed < noOfProcess;)

smallest = noOfProcess;

int found = 0;

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

if (processes[i].arrivalTime <= time && processes[i].burstTime <


processes[smallest].burstTime && processes[i].completed != 1)

smallest = i;

found = 1;

if (found)

AMISH SONI 2200910100019


Page |9

processes[smallest].startTime = time;

processes[smallest].finishTime = time + processes[smallest].burstTime;

processes[smallest].turnAroundTime = processes[smallest].finishTime -
processes[smallest].arrivalTime;

processes[smallest].waitTime = processes[smallest].turnAroundTime -
processes[smallest].burstTime;

processes[smallest].completed = 1;

time += processes[smallest].burstTime;

totalWaitTime += processes[smallest].waitTime;

totalTurnAroundTime += processes[smallest].turnAroundTime;

completed++;

else

time++;

averageWaitTime = (float)totalWaitTime / noOfProcess;

averageTurnAroundTime = (float)totalTurnAroundTime / noOfProcess;

printf("Process Arrival Burst Finish Wait Turn Around\n");

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

printf(" P%d\t %d\t %d\t %d\t %d\t %d\n", processes[i].processId,


processes[i].arrivalTime, processes[i].burstTime, processes[i].finishTime,
processes[i].waitTime, processes[i].turnAroundTime);

AMISH SONI 2200910100019


P a g e | 10

printf("\n");

printf("Average Turn Around Time = %f\n", averageTurnAroundTime);

printf("Average Wait Time = %f\n", averageWaitTime);

return 0;

Input/Output:

Enter number of processes: 4

Enter arrival time for process 1: 0

Enter burst time for process 1: 24

Enter arrival time for process 2: 3

Enter burst time for process 2: 7

Enter arrival time for process 3: 5

Enter burst time for process 3: 6

Enter arrival time for process 4: 10

Enter burst time for process 4: 10

Process Arrival Burst Finish Wait Turn Around

P1 0 24 24 0 24

AMISH SONI 2200910100019


P a g e | 11

P2 3 7 37 27 34

P3 5 6 30 19 25

P4 10 10 47 27 37

Average Turn Around Time = 30.000000

Average Wait Time = 18.250000

Experiment 4:

AMISH SONI 2200910100019


P a g e | 12

Implement Shortest Remaining Time Next (SRTN) CPU Scheduling


algorithm.

Program Code:

#include <stdio.h>

#include <limits.h>

struct Process

int processId, arrivalTime, burstTime, remainingTime, startTime, finishTime, waitTime,


turnAroundTime, completed;

};

int main()

int noOfProcess, time = 0, smallest, totalWaitTime = 0, totalTurnAroundTime = 0,


completed = 0;

float averageWaitTime, averageTurnAroundTime;

printf("Enter number of processes: ");

scanf("%d", &noOfProcess);

printf("\n");

struct Process processes[noOfProcess + 1];

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

AMISH SONI 2200910100019


P a g e | 13

processes[i].processId = i + 1;

processes[i].completed = 0;

processes[i].startTime = -1;

printf("Enter arrival time for process %d: ", processes[i].processId);

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

printf("Enter burst time for process %d: ", processes[i].processId);

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

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

printf("\n");

processes[noOfProcess].remainingTime = INT_MAX;

for (time = 0; completed < noOfProcess; time++)

smallest = noOfProcess;

int found = 0;

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

if (processes[i].arrivalTime <= time && processes[i].remainingTime <


processes[smallest].remainingTime && processes[i].completed != 1)

smallest = i;

found = 1;

AMISH SONI 2200910100019


P a g e | 14

if (found)

if (processes[smallest].startTime == -1)

processes[smallest].startTime = time;

processes[smallest].remainingTime--;

if (processes[smallest].remainingTime == 0)

processes[smallest].finishTime = time + 1;

processes[smallest].turnAroundTime = processes[smallest].finishTime -
processes[smallest].arrivalTime;

processes[smallest].waitTime = processes[smallest].turnAroundTime -
processes[smallest].burstTime;

processes[smallest].completed = 1;

totalWaitTime += processes[smallest].waitTime;

totalTurnAroundTime += processes[smallest].turnAroundTime;

completed++;

averageWaitTime = (float)totalWaitTime / noOfProcess;

averageTurnAroundTime = (float)totalTurnAroundTime / noOfProcess;

printf("Process Arrival Burst Finish Wait Turn Around\n");

AMISH SONI 2200910100019


P a g e | 15

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

printf(" P%d\t %d\t %d\t %d\t %d\t %d\n", processes[i].processId,


processes[i].arrivalTime, processes[i].burstTime, processes[i].finishTime,
processes[i].waitTime, processes[i].turnAroundTime);

printf("\n");

printf("Average Turn Around Time = %f\n", averageTurnAroundTime);

printf("Average Wait Time = %f\n", averageWaitTime);

return 0;

Input/Output:

Enter number of processes: 4

Enter arrival time for process 1: 0

Enter burst time for process 1: 24

Enter arrival time for process 2: 3

Enter burst time for process 2: 7

Enter arrival time for process 3: 5

Enter burst time for process 3: 6

Enter arrival time for process 4: 10

AMISH SONI 2200910100019


P a g e | 16

Enter burst time for process 4: 10

Process Arrival Burst Finish Wait Turn Around

P1 0 24 47 23 47

P2 3 7 10 0 7

P3 5 6 16 5 11

P4 10 10 26 6 16

Average Turn Around Time = 20.250000

Average Wait Time = 8.500000

AMISH SONI 2200910100019


P a g e | 17

Experiment 5:

Implement Round Robin CPU Scheduling algorithm.

Program Code:

#include <stdio.h>

struct Process

int processId, arrivalTime, burstTime, remainingTime, startTime, finishTime, waitTime,


turnAroundTime, completed;

};

int main()

int noOfProcesses, time = 0, totalWaitTime = 0, totalTurnAroundTime = 0, completed = 0,


timeSlice, contextSwitch;

float averageWaitTime, averageTurnAroundTime;

printf("Enter number of processes: ");

scanf("%d", &noOfProcesses);

printf("Enter time slice: ");

scanf("%d", &timeSlice);

printf("Enter context switching time: ");

scanf("%d", &contextSwitch);

printf("\n");

struct Process processes[noOfProcesses];

AMISH SONI 2200910100019


P a g e | 18

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

processes[i].processId = i + 1;

processes[i].completed = 0;

processes[i].startTime = -1;

processes[i].arrivalTime = 0;

printf("Enter burst time for process %d: ", processes[i].processId);

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

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

printf("\n");

for (int i = 0; completed < noOfProcesses;)

if (processes[i].remainingTime > 0 && processes[i].completed != 1)

if (processes[i].startTime == -1)

processes[i].startTime = time;

int run = (timeSlice > processes[i].remainingTime) ? processes[i].remainingTime :


timeSlice;

processes[i].remainingTime -= run;

time += run;

else if (processes[i].remainingTime == 0)

AMISH SONI 2200910100019


P a g e | 19

i = (i + 1) % noOfProcesses;

continue;

if (processes[i].remainingTime == 0 && processes[i].completed != 1)

processes[i].finishTime = time;

processes[i].turnAroundTime = processes[i].finishTime - processes[i].arrivalTime;

processes[i].waitTime = processes[i].turnAroundTime - processes[i].burstTime;

processes[i].completed = 1;

totalWaitTime += processes[i].waitTime;

totalTurnAroundTime += processes[i].turnAroundTime;

completed++;

i = (i + 1) % noOfProcesses;

time += contextSwitch;

printf("Process\t\t\t Burst\t\t\t Finish\t\t\t Wait\t\t\t Turn Around\n");

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

printf("P%d\t\t\t %d\t\t\t %d\t\t\t %d\t\t\t %d\t\t\t\n", processes[i].processId,


processes[i].burstTime, processes[i].finishTime, processes[i].waitTime,
processes[i].turnAroundTime);

return 0;

AMISH SONI 2200910100019


P a g e | 20

Input/Output:

Enter number of processes: 4

Enter time slice: 4

Enter context switching time: 1

Enter burst time for process 1: 10

Enter burst time for process 2: 1

Enter burst time for process 3: 2

Enter burst time for process 4: 5

Process Burst Finish Wait Turn Around

P1 10 24 14 24

P2 1 6 5 6

P3 2 9 7 9

P4 5 21 16 21

AMISH SONI 2200910100019


P a g e | 21

Experiment 6:

Implement Banker’s algorithm.

Program Code:

#include <stdio.h>

#include <conio.h>

int main() {

int k=0,a=0,b=0,instance[5],availability[5],allocated[10][5],need[10][5],MAX[10]
[5],process,P[10],no_of_resources, cnt=0,i, j;

printf("Enter the number of resources : ");

scanf("%d", &no_of_resources);

printf("\nEnter the max instances of each resource:\n");

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

availability[i]=0;

printf("%c = ",(i+97));

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

printf("\nEnter the number of processes : ");

scanf("%d", &process);

int op[process];

printf("\nEnter the allocation matrix \n ");

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

printf(" %c",(i+97));

printf("\n");

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

P[i]=i;

AMISH SONI 2200910100019


P a g e | 22

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

for (j=0;j<no_of_resources;j++) {

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

availability[j]+=allocated[i][j];

printf("\nEnter the MAX matrix \n ");

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

printf(" %c",(i+97));

availability[i]=instance[i]-availability[i];

printf("\n");

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

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

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

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

printf("\n");

A: a=-1;

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

cnt=0;

b=P[i];

for (j=0;j<no_of_resources;j++) {

need[b][j] = MAX[b][j]-allocated[b][j];

if(need[b][j]<=availability[j])

cnt++;

AMISH SONI 2200910100019


P a g e | 23

if(cnt==no_of_resources) {

op[k++]=P[i];

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

availability[j]+=allocated[b][j];

} else

P[++a]=P[i];

if(a!=-1) {

process=a+1;

goto A;

printf("Safe sequence is: \n");

printf("<");

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

printf(" P[%d] ",op[i]);

printf(">");

return 0;

Input/Output:

Enter the number of resources : 3

Enter the max instances of each resource:

a = 10

b=5

AMISH SONI 2200910100019


P a g e | 24

c=7

Enter the number of processes : 5

Enter the allocation matrix

abc

P[0] 0 1 0

P[1] 2 0 0

P[2] 3 0 2

P[3] 2 1 1

P[4] 0 0 2

Enter the MAX matrix

abc

P[0] 7 5 3

P[1] 3 2 2

P[2] 9 0 2

P[3] 4 2 2

P[4] 5 3 3

Safe sequence is:

< P[1] P[3] P[4] P[0] P[2] >

AMISH SONI 2200910100019


P a g e | 25

Experiment 7:

Implement FCFS Disk Scheduling Algorithm.

Program Code:

#include <stdio.h>

#include <stdlib.h>

void fcfs(int requests[], int n, int initial)

int totalMovement = 0;

int currentPosition = initial;

printf("Initial position of head: %d\n", initial);

printf("Sequence of requests: ");

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

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

totalMovement += abs(requests[i] - currentPosition);

currentPosition = requests[i];

printf("\nTotal head movement: %d\n", totalMovement);

int main()

int n, initial;

printf("Enter the initial position of the disk head: ");

scanf("%d", &initial);

printf("Enter the number of requests: ");

AMISH SONI 2200910100019


P a g e | 26

scanf("%d", &n);

int *requests = (int *)malloc(n * sizeof(int));

printf("Enter the sequence of requests:\n");

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

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

fcfs(requests, n, initial);

free(requests);

return 0;

Input/Output:

Enter the initial position of the disk head: 50

Enter the number of requests: 8

Enter the sequence of requests:

176

79

34

60

92

11

41

114

Initial position of head: 50

Sequence of requests: 176 79 34 60 92 11 41 114

Total head movement: 510

AMISH SONI 2200910100019


P a g e | 27

Experiment 8:
Implement SSTF Disk Scheduling Algorithm.

Program Code:

#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

int findShortestSeekTime(int requests[], int n, int currentPosition, int processed[])

int minSeekTime = INT_MAX;

int minIndex = -1;

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

if (!processed[i])

int seekTime = abs(requests[i] - currentPosition);

if (seekTime < minSeekTime)

minSeekTime = seekTime;

minIndex = i;

return minIndex;

void sstf(int requests[], int n, int initial)

AMISH SONI 2200910100019


P a g e | 28

int totalMovement = 0;

int currentPosition = initial;

int processed[n];

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

processed[i] = 0;

printf("Initial position of head: %d\n", initial);

printf("Sequence of requests: ");

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

int index = findShortestSeekTime(requests, n, currentPosition, processed);

if (index != -1)

printf("%d ", requests[index]);

totalMovement += abs(requests[index] - currentPosition);

currentPosition = requests[index];

processed[index] = 1;

printf("\nTotal head movement: %d\n", totalMovement);

int main()

int n, initial;

printf("Enter the initial position of the disk head: ");

AMISH SONI 2200910100019


P a g e | 29

scanf("%d", &initial);

printf("Enter the number of requests: ");

scanf("%d", &n);

int *requests = (int *)malloc(n * sizeof(int));

printf("Enter the sequence of requests:\n");

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

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

sstf(requests, n, initial);

free(requests);

return 0;

Input/Output:

Enter the initial position of the disk head: 100

Enter the number of requests: 6

Enter the sequence of requests:

150

85

90

175

60

110

Initial position of head: 100

Sequence of requests: 110 90 85 60 150 175

Total head movement: 185

AMISH SONI 2200910100019


P a g e | 30

Experiment 9:
Implement SCAN Disk Scheduling Algorithm.

Program Code:

#include <stdio.h>

#include <stdlib.h>

int compare(const void *a, const void *b)

return (*(int *)a - *(int *)b);

void scan(int requests[], int n, int initial, int direction, int disk_size)

int totalMovement = 0;

int currentPosition = initial;

int i;

requests[n] = initial;

n++;

qsort(requests, n, sizeof(int), compare);

int index = 0;

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

if (requests[i] == initial)

index = i;

break;

AMISH SONI 2200910100019


P a g e | 31

printf("Initial position of head: %d\n", initial);

printf("Sequence of requests: ");

if (direction == 1)

for (i = index; i < n; i++)

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

totalMovement += abs(requests[i] - currentPosition);

currentPosition = requests[i];

if (currentPosition != disk_size - 1)

printf("%d ", disk_size - 1);

totalMovement += abs(disk_size - 1 - currentPosition);

currentPosition = disk_size - 1;

for (i = index - 1; i >= 0; i--)

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

totalMovement += abs(requests[i] - currentPosition);

currentPosition = requests[i];

else

for (i = index; i >= 0; i--)

AMISH SONI 2200910100019


P a g e | 32

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

totalMovement += abs(requests[i] - currentPosition);

currentPosition = requests[i];

if (currentPosition != 0)

printf("0 ");

totalMovement += abs(currentPosition);

currentPosition = 0;

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

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

totalMovement += abs(requests[i] - currentPosition);

currentPosition = requests[i];

printf("\nTotal head movement: %d\n", totalMovement);

int main()

int n, initial, direction, disk_size;

printf("Enter the initial position of the disk head: ");

scanf("%d", &initial);

printf("Enter the number of requests: ");

scanf("%d", &n);

AMISH SONI 2200910100019


P a g e | 33

int *requests = (int *)malloc((n + 1) * sizeof(int));

printf("Enter the sequence of requests:\n");

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

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

printf("Enter the disk size: ");

scanf("%d", &disk_size);

printf("Enter the initial direction (1 for high, 0 for low): ");

scanf("%d", &direction);

scan(requests, n, initial, direction, disk_size);

free(requests);

return 0;

Input/Output:

Enter the initial position of the disk head: 100

Enter the number of requests: 6

Enter the sequence of requests:

40

10

90

180

150

120

Enter the disk size: 200

AMISH SONI 2200910100019


P a g e | 34

Enter the initial direction (1 for high, 0 for low): 0

Initial position of head: 100

Sequence of requests: 90 40 10 0 120 150 180

Total head movement: 280

AMISH SONI 2200910100019


P a g e | 35

Experiment 10:
Implement C-SCAN Disk Scheduling Algorithm.

Program Code:

#include <stdio.h>

#include <stdlib.h>

int compare(const void *a, const void *b)

return (*(int *)a - *(int *)b);

void cscan(int requests[], int n, int initial, int disk_size)

int totalMovement = 0;

int currentPosition = initial;

int i;

requests[n] = initial;

n++;

qsort(requests, n, sizeof(int), compare);

int index = 0;

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

if (requests[i] == initial)

index = i;

break;

AMISH SONI 2200910100019


P a g e | 36

printf("Initial position of head: %d\n", initial);

printf("Sequence of requests: ");

for (i = index; i < n; i++)

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

totalMovement += abs(requests[i] - currentPosition);

currentPosition = requests[i];

if (currentPosition != disk_size - 1)

printf("%d ", disk_size - 1);

totalMovement += abs(disk_size - 1 - currentPosition);

currentPosition = disk_size - 1;

printf("0 ");

totalMovement += abs(currentPosition - 0);

currentPosition = 0;

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

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

totalMovement += abs(requests[i] - currentPosition);

currentPosition = requests[i];

printf("\nTotal head movement: %d\n", totalMovement);

int main()

AMISH SONI 2200910100019


P a g e | 37

int n, initial, disk_size;

printf("Enter the initial position of the disk head: ");

scanf("%d", &initial);

printf("Enter the number of requests: ");

scanf("%d", &n);

int *requests = (int *)malloc((n + 1) * sizeof(int));

printf("Enter the sequence of requests:\n");

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

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

printf("Enter the disk size: ");

scanf("%d", &disk_size);

cscan(requests, n, initial, disk_size);

free(requests);

return 0;

Input/Output:

Enter the initial position of the disk head: 50

Enter the number of requests: 8

Enter the sequence of requests:

176

79

34

AMISH SONI 2200910100019


P a g e | 38

60

92

11

41

114

Enter the disk size: 200

Initial position of head: 50

Sequence of requests: 60 79 92 114 176 199 0 11 34 41

Total head movement: 379

AMISH SONI 2200910100019


P a g e | 39

Experiment 10:

To implement solution for Producer Consumer problem.

Program Code:

#include <stdio.h>

#include <stdlib.h>

int mutex = 1;

int full = 0;

int empty = 10, x = 0;

void producer()

--mutex;

++full;

--empty;

x++;

printf("Producer produces item %d\n", x);

++mutex;

void consumer()

--mutex;

--full;

++empty;

AMISH SONI 2200910100019


P a g e | 40

printf("Consumer consumes item %d\n", x);

x--;

++mutex;

int main()

int n, i;

printf("1. Press 1 for Producer\n"

"2. Press 2 for Consumer\n"

"3. Press 3 for Exit\n");

#pragma omp critical

for (i = 1; i > 0; i++) {

printf("Enter your choice:");

scanf("%d", &n);

switch (n) {

case 1:

if ((mutex == 1)

&& (empty != 0)) {

producer();

else {

printf("Buffer is full!\n");

AMISH SONI 2200910100019


P a g e | 41

break;

case 2:

if ((mutex == 1)

&& (full != 0)) {

consumer();

else {

printf("Buffer is empty!\n");

break;

case 3:

exit(0);

break;

Input/Output:

1. Press 1 for Producer

2. Press 2 for Consumer

3. Press 3 for Exit

Enter your choice:2

Buffer is empty!

Enter your choice:1

Producer produces item 1

AMISH SONI 2200910100019


P a g e | 42

Enter your choice:1

Producer produces item 2

Enter your choice:1

Producer produces item 3

Enter your choice:2

Consumer consumes item 3

Enter your choice:1

Producer produces item 3

Enter your choice:2

Consumer consumes item 3

Enter your choice:2

Consumer consumes item 2

Enter your choice:2

Consumer consumes item 1

Enter your choice:2

Buffer is empty!

Enter your choice:3

AMISH SONI 2200910100019

You might also like