0% found this document useful (0 votes)
51 views60 pages

Basic UNIX Commands Overview

The document outlines basic UNIX commands categorized into general purpose, directory, and file commands, providing syntax and examples for each. It also includes shell programming exercises such as finding the greatest of two numbers, generating Fibonacci series, calculating factorials, and sorting numbers. Additionally, it briefly mentions CPU scheduling algorithms like Round Robin, SJF, FCFS, and Priority.

Uploaded by

Nivedha.T
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)
51 views60 pages

Basic UNIX Commands Overview

The document outlines basic UNIX commands categorized into general purpose, directory, and file commands, providing syntax and examples for each. It also includes shell programming exercises such as finding the greatest of two numbers, generating Fibonacci series, calculating factorials, and sorting numbers. Additionally, it briefly mentions CPU scheduling algorithms like Round Robin, SJF, FCFS, and Priority.

Uploaded by

Nivedha.T
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

\EX. NO.

:1 ​ ​ ​ ​ BASICS OF UNIX COMMANDS

AIM:
​ To study the purpose and usage of some basic commands

UNIX Commands falls under three main categories

1. GENERAL PURPOSE COMMANDS

i) To display the present working directory


​ Syntax: $ pwd
ii) To clear the screen
​ Syntax: $ clear
iii) To calculate the values
​ Syntax: $ bc
iv) Uname: To know your machine name
-n: Tells machine name in network
​ Syntax: $ uname –n
v) To display the version number of the OS
​ Syntax: $ uname –r
vi) To display the current date
​ Syntax: $ date
vii) To display the list of users logged in
​ Syntax: $ who
viii) To display the current status details of our login
​ Syntax: $ who am i
ix) To compile and run shell program
​ Syntax: $ sh filename
​ Example: $ sh [Link]
x) To compile a C program
​ Syntax: $ cc –o filename.c
xi) To run a C program
​ Syntax: $ ./[Link]
xii) To display the calendar.
​ Syntax: $ cal
xiii) To display the previous, current and next month.
​ Syntax: $ cal -3
xiv) To display the current month starting from Sunday.
​ Syntax: $ cal –s
xv) To display month only.
​ Syntax: $ date+%m
​ Output: 11
xvi) To display month name and month
​ Syntax: $date +%h%m
​ Output: Nov 11
xvii) To display month name​
​ Syntax: $ date+%h
​ Output: Nov
xviii) To display the time in hours
​ Syntax: $ date+%H
​ Output: 16
xix) To display the time in minutes
​ Syntax: $ date+%M
​ Output: 50
xx) To display the time in AM or PM
​ Syntax: $ date+%r
​ Output: 16: 50:40​ PM
xxi) To display date of month
​ Syntax: $ date+%d
​ Output: 25
xxii) It is used to view more details of all the commands
​ Syntax: $ man command_name
​ Example: $ man date
​ Output: It displays more details of date command
xxiii) To display my login id
​ Syntax: $ log name
​ Output: cs1010

2. DIRECTORY COMMANDS
i) To create a directory
​ Syntax: $ mkdir dirname
​ Example: $ mkdir peacock
ii) To change the name of the directory
​ Syntax: $ cd dirname
​ Example: $ cd flower
iii) To remove the directory
​ Syntax: $ rmdir dirname
​ Example: $ rmdir flower

3. FILE COMMANDS
i) To create a file.
​ Syntax: $ cat>filename
​ Example: $ cat>ex1
ii) To view the content of the file.
​ Syntax: $ cat filename
​ Example: $ cat ex1
iii) To append some details with the existing details in the file
​ Syntax: $ cat>>filename
​ Example: $ cat>>ex1
iv) To concatenate multiple files
​ Syntax: $ cat file1 file2 > file3
​ Example: $ cat computer compiler>world
​ Output: The contents of computer and compiler are merged into a single file
​ ​ called world.
v) To know the list of all files in directory
​ Syntax: $ ls
vi) To copy the file to another file
​ Syntax: $ cp source destination
​ Example: $ cp ex1 ex2
vii) To rename the file
​ Syntax: $ mv oldfile newfile
​ Example: $ mv ex1 ex3
viii) To delete a file
​ Syntax: $ rm filename
​ Example: $ rm ex1
ix) To delete all files​
​ Syntax: $ rm *
x) To display the filename starting with single letter
​ Syntax: $ echo?
xi) To display the filename starting with two letters
​ Syntax: $ echo??
xii) To display the filename starting with the letter f
​ Syntax: $ echo f*
xiii) To display the filename ending with letter f.
​ Syntax: $ echo *f
xiv) To display first 10 lines
​ Syntax: $ head [count][filename]
​ Example: $ head 10 ex1
xv) To display first 6 characters
​ Syntax: $ head -6c filename
​ Example: $ head -6c ex1
xvi) To display 5 lines from 2 files
​ Syntax: $ head -5 file1 file2
​ Example: $ head -5 ex1 ex2
xvii) To display last 10 lines
​ Syntax: $ tail [count][filename]
​ Example: $ tail 10 ex3
xviii) To display the number of words in a file
​ Syntax: $ wc filename
​ Example: $ wc ex1
xix) To display the number of characters in a file
​ Syntax: $ wc –c filename
​ Example: $ wc –c ex1
xx) To display the number of lines
​ Syntax: $ wc –l filename
​ Example: $ wc –l ex3
xxi) To display number of lines with numbers
​ Syntax: $ nl filename
​ Example: $ nl ex1
xxii) To provide the line number starting from s
​ Syntax: $ nl –v3 filename
​ Example: $ nl –v3 ex3
xxiii) To increment the line number by 5
​ Syntax: $ nl –i5 filename
​ Example: $ nl –i5 ex3
xxiv) To reverse and sort the content of file
​ Syntax: $ sort –r filename
​ Example: $ sort –r ex1
xxv) To sort the content of the file
​ Syntax: $ sort filename
​ Example: $ sort ex1
xxvi) To sort and remove the duplicate
​ Syntax: $ sort –u filename
​ Example: $ sort –u ex1
xxvii) To display file contents page by page
​ Syntax: $ more filename

APPLICATION I
​ Create a directory called stud, change to the stud directory. Verify whether you have
changed to stud directory. Return to your original directory

APPLICATION II
​ Create a file called top display the first three and last three lines of a file top in the
directory

2. SHELL PROGRAMMING

Ex. No.: 2a​ ​ ​ GREATEST OF TWO NUMBERS


PROGRAM:

echo "Enter the two numbers"


read a b
if [ $a -gt $b ]
then
echo "$a is greater"
else
echo "$b is greater"
fi

OUTPUT:
[netlab@system-2 oslab]$ sh [Link]
Enter the two numbers
10 40
40 is greater
Ex. No.: 2b​ ​ ​ FIBONACCI SERIES
PROGRAM:

echo "Enter the value of n"


read n
f1=-1
f2=1
i=1
while [ $i -le $n ]
do
f3=`expr $f1 + $f2`
echo $f3
f1=$f2
f2=$f3
i=`expr $i + 1`
done

OUTPUT:
[netlab@system-2 oslab]$ sh [Link]
Enter the value of n
5
0
1
1
2
3

Ex. No.: 2c​ ​ ​ FACTORIAL OF A GIVEN NUMBER

PROGRAM:
echo "Enter the number"
read num
fact=1
i=1
while [ $i -le $num ]
do
fact=`expr $fact \* $i`
i=`expr $i + 1`
done
echo "The factorial of $num=$fact"

OUTPUT:
[netlab@system-2 oslab]$ sh [Link]
Enter the number
5
The factorial of 5=120
Ex. No.: 2d​ ​ ​ SUM OF n NUMBERS

PROGRAM:
echo "Enter the number"
read num
i=1
sum=0
while [ $i -le $num ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "The sum is $sum"

OUTPUT:
[netlab@system-2 oslab]$ sh [Link]
Enter the number
10
The sum is 55

Ex. No.: 2e​ ​ ​ SORTING OF n NUMBERS

PROGRAM:
echo "Sorting of n numbers"
echo "Enter the numbers"
cat>p
echo "ascending order"
sort -n p
echo "Descending Order"
sort -nr p

OUTPUT:
[netlab@system-2 oslab]$ sh [Link]
Sorting of n numbers
Enter the numbers
12​ 23​ 25​ 24​ 26​ 15​ 14​ 16​ 13 (Ctrl+D)
ascending order
12​ 13​ 14​ 15​ 16​ 23​ 24​ 25​ 26
Descending Order
26​ 25​ 24​ 23​ 16​ 15​ 14​ 13​ 12

Ex. No.: 2f ​ ​ CHECKING FOR A PRIME NUMBER

PROGRAM:
echo “Enter the number”
read num
k=2
i=0
c=num/2
while[$k –lt $c]
do
r=$[n%k]
if[$r –eq $i]
then
echo “Composite”
exit
else
k=$[k+1]
fi
done
echo “Prime no”

OUTPUT:
[netlab@system-2 oslab]$ sh [Link]
Enter the number
11
Prime no
25
Composite

Ex. NO.: 2g​ ​ MENU DRIVEN DIRECTORY ACTIONS

PROGRAM:
echo "Menu"
echo "[Link] a long list of file"
echo "[Link] files from the directory"
echo "[Link] a file"
read choice
case $choice in
1) ls -l;;
2) echo "Enter the file to be deleted"
read file
rm $file
echo "The file has been deleted";;
3) echo "Enter the name of the file to be inserted"
read new
echo "Enter the text"
cat>>new
echo "The file has been appended";;
esac
OUTPUT:
[netlab@system-2 oslab]$ cat>new
Good Morning
[netlab@system-2 oslab]$ sh [Link]
Menu
[Link] a long list of file
[Link] files from the directory
[Link] a file
1
total 100
-rw-rw-r--. 1 netlab netlab 236 2013-01-05 19:54 [Link]
-rw-rw-r--. 1 netlab netlab 149 2013-01-05 19:52 [Link]~
-rw-rw-r--. 1 netlab netlab 215 2013-01-05 19:49 [Link]
-rw-rw-r--. 1 netlab netlab 137 2013-01-05 19:48 [Link]~
-rw-rw-r--. 1 netlab netlab 193 2013-01-05 19:40 [Link]
-rw-rw-r--. 1 netlab netlab 110 2013-01-05 19:39 [Link]~
-rw-rw-r--. 1 netlab netlab 378 2013-01-05 20:06 [Link]
-rw-rw-r--. 1 netlab netlab 13 2013-01-05 20:07 new
-rw-rw-r--. 1 netlab netlab 32801 2013-01-05 19:20 OPERATING SYSTEM LAB
[Link]
-rw-rw-r--. 1 netlab netlab 13979 2013-01-05 19:55 OPERATING SYSTEM LAB [Link]
-rw-rw-r--. 1 netlab netlab 0 2013-01-05 20:06 p
-rw-rw-r--. 1 netlab netlab 326 2013-01-05 20:01 [Link]
-rw-rw-r--. 1 netlab netlab 127 2013-01-05 20:00 [Link]~
-rw-rw-r--. 1 netlab netlab 212 2013-01-05 19:58 [Link]
-rw-rw-r--. 1 netlab netlab 133 2013-01-05 19:56 [Link]~

[netlab@system-2 oslab]$ sh [Link]


Menu
[Link] a long list of file
[Link] files from the directory
[Link] a file
3
Enter the name of the file to be inserted
new
Enter the text
Have a Nice Day
The file has been appended

[netlab@system-2 oslab]$ cat new


Good Morning
Have a Nice Day

[netlab@system-2 oslab]$ sh [Link]


Menu
[Link] a long list of file
[Link] files from the directory
[Link] a file
2
Enter the file to be deleted
hello
rm: cannot remove `hello': No such file or directory
The file has been deleted

3. IMPLEMENT THE FOLLOWING CPU SCHEDULING ALGORITHMS


A) ROUND ROBIN B) SJF C) FCFS D) PRIORITY
Ex. No.: 3a FIRST COME FIRST SERVE
#include<math.h>
int main()
{
int i,j,n,t;
int ari[10],run[10],st[10];
int ft[10],wt[10],ta[10],name[10];
float totta,totwt,avgta,avgwt;
totwt=totta=0.0;
printf("\nINPUT");
printf("\n****");
printf("enter the number of jobs");
scanf("%d",&n);
printf("enter the arrival time for each job\n");
for(i=0;i<n;i++)
scanf("%d",&ari[i]);
printf("enter the runtime for each job\n");
for(i=0;i<n;i++)
scanf("%d",&run[i]);
for(i=0;i<n;i++)
name[i]=i+1;
for(i=0;i<n-1;i++)
{
for(j=j+1;j<n;j++)
{
if((ari[i]>ari[j])||(ari[i]==ari[j])&&(run[i]>run[j]))
{
t=ari[i];
ari[i]=ari[j];
ari[j]=t;
t=name[i];
name[i]=name[j];
name[j]=t;
t=name[i];
run[i]=run[j];
run[j]=t;
}
}
}
for(i=0;i<n;i++)
{
if(i==0)
st[i]=ari[i];
else
{
st[i]=st[i-1]+run[i-1];
if(st[i]<ari[i])
st[i]=ari[i];
}
wt[i]=abs(st[i]-ari[i]);
ft[i]=st[i]+run[i];
ta[i]=ft[i]+wt[i]-st[i];
totwt=totwt+wt[i];
totta=totta+ta[i];
}
avgwt=totwt;" \n";
avgta=totta;" \n";
printf("\noutput:");
printf("NAME ARRIVAL RUN START WAIT");
printf("final turn around");
for(i=0;i<n;i++)
{
printf("\n\n %d\t%d\t%d",name[i],ari[i],run[i]);
printf("\t%d\t%d\t%d\t%d",st[i],wt[i],ft[i],ta[i]);
}
printf("\n\n average waiting time\t average turn around");
printf("\n\n\t%f",avgwt);
printf("\n\t%f",avgta);
return(0);
}

OUTPUT:
[cse2a@sys-d02~] $ vi fcfs.c
[cse2a@sys-d02~] $ cc fcfs.c
[cse2a@sys-d02~] $ ./[Link]

enter the no. of jobs 3


enter arrival time
1
2
3
enter run time
4
5
6
output
job arrival runtime start waiting final turnaround
1 1 4 1 1 5 5
2 2 5 5 3 10 8
3 3 6 10 7 16 14

average waiting time average turnaroundtime


4.000000 9.000000

Ex. No.: 3b SHORTEST JOB FIRST SCHEDULING

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name, arrival time & execution time:");
flushall();
scanf("%s%d%d",pn[i],&at[i],&et[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(et[i]<et[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
st[i]=at[i];
else
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}
OUTPUT:

Input:

Enter the number of processes: 3


Enter the Process Name, Arrival Time & Burst Time: 1 4 6
Enter the Process Name, Arrival Time & Burst Time: 2 5 15
Enter the Process Name, Arrival Time & Burst Time: 3 6 11

Output:

Pname arrivaltime executiontime waitingtime tatime


1​ ​ 4​ ​ 6​ ​ 0​ 6
3​ ​ 6​ ​ 11​ ​ 4​ 15
2​ ​ 5​ ​ 15​ ​ 16​ 31

Average Waiting Time: 6.6667


Average Turn Around Time: 17.3333

Ex. No.: 3c PRIORITY SCHEDULING

#include<stdio.h>
#include<math.h>
main()
{
int i,j,n,t;
int ari[10],run[10],ft[10],wt[10],ta[10],st[10],pri[10],name[10];
int avgwt,avgta,totwt,totta;
totwt=totta=0;
printf("\n enter the [Link] jobs");
scanf("%d",&n);
printf("\n enter the priority");
for(i=0;i<n;i++)
scanf("%d",&pri[i]);
printf("\n enter the arrival time");
for(i=0;i<n;i++)
{
scanf("%d",&ari[i]);
}
printf("\n enter the run time");
for(i=0;i<n;i++)
{
scanf("%d",&run[i]);
}
for(i=0;i<n;i++)
name[i]=i+1;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(pri[i]>pri[j])
{
t=pri[i];
pri[i]=pri[j];
pri[j]=t;
t=name[i];
name[i]=name[j];
name[j]=t;
t=run[i];
run[i]=run[j];
run[j]=t;
t=ari[i];
ari[i]=ari[j];
ari[j]=t;
}
else
if(pri[i]==pri[j])
{
if(run[i]>run[j])
{
t=pri[i];
pri[i]=pri[j];
pri[j]=t;
t=name[i];
name[i]=name[j];
name[j]=t;
t=ari[i];
ari[i]=ari[j];
ari[j]=t;
t=run[i];
run[i]=run[j];
run[j]=t;
}
if(run[i]==run[j])
{
if(ari[i]>ari[j])
{
t=pri[i];
pri[i]=pri[j];
pri[j]=t;
t=name[i];
name[i]=name[j];
name[j]=t;
t=ari[i];
ari[i]=ari[j];
ari[j]=t;
t=run[i];
run[i]=run[j];
run[j]=t;
}
}
}
}
}
for(i=0;i<n;i++)
{if(i==0)
{
wt[i]=0;
st[i]=ari[i];
ft[i]=st[i]+run[i];
ta[i]=ft[i]+wt[i]-st[i];
}
else
{
st[i]=ft[i-1];
wt[i]=abs(st[i]-ari[i]);
ft[i]=st[i]+run[i];
ta[i]=ft[i]+wt[i]-st[i];
totwt=totwt+wt[i];
totta=totta+ta[i];
}
avgwt=totwt; "\n";
avgta=totta; "\n";
}
printf("\n \n job priority arrival run start waiting final turnaround");
for(i=0;i<n;i++)
{
printf("\n \n
%d\t%d\t%d\t%d\t%d\t%d\t%d\t",name[i],pri[i],ari[i],run[i],st[i],wt[i],ft[i],ta[i]);
}
printf("\n average waiting time:");
printf("%d",avgwt);
printf("\n average turnaround time");
printf("%d",avgta);
}

OUTPUT:

[cse2a@sys-d02~] $ vi priority.c
[cse2a@sys-d02~] $ cc priority.c
[cse2a@sys-d02~] $ ./[Link]

Enter the number of jobs 4


Enter the priority 2 1 5 7
Enter the arrival time 2 6 3 9
Enter the run time 1 5 9 3
Job priority arrival run time start waiting final turnaround
3 1 3 9 3 0 12
1 2 2 1 12 10 13
2 5 6 5 13 7 18
4 7 9 3 3 9 21
Avg turnaround TIME : 35
Avg waiting time :26

Ex. No.: 3d ROUND ROBIN SCHEDULING


#include<stdio.h>
main()
{
intI,j,n,t,q,a,ct=0,c[30],m[50],r[10];
intari[10],run[10],ft[10],wt[10],ta[10],st[10],nm[10];
floatawt,ata;
floattta=0.0,twt=0.0;
printf(“\n Enter [Link] jobs:”);
scanf(“%d”,&n);
printf(“\n Enter arrival time:”);
for(i=0;i<n;i++)
scanf(“%d”,&ari[i]);
printf(“\n Enter run time:”);
for(i=0;i<n;i++)
scanf(“%d”,&run[i]);
printf(“\n Enter quantum number:”);
scanf(“%d”,&q);
for(i=0;i<n;i++)
nm[i]=i+1;
for(i=0;i<n;i++)
m[i]=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
t=ari[i];
ari[i]=ari[j];
ari[j]=t;
t=run[i];
run[i]=run[j];
run[j]=t;
t=nm[i];
nm[i]=nm[j];
nm[j]=t;
}
}
}
for(i=0;i<n;i++)
r[i]=run[i];
a=ari[0];
printf(“\n The gantt chart is:”);
printf(“**************”);
for(j=0;j<30;j++)
{
for(i=0;i<n;i++)
{
if(ari[i]<=a+ct)
{
if(run[i]!=0)
{
if(run[i]==r[i])
{
st[i]=a+ct;
}
m[i]=m[i]+1;
if(run[i]<=q)
{
wt[i]=a+ct-(q*m[i])-ari[i]+q;
ct=ct+run[i];
ft[i]=ct+ari[0];
run[i]=0;
}
else
{
run[i]=run[i]-q;
ct=ct+q;
}
printf(“p%d”,nm[i]);
}
}
}
}
for(i=0;i<n;i++)
ta[i]=ft[i]-ari[i];
twt=twt+wt[i];
tta=ta[i];
}
awt=twt/n;
ata=tta/n;
printf(“ Job arrival runtime start waiting final turnaround”);
for(i=0;i<n;i++)
{
printf(“\n\nP %d\t%d\t%d\t%d”,nm[i],r[i],ari[i],st[i]);
printf(“\t%d\t%d\t%d”,wt[i],ft[i],ta[i]);
}
printf(“avg turnaround time %4.4f\n”,ata);
printf(“avg waiting time %4.4f\n”,awt);
}

OUTPUT:

[cse2a@sys-do2~] $ viround.c
[cse2a@sys-do2~] $ cc round.c
[cse2a@sys-do2~] $ ./[Link]
Enter no. of jobs:4
Enter the arrival time:
1
2
3
5
Enter the run time:
4
5
3
2
Enter quantum number:2
Job arrival run time start waiting final turnaround
4 1 4 1 6 11 10
1 2 5 3 8 15 13
2 3 3 5 8 14 11
3 5 2 7 6 9 4

Avg turnaround time:9.5


Avg waiting time:6.00

4. Implement all file allocation strategies


a) Sequential b) Indexed c) Linked

Ex. No.: 4a CONTIGUOUS ALLOCATION

PROGRAM:
#include<string.h>
struct block
{
​ int b_id;
​ int b_allocated;
};
struct block b[50];
int main()
{
​ int no,i,j,n,sblock,eblock,count=0;
​ int psize,flag=1,pname,bname;
​ printf("\nEnter the no. of block: ");
​ scanf("%d",&n);
​ for(i=0;i<n;i++)
​ {
​ ​ b[i].b_id=i;
​ ​ b[i].b_allocated=0;
​ }
​ printf("\nEnter the no. of block already exists: ");
​ scanf("%d",&no);
​ for(i=0;i<no;i++)
​ {
​ ​ printf("\nEnter the block: ");
​ ​ scanf("%d",&bname);
​ ​ b[bname].b_allocated=100;
​ }
​ printf("\nEnter the process name: ");
​ scanf("%d",&pname);
​ printf("\nEnter the process size: ");
​ scanf("%d",&psize);
​ for(i=0;i<n;i++)
​ {
​ ​ if(b[i].b_allocated==0)
​ ​ {
​ ​ ​ count=count+1;
​ ​ ​ if(count==1)
​ ​ ​ ​ sblock=i;
​ ​ ​ if(count==psize)
​ ​ ​ {
​ ​ ​ ​ eblock=i;
​ ​ ​ ​ for(j=sblock;j<=eblock;j++)
​ ​ ​ ​ ​ b[j].b_allocated=pname;
​ ​ ​ ​ printf("\n\nProcess %d is allocated %d blocks from %d to ​ ​
​ ​ ​ %d\n\n",pname,sblock,eblock,sblock);
​ ​ ​ ​ i=n+1;
​ ​ ​ ​ flag=1;
​ ​ ​ }
​ ​ }
​ ​ else
​ ​ {
​ ​ ​ count=0;
​ ​ ​ flag=0;
​ ​ }
​ }
​ if(flag==0)
​ printf("\nProcess not allocated");
​ count=0;
​ for(i=0;i<n;i++)
​ {
​ ​ count=count+1;
​ ​ if(count<no)
​ ​ ​ printf("b[%d]->%d\t\t",b[i].b_id,b[i].b_allocated);
​ ​ else
​ ​ {
​ ​ ​ count=0;
​ ​ ​ printf("\n\n\nb[%d]-->%d\t\t",b[i].b_id,b[i].b_allocated);
​ ​ ​ count=count+1;
​ ​ }
​ }
​ return 0;
}

OUTPUT:
[netlab@system-2 oslab]$ cc contiguous.c
[netlab@system-2 oslab]$ ./[Link]

Enter the no. of block: 4


Enter the no. of block already exists: 4
Enter the block: 2
Enter the block: 3
Enter the block: 5
Enter the block: 6
Enter the process name: 2
Enter the process size: 2

Process 2 is allocated 0 blocks from 1 to 0


b[0]->2 b[1]->0 b[2]->100 b[3]-->100​

Ex. No.: 4b LINKED ALLOCATION

PROGRAM:
#include<stdio.h>
void display();
struct block
{​
​ int p_id;
​ int b_allocated;
​ struct block *next;
}*sblock=NULL;
int n;
struct block b[50];
int main()
{
​ int i,pname,psize,no,bname,freespace=0,count,previous;
​ printf("\nEnter the no. of blocks: ");
​ scanf("%d",&n);
​ freespace=n;
​ for(i=0;i<n;i++)
​ {
​ ​ b[i].p_id=0;
​ ​ b[i].b_allocated=0;
​ }
​ printf("\nEnter the no. of blocks already allocated process name: ");
​ scanf("%d",&no);
​ for(i=0;i<no;i++)
​ {
​ ​ printf("\nEnter the block: ");
​ ​ scanf("%d",&bname);
​ ​ b[bname].p_id=100;
​ ​ b[bname].b_allocated=1;
​ ​ freespace=freespace-1;
​ }
​ display();
​ printf("\nEnter process name: ");
​ scanf("%d",&pname);
​ printf("\nEnter process size: ");
​ scanf("%d",&psize);
​ count=0;
​ if(psize<=freespace)
​ {
​ ​ for(i=0;i<n;i++)
​ ​ {
​ ​ ​ if(b[i].b_allocated==0)
​ ​ ​ {
​ ​ ​ ​ b[i].b_allocated=1;
​ ​ ​ ​ b[i].p_id=pname;
​ ​ ​ ​ count++;
​ ​ ​ ​ if(count==1)
​ ​ ​ ​ ​ sblock=&b[i];
​ ​ ​ ​ else
​ ​ ​ ​ ​ if(count>psize)
​ ​ ​ ​ ​ ​ break;
​ ​ ​ ​ ​ else
​ ​ ​ ​ ​ ​ b[previous].next=&b[i];
​ ​ ​ ​ previous=i;​
​ ​ ​ }
​ ​ }
​ ​ display();
​ ​ printf("\nProcess name %d\nStarting address %u\nEnding address ​​ ​
%u",pname,sblock,&b[i]);
​ }
​ else
​ ​ printf("\nProcess cannot be allocated");
}
void display()
{
​ int i,count=0;
​ printf("\nBlocks\n");
​ for(i=0;i<n;i++)
​ {
​ ​ if(count<2)
​ ​ ​ printf("%u-->\tb[%2d]->\t%2d->\t%2d->\t​ ​ ​ ​ ​
​ ​ %4u\t\t",&b[i],i,b[i].p_id,b[i].b_allocated,b[i].next);
​ ​ else
​ ​ {
​ ​ ​ count=0;
​ ​ ​ printf("\n%u-->\tb[%2d]->\t%2d->\t%2d->\t​​ ​ ​ ​
​ ​ %4u\t\t",&b[i],i,b[i].p_id,b[i].b_allocated,b[i].next);
​ ​ }
​ ​ count=count+1;
​ }
}

OUTPUT:
[netlab@system-2 oslab]$ cc linked.c
[netlab@system-2 oslab]$ ./[Link]

Enter the no. of blocks: 8


Enter the no. of blocks already allocated process name: 3
Enter the block: 1
Enter the block: 3
Enter the block: 5
Blocks
134519744-->b[ 0]->0->0->0 134519756-->b[ 1]->100->1->0
134519768-->b[ 2]->0->0->0 134519780-->b[ 3]->100->1->0
134519792-->b[ 4]->0->0->0 134519804-->b[ 5]->100->1->0
134519816-->b[ 6]->0->0->0 134519828-->b[ 7]->0->0->0
Enter process name: 2
Enter process size: 2
Blocks
134519744-->b[ 0]->2->1->134519768 134519756-->b[ 1]->100->1->0
134519768-->b[ 2]->2->1->0 134519780-->b[ 3]->100->1->0
134519792-->b[ 4]->2->1->0 134519804-->b[ 5]->100->1->0
134519816-->b[ 6]->0->0->0 134519828-->b[ 7]->0->0->0
Process name 2
Starting address 134519744
Ending address 134519792

Ex. No.: 4c INDEXED ALLOCATION

PROGRAM:
#include<stdio.h>
int b_allocated[30];
void display(int);
void main()
{
​ int j=0,index[50],indexloc,i,n,no,psize,pname,block,freespace=0,count=0;
​ printf("\nEnter the number of blocks: ");
​ scanf("%d",&n);
​ freespace=n;
​ printf("\nEnter the number of blocks that are already allocated: ");
​ scanf("%d",&no);
​ for(i=0;i<no;i++)
​ ​ b_allocated[i]=0;
​ printf("\nEnter the blocks allocated: ");
​ for(i=0;i<no;i++)
​ {
​ ​ scanf("%d",&block);
​ ​ b_allocated[block]=1;
​ ​ freespace=freespace-1;
​ }
​ printf("\nFreespace=%d\n",freespace);
​ display(n);
​ printf("\nEnter the name of the process: ");
​ scanf("%d",&pname);
​ printf("\nEnter the size of the process: ");
​ scanf("%d",&psize);
​ if(psize<freespace)
​ {
​ ​ for(i=0;i<n;i++)
​ ​ {
​ ​ ​ if(b_allocated[i]==0&&count<=psize)
​ ​ ​ {
​ ​ ​ ​ if(count==psize)
​ ​ ​ ​ {
​ ​ ​ ​ ​ b_allocated[i]=pname;
​ ​ ​ ​ ​ indexloc=i; ​
​ ​ ​ ​ ​ break;
​ ​ ​ ​ }
​ ​ ​ ​ else
​ ​ ​ ​ {
​ ​ ​ ​ ​ b_allocated[i]=1;
​ ​ ​ ​ ​ index[j]=0;
​ ​ ​ ​ ​ index[j]=i;
​ ​ ​ ​ ​ j=j+1;
​ ​ ​ ​ ​ count=count+1;
​ ​ ​ ​ }
​ ​ ​ }
​ ​ }
​ ​ display(n);
​ ​ printf("\nBlocks stored in index are: \n\n");
​ ​ for(i=0;i<j;i++)
​ ​ ​ printf("%d",index[i]);
​ ​ printf("\nDirectory Structure\n\n");
​ ​ printf("\nProcess\t\tIndex\n");
​ ​ printf("\n%d\t\t%d",pname,indexloc);
​ }
}
void display(int n)
{
​ int i,count=0;
​ printf("\n\n");
​ for(i=0;i<n;i++)
​ ​ if(count<3)
​ ​ {
​ ​ ​ printf("\t\tb[%2d]-->%d",i,b_allocated[i]);
​ ​ ​ count=count+1;
​ ​ }
​ ​ else
​ ​ {
​ ​ ​ printf("\n\n\t\tb[%2d]--%d",i,b_allocated[i]);
​ ​ ​ count=1;
​ ​ }
}

OUTPUT:
[netlab@system-2 oslab]$ cc indexed.c
[netlab@system-2 oslab]$ ./[Link]

Enter the number of blocks: 8


Enter the number of blocks that are already allocated: 4
Enter the blocks allocated: 1 3 2 5
Freespace=4
b[ 0]-->0 b[ 1]-->1 b[ 2]-->1

b[ 3]--1 b[ 4]-->0 b[ 5]-->1

b[ 6]--0 b[ 7]-->0
Enter the name of the process: 3
Enter the size of the process: 2
b[ 0]-->1 b[ 1]-->1 b[ 2]-->1

b[ 3]--1 b[ 4]-->1 b[ 5]-->1

b[ 6]--3 b[ 7]-->0
Blocks stored in index are:

04
Directory Structure
Process Index

3 6
5. IMPLEMENT SEMAPHORES

Ex. No.: 5​ PRODUCER CONSUMER PROBLEM - SEMAPHORES

PROGRAM:
#include<stdio.h>
#include<string.h>
# define SIZE 10
struct process
{
​ char a[10];
}buffer[10];
int mutex=1, full=0, empty=SIZE, flag=0;
int wait(int);
int signal(int);
main()
{
​ int ch,i;
​ printf("\nPRODUCER - CONSUMER PROBLEM\n");
​ while(1)
​ {
​ ​ printf("\nChoices are\n");
​ ​ printf("1. Producer Routine\n");
​ ​ printf("2. Consumer Routine\n");
​ ​ printf("3. Display the contents of the buffer\n");
​ ​ printf("4. Exit\n");
​ ​ printf("Enter your choice: ");
​ ​ scanf("%d",&ch);
​ ​ switch(ch)
​ ​ {
​ ​ ​ case 1:
​ ​ ​ ​ empty=wait(empty);
​ ​ ​ ​ mutex=wait(mutex);
​ ​ ​ ​ if(flag==0)
​ ​ ​ ​ {
​ ​ ​ ​ ​ printf("\nEnter the item to be added: ");
​ ​ ​ ​ ​ scanf("%s",&buffer[full]);
​ ​ ​ ​ ​ printf("Item inserted successfully\n\n");
​ ​ ​ ​ ​ full=signal(full);
​ ​ ​ ​ }
​ ​ ​ ​ else
​ ​ ​ ​ {
​ ​ ​ ​ ​ printf("Buffer Full\n");
​ ​ ​ ​ ​ flag=0;
​ ​ ​ ​ }
​ ​ ​ ​ mutex=signal(mutex);
​ ​ ​ ​ break;
​ ​ ​ case 2:
​ ​ ​ ​ full=wait(full);
​ ​ ​ ​ mutex=wait(mutex);
​ ​ ​ ​ if(flag==0)
​ ​ ​ ​ {
​ ​ ​ ​ ​ printf("\n\nItem %s is successfully consumed\n", ​ ​
​ ​ ​ ​ buffer[0].a);
​ ​ ​ ​ ​ for(i=0;i<SIZE;i++)
​ ​ ​ ​ ​ ​ strcpy(buffer[i].a,buffer[i+1].a);
​ ​ ​ ​ ​ empty=signal(empty);
​ ​ ​ ​ }
​ ​ ​ ​ else
​ ​ ​ ​ {
​ ​ ​ ​ ​ printf("No item is in buffer\n\n");
​ ​ ​ ​ ​ flag=0;
​ ​ ​ ​ }
​ ​ ​ ​ mutex=signal(mutex);
​ ​ ​ ​ break;
​ ​ ​ case 3:
​ ​ ​ ​ if(full!=0)
​ ​ ​ ​ {
​ ​ ​ ​ ​ for(i=0;i<full;i++)
​ ​ ​ ​ ​ ​ printf("%s\n", buffer[i].a);
​ ​ ​ ​ }
​ ​ ​ ​ else
​ ​ ​ ​ ​ printf("Buffer is empty\n\n");
​ ​ ​ ​ break;
​ ​ ​ case 4:
​ ​ ​ ​ exit(0);
​ ​ ​ default:
​ ​ ​ ​ printf("Please enter a valid option\n");
​ ​ }
​ }
}

int wait(int s)
{
​ if(s==0)
​ ​ flag=1;
​ else
​ ​ s--;
​ return s;
}

int signal(int s)
{
​ s++;
​ return s;
}

OUTPUT:
[netlab@system-2 oslab]$ cc semaphores.c
[netlab@system-2 oslab]$ ./[Link]

PRODUCER - CONSUMER PROBLEM

Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 1
Enter the item to be added: p1
Item inserted successfully
Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 1
Enter the item to be added: p2
Item inserted successfully
Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 1
Enter the item to be added: p3
Item inserted successfully
Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 2
Item p1 is successfully consumed
Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 3
p2
p3
Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 4

6. IMPLEMENT ALL FILE ORGANIZATION TECHNIQUES


A) SINGLE LEVEL DIRECTORY B) TWO LEVEL C) HIERARCHICAL
D) DAG
SINGLE LEVEL DIRECTORY

CODING:
#include<stdio.h>

void main()

​ int master,s[20];

​ char f[20][20][20];

​ char d[20][20];

​ int i,j;

​ printf("Enter the number of directories:");

​ scanf("%d",&master);

​ printf("Enter names of directories:");

​ for(i=0;i<master;i++)

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

​ printf("Enter size of directories:");

​ for(i=0;i<master;i++)

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

​ printf("Enter the filenames:");

​ for(i=0;i<master;i++)

​ {
​ ​ for(j=0;j<s[i];j++)

​ ​ ​ scanf("%s",&f[i][j]);

​ }

​ printf("\n");

​ printf("Directory \t size \t Filename\n");

​ printf("___________________________________________________\n");

​ for(i=0;i<master;i++)

​ {

​ ​ printf("%s \t\t %2d \t",d[i],s[i]);

​ ​ for(j=0;j<s[i];j++)

​ ​ ​ printf("%s\n\t\t ",f[i][j]);

​ ​ printf("\n");

​ }

​ printf("\t\n");

OUTPUT:
[cse2c@sys-d05 ~]$ cc single.c

[cse2c@sys-d05 ~]$ ./[Link]

Enter the number of directories:2

Enter names of directories: bin

root

Enter size of directories:2


2

Enter the filenames:tarzan

godzilla

eragon

VIP

Directory ​ size ​ Filename

_______________________________

bin ​ ​ 2 ​ tarzan

​ ​ godzilla

​ ​

root ​ ​ 2 ​ eragon

​ ​ VIP

​ ​

TWO LEVEL DIRECTORY

CODING:
#include<stdio.h>

struct st

char dname[10];

char sdname[10][10];

char fname[10][10][10];

int ds,sds[10];

}dir[10];
void main()

​ int i,j,k,n;

​ printf("enter number of directories:");

​ scanf("%d",&n);

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

​ {

​ ​ printf("enter directory %d name:",i+1);

​ ​ scanf("%s",&dir[i].dname);

​ ​ printf("enter size of directory %d:",i+1);

​ ​ scanf("%d",&dir[i].ds);

​ ​ for(j=0;j<dir[i].ds;j++)

​ ​ {

​ ​ ​ printf("enter subdirectory name and size:");

​ ​ ​ scanf("%s",&dir[i].sdname[j]);

​ ​ ​ scanf("%d",&dir[i].sds[j]);

​ ​ ​ for(k=0;k<dir[i].sds[j];k++)

​ ​ ​ {

​ ​ ​ ​ printf("enter file names:");

​ ​ ​ ​ scanf("%s",&dir[i].fname[j][k]);

​ ​ ​ }​ ​ }​ }

​ printf("\n");​

​ printf("\ndirname\t\tsize\tsubdirname\tsize\tfiles");

​ printf("\n******************************************************\n");
​ for(i=0;i<n;i++)

​ {

​ ​ printf("%s\t\t%d",dir[i].dname,dir[i].ds);

​ ​ for(j=0;j<dir[i].ds;j++)

​ ​ {

​ ​ ​ printf("\t%s\t\t%d\t",dir[i].sdname[j],dir[i].sds[j]);

​ ​ ​ for(k=0;k<dir[i].sds[j];k++)

​ ​ ​ ​ printf("%s\t",dir[i].fname[j][k]);

​ ​ ​ printf("\n\t\t");

​ ​ }

​ ​ printf("\n");

​ }

OUTPUT:
[cse2c@sys-d05 ~]$ cc two1.c

[cse2c@sys-d05 ~]$ ./[Link]

enter number of directories:2

enter directory 1 name:bin

enter size of directory 1:2

enter subdirectory name and size:branch 2

enter file names:roadrash

enter file names:tarzan

enter subdirectory name and size:branch1 1


enter file names:claw

enter directory 2 name:root

enter size of directory 2:1

enter subdirectory name and size:root1 1

enter file names:mortal

dirname​ size​ subdirname​ size​ files

******************************************************

bin​ ​ 2​ branch​ ​ 2​ roadrash​ tarzan​

​ ​ ​ branch1​ 1​ claw​

​ ​

root​ ​ 1​ root1​ ​ 1​ mortal​

7. IMPLEMENT BANKERS ALGORITHM FOR DEAD LOCK


AVOIDANCE
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,i,j,k,p,u=0,s=0,m;
int block[10],run[10],active[10],newreq[10];
int max[10][10],resalloc[10][10],resreq[10][10];
int totalloc[10],totext[10],simalloc[10];
clrscr();
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the no of resource classes:");
scanf("%d",&r);
printf("Enter the total existed resource in each class:");
for(k=1;k<=r;k++)
scanf("%d",&totext[k]);
printf("Enter the allocated resources:");
for(i=1;i<=n;i++)
for(k=1;k<=r;k++)
scanf("%d",&resalloc);
printf("Enter the process making the new request:");
scanf("%d",&p);
printf("Enter the requested resource:");
for(k=1;k<=r;k++)
scanf("%d",&newreq[k]);
printf("Enter the process which are n blocked or running:");
for(i=1;i<=n;i++)
{
if(i!=p)
{
printf("process %d:\n",i+1);
scanf("%d%d",&block[i],&run[i]);
}
}
block[p]=0;
run[p]=0;
for(k=1;k<=r;k++)
{
j=0;
for(i=1;i<=n;i++)
{
totalloc[k]=j+resalloc[i][k];
j=totalloc[k];
}
}
for(i=1;i<=n;i++)
{
if(block[i]==1||run[i]==1)
active[i]=1;
else
active[i]=0;
}
for(k=1;k<=r;k++)
{
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
}
for(k=1;k<=r;k++)
{
if(totext[k]-totalloc[k]<0)
{
u=1;break;
}
}
if(u==0)
{
for(k=1;k<=r;k++)
simalloc[k]=totalloc[k];
for(s=1;s<=n;s++)
for(i=1;i<=n;i++)
{
if(active[i]==1)
{
j=0;
for(k=1;k<=r;k++)
{
if((totext[k]-simalloc[k])<(max[i][k]-resalloc[i][k]))
{
j=1;break;
}
}
}
if(j==0)
{
active[i]=0;
for(k=1;k<=r;k++)
simalloc[k]=resalloc[i][k];
}
}
m=0;
for(k=1;k<=r;k++)
resreq[p][k]=newreq[k];
printf("Deadlock willn't occur");
}
else
{
for(k=1;k<=r;k++)
{
resalloc[p][k]=newreq[k];
totalloc[k]=newreq[k];
}
printf("Deadlock will occur");
}
getch();
}

OUTPUT:
/*include <stdio.h>
#include <stdlib.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10],
safeSequence[10];
int p, r, i, j, process, count;
count = 0;

printf("Enter the no of processes : ");


scanf("%d", &p);

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


completed[i] = 0;

printf("\n\nEnter the no of resources : ");


scanf("%d", &r);

printf("\n\nEnter the Max Matrix for each process : ");


for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}

printf("\n\nEnter the allocation for each process : ");


for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}

printf("\n\nEnter the Available Resources : ");


for(i = 0; i < r; i++)
scanf("%d", &avail[i]);

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

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


need[i][j] = Max[i][j] - alloc[i][j];

do
{
printf("\n Max matrix:\tAllocation matrix:\n");

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


{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}

process = -1;

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


{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}

if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}
while(count != p && process != -1);

if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}

Output:
[root@comps 111a1059]# gcc bankerssafesequence.c
[root@comps 111a1059]# ./[Link]
Enter the no of processes : 5

Enter the no of resources : 3

Enter the Max Matrix for each process :


For process 1 : 7
5
3

For process 2 : 3
2
2

For process 3 : 7
0
2

For process 4 : 2
2
2

For process 5 : 4
3
3

Enter the allocation for each process :


For process 1 : 0
1
0

For process 2 : 2
0
0

For process 3 : 3
0
2

For process 4 : 2
1
1

For process 5 : 0
0
2

Enter the Available Resources : 3


3
2

Max matrix: Allocation matrix:


7 5 3 0 1 0
3 2 2 2 0 0
7 0 2 3 0 2
2 2 2 2 1 1
4 3 3 0 0 2

Process 2 runs to completion!


Max matrix: Allocation matrix:
7 5 3 0 1 0
0 0 0 0 0 0
7 0 2 3 0 2
2 2 2 2 1 1
4 3 3 0 0 2

Process 3 runs to completion!


Max matrix: Allocation matrix:
7 5 3 0 1 0
0 0 0 0 0 0
0 0 0 0 0 0
2 2 2 2 1 1
4 3 3 0 0 2

Process 4 runs to completion!


Max matrix: Allocation matrix:
7 5 3 0 1 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
4 3 3 0 0 2

Process 1 runs to completion!


Max matrix: Allocation matrix:
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
4 3 3 0 0 2

Process 5 runs to completion!


The system is in a safe state!!
Safe Sequence : < 2 3 4 1 5 >
*/

8. IMPLEMENT AN ALGORITHM FOR DEAD LOCK DETECTION


Ex. No.:8 ALGORITHM FOR DEADLOCK DETECTION
#include<stdio.h>
#include<conio.h>
void main()
{
int found,flag,l,p[4][5],tp,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;
clrscr();
printf("enter total no of processes");
scanf("%d",&amp;tp);
printf("enter clain matrix");
for(i=1;i&lt;=4;i++)
for(j=1;j&lt;=5;j++)
{
scanf("%d",&amp;c[i][j]);
}
printf("enter allocation matrix");
for(i=1;i&lt;=4;i++)
for(j=1;j&lt;=5;j++)
{
scanf("%d",&amp;p[i][j]);
}
printf("enter resource vector:\n");
for(i=1;i&lt;=5;i++)
{
scanf("%d",&amp;r[i]);
}
printf("enter availability vector:\n");
for(i=1;i&lt;=5;i++)
{
scanf("%d",&amp;a[i]);
temp[i]=a[i];
}
for(i=1;i&lt;=4;i++)
{
sum=0;
for(j=1;j&lt;=5;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=1;i&lt;=4;i++)
{
for(l=1;l&lt;k;l++)
if(i!=m[l])
{
flag=1;
for(j=1;j&lt;=5;j++)
if(c[i][j]&gt;temp[j])
{
flag=0;
break;
}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j&lt;=5;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are:");
for(j=1;j&lt;=tp;j++)
{
found=0;
for(i=1;i&lt;k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
getch();
}

INPUT:​
enter total no. of processes : 4​
enter claim matrix :​
0 1 0 0 1​
0 0 1 0 1​
0 0 0 0 1​
1 0 1 0 1​
enter allocation matrix :​
1 0 1 1 0​
1 1 0 0 0​
0 0 0 1 0​
0 0 0 0 0​
enter resource vector :​
2 1 1 2 1​
enter the availability vector :​
00001

OUTPUT :​
deadlock causing processes are : 1 2

9. IMPLEMENT E ALL PAGE REPLACEMENT ALGORITHMS


A) FIFO B) LRU C) LFU
Ex. No.: 9a FIFO PAGE REPLACEMENT
PROGRAM:
#include<stdio.h>
void main()
{
​ int n,i,j,a[50],frame[10],no,k,avail,count=0;
​ printf("\nEnter the no. of page: ");
​ scanf("%d",&n);
​ printf("\nEnter the page no: ");
​ for(i=0;i<n;i++)
​ ​ scanf("%d",&a[i]);
​ printf("\nEnter the no. of frame: ");
​ scanf("%d",&no);
​ for(i=0;i<no;i++)
​ ​ frame[i]=-1;
​ j=0;
​ printf("\n Refernce string\t Page frame\n");
​ for(i=0;i<n;i++)
​ {
​ ​ printf("%d\t",a[i]);
​ ​ avail=0;
​ ​ for(k=0;k<no;k++)
​ ​ ​ if(frame[k]==a[i])
​ ​ ​ ​ avail=1;
​ ​ if(avail==0)
​ ​ {
​ ​ ​ frame[j]=a[i];
​ ​ ​ j=(j+1)%no;
​ ​ ​ count++;
​ ​ ​ for(k=0;k<no;k++)
​ ​ ​ ​ printf("%d\t",frame[k]);
​ ​ }
​ ​ printf("\n");
​ }
​ printf("\nPage fault id %d",count);
}

OUTPUT:
[netlab@system-2 oslab]$ cc fifopaging.c
[netlab@system-2 oslab]$ ./[Link]

Enter the no. of page: 20


Enter the page no: 3
5
1
2
1
7
0
6
0
7
8
9
4
1
5
6
1
4
7
5

Enter the no. of frame: 3

Refernce string Page frame


3 3 -1 -1
5 3 5 -1
1 3 5 1
2 2 5 1
1
7 2 7 1
0 2 7 0
6 6 7 0
0
7
8 6 8 0
9 6 8 9
4 4 8 9
1 4 1 9
5 4 1 5
6 6 1 5
1
4 6 4 5
7 6 4 7
5 5 4 7

Page fault id 16

Ex. No.: 9b LRU PAGE REPLACEMENT


PROGRAM:
#include<stdio.h>
#define frame 10
#define framesize 10
int arr[frame][framesize];
int page[framesize];
int main()
{
​ int i,j,k,n,b;
​ int failure=0,found;
​ for(i=0;i<frame;i++)
​ ​ for(j=0;j<framesize;j++)
​ ​ ​ arr[i][j]=-1;
​ printf("\nEnter the no. of frame available: ");
​ scanf("%d",&b);
​ printf("\nEnter the no. of pageframe to be called: ");
​ scanf("%d",&n);
​ printf("\nEnter the no. of page frame array: ");
​ for(i=0;i<n;i++)
​ ​ scanf("%d",&page[i]);
​ arr[0][0]=page[0];
​ failure++;
​ for(i=1;i<n;i++)
​ {
​ ​ for(j=0;j<b;j++)
​ ​ {
​ ​ ​ found=0;
​ ​ ​ if(arr[i][i-1]==page[i])
​ ​ ​ {
​ ​ ​ ​ found=1;
​ ​ ​ ​ break;
​ ​ ​ }
​ ​ }
​ ​ if(found)
​ ​ {
​ ​ ​ for(k=b;k>0;k--)
​ ​ ​ ​ arr[k][i]=arr[k-1][i-1];
​ ​ ​ arr[k][i]=page[i];
​ ​ ​ failure++;
​ ​ }
​ ​ else
​ ​ {
​ ​ ​ arr[0][i]=page[i];
​ ​ ​ for(k=1;k<b;k++)
​ ​ ​ ​ if(k>j)
​ ​ ​ ​ ​ arr[k][i]=arr[k][i-1];
​ ​ ​ ​ else
​ ​ ​ ​ ​ arr[k][i]=arr[k-1][i-1];
​ ​ }
​ }
​ printf("\n Page replacement using LRU\n");
​ for(i=0;i<b;i++)
​ {
​ ​ for(j=0;j<n;j++)
​ ​ ​ if(arr[i][j]!=-1)
​ ​ ​ ​ printf("%2d ",arr[i][j]);
​ ​ printf("\n");
​ }
​ printf("\nNo. of replacement failed: %d out of %d",failure,n);
​ printf("\nNo. of replacement success: %d out of %d",n-failure,n);
}

OUTPUT:
[netlab@system-2 oslab]$ cc lrupaging.c
[netlab@system-2 oslab]$ ./[Link]

Enter the no. of frame available: 3


Enter the no. of pageframe to be called: 10
Enter the no. of page frame array: 7 0 1 2 0 3 0 4 5 6
Page replacement using LRU
7 0 1 2 0 3 0 4 5 6
7 0 1 2 0 3 0 4 5
7 0 1 2 0 3 0 4

No. of replacement failed: 1 out of 10


No. of replacement success: 9 out of 10

Ex. No.: 9b LFU PAGE REPLACEMENT


#include<stdio.h>
int main()
{
int f,p;
int pages[50],frame[10],hit=0,count[50],time[50];
int i,j,page,flag,least,minTime,temp;

printf("Enter no of frames : ");


scanf("%d",&f);
printf("Enter no of pages : ");
scanf("%d",&p);

for(i=0;i<f;i++)
{
frame[i]=-1;
}
for(i=0;i<50;i++)
{
count[i]=0;
}
printf("Enter page no : \n");
for(i=0;i<p;i++)
{
scanf("%d",&pages[i]);
}
printf("\n");
for(i=0;i<p;i++)
{
count[pages[i]]++;
time[pages[i]]=i;
flag=1;
least=frame[0];
for(j=0;j<f;j++)
{
if(frame[j]==-1 || frame[j]==pages[i])
{
if(frame[j]!=-1)
{
hit++;
}
flag=0;
frame[j]=pages[i];
break;
}
if(count[least]>count[frame[j]])
{
least=frame[j];
}
}
if(flag)
{
minTime=50;
for(j=0;j<f;j++)
{
if(count[frame[j]]==count[least] && time[frame[j]]<minTime)
{
temp=j;
minTime=time[frame[j]];
}
}
count[frame[temp]]=0;
frame[temp]=pages[i];
}
for(j=0;j<f;j++)
{
printf("%d ",frame[j]);
}
printf("\n");
}
printf("Page hit = %d",hit);
return 0;
}

10. IMPLEMENT SHARED MEMORY AND IPC


Ex. No. 10a​ INTERPROCESS COMMUNICATION – MESSAGE QUEUES

PROGRAM:
#include<stdio.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/msg.h>
char name[50];
int main()
{
​ int pid,len,qid;
​ struct
​ {
​ ​ long mtype;
​ ​ char mtext[50];
​ }message;
​ system("clear");
​ printf("\n Inter Process communication using message queue \n");
​ qid = msgget((key_t)13,IPC_CREAT|0666);
​ printf("\n\nMessage queue is created \n");
​ if(qid==-1)
​ {
​ ​ printf("\nMessage queue is not created \n");
​ ​ exit(1);
​ }
​ printf("\nThe value of qid is %d",qid);
​ printf("\nEnter the message for communication: ");
​ scanf("%s",name);
​ strcpy([Link],name);
​ [Link]=1;
​ len=strlen([Link]);
​ pid=fork();
​ printf("\n The value of pid is %d \n",pid);
​ if(pid==0)
​ {
​ ​ msgsnd(qid,&message,len,IPC_NOWAIT);
​ ​ printf("\n\n MESSAGE SEND BY CHILD PROCESS\n");
​ }
​ if(pid>0)
​ {
​ ​ wait();
​ ​ msgrcv(qid,&message,strlen([Link]),0,IPC_NOWAIT|​ ​ ​
​ MSG_NOERROR);
​ ​ printf("\n MESSAGE RECEIVED BY PARENT PROCESS\n");
​ ​ printf("\n Received message is %s \n",[Link]);
​ }
​ if(pid==-1)
​ {
​ ​ printf("\n Error in creating a child \n");
​ ​ exit(1);
​ }
}

OUTPUT:
[netlab@system-2 oslab]$ cc ipcmsg.c
[netlab@system-2 oslab]$ ./[Link]

Inter Process communication using message queue


Message queue is created
The value of qid is 0
Enter the message for communication: Hello
The value of pid is 8517
The value of pid is 0
MESSAGE SEND BY CHILD PROCESS
MESSAGE RECEIVED BY PARENT PROCESS
Received message is Hello

Ex. No.: 10b​ INTERPROCESS COMMUNICATION – PIPES

PROGRAM:
#include<stdio.h>
main()
{
​ int pid,ppid,pdes[2],n,a;
​ printf("\nInterprocess communication using pipes");
​ if(pipe(pdes)==-1)
​ {
​ ​ printf("\n\tThere is no error");
​ ​ exit(1);
​ }
​ else
​ {
​ ​ pid=fork();
​ ​ if(pid==-1)
​ ​ ​ printf("\nThere is an error in the process");
​ ​ else if(pid)
​ ​ {
​ ​ ​ close(pdes[0]);
​ ​ ​ printf("\nParent is %d\n",ppid);
​ ​ ​ printf("\nProcess is %d\n",pid);
​ ​ ​ printf("\nEnter [Link] process");
​ ​ ​ scanf("%d",&a);
​ ​ ​ write(pdes[1],&a,sizeof(a));
​ ​ }
​ ​ else
​ ​ {
​ ​ ​ close(pdes[1]);
​ ​ ​ open(pdes[0]);
​ ​ ​ read(pdes[0],&n,sizeof(a));
​ ​ ​ if(n%2==0)
​ ​ ​ ​ printf("\nThe number %d is even",n);
​ ​ }
​ }
}​

OUTPUT:
[netlab@system-2 oslab]$ cc ipc3.c
[netlab@system-2 oslab]$ ./[Link]

Interprocess communication using pipes


Parent is 13860852
Process is 3976
Enter [Link] process88
Interprocess communication using pipes
The number 88 is even
​ ​
Ex. No. 10c INTERPROCESS COMMUNICATION – SHARED MEMORY
PROGRAM:
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
main()
{
​ int shmid;
​ key_t key=0x10;
​ shmid=shmget(key,100,IPC_CREAT|0666);
​ if( shmid<0 )
​ ​ printf("\nFirst SHMID failed\n");
​ else
​ ​ printf("\nFirst SHMID Succeded id=%d \n",shmid);
​ shmid=shmget(key,101,IPC_CREAT|0666);
​ if(shmid<0)
​ ​ printf("\nSecond SHMID failed\n");
​ else
​ ​ printf("\nSecond SHMID Succeded id=%d \n",shmid);
​ shmid=shmget(key,90,IPC_CREAT|0666);
​ if(shmid<0)
​ ​ printf("\nThird SHMID failed\n");
​ else
​ ​ printf("\n Third SHMID Succeded id=%d \n",shmid);
}

OUTPUT:
[netlab@system-2 oslab]$ cc ipcshared.c
[netlab@system-2 oslab]$ ./[Link]

First SHMID Succeded id=753682


Second SHMID failed
Third SHMID Succeded id=753682

Ex. No.: 10d INTERPROCESS COMMUNICATION-TO SEND A MESSAGE


#include<stdio.h>
#include<sys/types.h>
#include<string.h>
#include<sys/ipc.h>
#include<unistd.h>
main()
{
int mid;
charmsg[200];
printf(“\n Interprocess communication using message queues”);
mid=msgget(27,IPC_CREAT|0777);
if(mid==1)
{
printf(“Invalid message”);
exit;
}
printf(“\n Enter the message:”);
scanf(“%s”,&msg);
msgsnd(mid,msg,strlen(msg),0);
printf(“Message is sent”,msg);
printf(“The sent message %s”,msg);
}

OUTPUT:

[cse2a@sys-do2~] $ visend.c
[cse2a@sys-do2~] $ cc send.c
[cse2a@sys-do2~] $ ./[Link]
Interprocess communication using message queues
Enter the message:Hello
Message is sent
The sent message is Hello​

INTERPROCESS COMMUNICATION-TO RECEIVE A MESSAGE

#include<stdio.h>
#include<sys/types.h>
#include<string.h>
#include<sys/ipc.h>
#include<unistd.h>
main()
{
int mid;
charmsg[200];
printf(“\n Interprocess communication using message queues”);
mid=msgget(27,IPC_CREAT|0777);
if(mid==1)
{
printf(“Invalid message”);
exit;
}
msgrcv(mid,msg,strlen(msg),0);
printf(“Message is received”,msg);
printf(“The received message %s”,msg);
}

OUTPUT:

[cse2a@sys-do2~] $ vireceive.c
[cse2a@sys-do2~] $ cc receive.c
[cse2a@sys-do2~] $ ./[Link]
Interprocess communication using message queues
Message is received
The received message is Hello

11. IMPLEMENT PAGING TECHNIQUE OF MEMORY MANAGEMENT.


Ex. No. 11a ​ ​ ​ ​ PAGING
PROGRAM:
#include<stdio.h>
void main()
{
​ int imem[100][100],pmem[500],ptable[100],psize,n,i,j,phyadd;
​ printf("\nEnter the no. of pages: ");
​ scanf("%d",&n);
​ printf("\nEnter the page size: ");
​ scanf("%d",&psize);
​ printf("\nEnter the data values to be stored: ");​
​ for(i=0;i<n;i++)
​ ​ for(j=0;j<psize;j++)
​ ​ ​ scanf("%d",&imem[i][j]);
​ printf("\nEnter the base address for each page: ");
​ for(i=0;i<n;i++)
​ ​ scanf("%d",&ptable[i]);
​ ​printf("\nLogical memory\n");
​ printf("\nPage no\tOffset\tValues\n");
​ for(i=0;i<n;i++)
​ ​ for(j=0;j<psize;j++)
​ ​ ​ printf("\n\t%d\t%d\t%d\n",i,j,imem[i][j]);
​ ​printf("\nPage table\n");
​ printf("\nIndex\tbase address ");
​ for(i=0;i<n;i++)
​ ​ printf("\n%d\t%d",i,ptable[i]);
​ ​printf("\nPhysical Memory\n");
​ printf("\nLocation\tvalue\tpage no\n");
​ for(i=0;i<n;i++)​
​ ​ for(j=0;j<psize;j++)
​ ​ {
​ ​ ​ phyadd=(ptable[i]*psize)+j;
​ ​ ​ pmem[phyadd]=imem[i][j];
​ ​ ​ printf("\n%d\t%d\tpage%d",phyadd,imem[i][j],i);
​ ​ }
}​

OUTPUT:
[netlab@system-2 oslab]$ cc paging.c
[netlab@system-2 oslab]$ ./[Link]

Enter the no. of pages: 3


Enter the page size: 3
Enter the data values to be stored: 1 2 3 4 5 6 7 8 9
Enter the base address for each page: 100 101 102
Logical memory

Page no Offset Values


0 0 1
0 1 2
0 2 3
1 0 4
1 1 5
1 2 6
2 0 7
2 1 8
2 2 9

Page table Index base address


0 100
1 101
2 102
Physical Memory
Location value page no
300 1 page0
301 2 page0
302 3 page0
303 4 page1
304 5 page1
305 6 page1
306 7 page2
307 8 page2
308 9 page2

Ex. No. 11b ​ ​ BEST FIT MEMORY MANAGEMENT

PROGRAM:
#include<stdio.h>
void main()
{
​ int a[20],p[20],i,j,n,m,temp,b[20],temp1,temp2,c[20];
​ printf("\nMEMORY MANAGEMENT SCHEME - BEST FIT \n");
​ printf("Enter No. of Blocks: ");
​ scanf("%d",&n);
​ for(i=0;i<n;i++)
​ {
​ ​ printf("Enter the %dst block size: ",i);
​ ​ scanf("%d",&a[i]);
​ }
​ printf("Enter No. of Process: ");
​ scanf("%d",&m);
​ for(i=0;i<m;i++)
​ {
​ ​ printf("Enter the size of %dst process: ",i);
​ ​ scanf("%d",&p[i]);
​ }
​ for(i=0;i<n;i++)
​ {
​ ​ for(j=0;j<m;j++)
​ ​ {
​ ​ ​ if(a[i]>a[j])
​ ​ ​ {
​ ​ ​ ​ temp=a[i];
​ ​ ​ ​ a[i]=a[j];
​ ​ ​ ​ a[j]=temp;
​ ​ ​ }
​ ​ ​ if(p[i]>p[j])
​ ​ ​ {
​ ​ ​ ​ temp=p[i];
​ ​ ​ ​ p[i]=p[j];
​ ​ ​ ​ p[j]=temp;
​ ​ ​ }
​ ​ }
​ }
​ printf("\nProcess\tBlock Size\n");
​ for(i=0;i<n;i++)
​ ​ printf("%d\t%d\n",p[i],a[i]);
​ printf("\n\n");
​ for(i=0;i<n;i++)
​ {
​ ​ for(j=0;j<m;j++)
​ ​ {
​ ​ ​ if(p[j]<=a[i])
​ ​ ​ {
​ ​ ​ ​ printf("\nThe process %d [size %d] allocated to block ​ ​
​ ​ ​ %d\n", j,p[j],a[i]);
​ ​ ​ ​ p[j]=10000;
​ ​ ​ ​ break;
​ ​ ​ }
​ ​ }
​ }
​ for(j=0;j<m;j++)
​ ​ if(p[j]!=10000)
​ ​ ​ printf("\nThe process %d is not allocated\n",p[j]);
}

OUTPUT:
[netlab@system-2 oslab]$ cc bestfit.c
[netlab@system-2 oslab]$ ./[Link]

MEMORY MANAGEMENT SCHEME - BEST FIT


Enter No. of Blocks: 5
Enter the 0st block size: 500
Enter the 1st block size: 120
Enter the 2st block size: 180
Enter the 3st block size: 250
Enter the 4st block size: 350
Enter No. of Process: 5
Enter the size of 0st process: 600
Enter the size of 1st process: 100
Enter the size of 2st process: 160
Enter the size of 3st process: 400
Enter the size of 4st process: 300

Process​ Block Size


600​ ​ ​ 500
400​ ​ ​ 350
300​ ​ ​ 250
160​ ​ ​ 180
100​ ​ ​ 120

The process 1 [size 400] allocated to block 500


The process 2 [size 300] allocated to block 350
The process 3 [size 160] allocated to block 250
The process 4 [size 100] allocated to block 180
The process 600 is not allocated

Ex. No. 11c ​ ​ FIRST FIT MEMORY MANAGEMENT

PROGRAM:
#include<stdio.h>
void main()
{
​ int a[20],p[20],i,j,n,m,temp,b[20],temp1,temp2,c[20];
​ printf("\nMEMORY MANAGEMENT SCHEME - FIRST FIT \n");
​ printf("Enter No. of Blocks: ");
​ scanf("%d",&n);
​ for(i=0;i<n;i++)
​ {
​ ​ printf("Enter the %dst block size: ",i);
​ ​ scanf("%d",&a[i]);
​ }
​ printf("Enter No. of Process: ");
​ scanf("%d",&m);
​ for(i=0;i<m;i++)
​ {
​ ​ printf("Enter the size of %dst process: ",i);
​ ​ scanf("%d",&p[i]);
​ }
​ printf("\nProcess\tBlock Size\n");
​ for(i=0;i<n;i++)
​ ​ printf("%d\t\t%d\n",p[i],a[i]);
​ printf("\n\n");
​ for(i=0;i<n;i++)
​ {
​ ​ for(j=0;j<m;j++)
​ ​ {
​ ​ ​ if(p[j]<=a[i])
​ ​ ​ {
​ ​ ​ ​ printf("The process %d [size %d]allocated to block ​​ ​
​ ​ ​ %d\n",j,p[j],a[i]);
​ ​ ​ ​ p[j]=10000;
​ ​ ​ ​ ​ break;
​ ​ ​ }
​ ​ }
​ }
​ for(j=0;j<m;j++)
​ ​ if(p[j]!=10000)
​ ​ ​ printf("The process %d is not allocated\n",j);
}

OUTPUT:
[netlab@system-2 oslab]$ cc firstfit.c
[netlab@system-2 oslab]$ ./[Link]

MEMORY MANAGEMENT SCHEME - FIRST FIT


Enter No. of Blocks: 5
Enter the 0st block size: 120
Enter the 1st block size: 230
Enter the 2st block size: 340
Enter the 3st block size: 450
Enter the 4st block size: 560
Enter No. of Process: 5
Enter the size of 0st process: 530
Enter the size of 1st process: 430
Enter the size of 2st process: 630
Enter the size of 3st process: 203
Enter the size of 4st process: 130

Process​ Block Size


530​ ​ 120
430​ ​ 230
630​ ​ 340
203​ ​ 450
130​ ​ 560

The process 3 [size 203]allocated to block 230


The process 4 [size 130]allocated to block 340
The process 1 [size 430]allocated to block 450
The process 0 [size 530]allocated to block 560
The process 2 is not allocated

Ex. No. 11d ​ ​ WORST FIT MEMORY MANAGEMENT

PROGRAM:
#include<stdio.h>
void main()
{
​ int a[20],p[20],i,j,n,m,temp,b[20],temp1,temp2,c[20];
​ printf("\nMEMORY MANAGEMENT SCHEME - WORST FIT \n");
​ printf("Enter No. of Blocks: ");
​ scanf("%d",&n);
​ for(i=0;i<n;i++)
​ {
​ ​ printf("Enter the %dst block size: ",i);
​ ​ scanf("%d",&a[i]);
​ }
​ printf("Enter No. of Process: ");
​ scanf("%d",&m);
​ for(i=0;i<m;i++)
​ {
​ ​ printf("Enter the size of %dst process: ",i);
​ ​ scanf("%d",&p[i]);
​ }
​ for(i=0;i<n;i++)
​ {
​ ​ for(j=0;j<m;j++)
​ ​ {
​ ​ ​ if(a[i]>a[j])
​ ​ ​ {
​ ​ ​ ​ temp=a[i];
​ ​ ​ ​ a[i]=a[j];
​ ​ ​ ​ a[j]=temp;
​ ​ ​ }
​ ​ ​ if(p[i]<p[j])
​ ​ ​ {
​ ​ ​ ​ temp=p[i];
​ ​ ​ ​ p[i]=p[j];
​ ​ ​ ​ p[j]=temp;
​ ​ ​ }
​ ​ }
​ }
​ printf("\nProcess\tBlock Size\n");
​ for(i=0;i<n;i++)
​ ​ printf("%d\t%d\n",p[i],a[i]);
​ printf("\n\n");
​ for(i=0;i<n;i++)
​ {
​ ​ for(j=0;j<m;j++)
​ ​ {
​ ​ ​ if(p[j]<=a[i])
​ ​ ​ {
​ ​ ​ ​ printf("\nThe process %d [size %d] allocated to block
%d\n",j,p[j],a[i]);
​ ​ ​ ​ p[j]=10000;
​ ​ ​ ​ break;
​ ​ ​ }
​ ​ }
​ }
​ for(j=0;j<m;j++)
​ ​ if(p[j]!=10000)
​ ​ ​ printf("\nThe process %d is not allocated\n",p[j]);
}

OUTPUT
[netlab@system-2 oslab]$ cc worstfit.c
[netlab@system-2 oslab]$ ./[Link]

MEMORY MANAGEMENT SCHEME - WORST FIT


Enter No. of Blocks: 5
Enter the 0st block size: 520
Enter the 1st block size: 147
Enter the 2st block size: 236
Enter the 3st block size: 289
Enter the 4st block size: 600
Enter No. of Process: 5
Enter the size of 0st process: 196
Enter the size of 1st process: 245
Enter the size of 2st process: 570
Enter the size of 3st process: 145
Enter the size of 4st process: 600

Process​ Block Size


145​ ​ ​ 600
196​ ​ ​ 520
245​ ​ ​ 289
570​ ​ ​ 236
600​ ​ ​ 147

The process 0 [size 145] allocated to block 600


The process 1 [size 196] allocated to block 520
The process 2 [size 245] allocated to block 289
The process 570 is not allocated
The process 600 is not allocated

You might also like