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

OSFinalFully

The document outlines the study of hardware and software requirements for various operating systems including Windows, Linux, and Unix, highlighting their comparative analysis in terms of cost, ease of use, security, and gaming capabilities. It also discusses the advantages and disadvantages of each operating system, along with their minimum hardware and software requirements. Additionally, the document includes experiments related to UNIX system calls and process scheduling algorithms such as First Come First Serve, Shortest Job First, and SRTN.

Uploaded by

Dreadcore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

OSFinalFully

The document outlines the study of hardware and software requirements for various operating systems including Windows, Linux, and Unix, highlighting their comparative analysis in terms of cost, ease of use, security, and gaming capabilities. It also discusses the advantages and disadvantages of each operating system, along with their minimum hardware and software requirements. Additionally, the document includes experiments related to UNIX system calls and process scheduling algorithms such as First Come First Serve, Shortest Job First, and SRTN.

Uploaded by

Dreadcore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 1
Aim-: Study of hardware and software requirements of different
operating systems(UNIX, LINUX, WINDOWS XP, WINDOWS7/8.
OPERATING SYSTEM
An operating system acts as an interface between the software and different parts of the
computer or the computer hardware. The operating system is designed in such a way that
it can manage the overall resources and operations of the computer. It is a fully
integrated set of specialized programs that handle all the operations of the computer.

TYPES OF OPERATING SYSTEM


● Windows
● Linux
● Unix

COMPARATIVE ANANYSISOF VARIOUS OS


Parameters Windows Linux Unix

Cost Less Costly Free More Costly

Ease of use Easy to use Littly difficult for Easy to use


first time user

Security More prone to More vulnerable Less prone to


attack than attacks
Unix but less than
Windows
Gaming More games are Less games are Less games are
present. present present

Development and Developed and Linux is open Most of Unix


Distribution distributed by sourced and system are
Microsoft distributed by developed by AT&T
various vendors with other
commercial
vendors and non
profit organization.
ADVANTAGES AND DISADVANTAGES OF VARIOUS OPERATING
SYSTEM
Operating System Lab (KCS-451) PRASHANT YADAV 2100910100131
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering


1. WINDOWS
Advantages
● User friendly interface and easy to use
● Compatible to all hardware
● Support plug and play feature
● Provide software Development support
● Have both Desktop and touch screen support

Disadvantages
● Security Concerns
● Paid software
● Infection assults
● Rebooting a framework
● High Performance System Required

2. LINUX
Advantages
● Open Source
● No antivirus software needed
● Low system specifications
● No reboot needed for installing/uninstalling
● Less disk space needed

Disadvantages
● Software compatibility
● Gaming
● Limited software Availability
● Hardware Compatibility
● Learning curve

3. UNIX Advantages
● Very stable
● Less code to execute
● Ideal for web hosting
● Safe and secure
● Multitasking

Operating System Lab (KCS-451) PRASHANT YADAV 2100910100131


JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering


Disadvantages
● Not user friendly
● Cryptic commands
● Gaming
● Less softwares are available
● Software Compatibility

HARDWARE AND SOFTWARE MINIMUM REQUIREMENTS


1 WINDOWS 10
● Processor: 1 Gigahertz (GHz) or faster processor.
● RAM: 1 gigabyte (GB) for 32-bit OS or 2GB for 64-bit OS.
● Hard disk Space: 16 Gb for 32-bit OS or 20GB for 64-bit OS.
2 Windows XP
● Pentium 233 MHz processor or faster.
● At least 64 MB of RAM (128 MB is recommended).
● At least 1.5 GB of available space.
3 LINUX
● 32-bit Intel-compatible processor running at 2GHz or greater.
● 512 MB RAM
● Disk space- 2.5 Gb for pipeline Pilot server plus components
● A DVD –ROM drive
4 UNIX
● RAM:1 GB
● Processor: IBM 604e processor with a clock speed of 375 MHz or faster
● Disk space: 1GB free disk space

Operating System Lab (KCS-451) PRASHANT YADAV 2100910100131


JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 2
AIM- Execute various UNIX system calls for
1. Process Management
2. File Management
3. Input/Output System Calls

SYSTEM CALL
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 CALL


System calls in UNIX are used for file system control, process control, inter
process 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.

TYPES OF SYSTEM CALLS


There are three types of system calls which are as follows :-

Process Control:-
These system calls deal with processes such as process creation, process
termination etc.

File Management:-
These system calls are responsible for file manipulation such as creating a file,
reading a file, writing into a file etc.

Input/Output Management:-
These system calls are responsible for device manipulation such as reading
from device buffers, writing into device buffers etc.

Some of the examples of all the above types of system calls in Windows and UNIX
are given as follows −
COMMONLY USED UNIX SYSTEM CALLS FOR PROCESS MANAGEMENT:
 fork(): Creates a new process by duplicating the existing process.
 wait(): Suspends the execution of the current process until one of its child processes
terminates.
 waitpid(): Suspends the execution of the current process until a specific child
process terminates.
 waitid(): Suspends the execution of the current process until a specific child process
or group of child processes terminates.
 exit(): Terminates the current process and returns the exit status to the parent process.
 kill(): Sends a signal to a specified process.
Operating System Lab (KCS-451) PRASHANT YADAV 2100910100131
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering


 getpid(): Returns the process ID (PID) of the current process.
 getppid(): Returns the PID of the parent process of the current process.

Operating System Lab (KCS-451) PRASHANT YADAV 2100910100131


JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering


 getpgrp(): Returns the process group ID of the current process.
 setpgid(): Sets the process group ID of a process.
 setsid(): Creates a new session and sets the process group ID.
 nice(): Modifies the priority of a process.
 setpriority(): Sets the priority of a process.
 getpriority(): Gets the priority of a process.
 sched_yield(): Yields the processor to other processes.
 getrusage(): Gets resource usage statistics for the current or child processes.
 times(): Gets the current process and system times.
 getrlimit(): Gets the resource limits for the current process.
 setrlimit(): Sets the resource limits for the current process.

COMMONLY USED UNIX SYSTEM CALLS FOR FILE MANAGEMENT:


 open(): Opens a file or creates a new file if it doesn't exist.
 close(): Closes a file descriptor.
 read(): Reads data from a file.
 write(): Writes data to a file.
 lseek(): Changes the file offset (position) within a file.
 unlink(): Deletes a file.
 rename(): Renames a file.
 chmod(): Changes the permissions of a file.
 chown(): Changes the owner and group of a file.
 mkdir(): Creates a new directory.
 rmdir(): Deletes an empty directory.
 opendir(): Opens a directory for reading.
 readdir(): Reads the next directory entry.
 closedir(): Closes a directory.
 link(): Creates a hard link to a file.
 symlink(): Creates a symbolic (soft) link to a file.
 readlink(): Reads the value of a symbolic link.
 chdir(): Changes the current working directory.
 getcwd(): Gets the current working directory.

COMMONLY USED UNIX SYSTEM CALLS FOR INPUT/OUTPUT SYSTEM CALL:


 open(): Opens a file or creates a new file if it doesn't exist.
 close(): Closes a file descriptor.
 read(): Reads data from a file descriptor.
 write(): Writes data to a file descriptor.
 ioctl(): Performs device-specific input/output operations (e.g., configuring a serial port).
 select(): Monitors multiple file descriptors for read/write/exception events.
 poll(): Polls multiple file descriptors for events.
 recv() and send(): Receives and sends data over a network socket.
 socket(): Creates a new network socket.
Operating System Lab (KCS-451) PRASHANT YADAV 2100910100131
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering


 bind(): Associates a socket with a local address.
 listen(): Puts a socket into a passive listening mode.
 accept(): Accepts an incoming network connection on a listening socket.
 connect(): Initiates a connection to a remote network address.
 shutdown(): Shuts down all or part of a network connection.
 pipe(): Creates an interprocess communication (IPC) pipe.
 dup() and dup2(): Duplicates a file descriptor.
 fsync(): Flushes changes to a file to disk.
 ftruncate(): Truncates a file to a specified size.

Operating System Lab (KCS-451) PRASHANT YADAV 2100910100131


JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

Experiments 3.1

AIM : Implement First Come First Serve

Source Code:
#include <stdio.h>
void findwt(int wt[],char p[],int bt[],int n)
{
wt[0]=0; for(int i=1;i<n;i++)
wt[i]=bt[i- 1]+wt[i-1];
}

int findtat(int wt[],char p[],int bt[],int n,int tat[])


{
for(int i=0;i<n;i++) tat[i]=bt[i]
+wt[i]; return 0;
}

float avgtat(int tat[],int n)


{
float z=0; for(int i=0;i<n;i++)
z=z+tat[i]; return (z/n);
}

float avgwt(int wt[],int n)


{
float z=0; for(int i=0;i<n;i++)
z=z+wt[i]; return (z/n);
}

int main()
{
int n; printf("give the number of processes ");
scanf("%d",&n);
char p[n];
int
bt[n],wt[n],tat[n
]; for(int
i=0;i<n;i++)
{ printf("enter process and burst time ");

Operating System Lab (KCS-451) PRASHANT YADAV 2100910100131


JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

scanf("%s %d",&p[i],&bt[i]);
}
findwt(wt,p,bt,n);
findtat(wt,p,bt,n,tat);
printf("TABLE\n");
printf("Process\tBurst\tWait\
tTAT"); for(int i=0;i<n;i++)
{ printf("\n");
printf("%c \t%d \t
%d
t
%d",p[i],bt[i],wt[i]
,tat[i]); printf("\
n");
}
printf("avg turn around time : %f\
n",avgtat(tat,n)); printf("avg wait time :
%f",avgwt(wt,n));
return 0;
}

OUTPUT

Operating System Lab (KCS-451) PRASHANT YADAV 2100910100131


JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 3.2

AIM: Implement Shortest Job First

Source Code:

#include<stdio.h
> int main(){
int
b[20],p[20],w[20],t[20],i,j,n,total=0,totalT=0,pos,temp;
float aW,aT;
printf("Enter the number of process");
scanf("%d",&n);
printf("\nEnter burst time:\n");
for(int i=0;i<n;i++){ printf("p
%d\t",i+1); scanf("%d",&b[i]);
p[i]=i+1;
}
for(i=0;i<n;i++){
pos=i;
for(j=i+1;j<n;j++)
{ if(b[j]<b[pos]){
pos=j;
}
}
temp=b[i];
b[i]=b[pos];
b[pos]=temp
:
temp=p[i];
p[i]=p[pos];
p[pos]=temp
;
}
w[0]=0;
for(i=1;i<n;i++)
{ w[i]=0;
for(j=0;j<i;j++)
{ w[i]+=b[j];
}
total+=w[i];
}
aW=(float)total/n; printf("\nProcess\tBurstTime\tWaitingTime\
tTurnaroundTime"); for(int i=0;i<n;i++){
Operating System Lab (KCS-451) PRASHANT YADAV 2100910100131
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering


t[i]=b[i]+w[i];

Operating System Lab (KCS-451) PRASHANT YADAV 2100910100131


JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering


totalT+=t[i]; printf("\np%d\t\t%d\t\t%d\t\t
%d",p[i],b[i],w[i],t[i]);
}
aT=(float)totalT/n;
printf("\n\nAverage waiting time=%f",aW);
printf("\nAverage turn around time=%f",aT);
return 0;
}

OUTPUT

Operating System Lab (KCS-451) PRASHANT YADAV 2100910100131


JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 3.3
AIM: Implement SRTN

SOURCE CODE:
#include
<stdio.h> int
main()
{
int
a[10],b[10],x[10],i,j,smallest,count=0,ti
me,n; double avg=0,tt=0,end;
printf("enter the number of Processes:
"); scanf("%d",&n);
for(i=0;i<n;i++){
printf("enter process p%d arrival time and burst time
",i+1); scanf("%d %d",&a[i],&b[i]);
}
for(i=0;i<n;i
++)
x[i]=b[i];

b[9]=9999;

for(time=0;count!=n;time++)
{
smallest=9;
for(i=0;i<n;i
++)
{
if(a[i]<=time && b[i]<b[smallest] &&
b[i]>0 ) smallest=i;
}
b[smallest]--;
if(b[smallest]=
=0)
{ count++;
end=time
+1;
avg=avg+end-a[smallest]-
x[smallest]; tt= tt+end-
a[smallest];
}}
printf("\n\nAverage waiting time = %lf\
n",avg/n); printf("Average Turnaround
time = %lf",tt/n); return 0;}

OUTPUT:

Operating System Lab (KCS-451) Abhinav Airachia 2100910310005


JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering


EXPERIMENT 3.4
AIM : Implement primitive priority based scheduling

SOURCE CODE:

#include
&lt;stdio.h&gt; int
main()
{
int
a[10],b[10],x[10],i,j,smallest,count=0,time,n,
p[10]; int wt[10],tat[10];
double avg=0,tt=0,end;
printf(&quot;enter the number of Processes:
&quot;); scanf(&quot;%d&quot;,&amp;n);
for(i=0;i&lt;n;i++){
printf(&quot;enter process p%d arrival time and burst time and priority
&quot;,i+1); scanf(&quot;%d %d
%d&quot;,&amp;a[i],&amp;b[i],&amp;p[i]);
}
for(i=0;i&lt;n;i
++) x[i]=b[i];

b[9]=99
99;
p[9]=99
99;
for(time=0;count!=n;time++)
{
smallest=9;
for(i=0;i&lt;n;i
++)
{
if(a[i]&lt;=time &amp;&amp; p[i]&lt;p[smallest] &amp;&amp;
b[i]&gt;0 ) smallest=i;
}
b[smallest]--;
if(b[smallest]=
=0)
{count++;
end=time+1;
wt[i]=time+1-a[smallest]-
x[smallest]; tat[i]=time+1-
a[smallest]; avg=avg+end-
a[smallest]-x[smallest]; tt=
tt+end-a[smallest];
}}
printf(&quot;ID WT TAT\
n&quot;); for(i=0;i&lt;n;i++)
{ printf(&quot;%d %d\t%d\n&quot;,i+1,wt[i],tat[i]);}
printf(&quot;\n\nAverage waiting time = %lf\
n&quot;,avg/n); printf(&quot;Average Turnaround
time = %lf&quot;,tt/n); return 0;

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering


OUTPUT:

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 3.5
AIM: Implement Round Robin

SOURCE CODE:

#include<stdio.h
> int main(){
int i, n, total = 0, x, counter = 0, tq;
int wt = 0, tat = 0, a[10], b[10], temp[10];
float aW, aTat;
printf("Enter Total Number of Processes:");
scanf("%d", &n);
x =n;
printf("Enter arrivelTime BurstTime:\n");
for(i = 0; i <n; i++){
scanf("%d%d", &a[i],&b[i]);
temp[i] = b[i];
}
printf("Enter Time Quantum:");
scanf("%d", &tq);
printf("\nProcess\t BurstTime\tTurnaroundTime\tWaitingTime\n");
for(total = 0, i = 0; x != 0;){
if(temp[i] <= tq && temp[i] > 0){
total = total + temp[i];
temp[i] = 0;
counter = 1;
else if(temp[i] > 0)
{ temp[i] = temp[i]
- tq; total = total
+ tq;
}
if(temp[i] == 0 && counter == 1)
{ x--;
printf("%d\t\t%d\t\t%d\t\t%d\n", i + 1, b[i], total - a[i], total - a[i] - b[i]);
wt= wt + total - a[i] - b[i];
tat = tat+ total -
a[i]; counter = 0;
}
if(i == n- 1)
{ i = 0;
}else if(a[i + 1] <= total){
i++;
}else
{i=
0;
}}
aW= wt * 1.0 / n;
Operating System Lab (KCS-451) Abhinav Airachia
2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering


aTat= tat* 1.0 / n;
printf("\nAverage Waiting Time:%f", aW);
printf("\nAvg Turnaround Time:%f\n", aTat);
return 0;
}

OUTPUT

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 5

AIM: Implement Banker’s Algorithm

SOURCE CODE:
#include
<stdio.h> int
main(){
int n,m;
printf("enter number of processes");
scanf("%d",&n);
printf("enter number of types of resources");
scanf("%d",&m);
int alloc[n][m];
int max[n]
[m]; int
avail[m]; int
ind=0;
printf("enter allocation matrix elements");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&alloc[i]
[j]);
}
}
printf("enter maximum matrix elements");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&max[i]
[j]);
}
}
printf("enter available matrix elements");
for(int i=0;i<m;i++){
scanf("%d",&avail[i]);
}
int need[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
need[i][j]=max[i][j]-alloc[i][j];
}
}
int f[n];
for(int i=0;i<n;i++)
{ f[i]=0;
}
Operating System Lab (KCS-451) Abhinav Airachia
2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering


int ans[n];

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
if(f[i]==0){
int c=0;
for(int j=0;j<m;j++){
if(need[i][j]>avail[j])
{ c=1;
break;
}
}
if(c==0){
ans[ind+
+]=i;
for(int v=0;v<m;v++){ avail[v]+=alloc[i]
[v];
}
f[i]=1
;
}}
}}
int c=1;
for(int i=0;i<n;i++)
{ if(f[i]==0){
c=0;
printf("not in safe state");
break;
}
}
if(c==1){
printf("Following is the safe Sequence\n");
for (int i = 0; i < n-1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
}
return 0;
}

OUTPUT:-

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 6.1

AIM: Implement First Fit Contiguous Memory Allocation Technique

SOURCE CODE:
#include<stdio.h>
void First_Fit(int process_size[],int n,int block_size[],int m)
{ int i,j;
int allocation[n];
for(i=0;i<n;i++)
{allocation[i]=-1;
}
for(i=0;i<n;i++)
{ for(j=0;j<m;j++)
{if(block_size[j]>process_size[i])
{allocation[i]=j;
block_size[j]=block_size[j]-
process_size[i]; break;
}
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++)
{ printf(" P%i\t\t", i+1);
printf("%i\t\t", process_size[i]);
if (allocation[i] != -1)
printf("%i", allocation[i] + 1);
else
printf("Not
Allocated"); printf("\n");
}
}
int main()
{int
process_size[]={100,300,500,200,600}; int
n=5;
int block_size[]={212,417,112,426};
int m=4;
First_Fit(process_size,n,block_size,m)
; return 0;
}
OUTPUT:-

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 6.2
AIM: Implement Worst Fit Contiguous Memory Allocation Technique

SOURCE CODE:
#include <stdio.h>
void WorstFit(int blockSize[], int blocks, int processSize[], int processes)
{int
allocation[processes];
int occupied[blocks];
for(int i = 0; i < processes; i++)
{allocation[i] = -1;
}
for(int i = 0; i < blocks; i++)
{occupied[i] = 0;
}
for (int i=0; i < processes; i++)
{int indexPlaced = -1;
for(int j = 0; j < blocks; j++)
{if(blockSize[j] >= processSize[i] && !occupied[j])
{if (indexPlaced == -1)
{indexPlaced = j;
}
else if (blockSize[indexPlaced] <
blockSize[j]) indexPlaced = j;
}
}
if (indexPlaced != -1)
{allocation[i] = indexPlaced;
occupied[indexPlaced] = 1;
blockSize[indexPlaced] -=
processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock
no.\n"); for (int i = 0; i < processes; i++)
{printf("P%d \t\t\t %d \t\t\t", i+1,
processSize[i]); if (allocation[i] != -1)
printf("%d\n",allocation[i]
+ 1); else
printf("Not Allocated\n");
}
}
int main()
{int processSize[] = {100, 300, 500, 200, 600};
int blockSize[] = {212, 417, 112,
426}; int blocks = 4;
int processes = 5;
WorstFit(blockSize, blocks, processSize,
processes); return 0;
}
OUTPUT:-

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 6.3
AIM: Implement Best Fit Contiguous Memory Allocation Technique
SOURCE CODE:
#include <stdio.h>
void BestFit(int blockSize[], int blocks, int processSize[], int proccesses)
{intallocation[
proccesses];
int
occupied[bloc
ks];
for(int i = 0; i < proccesses; i++)
{allocation[i] = -1;
}
for(int i = 0; i < blocks; i++)
{occupied[i] = 0;
}
for (int i = 0; i < proccesses; i++)
{int indexPlaced = -1;
for (int j = 0; j < blocks; j++)
{if (blockSize[j] >= processSize[i] && !occupied[j])
{if
(indexPlac
ed == -1)
{indexPlac
ed = j;
}
else if (blockSize[j] <
blockSize[indexPlaced])
{indexPlaced = j;
}
}
}
if (indexPlaced != -1)
{allocation[i] = indexPlaced;
occupied[indexPlaced] = 1;
}
}
printf("\nProcess No.\tProcess
Size\tBlock no.\n"); for (int i = 0;
i < proccesses; i++)
{printf("%d \t\t\t %d \t\t\t",
i+1, processSize[i]); if
(allocation[i] != -1)
printf("%d\
n",allocation[i] +
1); else
printf("Not Allocated\n");
}
}int main()
{int blockSize[] = {212, 417, 112, 426};
int processSize[] = {100, 300,
500, 200, 600}; int blocks =
4;
int proccesses = 5;
BestFit(blockSize,
blocks,processSize,proccesses);
return 0 ;
} OUTPUT:-

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 4.1
AIM: Implementation of contiguous file storage allocation technique.

SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int start[10],num=0,count=0,length[10],j,f=1,i=0;
char name[20][10];
char del[2];
int ch=0;
while(1){
printf("Contiguous file allocation\n");
printf("1.File Creation\n");
printf("2.File Deletion\n");
printf("3.Display File Allocation Table\n");
printf("4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch){
case 1:
printf("Enter the name of the file\n");
scanf("%s",&name[i][0]);
printf("Enter the start block of the file\n");
scanf("%d",&start[i]);
printf("Enter the length of the file\n");
scanf("%d",&length[i]);
num++;
i++;
if(f==1){
f++;
}
for(j=0;j<num;j++){
if(start[j+1]<=start[j] || start[j+1]>=length[j]){
}
else{
count++;
}
}
if(count==1){
printf("%s cannot be allocated disk space\n",name[i-1]);
}
else{
printf("file %s allocated disk space\n",name[i-1]);
}
break;
case 2:
printf("Enter the name of the file to be deleted\n");
Operating System Lab (KCS-451)
scanf("%s",&del[0]); Abhinav Airachia
2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

printf("file %s deleted\n",&del[0]);
f--;
f--;
break;
case 3:
printf("File Allocation Table\n");
printf("File Name Start Block Length\n");
if(f==2){
printf("%s %d %d\n",name[0],start[0],length[0]);
}
for(int k=0,n=1;k<num && n<num ;k++,n++){
if(start[k+1]<=start[k] || start[k+1]>=length[k]){
printf("%s %d %d\n",name[n],start[n],length[n]);
}
}
break;
case 4:
exit(1);
default:
printf("invalid");
}
getchar();
}
return 0;
}

OUTPUT:

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 4.2
AIM-Write a program to implement Linked list file storage allocation
technique
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
void recursivePart(int pages[])
{ int st,len, k, c, j;
printf("Enter the index of the starting block and its length: ");
scanf("%d%d", &st, &len);
k = len;
if (pages[st] == 0){
for (j = st; j < (st + k); j++){
if (pages[j] == 0)
{ pages[j] = 1;
printf("%---->%d\n", j, pages[j]);
}
else {
printf("The block %d is already allocated \n", j);
k++;
}}}
else
printf("The block %d is already allocated \n", st);
printf("Do you want to enter more files? \n");
printf("Enter 1 for Yes, Enter 0 for No: ");
scanf("%d", &c);
if (c==1)
recursivePart(pages);
else
exit(0);
return;}
int main(){
int pages[50], p, a;
for (int i = 0; i < 50; i++)
pages[i] = 0;
printf("Enter the number of blocks already allocated: ");
scanf("%d", &p);
printf("Enter the blocks already allocated: ");
for (int i = 0; i < p; i++)
{
scanf("%d", &a);
pages[a] = 1;}
recursivePart(pages);
getch();
return 0;
}
OUTPUT

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 4.3
AIM: Write a program to implement Indirect file storage allocation techniques.
SOURCE CODE:
#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");

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

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();
}
OUTPUT:

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 7
AIM Calculation of external and internal fragmentation
1. Free space list of blocks from system
2. List process file from the system

SOURCE CODE:
#include <stdio.h>
#define DISK_SIZE 100
int disk[DISK_SIZE];
#Blocks
struct Block {
int start;
int size;
};
#Process File
struct ProcessFile {
int start;
int size;
};
int calculateExternalFragmentation(struct Block* blocks, int num_blocks) {
int total_gap_size = 0;
int last_block_end = 0;
for (int i = 0; i < num_blocks; i++) {
int block_start = blocks[i].start;
int block_size = blocks[i].size;
if (block_start > last_block_end) {
int gap_size = block_start - last_block_end;
total_gap_size += gap_size;
}
last_block_end = block_start + block_size;
}
int last_block_size = DISK_SIZE - last_block_end;
total_gap_size += last_block_size;
return total_gap_size;
}
int calculateInternalFragmentation(struct ProcessFile* files, int num_files) {
int total_internal_fragmentation = 0;
for (int i = 0; i < num_files; i++) {
int file_size = files[i].size;
int block_size = disk[files[i].start];
int internal_fragmentation = block_size - file_size;
total_internal_fragmentation += internal_fragmentation;
}
return total_internal_fragmentation;
}
int main() {
struct Block blocks[3] = {
{0, 30},
{40, 20},
{70, 10}
};
struct ProcessFile files[2] = {
{0, 15},
{40, 5}
};
for (int i = 0; i < DISK_SIZE; i++) {
Operating System Lab (KCS-451) Abhinav Airachia
2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

disk[i] = 0;
}
for (int i = 0; i < sizeof(blocks) / sizeof(blocks[0]); i++) {
for (int j = blocks[i].start; j < blocks[i].start + blocks[i].size; j++) {
disk[j] = 1;
}
}
int external_fragmentation = calculateExternalFragmentation(blocks, sizeof(blocks) /sizeof(blocks[0]));
printf("External Fragmentation: %d\n", external_fragmentation);
int internal_fragmentation = calculateInternalFragmentation(files, sizeof(files) / sizeof(files[0]));
printf("Internal Fragmentation: %d\n", internal_fragmentation);
return 0;
}
OUTPUT

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 8
AIM Implementation of compaction for the continually changing memory layout and calculate total movement of
data
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#define MEMORY_SIZE 100
struct AllocatedBlock {
int start;
int size;
};
struct AllocatedBlock allocated_blocks[MEMORY_SIZE];
int num_allocated_blocks = 0;
int total_movement = 0;
void allocateBlock(int size) {
int start = -1;
for (int i = 0; i < MEMORY_SIZE; i++) {
if (allocated_blocks[i].size == 0) {
start = i;
break;
}
}
if (start != -1) {
allocated_blocks[num_allocated_blocks].start = start;
allocated_blocks[num_allocated_blocks].size = size;
num_allocated_blocks++;
} else {
printf("No space available for allocation.\n");
}
}
void deallocateBlock(int index) {
int start = allocated_blocks[index].start;
int size = allocated_blocks[index].size;
for (int i = start; i < start + size; i++) {
allocated_blocks[i].size = 0;
}
}
void compactMemory() {
int new_start = 0;
total_movement = 0;
for (int i = 0; i < num_allocated_blocks; i++) {
if (allocated_blocks[i].start != new_start) {
int movement = allocated_blocks[i].start - new_start;
total_movement += abs(movement);
for (int j = 0; j < allocated_blocks[i].size; j++) {
allocated_blocks[new_start + j].size = allocated_blocks[i].size;
allocated_blocks[allocated_blocks[i].start + j].size = 0;
}
}
new_start += allocated_blocks[i].size;
}
}
void printMemoryLayout() {
printf("Memory Layout:\n");
for (int i = 0; i < MEMORY_SIZE; i++) {
if (allocated_blocks[i].size == 0) {
printf("[%d: Free]\n", i);
Operating System Lab (KCS-451) Abhinav Airachia
2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

} else {
printf("[%d: Allocated (%d bytes)]\n", allocated_blocks[i].start, allocated_blocks[i].size);
}
}
}

int main() {
allocateBlock(10);
allocateBlock(5);
allocateBlock(7);
allocateBlock(3);

printMemoryLayout();

compactMemory();

printf("\nMemory after compaction:\n");


printMemoryLayout();

printf("\nTotal movement of data during compaction: %d bytes\n", total_movement);

return 0;
}

Output:

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 9
AIM Implementation of resource allocation graph (RAG).
SOURCE CODE:
#include <stdio.h>
#include <stdbool.h>
#define MAX_PROCESSES 10
#define MAX_RESOURCES 10
bool isSafeState(int processes[], int available[], int max[][MAX_RESOURCES], int allocation[][MAX_RESOURCES], int num_processes, int
num_resources) {
bool finish[MAX_PROCESSES] = { false };
int work[MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
for (int i = 0; i < num_resources; i++) {
work[i] = available[i];
}
for (int i = 0; i < num_processes; i++) {
for (int j = 0; j < num_resources; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
int count = 0;
while (count < num_processes) {
bool found = false;
for (int i = 0; i < num_processes; i++) {
if (!finish[i]) {
int j;
for (j = 0; j < num_resources; j++) {
if (need[i][j] > work[j]) {
break;
}
}
if (j == num_resources) {
for (int k = 0; k < num_resources; k++) {
work[k] += allocation[i][k];
}
finish[i] = true;
found = true;
count++;
}
}
}
if (!found) {
break;
}
}
for (int i = 0; i < num_processes; i++) {
if (!finish[i]) {
return false;
}
}
return true;
}
int main() {
int num_processes = 5;
int num_resources = 3;
int available[MAX_RESOURCES] = { 3, 3, 2 };
Operating System Lab (KCS-451) Abhinav Airachia
2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

int max[MAX_PROCESSES][MAX_RESOURCES] = {
{ 7, 5, 3 },
{ 3, 2, 2 },
{ 9, 0, 2 },
{ 2, 2, 2 },
{ 4, 3, 3 }
};
int allocation[MAX_PROCESSES][MAX_RESOURCES] = {
{ 0, 1, 0 },
{ 2, 0, 0 },
{ 3, 0, 2 },
{ 2, 1, 1 },
{ 0, 0, 2 }
};
bool safe = isSafeState(NULL, available, max, allocation, num_processes, num_resources);
if (safe) {
printf("System is in a safe state.\n");
} else {
printf("System is in an unsafe state.\n");
}
return 0;
}
Output

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 10
AIM Conversion of resource allocation graph (RAG) to wait for graph (WFG) for each type of method
used for storing graph
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_RESOURCES 10
typedef struct Node {
int process;
struct Node* next;
} Node;
void addEdge(Node* adj[], int src, int dest) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->process = dest;
newNode->next = adj[src];
adj[src] = newNode;
}
void printGraph(Node* adj[], int num_processes) {
for (int i = 0; i < num_processes; i++) {
Node* temp = adj[i];
printf("Process %d -> ", i);
while (temp != NULL) {
printf("%d ", temp->process);
temp = temp->next;
}
printf("\n");
}
}
void convertRAGtoWFG(int max[][MAX_RESOURCES], int allocation[][MAX_RESOURCES], int num_processes, int num_resources) {
Node* adj[num_processes];
for (int i = 0; i < num_processes; i++) {
adj[i] = NULL;
}
for (int i = 0; i < num_processes; i++) {
for (int j = 0; j < num_resources; j++) {
if (max[i][j] - allocation[i][j] > 0) {
for (int k = 0; k < num_processes; k++) {
if (allocation[k][j] > 0) {
addEdge(adj, i, k);
}
}
}
}
}
printGraph(adj, num_processes);
}
int main() {
int num_processes = 5;
int num_resources = 3;
int max[MAX_RESOURCES][MAX_RESOURCES] = {
{ 7, 5, 3 },
{ 3, 2, 2 },
{ 9, 0, 2 },

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

{ 2, 2, 2 },
{ 4, 3, 3 }
};

int allocation[MAX_RESOURCES][MAX_RESOURCES] = {
{ 0, 1, 0 },
{ 2, 0, 0 },
{ 3, 0, 2 },
{ 2, 1, 1 },
{ 0, 0, 2 }
};

convertRAGtoWFG(max, allocation, num_processes, num_resources);

return 0;
}

OUTPUT:

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 11
AIM C program to implement the solution for Bounded Buffer problem using inter
process communication techniques-Semaphores
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{ int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{ printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s)
{return (--s);
}
int signal(int s)
{return(++s);
}
void producer()
{mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}
void consumer()
{mutex=wait(mutex);
full=wait(full);
empty=signal(empty);

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

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


x--;
mutex=signal(mutex);
}

OUTPUT:

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

EXPERIMENT 12
AIM C program to implement the solution for Readers-Writers problem using inter
process communication techniques-Semaphores
SOURCE CODE:
#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);

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005
JSS Academy of Technical Education-NOIDA

Department of Computer Science and Engineering

pthread_join(readerthreads[i],NULL);
}

OUTPUT:

Operating System Lab (KCS-451) Abhinav Airachia


2100910310005

You might also like