Operating System Commands and Scheduling
Operating System Commands and Scheduling
1
Objective:. Study of hardware and basic commands of different operating systems (UNIX,
LINUX, WINDOWS)
Theory:
COMMAND :
[Link] Command :
This command is used to display the current data and time.
Syntax: $date
[Link] Command :
This command is used to display the calendar of the year or the particular month of
calendar year.
Syntax :
a. $cal<year>
b. $cal<month><year>
Here the first syntax gives the entire calendar for given year & the second Syntax
gives the calendar of reserved month of that year.
[Link] Command :
This command is used to print the arguments on the screen .
Syntax : $echo <text>
Multi line echo command :
To have the output in the same line , the following commands can be used.
Syntax : $echo <text\>text
To have the output in different line, the following command can be used.
Syntax : $echo “text
>line2
>line3”
4.’who’ Command :
It is used to display who are the users connected to our computer currently.
Syntax : $who – option‟s
Options : -
H–Display the output with headers.
b–Display the last booting date or time or when the system was lastely
rebooted.
5.’who am i’ Command :
Display the details of the current working directory.
Syntax : $who am i
6.’tty’ Command :
It will display the terminal name.
Syntax : $tty
7.’CLEAR’ Command :
It is used to clear the screen.
Syntax : $clear
8.’MAN’ Command :
It help us to know about the particular command and its options & working. It is like
„help‟ command in windows .
Syntax : $man <command name>
[Link] Command :
It is used to manipulate the screen.
Syntax :$tput<argument>
Arguments :
[Link] – to clear the screen.
[Link] – Display the complete name of the terminal.
[Link] – background become white and foreground become black
color. [Link] – background become black and foreground becomes
white color. [Link] – Display the number of columns in our terminals.
[Link] Command :
To create or make a new directory in a current
directory .Syntax :$mkdir<directory name>
[Link] Command :
To change or move the directory to mentioned
directory .Syntax :$cd <directory name>
[Link] Command :
To remove a directory in the current directory & not the current directory
itself. Syntax :$rmdir<directory name>
FILE RELATED COMMANDS :
[Link] A FILE
To create a new file in the current directory we use CAT command.
Syntax :
$cat > filename.
The > symbol is redirectory we use cat command.
[Link] A FILE :
To display the content of file mentioned we use CAT command without „>‟ operator.
Syntax :
$cat <filename.
Options –s = to neglect the warning /error message.
[Link] CONTENTS :
To copy the content of one file with another. If file doesnot exist, a new file is created
and if the file exists with some data then it is overwritten.
Syntax :
$ cat <filename source>>><destination filename>
Options : -
-n content of file with numbers included with blank lines.
Syntax :
$cat –n <filename>
[Link] A FILE :
To sort the contents in alphabetical order or in reverse order.
Syntax :
$sort <filename >
Option : $ sort –r <filename>
[Link] Command :
To completely move the contents from source file to destination file and to remove
the source file.
Syntax :
$ mv <source filename><destination filename>
[Link] Command :
To permanently remove the file we use this command .
Syntax :
$rm<filename>
[Link] Command :
To list the content count of no of lines , words, characters .
Syntax :
$wc<filename>
Options :
-c – to display no of
characters. -l – to display
only the lines.
-w – to display the no of words.
[Link] PRINTER :
To print the line through the printer, we use lp command.
Syntax :
$lp<filename>
[Link] Command :
This command is used to display the contents of the file page wise & next page can be
viewed by pressing the enter key.
Syntax :
$pg<filename>
TAIL :This command is used to display the last ten lines of file.
Syntax: $tail<filename>
PAGE : This command shows the page by page a screenfull of information is displayed
afterwhich the page command displays a prompt and passes for the user to strike the enter
key to
continue scrolling.
Syntax: $pg <filename>
MORE :It also displays the file page by page .To continue scrolling with more
command ,press the space bar key.
Syntax: $more<filename>
GREP :This command is used to search and print the specified patterns from the
[Link]: $grep [option] pattern <filename>
PIPE :It is a mechanism by which the output of one command can be channeled into the
inputof another command.
$who | wc-l
Syntax: $ who | wc -l
TR :Thetr filter is used to translate one set of characters from the standard inputs to
another.
Syntax: $tr “[a-z]” “[A-Z]”
Experiment No. 2
(a) Objective: Implement CPU scheduling policy FIRST COME FIRST SERVE.
Theory:
(i) The number of processes/jobs in the system
(Computed through random functions in C)
(ii) Process ID, CPU Burst and arrival time of process.
Algorithm:
(i) We randomly generate the number of jobs. There must be a limit on the number of
jobs in a system.
(ii) The execution time of the generated jobs is also not known. Here, we are
generating the CPU burst of each job making use of the past history.
(iii) All the jobs are then arranged in a queue where searching is done to find the one
with the least CPU burst. There may be two jobs in queue with the same execution
time then FCFS approach is to be performed.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char pn[10][10];
intarr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,n;
inttotwt=0,tottat=0;
clrscr();
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Process Name, Arrival Time & Burst Time:");
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
}
for(i=0;i<n;i++)
{
if(i==0)
{
star[i]=arr[i];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
else
{
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
}
printf("\nPNameArrtimeBurtime Start TAT Finish");
for(i=0;i<n;i++)
{
printf("\n%s\t%6d\t\t%6d\t%6d\t%6d\t%6d",pn[i],arr[i],bur[i],star[i],tat[i],finish[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("\nAverage Waiting time:%f",(float)totwt/n);
printf("\nAverage Turn Around Time:%f",(float)tottat/n);
getch();
}
OUTPUT:-
Theory:
(i) The number of processes/jobs in the system
(computed through random functions in C)
(ii) The CPU Burst, arrival time of process.
Algorithm:
(i) We randomly generate the number of jobs. There must be a limit on the number of
jobs in a system.
(ii) The execution time of the generated jobs is also not known. Here, we are
generating the CPU burst of each job making use of the past history.
(iii) All the jobs are then arranged in a queue where searching is done to find the one
with the least CPU burst. There may be two jobs in queue with the same execution
time then FCFS approach is to be performed.
Note: If the algorithm is non preemptive in nature, then the newly arriving job is to be
added to the job queue even though it is of lesser execution time than the one
running on the processor.
Program:
#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];
inttotwt=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("\nAverageturnaroundtime is:%f",ata);
getch();
}
OUTPUT:-
Experiment No. 3
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
inttotwt=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,arrivaltime,execution time & priority:");
flushall();
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
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];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[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\tpriority\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverageturnaroundtime is:%f",ata);
getch();
}
OUTPUT:-
(b) Objective: Implement CPU Scheduling Policies: Multi level Queue Scheduling
#include<stdio.h>
#include<stdlib.h>
typedef struct process
{
int num;
int arrive;
int brust;
int pt;
int f;
int cpu;
int wait;
}
process;
typedef struct queue
{
process p[10];
int front;
int rear;
}queue;
void insert(queue *q,process t)
{
q->p[q->rear]=t;
q->rear++;
}
void pstack(queue *que)
{
int i,j;
for(i=0;i<3;i++)
{
printf("\n queue %d is \n",i+1);
printf("Process\tarive\tbrust\t type\t I/O\t cpu \twait\n");
for(j=0;j<que[i].rear;j++)
{
printf(" %d \t %d \t %d \t %d \t %d \t %d
\t %d\n",que[i].p[j].num,que[i].p[j].arrive,que[i].p[j].brust,que[i].p[j]
.pt,que[i].p[j].f,que[i].p[j].cpu,que[i].p[j].wait);
}
}
}
void fcfs_sort(queue *que)
{
process *temp;
int i,j,k,n;
for(k=0;k<3;k++)
{
n=que[k].rear;
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(que[k].p[j].arrive > que[k].p[j+1].arrive)
{
temp=&que[k].p[j];
que[k].p[j]=que[k].p[j+1];
que[k].p[j+1]=*temp;
}
}
}
}
}
void pque(queue *que)
{
int c=0,cp=0;
int w=0;
int j=0,k=0,l=0,t=0,i;
process *temp;
int max=0;
for(i=0;i<3;i++)
{
max=max+que[i].rear;
}
for(i=0;i<max;i++)
{
if(que[0].p[j].arrive <= cp && j< que[0].rear)
{
que[0].p[j].cpu=c;
que[0].p[j].wait=c-que[0].p[j].arrive;
if(que[0].p[j].wait < 1)
que[0].p[j].wait=0;
c=c+que[0].p[j].brust;
j++;
cp=c;
continue;
}
else if(que[1].p[k].arrive < cp && k < que[1].rear )
{
que[1].p[k].cpu=c;
que[1].p[k].wait=c-que[1].p[k].arrive;
if(que[1].p[k].wait < 1)
que[1].p[k].wait=0;
c=c+que[1].p[k].brust;
k++;
cp=c;
continue;
}
else if(que[2].p[l].arrive < cp && l < que[2].rear)
{
que[2].p[l].cpu=c;
que[2].p[l].wait=c-que[2].p[l].arrive;
if(que[2].p[l].wait < 1)
que[2].p[l].wait=0;
c=c+que[2].p[l].brust;
l++;
}
else
c=0;
}
}
int main()
{
queue que[3];
int j;
for(j=0;j<3;j++)
{
que[j].front=-1;
que[j].rear=0;
}
process temp;
int n;
printf("enter the number of process");
scanf("%d",&n);
int i;
for(i=0;i<n;i++)
{
[Link]=i+1;
[Link]=0;
[Link]=0;
printf("\n enter the ariive time of process %d =",i+1);
scanf("%d",&[Link]);
printf("\n enter the brust time of process %d =",i+1);
scanf("%d",&[Link]);
printf("\n enter the type of(1-back,2-forground,3-other) process %d =",i+1);
scanf("%d",&[Link]);
printf("\n enter the feedback of process(0-for normal,1-I/O) %d =",i+1);
scanf("%d",&temp.f);
if([Link]>=2 && temp.f==1)
insert(&que[[Link]-2],temp);
else if(temp.f==0)
insert(&que[[Link]-1],temp);
else
insert(&que[[Link]-1],temp);
}
pstack(que);
fcfs_sort(que);
printf("\n\nafter sorting of process list is \n\n");
pstack(que);
pque(que);
OUTPUT:-
queue 1 is
Process arive brust type I/O cpu wait
1 0 4 1 0 0 0
4 3 5 1 0 0 0
queue 2 is
Process arive brust type I/O cpu wait
2 1 6 2 0 0 0
3 2 2 3 1 0 0
5 4 3 2 0 0 0
queue 3 is
Process arive brust type I/O cpu wait
queue 1 is
Process arive brust type I/O cpu wait
1 0 4 1 0 0 0
4 3 5 1 0 0 0
queue 2 is
Process arive brust type I/O cpu wait
2 1 6 2 0 0 0
3 2 2 3 1 0 0
5 4 3 2 0 0 0
queue 3 is
Process arive brust type I/O cpu wait
queue 1 is
Process arive brust type I/O cpu wait
1 0 4 1 0 0 0
4 3 5 1 0 4 1
queue 2 is
Process arive brust type I/O cpu wait
2 1 6 2 0 9 8
3 2 2 3 1 15 13
5 4 3 2 0 17 13
queue 3 is
Process arive brust type I/O cpu wait
Experiment No.4
Objective: Implementation of resource allocation graph RAG)
#include<stdio.h>
int main()
{
int np, nr, temp, temp1;
printf("enter number of resources: ");
scanf("%d", &nr);
printf("enter number of processs: ");
scanf("%d", &np);
int rag[nr+np][nr+np];
int i, j;
for(i=0;i<np+nr;i++)
{
for(j=0; j<np+nr;j++)
{
rag[i][j]=0;
}
}
for(i=0;i<np;i++)
{
printf("enter the number of resources process %d, holding", i);
scanf("%d", &temp);
for(j=0; j<temp;j++)
{
printf("enter the ressorce number process %d holding: ", j);
scanf("%d", &temp1);
rag[np+temp1][i]=1;
}
printf("enter the number of resources process %d, requesting", i);
scanf("%d", &temp);
for(j=0; j<temp;j++)
{
printf("enter the ressorce number process %d requesting: ", i);
scanf("%d", &temp1);
rag[i][np+temp1]=1;
}
}
for(i=0;i<np+nr;i++)
{
for(j=0; j<np+nr;j++)
{
printf("%d ", rag[i][j]);
}
printf("\n ");
}
return 0;
}
OUTPUT:-
Experiment No. 5
Objective:. Implementation of Banker’s Algorithm.
Theory:
The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests
for safety by simulating the allocation for predetermined maximum possible amounts of all
resources, then makes an “s-state” check to test for possible activities, before deciding
whether allocation should be allowed to continue.
Following Data structures are used to implement the Banker’s Algorithm:
Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types.
Available :
● It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
● Available[ j ] = k means there are ‘k’ instances of resource type Rj
Max :
● It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a
system.
● Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.
Allocation :
●
It is a 2-d array of size ‘n*m’ that defines the number of resources of each type
currently allocated to each process.
● Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource
type Rj
Need :
● It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each
process.
● Need [ i, j ] = k means process Pi currently allocated ‘k’ instances of resource type Rj
● Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]
Allocationi specifies the resources currently allocated to process Pi and Needi specifies the
additional resources that process Pi may still request to complete its task.
Algorithm:
Banker’s algorithm consist of Safety algorithm and Resource request algorithm
Safety Algorithm
The algorithm for finding out whether or not a system is in a safe state can be described as
follows:
1. Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work= Available
Finish [i]=false; for i=1,2,……,n
2. Find an i such that both
a) Finish [i]=false
b) Need_i<=work
if no such i exists goto step (4)
3. Work=Work + Allocation_i
Finish[i]= true
goto step(2)
4. If Finish[i]=true for all i,
then the system is in safe state.
(i) Perform Banker's algorithm when a request for R is made.
(ii) Compute Need[i,j]=Max[i,j]-Allocation[i,j].
(iii) Update accordingly.
Once the resources are allocated, check to see if the system state is safe. If unsafe, the
process must wait and the old resource-allocated state is restored.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
intclm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5],comp[7];
intfirst,p,r,i,j,prc,count,t;
clrscr();
count=0;
for(i=1;i<=7;i++)
comp[i]=0;
printf("Enter the no of processes:\n");
scanf("%d",&p);
printf("Enter the no of resources:\n");
scanf("%d",&r);
printf("Enter the claim for each process:");
for(i=1;i<=p;i++)
{
printf("\nFor process %d",i);
for(j=1;j<=r;j++)
{
scanf("%d",&clm[i][j]);
}
}
printf("Enter the allocation for each process:\n");
for(i=1;i<=p;i++)
{
printf("\nFor process ",i);
for(j=1;j<=r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter total no of each resource:");
for(j=1;j<=r;j++)
scanf("%d",&rsrc[j]);
for(j=1;j<=r;j++)
{
int total=0;
avail[j]=0;
for(i=1;i<=p;i++)
{total+=alloc[i][j];}
avail[j]=rsrc[j]-total;
}
do
{
for(i=1;i<=p;i++)
{
for(j=1;j<=r;j++)
{
req[i][j]=clm[i][j]-alloc[i][j];
}
}
printf("\n\nAvailableresorces is:");
for(j=1;j<=r;j++)
{ printf(" ",avail[j]); }
printf("\nClaim matrix:\t\tAllocation matrix:\n");
for(i=1;i<=p;i++)
{
for(j=1;j<=r;j++)
{
printf("%d",clm[i][j]);
}
printf("\t\t\t");
for(j=1;j<=r;j++)
{
printf("%d",alloc[i][j]);
}
printf("\n");
}
prc=0;
for(i=1;i<=p;i++)
{
if(comp[i]==0)//if not completed
{
prc=i;
for(j=1;j<=r;j++)
{
if(avail[j]
{
prc=0;
break;
}
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\nProcess ",prc,"runs to completion!");
count++;
for(j=1;j<=r;j++)
{
avail[j]+=alloc[prc][j];
alloc[prc][j]=0;
clm[prc][j]=0;
comp[prc]=1;
}
}
}
while(count!=p&&prc!=0);
if(count==p)
printf("\nThe system is in a safe state!!");
else
printf("\nThe system is in an unsafe state!!");
getch();
}
OUTPUT:-
Availableresorces is: 3 3 4
Claim matrix: Allocation matrix:
753 010
322 200
902 302
222 211
#include<stdio.h>
int main()
{
int np, nr, temp, temp1;
printf("enter number of resources: ");
scanf("%d", &nr);
printf("enter number of processs: ");
scanf("%d", &np);
int rag[nr+np][nr+np];
int i, j;
for(i=0;i<np+nr;i++)
{
for(j=0; j<np+nr;j++)
{
rag[i][j]=0;
}
}
for(i=0;i<np;i++)
{
printf("enter the number of resources process %d, holding", i);
scanf("%d", &temp);
for(j=0; j<temp;j++)
{
printf("enter the ressorce number process %d holding: ", j);
scanf("%d", &temp1);
rag[np+temp1][i]=1;
}
printf("enter the number of resources process %d, requesting", i);
scanf("%d", &temp);
for(j=0; j<temp;j++)
{
printf("enter the ressorce number process %d requesting: ", i);
scanf("%d", &temp1);
rag[i][np+temp1]=1;
}
}
for(i=0;i<np+nr;i++)
{
for(j=0; j<np+nr;j++)
{
printf("%d ", rag[i][j]);
}
printf("\n ");
}
int wfg[np][np];
for(i=0;i<np;i++)
{
for(j=0; j<np;j++)
{
wfg[i][j]=0;
}
}
int k;
for(i=0;i<np;i++)
{
for(j=np;j<np + nr; j++)
{
if(rag[i][j] == 1)
{
for(k=0;k<np;k++)
{
if(rag[j][k] == 1)
wfg[i][k] = 1;
}
}
}
}
for(i=0;i<np;i++)
{
for(j=0; j<np;j++)
{
printf("%d ", wfg[i][j]);
}
printf("\n ");
}
return 0;
}
OUTPUT:
Experiment No. 7
Objective:. Implementation of System Calls.
Theory:
In computing, a system call is the programmatic way in which a computer program requests a
service from the kernel of the operating system it is executed on. A system call is a way for
programs to interact with the operating system. A computer program makes a system call
when it makes a request to the operating system’s kernel. System call provides the services of
the operating system to the user programs via Application Program Interface(API). It
provides an interface between a process and operating system to allow user-level processes to
request services of the operating system. System calls are the only entry points into the kernel
system. All programs needing resources must use system calls.
Services Provided by System Calls :
1. Process creation and management
2. Main memory management
3. File Access, Directory and File system management
4. Device handling(I/O)
5. Protection
6. Networking, etc.
Types of System Calls : There are 5 different categories of system calls –
1. Process control: end, abort, create, terminate, allocate and free memory.
2. File management: create, open, close, delete, read file etc.
3. Device management
4. Information maintenance
5. Communication
OUTPUT:
c) Aim: To write C programs to simulate UNIX command execlp()
Program:
#include<stdio.h>
#include<sys/types.
h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
printf("\n fork program started");
execlp("/bin/ls","ls",NULL);
}
else
{
printf("\nend");
}
}
OUTPUT:
d) Aim: To write C programs to simulate UNIX command wait()
Program:
#include<unistd.h>
#include<stdio.h>
main()
{
int i=0,pid;
pid=fork();
if(pid==0)
{
printf("child process started\n");
for(i=0;i<10;i++)
printf("\n%d",i);
printf("\n child process
ends");
}
else
{
printf("\n parent process starts");
wait(0);
printf("\n parent process ends");
}
}
OUTPUT:
e) Aim: To write C programs to simulate UNIX command sleep()
Program :
#include<unistd.h>
#include<stdio.h>
main()
{
int i=0,pid;
printf("\n ready for
fork\n");
pid=fork(); if(pid==0)
{
printf("\n child process started \n");
sleep(4);
for(i=0;i<10;i++)
printf("\n%d",i);
printf("\n child process ends");
}
else
{
printf("\n I am the parent");
printf("\n parent process ends");
}
}
OUTPUT:
Experiment No. 8
Objective:. Implementation of Contiguous allocation techniques:
(a) Best-Fit (b) Worst-Fit (c) First-Fit
Theory:
One of the simplest methods for memory allocation is to divide memory into several fixed-
sized partitions. Each partition may contain exactly one process. In this multiple-partition
method, when a partition is free, a process is selected from the input queue and is loaded into
the free partition. When the process terminates, the partition becomes available for another
process. The operating system keeps a table indicating which parts of memory are available
and which are occupied. Finally, when a process arrives and needs memory, a memory
section large enough for this process is provided. When it is time to load or swap a process
into main memory, and if there is more than one free block of memory of sufficient size, then
the operating system must decide which free block to allocate. Best-fit strategy chooses the
block that is closest in size to the request. First-fit chooses the first available block that is
large enough. Worst-fit chooses the largest available block.
First Fit
In the first fit approach is to allocate the first free partition or hole large enough
which can accommodate the process. It finishes after finding the first suitable free
partition.
Best Fit
The best fit deals with allocating the smallest free partition which meets the
requirement of the requesting process. This algorithm first searches the entire list of
free partitions and considers the smallest hole that is adequate. It then tries to find a hole
which is close to actual process size needed.
Worst fit
In worst fit approach is to locate largest available free portion so that the portion left
will be big enough to be useful. It is the reverse of best fit.
Algorithm:
First Fit
1. Get no. of Processes and no. of blocks.
2. After that get the size of each block and process requests.
3. Now allocate processes
if(block size >= process size)
//allocate the process
else
//move on to next block
4. Display the processes with the blocks that are allocated to a respective process.
5. Stop.
Best Fit
1. Get no. of Processes and no. of blocks.
2. After that get the size of each block and process requests.
3. Then select the best memory block that can be allocated using the above definition.
4. Display the processes with the blocks that are allocated to a respective process.
5. Value of Fragmentation is optional to display to keep track of wasted memory.
6. Stop.
Worst Fit
1. Get no. of Processes and no. of blocks.
2. After that get the size of each block and process requests.
3. Then select the largest available memory block that can be allocated using the above
definition.
4. Display the processes with the blocks that are allocated to a respective process.
5. Value of Fragmentation is optional to display to keep track of wasted memory.
6. Stop.
Program:
a) First-fit
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
OUTPUT:
b) Best-fit
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
OUTPUT:
c)Worst-Fit
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
OUTPUT:
Experiment No.9
Objective:. Implementation of Producer Consumer Problem.
Theory:
Producer consumer problem is a classical synchronization problem. We can solve this
problem by using semaphores.
A semaphore S is an integer variable that can be accessed only through two standard
operations : wait() and signal().
The wait() operation reduces the value of semaphore by 1 and the signal() operation increases
its value by 1.
wait(S){
while(S<=0); // busy waiting
S--;
}
signal(S){
S++;
}
Algorithm:
Step 1: Start
Step 2: Define the maximum buffer size.
Step 3:Enter the number of producers and consumers.
Step 4: The producer produces the job and put it in the buffer.
Step 5:The consumer takes the job from the buffer.
Step 6:If the buffer is full the producer goes to sleep.
Step 7:If the buffer is empty then consumer goes to sleep.
Step 8:Stop
Program:
#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("\[Link]\[Link]\[Link]");
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);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
OUTPUT:
Experiment No: 10
Objective: Implement the solutions for Readers-Writers problem using inter process
communication technique –Semaphore
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t mutex,writeblock;
int data = 0,rcount = 0;
void *reader(void *arg)
{
int f;
f = ((int)arg);
sem_wait(&mutex);
rcount = rcount + 1;
if(rcount==1)
sem_wait(&writeblock);
sem_post(&mutex);
printf("Data read by the reader%d is %d\n",f,data);
sleep(1);
sem_wait(&mutex);
rcount = rcount - 1;
if(rcount==0)
sem_post(&writeblock);
sem_post(&mutex);
}
void *writer(void *arg)
{
int f;
f = ((int) arg);
sem_wait(&writeblock);
data++;
printf("Data writen by the writer%d is %d\n",f,data);
sleep(1);
sem_post(&writeblock);
}
int main()
{
int i,b;
pthread_t rtid[5],wtid[5];
sem_init(&mutex,0,1);
sem_init(&writeblock,0,1);
for(i=0;i<=2;i++)
{
pthread_create(&wtid[i],NULL,writer,(void *)i);
pthread_create(&rtid[i],NULL,reader,(void *)i);
}
for(i=0;i<=2;i++)
{
pthread_join(wtid[i],NULL);
pthread_join(rtid[i],NULL);
}
return 0;
}
OUTPUT: