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

Staff Manual: Dr.M.G.R. University Department of Computer Science and Engineering & Information Technology

The document contains instructions for experiments in the Operating Systems lab course. It includes: 1. An introduction to basic Unix commands such as date, who, pwd, ls, mkdir, cd, rmdir, cat, cp, mv, ln, rm, chmod and more. 2. Details on shell programming experiments including writing programs to find the sum of two numbers, generate a Fibonacci series, and determine if a number is positive or negative. 3. An outline of 13 total experiments covering topics like scheduling algorithms, inter-process communication, memory allocation techniques, dining philosophers problem, and more.

Uploaded by

wogam
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)
84 views

Staff Manual: Dr.M.G.R. University Department of Computer Science and Engineering & Information Technology

The document contains instructions for experiments in the Operating Systems lab course. It includes: 1. An introduction to basic Unix commands such as date, who, pwd, ls, mkdir, cd, rmdir, cat, cp, mv, ln, rm, chmod and more. 2. Details on shell programming experiments including writing programs to find the sum of two numbers, generate a Fibonacci series, and determine if a number is positive or negative. 3. An outline of 13 total experiments covering topics like scheduling algorithms, inter-process communication, memory allocation techniques, dining philosophers problem, and more.

Uploaded by

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

Dr.M.G.R.

Educational and Research Institute


University
Department of Computer Science and Engineering
&
Information Technology

Subject Name: Operating system Lab


Subject code: BCSL1204
Programme Name: B.Tech., CSE
Regulation: 2013

STAFF MANUAL
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Table of Contents

ExNo Name Of The Experiments Page no

1 Basic Unix Commands

2 Shell Programming

3 System Calls

Implementation Of Scheduling Algorithms-


4
Fcfs,Sjf,Priority,Round Robin

Interprocess Communication-Using Message


5

Interprocess Communication – Using Shared Memory


6

7 Bankers Algorithm

Memory Allocation Techniques


8

9 Dining Philosphers problem

10 Page Replacement Algorithms

11 Paging

12 Segmentation

13 File Systems

2
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex.No:1
UNIX INTRODUCTION

AIM:

To study the basics of UNIX operating system, commands and vi editor

UNIX Commands

General Commands:

1. date: This tells the current date and time.


$ date
Thu Oct 15 09:34:50 PST 2005

2. who: Gives the details of the user who have logged into the system.
$ who
abc tty() oct 15 11:17
Xyz tty4 oct 15 11:30

3. whoami: Gives the details regarding the login time and system’s name for the
connection being used.
$ whoami.
user 1 ttya Oct 15 12:20.

4. man: It displays the manual page of our terminal with the command ‘man’
command name.
$ man who

5. head and tail: ‘head’ is used to display the initial part of the text file and ‘tail’
is used to display the last part of the file.
$ head [-count] [filename]
$ tail [-count] [filename]

6. pwd: It displays the full path name for the current directory we are working in.
$ pwd
/usr/tmp.

7. is : It displays the list of files in a current working directory.


$ ls
$ ls -l = lists files in long format.
$ ls –t = lists in order of last modification time.
$ ls –a = lists all entries , including the hidden files.
$ ls –d = lists directory files instead of its contents.
$ ls –p = puts a slash after each directory.
$ ls –u = lists in order of last access time.

8. mkdir: It is used to create a new directory.


$ mkdir directory name.
3
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

9. cd : It is used to change from the working directory to any other directory


specified.
$ cd changes to home directory.
$ cd.. changes to parent directory.
$ cd / changes to root directory.
$ cd dirl changes to directory dirl.

10.rmdir: It is use to remove the directory specified in the command line.


$ rmdir directory name

11.cat : This command helps us to specify the contents of the file we specify.
$ cat [option….[file….]]

12. cp : This command is used to create duplicate copied of ordinary files.


$ cp file target
$ cp file1 file2 [file1 is copied to file2]

13.mv: This command is used to rename and move ordinary and directory files.
$ mv file1 file2
$ mv directory1 directory2

14. ln: This is used to link file.


$ ln file1 [file2….] target

15. rm: This command is used to remove one or more files from the
directory.This can be used to delete all files as well as directory.
$ rm [option…..] file

16. chmod: Change the access permissions of a file or a directory.


$ chmod mode file
$ chmod [who] [+/-/=] [permission…] file

who = a – all users


g – group
o – others
u –user
[+/-/=] + adds
- removes
= assigns

[permission] r = read
w =write
x =execute
Ex.:: $ chmod 754 prog1.

17. chown: change the owner ID of the files or directories.


Owner may be decimal user ID or a login name found in the
file/etc/passwd.
This utility is governed by the chown kernel authorization. If it is not
granted,ownership can only be changed by root.

4
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex:: $ chown tutor test

18. wc:counts and displays the lines,words and characters in the files specified.
$ wc
$ wc
$ wc
$ wc
Ex:: $ wc prog2.
3 9 60 prog2.

19. grep : searches the file for the pattern.


$ grep [option…] pattern [file….].
Display the lines containing the pattern on the standard output.
$ grep c report only the number of matching lines.
$ grep-l list only the names of files containing pattern.
$grep-v display all lines expert those containing pattern.

Ex: grep c”the”prog2.

20. cut: cuts out selected fields of each line of a file.


$ cut -first [-d char] [file1 file2…].
-d = it is delimiter. Default is tab.

Ex: cut –f1, 3 –d”w”prog2.

21.paste: merges the corresponding lines of the given files.


$ paste –d file1 file2
Option – d allows replacing tab character by one or more alternate
characters.
Ex:: paste prog1 prog2

22. sort : arranges lines in alphabetic or numeric order.


$ sort [option]file.
Option –d dictionary order
Option –n arithmetic order
Option –r reverse order

Ex :: $ ls – l | sort –n

Result
Thus the Basics of Unix operating system , commands and vi have been studied
successfully.

5
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex.No:2
SHELL PROGRAMMING
AIM:

To study and perform some shell programming utilities like:


a) To develop menu driven command routine.
b) Testing of a file or a directory.
c) Finding the duration of a user’s login time.
d) Clean up routine.
e) Calculator.
SHELL PROGRAMS

a. SUM OF TWO NUMBERS


Aim:
To find the sum of two given numbers.

Program
echo " enter first no "
read a
echo " enter second no "
read b
c= expr $a + $b

INPUT
[student@localhost student]$ sh sum
enter first no
3
enter second no
4
OUTPUT:
7

b. FIBONACCI SERIES
Aim:
To find the Fibonacci series of the given number.
Program:
echo "enter a number"
read n
i=0
a=0
b=1
c=0
echo "fibonacci series is"
echo "$a"
echo "$b"
n1=` expr $n - 2`
while [ $i -lt $n1 ]
do

6
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

c=` expr $a + $b`


echo "$c"
a=$b
b=$c
i=` expr $i + 1`
done

INPUT:
[student@localhost student]$ sh fibo
enter a number
5

OUTPUT:

fibonacci series is
0
1
1
2
3
c. POSITIVE OR NEGATIVE

Aim:
To find whether the given number is positive or negative.

Program:

echo " enter a number "


read a
if [ $a -eq 0 ]
then
echo " no is zero "
elif [ $a -gt 0 ]
then
echo " no is positive "
else
echo " no is negative "
fi

INPUT:
[student@localhost student]$ sh positive
enter a number
4
OUTPUT:
no is positive

INPUT:
[student14@localhost student14]$ sh positive

7
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

enter a number
-5
OUTPUT:
no is negative

INPUT:
[student14@localhost student14]$ sh positive
enter a number
0
OUTPUT:
no is zero

d. GREATEST OF THREE NUMBERS


Aim:
To find the Greatest of the given three numbers.
Program:

echo " enter the three numbers "


read x
read y
read z
if [ $x -gt $y -a $x -gt $z ]
then
echo "$x is greater"
elif [ $y -gt $x -a $y -gt $z ]
then
echo "$y is greater"
else
echo "$z is greater"
fi
INPUT:
[student@localhost student]$ sh greatest
enter the three numbers
5
8
9

OUTPUT:
9 is greater

e. ARMSTRONG NUMBER
Aim:
To find whether the given number is an Armstrong number or not.

Program:
echo "enter the number"
read num
ans=0
n=$num

8
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

while [ $n -gt 0 ]
do
q=` expr $n % 10`
ans=` expr $ans + $q \* $q \* $q`
n=` expr $n / 10`
done
if [ $ans -eq $num ]
then
echo "number is armstrong"
else
echo "number is not armstrong"
fi
INPUT:
[student@localhost student]$ sh armstrong
enter the number
153
OUTPUT:
number is Armstrong
INPUT:
[student@localhost student]$ sh armstrong
enter the number
243
OUTPUT:
number is not armstrong
f. FACTORIAL NUMBER
Aim : To write a program to generate the factorial of given number.
Program:

n=0
fact=1
g=0
y=1
echo "enter no to find the factorial:"
read n
g=$n
while [ $n -ge $y ]
do
fact=` expr $fact \* $n`
n=` expr $n - 1 `
done
echo "factorial for $g is $fact"

INPUT:
[student@localhost student]$ sh fact
enter no to find the factorial:
5

OUTPUT:
factorial for 5 is 120

RESULT: Thus the shell Programs were implemented and output was verified successfully.
9
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex.No:3

PROCESS CREATION

Aim:

To write a C++ program to create a process using the fork system calls.

Program :

#include<iostream.h>
#include<sys/types.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
pid_t child;
cout<<"pid:"<<getpid()<<"patent:"<<getppid()<<endl;
switch(child=fork())
{
case (pid_t) -1: perror ("fork");
break;
case (pid_t) 0: cout<<"child created:pid:"<<getpid()<<"patent:"<<getppid()<<endl;
exit(0);
default:cout<<"parent after fork pid:"<<"child pid:"<<child<<endl;
}
return 0;
}

INPUT:
[student@localhost student]$ cc proc.c

OUTPUT:
pid:26482parent:26292
parent after fork pid:child pid:26483
child created:pid:26483parent:1

RESULT: Thus the Program was implemented and output was verified successfully

10
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex.No:4
CPU SCHEDULING POLICIES

AIM:

To Implement CPU Scheduling Algorithms

1. FCFS (First Come First Served),


2. Shortest Job First,
3. Priority Based Scheduling,
4. Round Robin,
5. Comparative Study.

Ex No.4 (a)
FIRST COME FIRST SERVED CPU SCHEDULING
Program:
#include<stdio.h>
main()
{
int i,n,w[10],e[10],b[10];
float wa=0,ea=0;
printf("\nEnter the no of jobs: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the burst time of job %d :",i+1);
scanf("%d",&b[i]);
if(i==0)
{
w[0]=0;
e[0]=b[0];
}
else
{
e[i]=e[i-1]+b[i];
w[i]=e[i-1];
}
}
printf("\n\n\tJobs\tWaiting time \tBursttime\tExecution time\n");
printf("\t-------------------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("\t%d\t\t%d\t\t%d\t\t%d\n",i+1,w[i],b[i],e[i]);
wa+=w[i];
ea+=e[i];
}
wa=wa/n;
ea=ea/n;
printf("\n\nAverage waiting time is :%2.2f ms\n",wa);
11
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

printf("\nAverage execution time is:%2.2f ms\n\n",ea);


}

INPUT:

Enter the no of jobs: 4


Enter the burst time of job 1 : 5
Enter the burst time of job 2 : 4
Enter the burst time of job 3 : 3
Enter the burst time of job 4 : 6
OUTPUT:
Jobs Waiting time Burst time Execution time
----------------------------------------------------------------
1 0 5 5
2 5 4 9
3 9 3 12
4 12 6 18
Average waiting time is : 6.50 ms
Average execution time is: 11.00 ms

RESULT: Thus the Program was implemented and output was verified successfully.

Ex No.4 (b)

SHORTEST JOB FIRST CPU SCHEDULING


Program:
#include<stdio.h>
struct sjfs {
char pname[10];
int btime;
}proc[10],a;
void main( )
{
struct sjfs proc[10];
int n,i,j;
int temp=0,temp1=0,temp2;
char name[20];
float tt,awt;
printf("Enter the number of processes:\n");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Enter the process name: \n");
scanf("%s",&proc[i].pname);
printf("Enter the Burst time:\n");
scanf("%d",&proc[i].btime);
12
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

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

{
printf("Enter the process name: \n");
scanf("%s",&proc[i].pname);
printf("Enter the Burst time:\n");
scanf("%d",&proc[i].btime);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(proc[i].btime<proc[j].btime)
{
a=proc[i];
proc[i]=proc[j];
proc[j]=a;
}
}
}
printf("-------------------- CPU SCHEDULING ALGORITHM - SJFS -------------------------- ");
printf("\n\tprocess name \tBurst time \twaiting time \tturnaround time\n");
temp=0;
for(i=0;i<n;i++)
{
temp=temp1+temp+proc[i].btime;
temp1=temp1+proc[i].btime;
temp2=temp1-proc[i].btime;
printf("\n\t %s \t %d ms \t %d ms \t %d ms\n",proc[i].pname,proc[i].btime,temp2,temp1);
}
printf("-------------------------------------------------------------------------------");
awt=(temp-temp1)/n;
tt=temp/n;
printf("\nThe Average Waiting time is %4.2f milliseconds\n",awt);
printf("\nThe Average Turnaround time is %4.2f",tt);
}

13
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

INPUT:
Enter the number of processes:
4
Enter the process name:
p1
Enter the Burst time:
5
Enter the process name:
p2
Enter the Burst time:
5
Enter the process name:
p3
Enter the Burst time:
6
Enter the process name:
p4
Enter the Burst time:
2

OUTPUT:
-------------------- CPU SCHEDULING ALGORITHM - SJFS --------------------------
Process name Burst time waiting time turnaround time

p4 2 ms 0ms 2 ms

p1 5 ms 2 ms 7 ms

p2 5 ms 7 ms 12 ms

p3 6 ms 12 ms 18 ms
-----------------------------------------------------------------------------------------------------
The Average Waiting time is 5.00 milliseconds

The Average Turnaround time is 9.00 milliseconds

RESULT: Thus the Program was implemented and output was verified successfully.

14
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex. No.4 (c)


ROUND ROBIN CPU SCHEDULING

Program:
#include<stdio.h>
#include<malloc.h>
void line(int i)
{
int j;
for(j=1; j<=i; j++)
printf("-");
printf("\n");
}
struct process
{
int p_id;
int etime, wtime, tatime;
};
struct process *p, *tmp;
int i, j, k, l, n, time_slice, ctime;
float awtime=0, atatime=0;
int main()
{
printf("Process Scheduling - Round Robin \n");
line(29);
printf("Enter the no.of processes : ");
scanf("%d", &n);
printf("Enter the time slice : ");
scanf("%d", &time_slice);
printf("Enter the context switch time : ");
scanf("%d", &ctime);
p=(struct process*) calloc(n+1, sizeof(struct process));
tmp=(struct process*) calloc(n+1, sizeof(struct process));
for(i=1; i<=n; i++)
{
printf("Enter the execution time of process %d : ", i-1);
scanf("%d", &p[i].etime);
p[i].wtime=(time_slice+ctime)*i-1;
awtime += p[i].wtime;
p[i].p_id=i-1;
tmp[i]=p[i];
}
i=0; j=1; k=0;
while(i<n)
{
for(j=1; j<=n; j++)
{
if(tmp[j].etime <= time_slice && tmp[j].etime!=0)
{
k=k+tmp[j].etime;

15
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

tmp[j].etime=0;
p[j].tatime=k;
atatime += p[j].tatime;
k=k+ctime;
i++;
}
if(tmp[j].etime>time_slice && tmp[j].etime!=0)
{
k=k+time_slice+ctime;
tmp[j].etime -= time_slice;
}
}
}
awtime=awtime/n;
atatime=atatime/n;
printf("\nShedule \n");
line(60);
printf("Process\t\tExecution\tWait\t\tTurnaround\n");
printf("Id No\t\ttime\t\ttime\t\ttime\n");
line(60);
for(i=1;i<=n;i++)
printf("%7d\t%14d\t%8d\t%14d \n", p[i].p_id, p[i].etime, p[i].wtime,
p[i].tatime);
line(60);
printf("Avg waiting time :\t%2f \n",awtime);
printf("Avg turn around time :\t%2f\n",atatime);
line(60);
return(0);
}
Input :
Round Robin Scheduling
Enter the no of processes : 3
Enter the time slice : 4
Enter the context switch time : 5
Enter the execution time of process 0 : 6
Enter the execution time of process 1 : 6
Enter the execution time of process 2 : 5
Output:
Schedule
------------------------------------------------------------
Process Execution Wait Turnaround
Id No time time time
------------------------------------------------------------
0 6 8 29
1 6 17 36
2 5 26 42
------------------------------------------------------------
Avg waiting time : 17.000000
Avg turn around time : 35.666668
------------------------------------------------------------
RESULT: Thus the Program was implemented and output was verified successfully.

16
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex No.4 (d)
PRIORITY CPU SCHEDULING

Program:

#include<stdio.h>
#include<malloc.h>
void line(int i)
{
int j;
for(j=1;j<=i;j++)
printf("-");
printf("\n");
}
struct process
{
int p_id,priority;
int etime,wtime,tatime;
};
struct process *p,temp;
int i,j,k,l,n;
float awtime=0,atatime=0;
int main()
{
printf("Priority Scheduling\n");
line(29);
printf("Enter the no of processes : ");
scanf("%d",&n);
p=(struct process *) calloc(n+1,sizeof(struct process));
p[0].wtime=0;
p[0].tatime=0;
for(i=1;i<=n;i++)
{
printf("Enter the execution time of process %d:",i-1);
scanf("%d",&p[i].etime);
printf("Enter the priority of process %d:",i-1);
scanf("%d",&p[i].priority);
p[i].p_id=i;
}
for(i=1;i<=n;i++)
for(j=1;j<=n-i;j++)
if(p[i].priority>p[j+1].priority)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
for(i=1;i<=n;i++)
{
p[i].wtime=p[i-1].tatime;
p[i].tatime=p[i-1].tatime+p[i].etime;
17
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

awtime+=p[i].wtime;
atatime+=p[i].tatime;
}
awtime=awtime/n;
awtime=atatime/n;
printf("\nSchedule\n");
line(60);
printf("Process\t\texection\twait\t\tturnaround\n");
printf("Id No\t\t time\t\ttime\t\ttime\n");
line(60);
for(i=1;i<=n;i++)
printf("%7d\t%14d\t%8d\t%14d\n",p[i].p_id,p[i].etime,p[i].tatime);
line(60);
printf("Avg waiting time:\t %2f\n",awtime);
printf("Avg turnaround time:\t %2f\n",atatime);
line(60);
return(0);
}
Input:

Priority Scheduling
-------------------------------------------------------
Enter the no of processes : 3

Enter the execution time of process0 : 5


Enter the priority of process0 : 6
Enter the execution time of process1: 3
Enter the priority of process1: 5
Enter the execution time of processes2: 1
Enter the priority of process2: 3
Output:
Schedule
------------------------------------------------------------
Process execution waiting turnaround
Id No time time time
------------------------------------------------------------
1 3 3 1073834212
2 1 4 1073834212
0 5 9 1073834212
------------------------------------------------------------
Avg waiting time: 5.333333
Avg turnaround time: 16.000000
------------------------------------------------------------

RESULT: Thus the Program was implemented and output was verified successfully

18
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex.No: 5
INTERPROCESS COMMUNICATION USING SHARED MEMORY

AIM:
To implement Inter Process Communication using Message queue.

Program:

SHARED MEMORY –SENDING

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#define SHMKEY 75
#define K 1024
int main()
{
int shmid;
char *addr1;
printf("\n\t\tSENDING---USING SHARED MEMORY\n");
printf("\t\t`````````````````````````````````");
shmid=shmget(SHMKEY,128*K,IPC_CREAT|0777);
addr1=shmat(shmid,0,0);
printf("\n the address is:0x%x\n",addr1);
printf("\n enter the message to send:");
scanf("%s",addr1);
return(0);
}

SHARED MEMORY –RECEIVING

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#define SHMKEY 75
#define K 1024
int main()
{
int shmid;
char *addr1;
printf("\n\t\treceiving--USING SHARED MEMORY\n");
printf("\t\t`````````````````````````````````");
shmid=shmget(SHMKEY,128*K,IPC_CREAT|0777);
addr1=shmat(shmid,0,0);
printf("\n the address is:0x%x\n",addr1);
printf("\n the received message is:%s\n",addr1);
return(0);
}

19
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

OUTPUT:

SENDING – USING SHARED MEMORY

The address is : 0xf6fc7000

Enter the message to send : ME

RECEIVING – USING SHARED MEMORY

the address is : 0xf6fc7000

The received message is : ME

RESULT: The IPC using Shared memory segment is implemented and verified successfully.

20
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex.No: 6
IMPLEMENTATION OF BANKER’S ALGORITHM

Aim: To write a C program to implement Bankers Algorithm.


Program:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,z;
int res[10];
int resource,process,boolean=0;
int allocation[10][10],sel[10],max[10][10],need[10][10],work[10],work1[10];
int check=0;
int adpro;
int ar[10];
printf("Welcome to Bankers Algorithms");
printf("\n Enter the no .of processes:");
scanf("%d",&process);
printf("\n Enter the number of Resources");
scanf("%d",&resource);
for(i=0;i<resource;i++)
{
printf("Enter the instances of Resource %d:",i+1);
scanf("%d",&res[i]);
}
for(i=0;i<process;i++)
{
printf("\n Enter allocated resources for process %d:",i+1);
for(j=0;j<resource;j++)
{
printf("\n Resource %d:",j+1);
scanf("%d",&allocation[i][j]);
}
}
for(i=0;i<10;i++)
sel[i]=-1;
for(i=0;i<process;i++)
{
printf("Enter maximum need of process: %d",i+1);
for(j=0;j<resource;j++)
{
printf("\n Resource %d:",j+1);
scanf("%d",&max[i][j]);
}
}
for(i=0;i<process;i++)
for(j=0;j<resource;j++)
need[i][j]=max[i][j]-allocation[i][j];
21
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

printf("\n The Need is \n");


for(i=0;i<process;i++)
{
for(j=0;j<resource;j++)
printf("\t%d",need[i][j]);
printf("\n");
}
printf("\n The Avalilable is ");
for(i=0;i<resource;i++)
{
work[i]=0;
for(j=0;j<process;j++)
work[i]=work[i]+allocation[j][i];
work[i]=res[i]-work[i];
}
for(z=0;z<process;z++)
{
for(i=0;i<process;i++)
{
for(j=0;j<resource;j++)
{
if(work[j]>=need[i][j]&&sel[i]==-1)
{
boolean=1;
}
else
{
boolean=0;
break;
}
}
if(boolean==1)
{
sel[i]=1;
for(j=0;j<resource;j++)
{
work[j]=work[j]+allocation[i][j];
}
}
}
}
if(check==1)
printf("\n System is in Unsafe mode");
else
printf("\n System is in safe mode");
}

Input:

Enter the no .of processes:5


Enter the number of Resources3

22
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Enter the instances of Resource 1:10


Enter the instances of Resource 2:5
Enter the instances of Resource 3:7
Enter allocated resources for process 1:
Resource 1:0
Resource 2:1
Resource 3:0
Enter allocated resources for process 2:
Resource 1:2
Resource 2:0
Resource 3:0
Enter allocated resources for process 3:
Resource 1:3
Resource 2:0
Resource 3:2
Enter allocated resources for process 4:
Resource 1:2
Resource 2:1
Resource 3:1
Enter allocated resources for process 5:
Resource 1:0
Resource 2:0
Resource 3:2
Enter maximum need of process: 2
Resource 1:3
Resource 2:2
Resource 3:2
Enter maximum need of process: 3
Resource 1:9
Resource 2:0
Resource 3:2
Enter maximum need of process: 4
Resource 1:2
Resource 2:2
Resource 3:2
Enter maximum need of process: 5
Resource 1:4
Resource 2:3
Resource 3:3

Output:
The Need is
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1
The Available is 3 3 2
System is in safe mode
Result: The Banker’s Algorithm was implemented and verified successfully.
23
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex.No: 7
IMPLEMENTAION OF DINING PHILOSOPHER’S PROBLEM

Aim: Write a C program to implement Dining Philosophers problem.


Program:
#include<stdio.h>
char state[10],self[10],spoon[10];
void test(int k)
{
if((state[(k+4)%5]!='e')&&(state[k]=='h')&&(state[(k+1)%5]!='e'))
{
state[k]='e';
self[k]='s';
spoon[k]='n';
spoon[(k+4)%5]='n';
}
}
void pickup(int i)
{
state[i]='h';
test(i);
if(state[i]=='h')
{
self[i]='w';
}
}
void putdown(int i)
{
state[i]='t';
spoon[i]='s';
spoon[i-1]='s';
test((i+4)%5);
test((i+1)%5);
}

int main()
{
int ch,a,n,i;
printf("\t\t Dining Philosopher Problem\n");
for(i=0;i<5;i++)
{
state[i]='t';
self[i]='s';
spoon[i]='s';
}
printf("\t\t Initial State of Each Philosopher\n");
printf("\n\t Philosopher No.\t Think/Eat \tStatus \tspoon");
for(i=0;i<5;i++)
{
24
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

printf("\n\t\t %d\t\t%c\t\t%c\t%c\n",i+1,state[i],self[i],spoon[i]);
}
printf("\n 1.Exit \n 2.Hungry\n3.Thinking\n");
printf("\n Enter your choice\n");
printf("\t\t");
scanf("%d",&ch);
while(ch!=1)
{
switch(ch)
{
case 2:
{
printf("\n\t Enter which philosopher is hungry\n");
printf("\t\t");
scanf("%d",&n);
n=n-1;
pickup(n);
break;
}
case 3:
{
printf("\n\t Enter which philosopher is thinking\n");
printf("\t\t");
scanf("%d",&n);
n=n-1;
putdown(n);
break;
}
}
printf("\n\t State of Each philosopher\n\n");
printf("\n\t Philosoper No.\t Thinking\t Hungry");
for(i=0;i<5;i++)
{
printf("\n\t\t %d\t\t%c\t\t%c\t%c\n",i+1,state[i],self[i],spoon[i]);
}
printf("\n 1.Exit\n 2.Hungry\n 3.Thinking\n");
printf("\n Enter your choice\n");
printf("\t\t");
scanf("%d",&ch);
}
}
Output:

Initial State of Each Philosopher


Philosopher No. Think/Eat Status spoon
1 t s s
2 t s s
3 t s s
4 t s s
5 t s s

25
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

1.Exit
2.Hungry
3.Thinking
Enter your choice: 2
Enter which philosopher is hungry : 4
State of Each philosopher
Philosopher No. Think/Eat Status spoon
1 t s s
2 t s s
3 t s n
4 e s n
5 t s s
1.Exit
2.Hungry
3.Thinking

Enter your choice 2


Enter which philosopher is hungry : 3
State of Each philosopher
Philosopher No. Think/Eat Status spoon
1 t s s
2 t s s
3 h w n
4 e s n
5 t s s

Result: The Dining philosophers Problem was implemented and verified.

26
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex.No:8(a)
FIRST FIT MEMORY ALLOCATION

Aim: To write a C++ program to implement First Fit Memory allocation technique.

Program:
#include<stdio.h>
#include<string.h>
main()
{
int n,j,i,size[10],sub[10],f[10],m,x,ch,t;
int cho;
printf("\t\t MEMORY MANAGEMENT \n");
printf("\t\t =================\n");
printf("\tEnter the total no of blocks: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter the size of blocks: ");
scanf("%d",&size[i]);
}
cho=0;
while(cho==0)
{
printf("\n Enter the size of the file: ");
scanf("%d",&m);
x=0;
for(i=1;i<=n;i++)
{
if(size[i]>=m)
{
printf("\n size can occupy %d",size[i]);
size[i]-=m;
x=i;
break;
}
}
if(x==0)
{
printf("\n\nBlock can't occupy\n\n");
}
printf("\n\nSNO\t\tAvailable block list\n") ;
for(i=1;i<=n;i++)
printf("\n\n%d\t\t\t%d",i,size[i]);
printf("\n\n Do u want to continue.....(0-->yes/1-->no): ");
scanf("%d",&cho);
}
}

27
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Input:

Enter the total no of blocks: 4


Enter the size of blocks: 50
Enter the size of blocks: 20
Enter the size of blocks: 30
Enter the size of blocks: 40
Enter the size of the file: 25

Output:
size can occupy 50
SNO Available block list
1 25
2 20
3 30
4 40
Do u want to continue.....(0-->yes/1-->no): 0
Enter the size of the file: 28
size can occupy 30
SNO Available block list
1 25
2 20
3 2
4 40
Do u want to continue.....(0-->yes/1-->no):0
Enter the size of the file: 32
size can occupy 40
SNO Available block list
1 25
2 20
3 2
4 8
Do u want to continue.....(0-->yes/1-->no): 0

RESULT:
Thus the memory allocation techniques first fit was implemented.

28
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex.No. 8(b)
BEST FIT MEMORY ALLOCATION

Aim: To write a C program to implement Best Fit Memory allocation technique.


Program:

#include<stdio.h>
main()
{
int n,j,i,size[10],sub[10],f[10],m,x,ch,t;
int cho;

printf("\t\t MEMORY MANAGEMENT \n");


printf("\t\t =================\n");
printf("\tENTER THE TOTAL NO OF blocks : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter the size of blocks : ");
scanf("%d",&size[i]);
}
cho=0;
while(cho==0)
{
printf("\n Enter the size of the file : ");
scanf("%d",&m);
for(i=1;i<=n;i++)
{
sub[i]=size[i];
f[i]=i;
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
if(sub[i]>sub[j])
{
t=sub[i];
sub[i]=sub[j];
sub[j]=t;
t=f[i];f[i]=f[j];
f[j]=t;
}
}
for(i=1;i<=n;i++)
{
if(size[f[i]]>=m)
{
printf("size can occupy %d : ",size[f[i]]);
size[f[i]]-=m;
x=i;
break;
29
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

}
}

if(x==0)
{
printf("block can't occupy");
}
printf("\n\nSNO\t\t Available Block size\n") ;
for(i=1;i<=n;i++)
printf("\n%d\t\t%d",i,size[i]);
printf("\n\n Do u want to continue.....(0-->yes\t/1-->no)");
scanf("%d",&cho);
}

Input:

ENTER THE TOTAL NO OF blocks : 4


Enter the size of blocks : 50
Enter the size of blocks : 100
Enter the size of blocks : 200
Enter the size of blocks : 150
Enter the size of the file : 95

Output:
size can occupy 100 :
SNO SIZE
1 50
2 5
3 200
4 150
Do u want to continue.....(0-->yes /1-->no)0

Enter the size of the file : 48


size can occupy 50 :
SNO Available Block list
1 2
2 5
3 200
4 150

Do u want to continue.....(0-->yes /1-->no)1

30
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

RESULT:
Thus the best fit memory allocation technique was implemented.

31
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex.No. 8(c)
WORST FIT MEMORY ALLOCATION

Aim: To write a C program to implement Worst Fit Memory allocation technique.

Program:

#include<stdio.h>
int p[10]={0},m=0,x=0,b[10]={0},a[10]={0};
main()
{
int j=0,n=0,pra[10]={0},pro[10]={0},z[10],ch,flag,flag2,sum=0,sum2=0;
int c=0,i=0,k=0;
printf("\n\t\tMEMORY MANAGEMENT POLICIES\n");
printf("\n enter the no of process:\t");
scanf("%d",&n);
printf("\n enter the no of partition:\t");
scanf("%d",&m);
printf("\nprocess information\n");
for(i=0;i<n;i++)
{
printf("\n enter the memory required for process P%d:",i+1);
scanf("\t%d",&a[i]);
pro[i]=a[i];
}
printf("\n memory partition information\n");
for(j=0;j<m;j++)
{
printf("\n enter the block size of block B%d:",j+1);
scanf("\t%d",&p[j]);
pra[j]=p[j];
}

arrange();
printf("\n process partition\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i]<p[j])
{
printf("%d\t%d\n",a[i],p[j]);
p[j]=0;
flag=i;
break;
}
}
if(flag!=i)
printf("%d\t%s\n",a[i],"waiting");
arrange();
32
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

}
}

arrange()
{
int i,j,t;
for(i=0;i<m-1;i++)
for(j=0;j<m-i-1;j++)
{
if(p[j]<p[j+1])
{
t=p[j+1];
p[j+1]=p[j];
p[j]=t;
}
}
}
Input:

MEMORY MANAGEMENT POLICIES


enter the no of process: 5
enter the no of partition: 5
process information
enter the memory required for process P1:212
enter the memory required for process P2:417
enter the memory required for process P3:112
enter the memory required for process P4:321
enter the memory required for process P5:460
memory partition information
enter the block size of block B1:400
enter the block size of block B2:300
enter the block size of block B3:500
enter the block size of block B4:200
enter the block size of block B5:600

Output:
Process partition
212 600
417 500
112 400
321 waiting
460 waiting

RESULT:
Thus the Worst fit memory allocation technique was implemented.

33
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex.No: 9(a)
FIRST IN FIRST OUT PAGE REPLACEMENT

Aim: To write a C Program to implement FIFO page replacement algorithm.

Program:
#include<stdio.h>
main()
{
int main_mem,cur=0,i=0,j,fault=0;
static int page[100],page_mem[100],flag,num;
for(i=0;i<100;i++)
page_mem[i]=-2;
printf("\n\t\t\t\t paging->fifo\n");
printf("\n\n Enter the number of pages in main memory ");
scanf("%d",&main_mem);
printf("\n enter no of page references");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\n Enter page reference:");
scanf("%d",&page[i]);
}
printf("\n\t\t\t\t Fifo-> paging \n\n\n");
for(i=0;i<main_mem;i++)
printf("\t page %d",i+1);
for(i=0;i<num;i++)
{
for(j=0;j<main_mem;j++)
if(page[i]==page_mem[j])
{
flag=1;
break;
}
if(!flag)
{
page_mem[cur]=page[i];
fault++;
}
printf("\n\n");
for(j=0;j<main_mem;j++)
printf("\t%d",page_mem[j]);
if(!flag&&cur<main_mem-1)
cur++;
else if(!flag)

34
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

cur=0;
flag=0;
}
printf("\n\n-2 refers to empty blocks\n\n")
printf("\n\n No of page faults:%d\n",fault);
}

Input:
Enter the number of pages in main memory: 3
enter no of page references: 8
Enter page reference: 2
Enter page reference: 0
Enter page reference: 3
Enter page reference: 0
Enter page reference: 2
Enter page reference: 3
Enter page reference: 5
Enter page reference: 9

Output:
FIFO - PAGING
page 1 page 2 page 3
2 -2 -2
2 0 -2
2 0 3
2 0 3
2 0 3
2 0 3
5 0 3
5 9 3

-2 refers to empty blocks

No of page faults: 5

35
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

RESULT:
Thus the FIFO page replacement algorithm was implemented.

36
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex.No: 9(b)
LEAST RECENTLY USED PAGE REPLACEMENT

Aim: To write a C Program to implement LRU page replacement algorithm.

Programs:

#include<stdio.h>
#include<stdlib.h>
#define max 100
int frame[10],count[10],cstr[max],tot,nof,fault;
main()
{
getdata();
push();
}
getdata()
{
int pno,i=0;
printf("\n\t\tL R U - Page Replacement Algorithm\n");
printf("\nEnter No. of Pages in main memory:");
scanf("%d",&nof);
printf("\nEnter the no of page references:\n");
scanf("%d",&pno);
for(i=0;i<pno;i++)
{printf("Enter page reference%d:",i);
scanf("%d",&cstr[i]);
}
tot=i;
for(i=0;i<nof;i++)
printf("\tpage%d\t",i);
}
push()
{
int x,i,j,k,flag=0,fault=0,nc=0,mark=0,maximum,maxpos=-1;
for(i=0;i<nof;i++)
{
frame[i]=-1;
count[i]=mark--;
}
for(i=0;i<tot;i++)
{
flag=0;
x=cstr[i];
nc++;
for(j=0; j<nof; j++)
{
for(k=0; k<nof;k++)
count[k]++;
37
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

if(frame[j]==x)
{
flag=1;
count[j]=1;
break;
}
}
if(flag==0)
{
maximum = 0;
for(k=0;k<nof;k++)
{
if(count[k]>maximum && nc>nof)
{
maximum=count[k];
maxpos = k;
}
}
if(nc>nof)
{
frame[maxpos]=x;
count[maxpos]=1;
}
else
frame[nc-1]=x;
fault++;
dis();
}
}
printf("\nTotal Page Faults :%d",fault);
}
dis()
{
int i=0;
printf("\n\n");
while(i<nof)
{
printf("\t%d\t",frame[i]);
i++;
}
}

Input:

Enter the number of pages in main memory: 3

Enter the no of page references: 8


Enter page reference0: 0
Enter page reference1: 1
Enter page reference2: 2
38
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Enter page reference3: 1


Enter page reference4: 2
Enter page reference5: 5
Enter page reference6: 0
Enter page reference7: 1

Output:
Page 0 page 1 page 2
0 -1 -1
0 1 -1
0 1 2
5 1 2
5 0 2
5 0 1

Total Page Faults : 6

Result:
Thus the page replacement algorithm LRU was implemented.

39
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex.No:10 IMPLEMENTATION OF PAGING

AIM:
To implement paging concept.

PROGRAM:
#include<stdio.h>
Structptable
{
Intpno, staddr;
} p[50];
Structpcount
{
Intpnum;
Char data[50];
}pg[50][50];
Main()
{
Intn,psize,I,j,k,st,pagenum,disp,phyaddr,laddr,found=0;
Printf(“\n Memory in formation ”);
Printf(“\n Enter the no of pages: ”);
Scanf(“%d”,&n);
Printf(“\n Enter page size: ”);
Scanf(“%d”,&psize);
For(i=1;i<=n;i++)
{
Printf(“\n Enter the starting address for page[%d] ”,i);
Scanf(“%d”,&st);
For(j=1;j<=psize;j++)
{
Printf(“page[%d] \t data”,st);
Scanf(“%s”,&pg[i][j],data);
Pg[i][j].pnum=st;
St=st+1;
}
}
Printf(“\n memory \n”);
For(i=1;i<=n;i++)
{
For(j=1;j<=psize;j++)
{
Printf(“\n page[%d] \t data %s”,pg[i][j].pnum,pg[i][j].data);
}
}
Printf(“\n Page table details \n”);
Printf(“\n Enter the starting address for the page:”);
For(k=1;k<=n;k++)
{
Printf(“page[%d] \t starting address:\n”,k);
P(i<pno)=k;
Scanf(“%d”,&p(k),staddr);
40
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

}
Printf(“\n page number \t starting address \n”);
For(k=1;k<=n;k++)
{
Printf(“\n %d \t \t %d \n”,p(k).pno,p(k).staddr);
}
Printf(“\n Enter the logical address:”);
Scanf(“%d”,&laddr);
Page num=laddr\100;
Disp=laddr%100;
Printf(“\n pagenum; %d \t disp; %d ”,pagenum,disp);
For(k=1;k<=n;k++)
If(p(k).pno=pagenum)
{
Phyaddr=p(k).staddr+disp;
Found=1;
Break; }
If(found==1)
{
Printf(“\n physical address:%d ”,phyaddr);
For(j=1;j<=psize;j++)
{
If(pg[pagenum][j].pnum==phyaddr)
Printf(“\n data %s \n”,pg[pagenum][j].data);
}}
Else
Printf(“\n not found \n”); }

41
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

OUTPUT:
Memory in formation
Enter the no of pages:2
Enter page size:2
Enter the starting address for page(1):100
Page(100)data:df
Page(101)data:dsf
Enter the starting address for page(2):200
Page(200)data:dskf
Page(201)data:maan
Memory
Page(100)data:df
Page(101)data:dsf
Page(200)data:dskf
Page(201)data:maan
Page table details
Enter the starting address for the page:
Page[1] starting address:100
Page[2] starting address:200
Page number Starting address
1 100
2 200

Enter the logical address:203


Page num:2 disp:3
Physical address:203

RESULT:

Thus the concept of paging was implemented.

42
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex.No:11 IMPLEMENTATION OF SEGMENTATION

AIM:
To implement segmentation concept .

PROGRAM:
#include<stdio.h>
Structptable
{
Intpno, staddr;
} p[50];
Structpcount
{
Intpnum;
Char data[50];
}pg[50][50];
Main()
{
Intn,psize,I,j,k,st,pagenum,disp,phyaddr,laddr,found=0;
Printf(“\n Memory in formation ”);
Printf(“\n Enter the no of pages: ”);
Scanf(“%d”,&n);
Printf(“\n Enter page size: ”);
Scanf(“%d”,&psize);
For(i=1;i<=n;i++)
{
Printf(“\n Enter the starting address for page[%d] ”,i);
Scanf(“%d”,&st);
For(j=1;j<=psize;j++)
{
Printf(“page[%d] \t data”,st);
Scanf(“%s”,&pg[i][j],data);
Pg[i][j].pnum=st;
St=st+1;
}
}
Printf(“\n memory \n”);
For(i=1;i<=n;i++)
{
For(j=1;j<=psize;j++)
{
Printf(“\n page[%d] \t data %s”,pg[i][j].pnum,pg[i][j].data);
}
}
Printf(“\n Page table details \n”);
Printf(“\n Enter the starting address for the page:”);
For(k=1;k<=n;k++)
{
Printf(“page[%d] \t starting address:\n”,k);
P(i<pno)=k;
Scanf(“%d”,&p(k),staddr);
43
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

}
Printf(“\n page number \t starting address \n”);
For(k=1;k<=n;k++)
{
Printf(“\n %d \t \t %d \n”,p(k).pno,p(k).staddr);
}
Printf(“\n Enter the logical address:”);
Scanf(“%d”,&laddr);
Page num=laddr\100;
Disp=laddr%100;
Printf(“\n pagenum; %d \t disp; %d ”,pagenum,disp);
For(k=1;k<=n;k++)
If(p(k).pno=pagenum)
{
Phyaddr=p(k).staddr+disp;
Found=1;
Break; }
If(found==1)
{
Printf(“\n physical address:%d ”,phyaddr);
For(j=1;j<=psize;j++)
{
If(pg[pagenum][j].pnum==phyaddr)
Printf(“\n data %s \n”,pg[pagenum][j].data);
}}
Else
Printf(“\n not found \n”); }

44
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

OUTPUT:
memory information
----------------------------
Enter the no of segments:2
Enter starting address and limit of the segment(1): 100 2
Segment(100) data: maan
Segment(101) data:ford
Enter starting address and limit of the segment(2): 200 3
Segment(200) data: raj
Segment(201) data: srini
Segment(202) data: data
Memory address data
100 maan
101 ford
200 raj
201 srini
202 data
Segmentation table details
----------------------------------
Segment starting address length
1 100 2
2 200 3
Enter logical address:100
Segment no:1
Disp 0

RESULT:

Thus the concept of segmentation was implemented.

45
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Ex.No:12 IMPLEMENTATION OF FILE SYSTEMS

Aim
To implement file allocation on free disk space in a contiguous manner.

Program
/* Contiguous Allocation - cntalloc.c */
#include <stdio.h>
#include <string.h>
int num=0, length[10], start[10];
char fid[20][4], a[20][4];
void directory()
{
int i;
printf("\nFile Start Length\n");
for(i=0; i<num; i++)
printf("%-4s %3d %6d\n",fid[i],start[i],length[i]);
}
void display()
{
int i;
for(i=0; i<20; i++)
printf("%4d",i);
printf("\n");
for(i=0;i<20;i++)
printf("%4s", a[i]);
}
main()
{
int i,n,k,temp,st,l,ch,flag;
char id[4];
for(i=0; i<20; i++)
strcpy(a[i],"");
printf("Disk space before allocation:\n");
display();
do
{
printf("\nEnter File name (max 3 char) : ");
scanf("%s", id);
printf("Enter start block : ");
scanf("%d", &st);
printf("Enter no. of blocks : ");
scanf("%d", &l);

46
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

strcpy(fid[num],id);
length[num]=l;
flag = 0;
CS2257 Operating System Lab
if((st+l) > 20)
{
printf("Requirement exceeds range\n");
continue;
}
for(i=st; i<(st+l); i++)
if(strcmp(a[i],"") != 0)
flag = 1;
if (flag == 1)
{
printf("Contiguous allocation not possible.\n");
continue;
}
start[num] = st;
for(i=st; i<(st+l); i++)
strcpy(a[i],id);;
printf("Allocation done\n");
num++;
printf("\nAny more allocation (1. yes / 2. no)? : ");
scanf("%d",&ch);
} while (ch == 1);
printf("\n\t\t\tContiguous Allocation\n");
printf("Directory:");
directory();
printf("\nDisk space after allocation:\n");
display();
printf("\n");
}

Output
$ gcc cntalloc.c
$ ./a.out
Disk space before allocation:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Enter File name (max 3 char) : ls
Enter start block : 3
Enter no. of blocks : 4
Allocation done
Any more allocation (1. yes / 2. no)? : 1
Enter File name (max 3 char) : cp
Enter start block : 14
Enter no. of blocks : 3
Allocation done
Any more allocation (1. yes / 2. no)? : 1
Enter File name (max 3 char) : tr
47
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204

Enter start block : 18


Enter no. of blocks : 3
Requirement exceeds range
Enter File name (max 3 char) : tr
Enter start block : 10
Enter no. of blocks : 3
Allocation done
Any more allocation (1. yes / 2. no)? : 1
Enter File name (max 3 char) : mv
Enter start block : 0
Enter no. of blocks : 2
Allocation done
Any more allocation (1. yes / 2. no)? : 1
Enter File name (max 3 char) : ps
Enter start block : 12
Enter no. of blocks : 3
Contiguous allocation not possible.
Enter File name (max 3 char) : ps
Enter start block : 7
Enter no. of blocks : 3
Allocation done
Any more allocation (1. yes / 2. no)? : 2
Contiguous Allocation
Directory:
File Start Length
ls 3 4
cp 14 3
tr 10 3
mv 0 2
ps 7 3
Disk space after allocation:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
mv mv ls ls ls ls ps ps ps tr tr tr cp cp cp

Result
Thus contiguous allocation is done for files with the available free blocks.

48

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy