0% found this document useful (0 votes)
5 views52 pages

Lab Manual OS

The document outlines a comprehensive Operating System lab curriculum, detailing various experiments related to OS concepts and programming assignments in C. It covers topics such as types of operating systems, hardware and software requirements, system calls in UNIX, and implementations of scheduling algorithms like FCFS, SJF, and Priority Scheduling. Additionally, it provides insights into the functions of operating systems and includes specific coding examples for practical understanding.

Uploaded by

anup32400
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)
5 views52 pages

Lab Manual OS

The document outlines a comprehensive Operating System lab curriculum, detailing various experiments related to OS concepts and programming assignments in C. It covers topics such as types of operating systems, hardware and software requirements, system calls in UNIX, and implementations of scheduling algorithms like FCFS, SJF, and Priority Scheduling. Additionally, it provides insights into the functions of operating systems and includes specific coding examples for practical understanding.

Uploaded by

anup32400
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/ 52

INDEX

OPERATING SYSTEM LAB

S.NO. NAME OF EXPERIMENT


0. Introduction of Operating System and it’s types.
1. Study of hardware and software requirements of different operating
systems(UNIX,LINUX,WINDOWS XP,
WINDOWS 7/8)
2. Execute various UNIX system calls for Process Management, File
Management and Input
/Output.
3. Write a program in C to implement First Come
First Serve (FCFS) Scheduling Algorithm.
4. Write a program in C to implement Non
Preemptive Shortest Job First Scheduling Algorithm.
5. Write a program in C to implement Priority
Scheduling Algorithm.
6. Write a program in C to implement Round Robin
Scheduling Algorithm.
7. Write a program in C to implement:
i. Contiguous(using array) ii.
Linked-list(using linked
list)
8. Write a program in C to implement contiguous allocation techniques:
i. Worst-Fit
ii. Best-Fit iii. First-Fit
9. Write a program in C for calculation of external and internal fragmentation
(i) Free space list of blocks from the system
(ii) List process file from the system
10. Write a program in C to implement Resource
Allocation Graph(RAG)
11. Write a program in C to implement Banker’s algorithm.
12. Write a program in C to convert Resource
Allocation Graph(RAG) to wait for graph (WFG) for each type of method used
for storing graph.
13. Write a program in C to implement the solution for Bounded Buffer (producer –
Consumer) problem using inter process communication
techniques-Semaphores.
14. Write a program in C to implement the solution
for Readers-Writer problem using inter process communication techniques-
Semaphores.
EXPERIMENT - 0

Aim – Introduction to operating system


What is an Operating System?
An Operating System (OS) is a software that acts as an interface between
computer hardware components and the user. Every computer system must
have at least one operating system to run other programs. Applications like
Browsers, MS Office, Notepad Games, etc., need some environment to run and
perform its tasks.
The OS helps you to communicate with the computer without knowing how to
speak the computer’s language. It is not possible for the user to use any computer
or mobile device without having an operating system.

Types of Operating System (OS)


Following are the popular types of OS (Operating System):

• Batch Operating System


• Multitasking/Time Sharing OS
• Multiprocessing OS
• Real Time OS
• Distributed OS
• Network OS
• Mobile OS

1. Batch Operating System


Some computer processes are very lengthy and time-consuming. To speed the
same process, a job with a similar type of needs are batched together and run as
a group.
The user of a batch operating system never directly interacts with the computer.
In this type of OS, every user prepares his or her job on an offline device like a
punch card and submit it to the computer operator.

2. Multi-Tasking/Time-sharing Operating systems Time-


sharing operating system enables people located at a different
terminal(shell) to use a single computer system at the same time. The
processor time (CPU) which is shared among multiple users is termed as time
sharing.
3. Real time OS
A real time operating system time interval to process and respond to inputs is
very small. Examples: Military Software Systems, Space Software Systems are the
Real time OS example.

4. Distributed Operating System


Distributed systems use many processors located in different machines to provide
very fast computation to its users.

5. Network Operating System


Network Operating System runs on a server. It provides the capability to serve to
manage data, user, groups, security, application, and other networking functions.

6. Mobile OS
Mobile operating systems are those OS which is especially that are designed to
power smartphones, tablets, and wearables devices.

Some most famous mobile operating systems are Android and iOS, but others
include BlackBerry, Web, and watchOS.

Functions of Operating System


Some typical operating system functions may include managing memory, files,
processes, I/O system & devices, security, etc.

Below are the main functions of Operating System:

Functions of Operating System


In an operating system software performs each of the function:

1. Process management: Process management helps OS to create and


delete processes. It also provides mechanisms for synchronization and
communication among processes.
2. Memory management: Memory management module performs the
task of allocation and de-allocation of memory space to programs in
need of this resources.
3. File management: It manages all the file-related activities such as
organization storage, retrieval, naming, sharing, and protection of files.
4. Device Management: Device management keeps tracks of all devices.
This module also responsible for this task is known as the I/O controller.
It also performs the task of allocation and de-allocation of the devices.
5. I/O System Management: One of the main objects of any OS is to hide
the peculiarities of that hardware devices from the user.
6. Secondary-Storage Management: Systems have several levels of
storage which includes primary storage, secondary storage, and cache
storage. Instructions and data must be stored in primary storage or
cache so that a running program can reference it.
EXPERIMENT-1
AIM - Study of hardware and software requirements of different
operating systems (UNIX,LINUX,WINDOWS XP, WINDOWS 7/8)

Objective: Study of hardware and software requirements of different


operating systems (UNIX, LINUX,WINDOWS XP, WINDOWS7/8)

Content:
Hardware requirements: The most common set of requirements defined by
any operating system or software application is the physical computer
resources, also known as hardware, A hardware requirements list is often
accompanied by a hardware compatibility list (HCL), especially in case of
operating systems.

Hardware and Software Minimum Requirements

1. Windows 10

Processor: I gigahertz (GHz) or faster processor or


SoC RAM: I gigabyte (GB) for 32-bit or 2 GB for 64-
bit
Hard disk space: 16 GB for 32-bit OS or 20 GB for
64-bit OS Graphics card: DirectX 9 or later with
WDDM 1.0 driver Display: 800 x 6009 with WDDM
driver

2. WINDOWS XP
The minimum hardware requirements for Windows XP Home Edition are:
Pentium 233- megahertz (MHz) processor or faster (300 MHz is recommended)

At least 64 megabytes (MB) of RAM (128 MB is recommended)


At least 1.5 gigabytes (GB) of available space on the hard disk CD-ROM or DVD-
ROM drive iv. Keyboard and a Microsoft Mouse or some other compatible
pointing device
Video adapter and monitor with Super VGA (800 x 600)or higher
resolution Sound card
Speakers or headphones

3. UNIXOS

RAM:1 GB
Processor: IBM 604e processor with a clock speed of 375 MHz or faster
Free disk space: /tmp must have 1 GB free disk space. If Tivoli Identity Manager
installs, Web Sphere Application Server, (WAS_HOME) must have 800 MB free
disk space and /var must have 300 MB free disk space. Allocate 500 MB for
/itim45.

4. LINUX

32- bit Intel-compatible processor running at 2 GHz or

greater . 512 MB RAM .


Disk space: 2.5 GB for Pipeline Pilot server plus
components A DVD-ROM drive
EXPERIMENT-2
Aim: - Execute various UNIX system calls for
i. Process management

ii. File management

iii. Input/Output System calls

Content:
The interface between a process and an operating system is provided by system
calls. In general, system calls are available as assembly language instructions.
They are also included in the manuals used by the assembly level programmers.

Unix System Calls


System calls in Unix are used for file system control, process control, interprocess
communication etc. Access to the Unix kernel is only available through these
system calls. Generally, system calls are similar to function calls, the only
difference is that they remove the control from the user process.
There are around 80 system calls in the Unix interface currently. Details about
some of the important ones are given as follows -

System Call Descripti


on

access() This checks if a calling process has access to the required file
chdir()
The chdir command changes the current directory of the system

chmod() The mode of a file can be changed using this command

chown() This changes the ownership of a particular file


kill()
This system call sends kill signal to one or more processes
link()
A new file name is linked to an existing file using link system call.
open() This opens a file for the reading or writing process

pause() The pause call suspends a file until a particular signal occurs.

stime() This system call sets the correct time.

times() Gets the parent and child process times

alarm() The alarm system call sets the alarm clock of a process

System Call Descripti


on
fork()
A new process is created using this command

chroot() This changes the root directory of a file.


exit()
The exit system call is used to exit a process.
File Structure Related Call

Pipeline Messages pipe() msgget() msgsnd() msgrcv()


msgct1()

Semaphores semget() semop()

Shared Memory shmget() shmat() shmdt()

Process Creation and exec()


Termination fork()
wait()
exit()
Process owner and group getuid()
geteuid()
getgid()
getegidd()
Process Identity getpid() getppid()

Process Control signal()kill()

Process Related Calls


PERIMENT–3

AIM – Write a program in C to implement First Come First Serve (FCFS) Scheduling
Algorithm

CODE –
int main()
{
int
n,bt[20],wt[20],tat[20],avwt=0,avt
at=0,i,j; printf("Enter total number of
processes(maximum 20):"); scanf("%d",&n);

printf("\nEnter Process Burst


Time\n"); for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0; //waiting time for first process is 0
//calculating waiting time for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j
++)
wt[i]+=bt[j]
;
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");

//calculatingturnaroundtime
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i; avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d\n",avtat);
return 0;
}

OUTPUT –
Enter total number of processes(maximum

20) : 3 Enter process Burst Time

P[1] : 4

P[2] : 2

P[3] : 6

Processes Burst time Waiting time Turn around


d time

1 40 4
2 2 4 6

3 6 6 12

Average waiting time = 3


Average turnaround time = 7
EXPERIMENT – 4
AIM – Write a Program in C to implement Non Pre-emptive Shortest Job First
scheduling algorithm

CODE –
#include<stdio.h>

int main()
{
int A[100][4]; // Matrix for storing Process Id, Burst
// Time, Average Waiting Time &
Average // Turn Around Time.
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("Process Burst Time Waiting TimeTurn Around Time\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 –
Enter number of process: 5
Enter Burst Time:
P1: 4

P2: 12
P3: 6
P4: 3
P5: 4
Proces Burst Waiting Turn Around
s Time Time Time
P4 3 0 3
P1 4 3 7
P5 4 7 11
P3 6 11 17
P2 12 17 29

Average Waiting Time= 7.600000


Average Turnaround Time= 13.400000
EXPERIMENT – 5

AIM – Write a program in C to implement Priority Scheduling algorithm


CODE -
#include<stdio.h>

struct process
{
int WT,AT,BT,TAT,PT;
};
struct process a[10];
int main()
{
int n,temp[10],t,count=0,short_p;

float total_WT=0,total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of the process\n");
scanf("%d",&n);

printf("Enter the arrival time , burst time and priority of the process\n");
printf("Arrival Time\t Burst Time\t Priority\n");

for(int i=0;i<n;i++)
{
scanf("%d%d%d",&a[i].AT,&a[i].BT,&a[i].PT);
// copying the burst time in a temp array for futher use
temp[i]=a[i].BT;
}
// we initialize the burst time of a process with maximum
a[9].PT=10000;
for(t=0;count!=n;t++)
{
short_p=9;
for(int i=0;i<n;i++)
{
if(a[short_p].PT>a[i].PT && a[i].AT<=t && a[i].BT>0)
{
short_p=i;
}
}
a[short_p].BT=a[short_p].BT-1;

// if any process is completed if(a[short_p].BT==0)


{
// one process is completed so count increases by 1
count++;
a[short_p].WT=t+1-a[short_p].AT-temp[short_p];
a[short_p].TAT=t+1- a[short_p].AT;

//totalcalculation
total_WT=total_WT+a[short_p].WT;
total_TAT=total_TAT+a[short_p].TAT;
}
}

Avg_WT=total_WT/n;
Avg_TAT=total_TAT/n;
// printing of the answer
printf("Proocess ID\t Waiting Time\t Turn Around Time\n");

for(int i=0;i<n;i++)
{
printf("%d %d\t%d\n",i+1,a[i].WT,a[i].TAT);
}

printf("Avg waiting time of the process is %f\n",Avg_WT);


printf("Avg turn around time of the process is %f\n",Avg_TAT);
return 0;
}

OUTPUT:
Enter the number of the process 3
Enter the arrival time , burst time and priority of the process

Arrival Time Burst Time Priority


0 3 3

1 5 1
2 2 2

Turn Around
Process ID Waiting
Time
Time
1 7 10
2 0 5
3 4 6

Avg waiting time of the process is 3.666667


Avg turn around time of the process is 7.000000
Experiment No : 6
AIM – Write a Program in C to implement Round Robin Scheduling Algorithm.
CODE –
#include<stdio.h>

int main()

int i, limit, total = 0, x, counter = 0, time_quantum;

int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10],

temp[10];

float average_wait_time, average_turnaround_time;

printf("Enter Total Number of Processes: ");

scanf("%d", &limit);

x = limit;

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

printf("\nEnter Details of Process[%d]\n", i + 1);

printf("Arrival Time: ");

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

printf("Burst Time: ");

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

temp[i] = burst_time[i];

printf("\nEnter Time Quantum: ");


scanf("%d", &time_quantum);

printf("\nProcess ID\t\tBurstTime Turnaround Timet Waiting

Timen");

for(total = 0, i = 0; x != 0;){

if(temp[i] <= time_quantum && temp[i] > 0)

{ total = total + temp[i];

temp[i] = 0;

counter = 1;

else if(temp[i] > 0){

temp[i] = temp[i] - time_quantum;

total = total + time_quantum;}

if(temp[i] == 0 && counter == 1)

{ x--;

printf("\nProcess[%d]\t\t%d\t\t %d\t\t\t %d", i + 1, burst_time[i],


total - arrival_time[i], total - arrival_time[i] - burst_time[i]);

wait_time = wait_time + total - arrival_time[i] - burst_time[i];

turnaround_time = turnaround_time + total - arrival_time[i];

counter = 0;

if(i == limit - 1)

{ i = 0;

}
else if(arrival_time[i + 1] <= total)

{ i++;

else{

i = 0;
}}
average_wait_time=wait_time*1.0/limit;

average_turnaround_time = turnaround_time * 1.0 / limit;

printf("\n\nAverage Waiting Time: %f", average_wait_time);

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

printf("\n\n ------------ Experiment Successfully Done by ");

return 0;

OUTPUT –
Enter Total Number of Processes: 4
Enter Details of Process[1]

Arrival Time: 2

Burst Time: 4

Enter Details of Process[2]

Arrival Time: 4

Burst Time: 5
Enter Details of Process[3

] Arrival Time: 6

Burst Time: 4

Enter Details of Process[4

] Arrival Time: 3

Burst Time: 6

Enter Time Quantum: 3

Process ID Burst Timet Turnaround Timet Waiting Timen


Process[1] 4 2 -2
Process[2] 5 11 6
Process[3] 4 10 6
Process[4] 6 16 1
0

Average Waiting Time: 5.000000

Avg Turnaround Time: 9.750000


Experim
ent No : 7
AIM - Implement file storage allocation technique:
i. Contiguous(using array)

ii. Linked-list(using linked-list)

iii. Indirect (indexing)

Content: The allocation methods define how the files are stored in the disk
blocks. There are three main disk space or file allocation methods.

Contiguous
Allocation Linked
Allocation Indexed
Allocation

The main idea behind these methods is to provide:


Efficient disk space utilization. Fast access to the file blocks.

Program
SEQUENTIAL FILE ALLOCATION

#include<conio.h>
#include<stdio.h>
void main()
{
int f[50], i, st, len, j, c, k,
count = 0; clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Files Allocated are : \n");
count=0;
printf(“Enter starting block and length of files: ”);
scanf("%d%d", &st,&len);
for(k=st;k<(st+len);k++)
if(f[k]==0) count++; if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf(” The file is allocated to disk\n");
}
else
printf(” The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)"); scanf("%d",
&c);
if(c==1)
goto x;
else
exit();
getch();
}

Program Output:
Files Allocated are :
Enter starting block and length
of files: 14 3 14 1
15 1
16 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 1 The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 4
The file is not allocated
Do you want to enter more file(Yes - 1/No – 0)
LINKED FILE ALLOCATION

#include<conio.h>

#include<stdlib.h>
#include<conio.h>
void main()
{
int f[50], p,i, st, len, j, c, k, a;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: ");
scanf("%d",&p);
printf("Enter blocks already allocated: ");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{
if(f[j]==0)
{ f[j]=1;
printf("%d >%d\n",j,f[j]);
}
else
{
printf("%d Block is already allocated \n",j);
k++;
}
}
}
else
printf("%d starting block is already allocated \n",st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}

Program Output:
Enter how many blocks already allocated: 3
Enter blocks already allocated: 1 3 5
Enter index starting block and length: 2 2 2 >1
3
4 Block is already
allocated 4 >1
Do you want to enter more file(Yes - 1/No - 0)0
INDEXED ALLOCATION TECHNIQUE

i#include<stdio.h>

#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk
: \n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d >%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}

Program Output:
Enter the index block: 5
Enter no of blocks needed and no of files for the
index 5 on the disk : 4
1234
Allocated
File
Indexed
5 >1 :
1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0)1
Enter the index block: 4
5 index is already allocated
6 Enter the index block: 6
Enter no of blocks needed and no of files for the
index 6 on the disk : 2
78
Allocated File Indexed 6
>7 : 1
6-------->8 : 1
Do you want to enter more file(Yes - 1/No - 0)0
EXPERIMENT – 8
AIM – Write a Programme in C to implement Contiguous Allocation
Techniques
1. Worst fit
2. Best fit
3. First fit

CODE –
Worst fit

#include<iostream>

#include<memset>

using namespace std;


void worstFit(int blockSize[], int m, int processSize[], int n)
{
prinf(“Worst fir: \n”);
int allocation[n];
memset(allocation, -1, sizeof(allocation));
for (int i=0; i<n; i++)
{
int wstIdx = -1;
for (int j=0; j<m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (wstIdx == -1)
wstIdx = j;
else if (blockSize[wstIdx] < blockSize[j])
wstIdx = j;
}
}
if (wstIdx != -1)
{
allocation[i] = wstIdx;

blockSize[wstIdx] -= processSize[i];
}
}
printf( "\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++)
{
printf("%d \t\t %d \t\t",i+1,processSize[i]);

if (allocation[i] != -1)
printf(“%d”, allocation[i] + 1);
else
printf("Not Allocated");
printf(“\n”);
}
}

Best fit
void bestFit(int blockSize[], int m, int processSize[], int n)
{
prinf(“Best fit: \n”);
int allocation[n];
memset(allocation, -1, sizeof(allocation));
for (int i=0; i<n; i++)
{
int bestIdx = -1;
for (int j=0; j<m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (bestIdx == -1) bestIdx = j;
else if (blockSize[bestIdx] > blockSize[j])
bestIdx = j;
}
}

if (bestIdx != -1)
{
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}

printf( "\nProcess No.\tProcess Size\tBlock no.\n");


for (int i = 0; i < n; i++)
{
printf("%d \t\t" %d "\t\t",i+1, processSize[i]);

if (allocation[i] != -1)
printf(“%d”,allocation[i] + 1);
else
printf("Not Allocated");
printf(“\n”);
}
}

First fit
void firstFit(int blockSize[], int m, int processSize[], int n)
{
printf(“First Fit: \n”);
int allocation[n];
memset(allocation, -1, sizeof(allocation));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{
allocation[i] = j;
blockSize[j] -= processSize[i];

break;
}
}
}

printf( "\nProcess No.\tProcess Size\tBlock no.\n");


for (int i = 0; i < n; i++)
{
printf("%d \t\t" %d "\t\t",i+1, processSize[i]);

if (allocation[i] != -1)
printf(“%d”,allocation[i] + 1);
else
printf( "Not Allocated");
printf(“\n”);
}
}

void main()
{

int blockSize[] = {100, 500, 200, 300, 600};


int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);
worstFit(blockSize, m, processSize, n);

bestFit(blockSize, m, processSize, n);

firstFit(blockSize, m, processSize, n);

return 0 ;

OUTPUT
T–
Worst fit
Process Process Block no.
No. Size
1 212 5
2 417 2
3 112 5
4 426 Not Allocated

Best fit:
Process Process Block no.
No. Size
1 212 4
2 417 2
3 112 3
4 426 5
First Fit:
Process Process Block no.
No. Size
1 212 2
2 417 5
3 112 2
4 426 Not Allocated
EXPERIMENT-9

AIM- Write a C program to implement file storage allocation techniques for


calculation of external and internal fragmentation.

#include < stdio.h>

#include<conio.h>

void main()
{
int f[50], i, st, len, j, c, k, count = 0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Files Allocated are : \n"); x:
count=0;
printf(“Enter starting block and length of files: ”);
scanf("%d%d",&st,&len);
for(k=st;k<(st+len);k++)
if(f[k]==0) count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[j]==0)
{ f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf(” The file is allocated to disk\n");
}
else
printf(” The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit();
getch();
}

OUTPUT –
Files Allocated are :
Enter starting block and length of files: 14 3 14 1
15 1
16 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 1
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 4
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)0
Experiment No :10
AIM – Write a Program in C to implement Resource Allocation Graph ( RAG).
CODE –
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int proc,res,i,j,row=0,flag=0;

static int pro[3][3],req[3][3],st_req[3][3],st_pro[3][3];

int main()

{
printf("\nEnter the number of Processes: ");
scanf("%d",&proc);
printf("\nEnter the number of Resources: ");
scanf("%d",&res);
printf("\nEnter the Process Matrix:\n");
for(i=0;i<proc;i++)
for(j=0;j<res;j++)
scanf("%d",&pro[i][j]);

printf("\nEnter the Request Matrix:\n");


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

scanf("%d",&req[i][j]);
row=0;
while(1) {
for(i=0;i<res;i++){
if(pro[row][i]==1){
if(st_pro[row][i]>1 && flag==1)
{ printf("\nDeadlock Occured");
return 0;
}
st_pro[row][i]++;
row=i; break;
}}
for(i=0;i<proc;i++){
if(req[row][i]==1){
if(st_req[row][i]>1)
{ printf("\nDeadlock Occured");
return 0;
}
st_req[row][i]++;
row=i;
flag=1;
break;
}} }

printf("\nNo Deadlock Detected");


}
OUTPUT –

Creating a Channel creat() open() close()

Input/Output read() write()

Random Access lseek()

Channel Duplication dup()


Aliasing and removing link()
Files unlink()
File Status stat() fstat()

Access Control access


()
chmo
d()
chow
n()
umas
k()
Device Control ioctl()
EXPERIMENT – 11
AIM- To implement banker’s algorithm using C language.

#include<stdio.h>
#include<conio.h>
void main()
{
int
k=0,output[10],d=0,t=0,ins[5],i,avail[5],allocated[10][5],need[10][5],MAX[10][5],
pno,P[10], j,rz, count=0;
clrscr();
printf("\n Enter the number of resources : ");
scanf("%d", &rz);
printf("\n enter the max instances of each resources\n");
for (i=0;i<rz;i++) {
avail[i]=0;
printf("%c= ",(i+97));
scanf("%d",&ins[i]);
}
printf("\n Enter the number of processes : ");
scanf("%d", &pno);
printf("\n Enter the allocation matrix \n ");
for (i=0;i<rz;i++)
printf(" %c",(i+97));
printf("\n");
for (i=0;i <pno;i++) {
P[i]=i;
printf("P[%d] ",P[i]);
for (j=0;j<rz;j++) {
scanf("%d",&allocated[i][j]);
avail[j]+=allocated[i][j];
}

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

for (i=0;i<rz;i++) {
printf(" %c",(i+97));
avail[i]=ins[i]-avail[i];
}
printf("\n");
for (i=0;i <pno;i++) {
printf("P[%d] ",i);

for (j=0;j<rz;j++)
scanf("%d", &MAX[i][j]);

}
printf("\n");
A: d=-1;
for (i=0;i <pno;i++) {

count=0;
t=P[i];
for (j=0;j<rz;j++) {
need[t][j] = MAX[t][j]-allocated[t][j];

if(need[t][j]<=avail[j])
count++;
}
if(count==rz) {
output[k++]=P[i];

for (j=0;j<rz;j++)
avail[j]+=allocated[t][j];
}

else
P[++d]=P[i];
}
if(d!=-1) {
pno=d+1;
goto A;
}
printf("\t <"); for
(i=0;i<k;i++)
printf(" P[%d] ",output[i]);
printf(">");
getch();
}
EXPERIMENT – 12

AIM – Write a program in C to convert Resource Allocation Graph(RAG) to wait


for graph (WFG) for each type of method used for storing graph.

CODE –
#include <stdio.h>
#include <stdlib.h>

// Structure for a node in the adjacency list


struct Node {
int dest;
struct Node* next;
};

// Structure for the adjacency list


struct AdjList {
struct Node* head;
};
// Structure for the graph
struct Graph {
int V;
struct AdjList* array;
};
// Function to create a new node in the adjacency list
struct Node* newNode(int dest) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
// Function to create a graph with V vertices
struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->array = (struct AdjList*)malloc(V * sizeof(struct AdjList));
for (int i = 0; i < V; i++) {
graph->array[i].head = NULL;
}
return graph;
}
// Function to add an edge to the graph
void addEdge(struct Graph* graph, int src, int dest) {
struct Node* newNode = newNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
}
// Function to print the wait-for-graph
void printWFG(struct Graph* wfg) {
for (int i = 0; i < wfg->V; i++) {
struct Node* node = wfg->array[i].head;
printf("%d -> ", i);
while (node != NULL)
{
printf("%d, ", node->dest);
node = node->next;
}
printf("\n");
}
}
// Function to convert the RAG to WFG
struct Graph* convertRAGtoWFG(struct Graph* rag) {
struct Graph* wfg = createGraph(rag->V);
// Loop through all vertices in the RAG
for (int i = 0; i < rag->V; i++) {
// Loop through all vertices adjacent to the
current vertex in the RAG
struct Node* node = rag->array[i].head;
while (node != NULL) {
int dest = node->dest;
// Loop through all vertices adjacent to the
destination vertex in the RAG
struct Node* destNode = rag->array[dest].head;
while (destNode != NULL) {
int destDest = destNode->dest;
// Add an edge from the current vertex to the destination-destination vertex in
the WFG
addEdge(wfg, i, destDest);
destNode = destNode->next;
}
node = node->next;
}
}
return wfg;
}

// Main function
int main() {
struct Graph* rag = createGraph(4);
addEdge(rag, 0, 1);
addEdge(rag, 0, 2);
addEdge(rag, 1, 2);
addEdge(rag, 2, 0);
addEdge(rag, 2, 3);
addEdge(rag, 3, 3);
struct Graph* wfg = convertRAGtoWFG(rag);
printf("Wait-for-Graph:\n");
printWFG(wfg);
return 0;
}
EXPERIMENT – 13
AIM- Write a program in C to implement the solution for bounded
buffer problem using inter process communication techniques-
Semaphores

#include <stdio.h>

#include <stdlib.h>// Initialize a mutex to 1

int mutex = 1;// Number of full slots as 0

int full = 0;// Number of empty slots as size of buffer

int empty = 10, x = 0;

// Function to produce an item and


// add it to the

buffer void producer()


{
// Decrease mutex value by 1
--mutex;

// Increase the number of full


// slots by 1
++full;

// Decrease the number of empty


// slots by 1
--empty;

//Item produced
x++;
printf("\nProducer produces" "item %d",x);

// Increase mutex value by 1


++mutex;
}
// Function to consume an item and
// remove it from

buffer void consumer()


{
// Decrease mutex value by 1
--mutex;
// Decrease the number of full
// slots by 1
--full;
// Increase the number of empty
// slots by 1
++empty;
printf("\nConsumer consumes " "item %d",x);

x--;// Increase mutex value by 1

++mutex;
}
// Driver Code

int main()
{
int n, i;
printf("\n1. Press 1 for Producer" "\n2. Press 2 for Consumer" "\n3.
Press 3 for Exit");

// Using '#pragma omp parallel for can give wrong value due to
synchronization issues.

// 'critical' specifies that code is executed by only one thread at a time i.e., only
one thread enters
// the critical section at a given
time #pragma omp critical

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


printf("\nEnter your choice:");
scanf("%d", &n);// Switch Cases
switch (n)
{ case 1:

// If mutex is 1 and empty is non-zero, then it is possible to produce


if ((mutex == 1)&& (empty != 0))
{ producer();
}
// Otherwise, print buffer is full
else {
printf("Buffer is full!");
}
break;
case 2:

// If mutex is 1 and full is non-zero, then it is possible to consume


if ((mutex == 1)&& (full != 0))
{ consumer();
}
// Otherwise, print Buffer is empty
else {
printf("Buffer is empty!");
}
break;

// Exit Condition
case 3:
exit(0);
break;
}

}
}

OUTPUT
EXPERIMENT – 14
AIM- Write a program in C to implement the solution for reader writer
problem using inter process communication techniques-Semaphores

#include<semaphore.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
sem_t x,y;
pthread_t tid;
pthread_t
writerthreads[100],readerthreads[100];
int readercount = 0;
void *reader(void* param)
{
sem_wait(&x);
readercount++;
if(readercount==1)
sem_wait(&y);
sem_post(&x);
printf("%d reader is inside\n",readercount);
usleep(3);
sem_wait(&x);
readercount--;
if(readercount==0)
{
sem_post(&y);
}
sem_post(&x);
printf("%d Reader is leaving\n",readercount+1);
return NULL;
}

void *writer(void* param)


{
printf("Writer is trying to enter\n");
sem_wait(&y);
printf("Writer has entered\n");
sem_post(&y);
printf("Writer is leaving\n");
return NULL;
}
int main()
{
int n2,i;
printf("Enter the number of readers:");
scanf("%d",&n2);
printf("\n");
int n1[n2];
sem_init(&x,0,1);
sem_init(&y,0,1);
for(i=0;i<n2;i++)
{
pthread_create(&writerthreads[i],NULL,reader,NULL);
pthread_create(&readerthreads[i],NULL,writer,NULL);
}
for(i=0;i<n2;i++)
{
pthread_join(writerthreads[i],NULL);
pthread_join(readerthreads[i],NULL);
}

OUTPUT –
1 reader is inside
Writer is trying to
enter 2 reader is
inside
2 Reader is leaving
2 reader is inside
Writer is trying to
enter 2 Reader is
leaving
1 Reader is leaving

You might also like