Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 98

CS8461-Operating Systems Lab Department of CSE 2020-2021

You Choose, We Do it
St. JOSEPH’S COLLEGE OF ENGINEERING
(An Autonomous Institution)

St. Joseph’s Group of Institutions


Jeppiaar Educational Trust
OMR, Chennai-119

COMPUTER SCIENCE & ENGINEERING


LAB RECORD

Name: KAARTHIK N

Year: 2020-21 Semester: IVth


Branch: BE-CSE

Roll No: 19CS220 RegNo: 312319104061

St.Joseph’s College of Engineering 1


CS8461-Operating Systems Lab Department of CSE 2020-2021

You Choose, We Do it

St. JOSEPH’S COLLEGE OF ENGINEERING


St. Joseph’s Group of Institutions
OMR, CHENNAI-119

BONAFIDE CERTIFICATE

This is a certified bonafide record book of KAARTHIK N

Register No: 312319104061 Submitted for the University Practical

Examinations held on _____________in Database Management System

Laboratory during the year 2020/ 2021

LAB IN – CHARGE

INTERNAL EXAMINER EXTERNAL EXAMINER


St.Joseph’s College of Engineering 2
CS8461-Operating Systems Lab Department of CSE 2020-2021

St.Joseph’s College of Engineering 3


CS8461-Operating Systems Lab Department of CSE 2020-2021

You Choose, We Do it
St. JOSEPH'S COLLEGE OF ENGINEERING
St. Joseph’s Group of Institutions
OMR, Chennai – 600 119
CS8394 – OPERATING SYSTEMS LABORATORY

INDEX
Name of the Student: KAARTHIK N Register Number:312319104061

St.Joseph’s College of Engineering 4


CS8461-Operating Systems Lab Department of CSE 2020-2021

Name of the Lab : Staff Name: Dr. G.MURUGESAN


OPERATING SYSTEMS LABORATORY
Code : CS8394

Exp. Date of Date of Signature


Name of the Experiment Remarks
No. Experiment Submission with Date

1 Basics of UNIX commands 25-02-21 04-03-21

2 Implementation of UNIX system calls 04-03-21 11-03-21

3 SIMULATION OF UNIX COMMANDS 11-03-21 18-03-21

4 SHELL PROGRAMMING 18-03-21 25-03-21

5 ROUND ROBIN SCHEDULING 25-03-21 01-04-21

6 IMPLEMENTATION OF SEMAPHORES 01-04-21 08-04-21

7 IMPLEMENTATION OF SHARED MEMORY AND IPC 08-04-21 15-04-21

8 BANKERS ALGORITHM FOR DEAD LOCK


15-04-21 22-04-21
AVOIDANCE

9 IMPLEMENTATION OF DEAD LOCK DETECTION 22-04-21 29-04-21

10 IMPLEMENTATION OF THREADING &


29-04-21 03-05-21
SYNCHRONIZATION

11 FIRST,WORST,BEST FIT MEMORY ALLOCATION 03-05-21 06-05-21

12
IMPLEMENTATION OF PAGING TECHNIQUE 06-05-21 10-05-21

13 PAGE REPLACEMENT ALGORITHMS 06-05-21 10-05-21


SINGLE,TWO,HIERARCHICAL&DAG LEVEL
14 DIRECTORY 10-05-21 13-05-21

15 FILE ALLOCATION TECHNIQUES 13-05-21 15-05-21

16 MEMORY MANAGEMENT USING SEGMENTATION 13-05-21 15-05-21

St.Joseph’s College of Engineering 5


CS8461-Operating Systems Lab Department of CSE 2020-2021

Ex No: 1 Basics of UNIX commands.

1. Calendar Command:
kaart@LAPTOP-UFVFSEV2 ~
$ cal 11 2016
November 2016
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 22 29 30 31

2. Date command:
kaart@LAPTOP-UFVFSEV2 ~
$ date
Wed Oct 21 12:45:25 IST 2016
[Anu@cse clab]$ date +%m
10

3.Who command:
kaart@LAPTOP-UFVFSEV2 ~
$ who
Anu tty5 2014-10-21 12:50
Anu pts/1 2014-10-21 10:50 (197.168.1.5)
karthi pts/2 2014-10-21 10:51 (197.168.1.6)

4.Who am I command:

kaart@LAPTOP-UFVFSEV2 ~
$ whoami
Anu

5.Finger Command:
kaart@LAPTOP-UFVFSEV2 ~
$ finger
Login Name Tty Idle Login Time Office Office Phone
karthi pts/2 29 Oct 22 12:53 (197.168.1.6)
Anu tty5 31 Oct 22 12:52
Anu pts/1 Oct 22 12:55 (197.168.1.5)

6.Help command:
To display the help information about a particular command.
kaart@LAPTOP-UFVFSEV2 ~
$ man clear
clear - clear the terminal screen

7.Clear Command:
To clear the screen.
kaart@LAPTOP-UFVFSEV2 ~
$ clear

8. ID command:
To display the user id and group id information about a user. i.e. user identity.
kaart@LAPTOP-UFVFSEV2 ~
$ id

St.Joseph’s College of Engineering 6


CS8461-Operating Systems Lab Department of CSE 2020-2021

uid=835(Anu) gid=835(Anu) groups=835(Anu) context=user_u:system_r:unconfined_t


9. write command:
Send message to another user.
kaart@LAPTOP-UFVFSEV2 ~
$ [Anu@cse clab]$ write Anu
hru
10. echo command:
Display a line of text.
kaart@LAPTOP-UFVFSEV2 ~
$ echo SJCE
SJCE

11. tty command:


print the file name of the terminal connected to standard input

kaart@LAPTOP-UFVFSEV2 ~
$ tty
/dev/pts/5
12. Changing Password:

kaart@LAPTOP-UFVFSEV2 ~
$ passwd - update a user’s authentication tokens(s)
Changing password for user Anu.
Changing password for Anu
(current) UNIX password:
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

FILE MANIPULATION COMMANDS


1.Create a file:

kaart@LAPTOP-UFVFSEV2 ~
$ vi f1.c
kaart@LAPTOP-UFVFSEV2 ~
$ vi f2.c

2.Head Command:
Displays the initial part of the text file. By default, it displays the first 10 lines of a file.

kaart@LAPTOP-UFVFSEV2 ~
$ head -1 f3.txt
Hi this is Unix Lab

3.Tail command:
Displays the last part of he text file. By default, it displays the last 10 lines of a file.
kaart@LAPTOP-UFVFSEV2 ~
$ ail -1 f3.txt
I am studying B.Tech IT in SJCE.

4.List information about files and directories


kaart@LAPTOP-UFVFSEV2 ~
$ ls
f1.c f2.c f3.txt f4.txt Anual

5.Copy a file:

St.Joseph’s College of Engineering 7


CS8461-Operating Systems Lab Department of CSE 2020-2021

kaart@LAPTOP-UFVFSEV2 ~
$ cp f1.c f5.c
kaart@LAPTOP-UFVFSEV2 ~
$ ls
f1.c f2.c f3.txt f4.txt f5.c Anual
6.Move or rename a file:
kaart@LAPTOP-UFVFSEV2 ~
$ mv f1.c f4.c
kaart@LAPTOP-UFVFSEV2 ~
$ ls
f2.c f3.txt f4.txt f5.c f4.c Anual

7.Remove a file:
kaart@LAPTOP-UFVFSEV2 ~
$ rm f3.txt
kaart@LAPTOP-UFVFSEV2 ~
$ ls
f2.c f4.txt f5.c f4.c Anual
kaart@LAPTOP-UFVFSEV2 ~
$
8.Change the access permission of a file:
kaart@LAPTOP-UFVFSEV2 ~
$ chmod 777 f2.c
-rwxrwxrwx 1 Anu Anu 52 Oct 29 11:31 f2.c

9.Display the contents of a file onto screen:


kaart@LAPTOP-UFVFSEV2 ~
$ cat f4.c
India is good country

10.Word count command:


It is used to count the number of lines,words and characters in a file.
kaart@LAPTOP-UFVFSEV2 ~
$ wc f1.txt
8 26 150 f4.txt

11.Find Command:
It is used to locate files in a directory and its subdirectories.
kaart@LAPTOP-UFVFSEV2 ~
$ find
.
./1.c
./asd.bak
./f1.txt
./file_copy.c
./f2.c
./a.out

12. Comparing files:


compare two files
kaart@LAPTOP-UFVFSEV2 ~
$ cmp f1 f2 -
f1 f2 differ: byte 3, line 2

13. Differentiating Files:


find differences between two files
kaart@LAPTOP-UFVFSEV2 ~

St.Joseph’s College of Engineering 8


CS8461-Operating Systems Lab Department of CSE 2020-2021

$ diff f1 f2
3c2
< I am
---
> IT

14. Touch Command:


change file timestamps
kaart@LAPTOP-UFVFSEV2 ~
$ ls -l f2
-rw-rw-r-- 1 Anu Anu 6 Oct 29 09:50 f2

kaart@LAPTOP-UFVFSEV2 ~
$ touch f2
kaart@LAPTOP-UFVFSEV2 ~
$ ls -l f2
-rw-rw-r-- 1 Anu Anu 6 Oct 29 09:53 f2

DIRECTORY COMMANDS
1. Create a directory:
kaart@LAPTOP-UFVFSEV2 ~
$ mkdir chem.

2.List all directories (including .(dot) entries in the long listing.)


kaart@LAPTOP-UFVFSEV2 ~
$ total 104
drwx------ 4 Anu Anu 4096 Oct 29 11:31 .
drwxr-xr-x 17 root root 4096 Oct 29 11:52 ..
-rw------- 1 Anu Anu 3 Oct 29 10:55 .Anu_history
-rw-r--r-- 1 Anu Anu 24 Oct 29 10:51 .Anu_logout

3. Change from one directory to another:


kaart@LAPTOP-UFVFSEV2 ~
$ cd Anual
4. Display the full path of the current working directory:
kaart@LAPTOP-UFVFSEV2 ~
$ pwd
/home/Anu/Anual.

5. Remove a directory:
rkaart@LAPTOP-UFVFSEV2 ~
$ mdir Anual
kaart@LAPTOP-UFVFSEV2 ~
$ ls
f1.c f2.c f3.txt f4.txt f4.c f5.c

6.Change the owner of a specified file:


Only the super-user can change this.
kaart@LAPTOP-UFVFSEV2 ~
$ chown Anu f2.c

7.Change the group of a file:


Only the super-user can change this. It changes the group-id of the file o a group called ‘Anu’ .
kaart@LAPTOP-UFVFSEV2 ~

St.Joseph’s College of Engineering 9


CS8461-Operating Systems Lab Department of CSE 2020-2021

$ chgrp Anu f2.c


8.Link Command:
make link between files
kaart@LAPTOP-UFVFSEV2 ~
$ ls
f1.c f2.c f4.c f5.c f3.txt f4.txt Anu
kaart@LAPTOP-UFVFSEV2 ~
$ ln -t Anu f5.c
kaart@LAPTOP-UFVFSEV2 ~
$ cd Anu
kaart@LAPTOP-UFVFSEV2 ~
$ ls
F5.c

Ex. No: 2(a)Implementation of UNIX system calls : fork, exec, getpid, exit, wait, close.

AIM

St.Joseph’s College of Engineering 10


CS8461-Operating Systems Lab Department of CSE 2020-2021

To write a program for implementing process management using the following system calls
of UNIX operating system: fork, exec, getpid, exit, wait, close.

ALGORITHM

1. Read the input from the command line.


2. Use fork() system call to create process, getppid() system call used to get the parent
process ID and getpid() system call used to get the current process ID
3. execvp() system call used to execute that command given on that command line
argument
4. execlp() system call used to execute specified command.
5. Open the directory at specified in command line input.
6. Display the directory contents.

PROGRAM

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char * argv[])
{
char * ls_args[3] = { "ls", "-l", NULL} ;
pid_t c_pid, pid;
int status;
c_pid = fork();
if (c_pid == 0)
{
printf("Child: executing ls\n");
execvp( ls_args[0], ls_args);
perror("execve failed");
}
else if (c_pid > 0)
{
if( (pid = wait(&status)) < 0)
{
perror("wait");
_exit(1);
}
printf("Parent: finished\n");
}else
{
perror("fork failed");
_exit(1);
}
return 0;
}

St.Joseph’s College of Engineering 11


CS8461-Operating Systems Lab Department of CSE 2020-2021

OUTPUT

St.Joseph’s College of Engineering 12


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT
Thus the program for implementation of various UNIX system calls was executed successfully

Ex No:2 (b) Implementation of Unix system calls : opendir, readdir.

AIM
To write a program for implementing the following directory system calls of UNIX operating
system: opendir, readdir.

ALGORITHM
1.Open the directory specified in command line input.
2. Display the directory contents.

PROGRAM
#include<sys/types.h>
#include<dirent.h>
#include<stdio.h>

St.Joseph’s College of Engineering 13


CS8461-Operating Systems Lab Department of CSE 2020-2021

main(int c,char* arg[])


{
DIR *d;
struct dirent *r;
d=opendir(arg[1]);
printf("\n\t Name of the Files \n");
while((r=readdir(d)) != NULL)
printf("\t %s \n",r->d_name);
}

OUTPUT

St.Joseph’s College of Engineering 14


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT
Thus the program for implementing directory system calls was successfully executed.

Ex No: 3 SIMULATION OF UNIX COMMANDS

AIM
To write a C program to simulate UNIX commands like ls,cp,grep.

ALGORITHM
1. Create a file using cat command.
2. Simulate ls command to list all the files in current working directory
3. Open the file in read mode and copy the contents of the file.
4. Input the word to be searched in the file.
5. Display the word if searching is successful.

PROGRAM
(i) ls

St.Joseph’s College of Engineering 15


CS8461-Operating Systems Lab Department of CSE 2020-2021

#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
#include <stdio.h>
#define FALSE 0
#define TRUE 1
extern int alphasort();
char pathname[MAXPATHLEN];
main() {
int count,i;
struct dirent **files;
int file_select();
if (getwd(pathname) == NULL )
{
printf("Error getting pathn");
}
printf("Current Working Directory = %sn",pathname);
count = scandir(pathname, &files, file_select, alphasort);
if (count <= 0)
{
printf("No files in this directoryn");
}
printf("Number of files = %dn",count);
for (i=1;i<count;++ i)
printf("%s \n",files[i-1]->d_name);
}
int file_select(struct direct *entry)
{
if ((strcmp(entry->d_name, ".") == 0) ||(strcmp(entry->d_name, "..") == 0))
return (FALSE);
else
return (TRUE);
}

St.Joseph’s College of Engineering 16


CS8461-Operating Systems Lab Department of CSE 2020-2021

OUTPUT

(ii) cp
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
main( int argc,char *argv[] )
{
int i,fd1,fd2;
char *file1,*file2,buf[2];
file1=argv[1];
file2=argv[2];
printf("file1=%s file2=%s",file1,file2);
fd1=open(file1,O_RDONLY,0777);
fd2=creat(file2,0777);
while(i=read(fd1,buf,1)>0)
write(fd2,buf,1);
close(fd1);
close(fd2);
}

St.Joseph’s College of Engineering 17


CS8461-Operating Systems Lab Department of CSE 2020-2021

OUTPUT

(iii) grep
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<string.h>
#include <fcntl.h>
void match_pattern(char *argv[])
{
int fd,r,j=0;
char temp,line[100];
if((fd=open(argv[2],O_RDONLY)) != -1)
{
while((r=read(fd,&temp,sizeof(char)))!= 0)
{
if(temp!='\n')
{
line[j]=temp;
j++;
}
else
{
if(strstr(line,argv[1])!=NULL)
printf("%s\n",line);
memset(line,0,sizeof(line));
j=0;
}

}
}
}

main(int argc,char *argv[])


{

St.Joseph’s College of Engineering 18


CS8461-Operating Systems Lab Department of CSE 2020-2021

struct stat stt;


if(argc==3)
{
if(stat(argv[2],&stt)==0)
match_pattern(argv);
else
{
perror("stat()");
exit(1);
}
}
}

St.Joseph’s College of Engineering 19


CS8461-Operating Systems Lab Department of CSE 2020-2021

OUTPUT:

RESULT
Thus the C program to simulate UNIX commands like ls,cp,grep was executed successfully.

St.Joseph’s College of Engineering 20


CS8461-Operating Systems Lab Department of CSE 2020-2021

Ex No: 4(a ) SHELL PROGRAMMING

LARGEST OF N NUMBERS
AIM:
To write a shell program to find largest of n numbers.

ALGORITHM:
1. Input the total number of elements (n) .
2. Let i=0
3. Read the elements one by one in an array a$i .
4. Initialize large=0 .
5. Using a for loop, do the following for all elements in the array:
Let temp=a$i ie. Assign the array element to temp.
If temp greater than large, then reassign large=temp
6. large displays the largest of all elements

PROGRAM:
echo "Enter the total number of elements"
read n
echo "Enter the elements One by One"
i=0
for((i=0 ; i<$n ; i++))
do
read a$i
done
large=0
for((i=0 ; i<$n ; i++))
do
((temp=a$i))
if (($temp>$large))
then
((large=$temp))
fi
done
echo "The largest of all the elements is: $large"

St.Joseph’s College of Engineering 21


CS8461-Operating Systems Lab Department of CSE 2020-2021

OUTPUT:

RESULT:Thus the program was executed successfully.

Ex No: 4(b) IMPLEMENT UNIX COMMANDS USING CASE

St.Joseph’s College of Engineering 22


CS8461-Operating Systems Lab Department of CSE 2020-2021

AIM:
To write a shell program using case…esac to execute unix commands .

ALGORITHM:
1. Get the choice from the user.
2. List the choices for date command, list of users logged in and name of the home directory.
3. Date command displays today’s date.
4. Who command displays the users who have logged in.
5. $Home command is used to display the home directory.
6. Depending upon the choice, any one of the above command is executed using case…esac.

PROGRAM:
while true
do
echo "Enter your choice:"
echo "Enter w to quit"
echo "Menu"
echo "1.Today's date"
echo "2.List of users logged in"
echo "3.Name of the home directory"
read choice
case $choice in
1) date;;
2) who;;
3) echo "Home directory is $HOME";;
w)break;;
*)echo "Invalid choice"
esac
done

OUTPUT:

St.Joseph’s College of Engineering 23


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT:
Thus the program was executed successfully.

St.Joseph’s College of Engineering 24


CS8461-Operating Systems Lab Department of CSE 2020-2021

EXP-4(c)

PROGRAM:
echo Enter the first string
read str1
echo Enter the second string
read str2
if [ $str1 = $str2 ]
then
echo strings are equal
else
echo strings are unequal
fi

OUTPUT:

St.Joseph’s College of Engineering 25


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT:
Thus the program was executed successfully.

EXP-4(d)

St.Joseph’s College of Engineering 26


CS8461-Operating Systems Lab Department of CSE 2020-2021

PROGRAM:
echo Enter A
read a
echo Enter B
read b
echo Enter C
read c
if [ $a -gt $b -a $a -gt $c ]
then
echo A is greater
elif [ $b -gt $c ]
then
echo B is greater
else
echo C is greater
fi

OUTPUT:

St.Joseph’s College of Engineering 27


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT:
Thus the program was executed successfully.

St.Joseph’s College of Engineering 28


CS8461-Operating Systems Lab Department of CSE 2020-2021

Ex No: 5(a) ROUND ROBIN SCHEDULING

AIM:
To write a ‘C’ program to implement ROUND ROBIN Scheduling.

ALGORITHM:
Step 1: Include the header files for simulating ROUND ROBIN scheduling scheme.
Step 2: Declare the variables to calculate the essential aspects of ROUND ROBIN.
Step 3: Initially get the time slice along with process time and burst time.
Step 4: Start from the 1st process and execute it for the given time slice and move to 2 nd process and so on till all
process gets executed for each of the time slice allotted.
Step 5: Prepare the gantt chart for each process getting executed within the time slice in a round robin fashion.
Step 6: Calculate the waiting time and turn around time along with their averages display the result.
// ROUND ROBIN SCHEDULING ALGORITHM

PROGRAM:
#include<stdio.h>
void main()
{
int i,x=-1,k[10],m=0,n,t,s=0;
int a[50],temp,b[50],p[10],bur[10],bur1[10];
int wat[10],tur[10],ttur=0,twat=0,tres=0,j=0;
float awat,atur,ares;
printf("Enter no. of process :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time for process %d: ",(i+1));
scanf("%d",&bur[i]);
bur1[i]=bur[i];
}
printf("\nEnter the slicing time :");
scanf("%d",&t);
for(i=0;i<n;i++)
{
b[i]=bur[i]/t;
if((bur[i]%t)!=0)
b[i]=b[i]+1;
m=b[i]+m;
}
printf("\nGAANT CHART\n\n");
for(i=0;i<m;i++)
{
printf("--------");
}
printf("\n");
a[0]=0;
while(j<m)
{
if(x==n-1)
x=0;
else
x++;

St.Joseph’s College of Engineering 29


CS8461-Operating Systems Lab Department of CSE 2020-2021

if(bur[x]>=t)
{
bur[x]=bur[x]-t;
a[j+1]=a[j]+t;
if(b[x]==1)
{
p[s]=x;
k[s]=a[j+1];
s++;
}
j++;
b[x]=b[x]-1;
printf(" P%d |",x+1);
}
else if(bur[x]!=0)
{
a[j+1]=a[j]+bur[x];
bur[x]=0;
if(b[x]==1)
{
p[s]=x;
k[s]=a[j+1];
s++;
}
j++;
b[x]=b[x]-1;
printf(" P%d |",x+1);
}
}
printf("\n");
for(i=0;i<m;i++)
printf("--------");
printf("\n");
for(j=0;j<=m;j++)
printf("%d\t",a[j]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i]>p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=k[i];
k[i]=k[j];
k[j]=temp;
} } }
for(i=0;i<n;i++)
{
wat[i]=k[i]-bur1[i];
tur[i]=k[i];
}

St.Joseph’s College of Engineering 30


CS8461-Operating Systems Lab Department of CSE 2020-2021

printf("\nPROCESS\tBURST TIME \t WAITING TIME \t TURN AROUND TIME\t");


printf("RESPONSE TIME\n");
for(i=0;i<n;i++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d\n",p[i]+1,bur1[i],wat[i],
tur[i],a[i]);
}
for(i=0;i<n;i++)
{
ttur=ttur+tur[i];
twat=twat+wat[i];
tres=tres+a[i];
}
awat=(float)twat/n;
atur=(float)ttur/n;
ares=(float)tres/n;
printf("\nTotal waiting time:%d",twat);
printf("\nTotal turnaround time:%d",ttur);
// printf("\nTotal response time:%d",tres);
printf("\nAverage waiting time :%.2f",awat);
printf("\nAverage turn around time :%.2f",atur);
//printf("\nAverage response time:%.2f",ares);
}
OUTPUT

St.Joseph’s College of Engineering 31


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT: Thus the program was executed successfully.

St.Joseph’s College of Engineering 32


CS8461-Operating Systems Lab Department of CSE 2020-2021

Ex No: 5(b) SJF SCHEDULING

AIM:
To write a ‘C’ program to implement SJF Scheduling.

ALGORITHM:
Step 1: Include the header files for simulating SJF scheme.
Step 2: Declare the variables to calculate the essential aspects of SJF.
Step 3: Initially get the process time and burst time.
Step 4: Swap all the processes in such a way that they are arranged in shortest job first (SJF) order.
Step 5: Prepare the gantt chart for each process so that their waiting time, turn around time is calculated.
Step 6: Calculate the average waiting time and average turn around time and display the result.
// SJF SCHEDULING ALGORITHM

PROGRAM:
#include<stdio.h>
void main()
{
int i,j,k,n,p[10],br[10],wt[10],tr[10],ttr=0,twt=0,t;
float awat,atur;
printf("\nEnter no. of process :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the burst time for process %d: ",(i+1));
scanf("%d",&br[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(br[i]>br[j])
{
t=br[i];
br[i]=br[j];
br[j]=t;
t=p[i];
p[i]=p[j];
p[j]=t;
}}}
printf("\nPROCESS\tBURST TIME \t WAITING TIME \t TURN AROUND TIME\n");
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i+1]=wt[i]+br[i];
tr[i]=wt[i]+br[i];
}
for(i=0;i<n;i++)
{
ttr=ttr+tr[i];
twt=twt+wt[i];
} for(i=0;i<n;i++) { printf("\n%d\t\t%d\t\t%d\t\t%d\n",p[i],br[i],wt[i],tr[i]);

St.Joseph’s College of Engineering 33


CS8461-Operating Systems Lab Department of CSE 2020-2021

} printf("\nGAANT CHART\n\n");
for(i=0;i<n;i++)
{
printf("--------");
}
printf("\n");
for(i=0;i<n;i++)
{
printf(" P%d |",p[i]);
}
printf("\n");
for(i=0;i<n;i++)
{
printf("--------");
}
printf("\n");
for(i=0;i<=n;i++)
{
printf("%d\t",wt[i]);
}
awat=(float)twt/n;
atur=(float)ttr/n;
printf("\nTotal waiting time :%d\n",twt);
printf("\nTotal turnaround time :%d\n",ttr);
printf("\nAverage waiting time :%.2f\n",awat);
printf("\nAverage turn around time :%.2f\n",atur);
}
OUTPUT:

St.Joseph’s College of Engineering 34


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT:
Thus the program was executed successfully.

St.Joseph’s College of Engineering 35


CS8461-Operating Systems Lab Department of CSE 2020-2021

Ex No: 5(c) FCFS SCHEDULING

AIM:
To write a ‘C’ program to implement FCFS Scheduling.

ALGORITHM:
Step 1: Start.
Step 2: Include the header files for simulating FCFS scheme.
Step 3: Declare the variables to calculate the essential aspects of FCFS.
Step 4: Initially get the process time, arrival time and burst time.
Step 5: Swap all the processes in such a way that they are arranged in FCFS order.
Step 6: Prepare the gantt chart for each process so that their waiting time, turn around time is calculated.
Step 7: Calculate the average waiting time and average turn around time and display the result.
Step 8: Stop

// FCFS SCHEDULING ALGORITHM

PROGRAM:
#include<stdio.h>
void main()
{
int i,n,br[10],wt[10],wat[10],tr[10],ttr=0,twt=0,twat=0;
float awat,atur;
printf("Enter no. of process :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time for process %d: ",(i+1));
scanf("%d",&br[i]);
}
printf("\nPROCESS\tBURST TIME\tWAITING TIME\tTURN AROUND TIME\n");
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i+1]=wt[i]+br[i];
tr[i]=wt[i]+br[i];
printf("\n%d\t\t%d\t\t%d\t\t%d\n",i+1,br[i],wt[i],tr[i]);
}
for(i=0;i<n;i++)
{
ttr=ttr+tr[i];
twat=twat+wt[i];//wat i changed to wt
}
printf("\nGAANT CHART\n\n");
for(i=0;i<n;i++)
{
printf("--------");
}
printf("\n");
for(i=0;i<n;i++)
{
printf(" P%d |",i+1);
}

St.Joseph’s College of Engineering 36


CS8461-Operating Systems Lab Department of CSE 2020-2021

printf("\n");
for(i=0;i<n;i++)
{
printf("--------");
}
printf("\n");
for(i=0;i<=n;i++)
{
printf("%d\t",wt[i]);
}
awat=(float)twat/n;
atur=(float)ttr/n;
printf("\n\nTotal waiting time :%d",twat);
printf("\nTotal turn around time :%d",ttr);
printf("\n\nAverage waiting time :%.2f",awat);
printf("\nAverage turn around time :%.2f\n",atur);
}

St.Joseph’s College of Engineering 37


CS8461-Operating Systems Lab Department of CSE 2020-2021

OUTPUT

RESULT:
Thus the program was executed successfully.
Ex No: 5(d) PRIORITY SCHEDULING

St.Joseph’s College of Engineering 38


CS8461-Operating Systems Lab Department of CSE 2020-2021

AIM:
To write a ‘C’ program to implement PRIORITY Scheduling.

ALGORITHM:
Step 1: Start
Step 2: Include the header files for simulating priority scheme.
Step 3: Declare the variables to calculate the essential aspects of priority scheme.
Step 4: Initially get the process time, arrival time and burst time along with their priorities.
Step 5: Execute the process by referring to their priority values.
Step 6: Prepare the gantt chart for each process so that their waiting time, turn around time is calculated.
Step 7: Calculate the average waiting time and average turn around time and display the result.

PROGRAM:
#include<stdio.h>
void main()
{
int i,j,k,n,p[10],pri[10],bur[10],wat[10], tur[10],ttur=0,twat=0,t;
float awat,atur;
printf("\nEnter no. of process :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the burst time for process %d: ",(i+1));
scanf("%d",&bur[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
{ printf("\nEnter the priority for process %d: ",(i+1));
scanf("%d",&pri[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(pri[i]>pri[j])
{
t=bur[i];
bur[i]=bur[j];
bur[j]=t;
t=p[i];
p[i]=p[j];
p[j]=t;
t=pri[i];
pri[i]=pri[j];
pri[j]=t;
}}}
printf("\nPROCESS\tBURST TIME \t PRIORITY \t WAITING TIME \tTURNAROUND TIME\n");
wat[0]=0;
for(i=0;i<n;i++)
{
wat[i+1]=wat[i]+bur[i];
tur[i]=wat[i]+bur[i];

St.Joseph’s College of Engineering 39


CS8461-Operating Systems Lab Department of CSE 2020-2021

}
for(i=0;i<n;i++)
{ ttur=ttur+tur[i];
twat=twat+wat[i];
}
for(i=0;i<n;i++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d\n",p[i],bur[i],pri[i],wat[i],tur[i]);
}
printf("\nGAANT CHART\n\n");
for(i=0;i<n;i++)
{ printf("--------");
}
printf("\n");
for(i=0;i<n;i++)
{ printf(" P%d |",p[i]);
}
printf("\n");
for(i=0;i<n;i++)
{ printf("--------");
}
printf("\n");
for(i=0;i<=n;i++)
{ printf("%d\t",wat[i]);
}
awat=(float)twat/n;
atur=(float)ttur/n;
printf("\nTotal waiting time :%d\n",twat);
printf("\nTotal turnaround time :%d\n",ttur);
printf("\nAverage waiting time :%.2f\n",awat);
printf("\nAverage turn around time :%.2f\n",atur);
}

OUTPUT

St.Joseph’s College of Engineering 40


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT: Thus the program was executed successfully.

St.Joseph’s College of Engineering 41


CS8461-Operating Systems Lab Department of CSE 2020-2021

Ex No: 6 IMPLEMENTATION OF SEMAPHORES

AIM:
To write a program to implement producer consumer problem using semaphore

ALGORITHM:
Step 1: Declare the buffer size variable.
Step 2: Initialize empty, full and the value of mutex.
Step 3: For case 1 , produce the item till the buffer is full after performing the wait operation
Of variables empty and mutex.
Step 4: Then perform signal of full.
Step 5: For case 2, consume the item till the buffer is empty. Perform the wait and signal operation..
Step 6: print the contents of the buffer.

PROGRAM:
#include<stdio.h> #include<string.h>
#define SIZE 3
struct process
{
char a[10];
}buffer[SIZE];
int mutex=1,full=0,empty=SIZE,f=0;
int main()
{
//int mutex=1,full=0,empty=SIZE,f=0;
int ch,i;
printf("\tProducer Consumer Problem:\n");
while(1)
{
printf("\n The choices are\n");
printf("1.Producer Routine\n2.Consumer Routine\n3.Buffer Contents\n4.Exit\n");
printf("Enter your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
empty=wait(empty);
mutex=wait(mutex);
if(f==0)
{
printf("\nEnter the item to be added:\n");
scanf("%s",&buffer[full]);
full=signal(full);
printf("\nItem Produced Successfully:\n");
}
else
{
printf("\nBuffer is full\n");
f=0;
}
mutex=signal(mutex);
break;
case 2:

St.Joseph’s College of Engineering 42


CS8461-Operating Systems Lab Department of CSE 2020-2021

full=wait(full);
mutex=wait(mutex);
if(f==0)
{
printf("\nOne Item is Consumed:\n");
printf("\nConsumed item is: %s\n",buffer[0].a);
for(i=0;i<SIZE;i++)
strcpy(buffer[i].a,buffer[i+1].a);
empty=signal(empty);
}
else
{
printf("\nBuffer is empty\n");
f=0;
}
mutex=signal(mutex);
break;
case 3:
if(full!=0)
{
for(i=0;i<full;i++)
printf("\n%s\n",buffer[i].a);
}
else
printf("\nBuffer is empty\n");
break;
case 4:
exit(0);
default:
printf("Enter Correct Option");
break;
}}}
int wait(int s)
{
if(s==0)
f=1;
else
s--;
return s;
}
int signal(int s)
{
s++;
return s;
}

OUTPUT

St.Joseph’s College of Engineering 43


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT: Thus the program was executed successfully.

St.Joseph’s College of Engineering 44


CS8461-Operating Systems Lab Department of CSE 2020-2021

Ex No: 7 IMPLEMENTATION OF SHARED MEMORY AND IPC

AIM
To write a C program to illustrate interprocess communication using Shared Memory system calls.

ALGORITHM
1. Start the process.
2. Create shared memory using shmget( ) system call
3. If successfull it returns positive value
4. Attach the created shared memory using shmat( ) systemcall.
5. Write to shared memory using shmsnd( ) system call
6. Read the contents from shared memory using shmrcv( ) systemcall
7. Stop the Process.

PROGRAM
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
int main ()
{
int segment_id;
char* shared_memory;
struct shmid_ds shmbuffer;
int segment_size;
const int shared_segment_size = 0x6400;
segment_id = shmget (IPC_PRIVATE, shared_segment_size,
IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
shared_memory = (char*) shmat (segment_id, 0, 0);
printf ("shared memory attached at address %p\n", shared_memory);
shmctl (segment_id, IPC_STAT, &shmbuffer);
segment_size = shmbuffer.shm_segsz;
printf ("segment size: %d\n", segment_size);
sprintf (shared_memory, "Hello, World");
shmdt (shared_memory);
shared_memory = (char*) shmat (segment_id, (void*) 0x5000000, 0);
printf ("shared memory reattached at address %p\n", shared_memory);
printf ("%s\n", shared_memory);
shmdt (shared_memory);
shmctl (segment_id, IPC_RMID, 0);

St.Joseph’s College of Engineering 45


CS8461-Operating Systems Lab Department of CSE 2020-2021

return 0;
}

OUTPUT

St.Joseph’s College of Engineering 46


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT
Thus the program for Interprocess communication using Shared Memory system calls was
executed successfully.

Ex.No.8 BANKERS ALGORITHM FOR DEAD LOCK AVOIDANCE

St.Joseph’s College of Engineering 47


CS8461-Operating Systems Lab Department of CSE 2020-2021

AIM:

To implement deadlock avoidance by using Banker’s Algorithm.

ALGORITHM:

1. Start the program.


2. Get the values of resources and processes.
3. Get the avail value.
4. After allocation find the need value.
5. Check whether its possible to allocate.
6. If it is possible then the system is in safe state.
7. Else system is not in safety state.
8. If the new request comes then check that the system is in safety or not if we allow the request.

PROGRAM
#include<stdio.h>
#include<process.h>
void main()
{
int allocation[10][5],max[10][5],need[10][5],available[3],flag[10],sq[10];
int n,r,i,j,k,count,count1=0;
printf("\n Input the number of processes running ( <10 )..");
scanf("%d",&n);
for(i=0;i<10;i++)
flag[i]=0;
printf("\n Input the number of resources ( <5 )..");
scanf("%d",&r);
printf("\n Input the allocation matrix for the processes in row major order..\n");
for(i=0;i<n;i++)
{
printf("\n Process %d\n",i);
for(j=0;j<r;j++)
{
printf("\n Resource %d\n",j);
scanf("%d",&allocation[i][j]);
}
}
printf("\n Input the no. of resources that a process can maximum have..\n");
for(i=0;i<n;i++)
{
printf("\n Process %d\n",i);
for(j=0;j<r;j++)
{
printf("\n Resource %d\n",j);
scanf("%d",&max[i][j]);
}
}
printf("\n Input the no. of available instances of each resource..\n");
for(i=0;i<r;i++)
{
printf("\n Resource %d : ",i);
scanf("%d",&available[i]);

St.Joseph’s College of Engineering 48


CS8461-Operating Systems Lab Department of CSE 2020-2021

}
printf("\n The need matrix is as follows : \n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]= max[i][j]-allocation[i][j];
printf("\t %d",need[i][j]);
}
printf("\n");
}
do{
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
if(flag[i]==0)
{
count=0;
for(j=0;j<r;j++)
{
if(available[j]>=need[i][j])
count++;
}
if(count==r)
{
count1++;
flag[i]=1;
sq[count1-1]=i;
for(j=0;j<r;j++)
{
available[j]=available[j]+allocation[i][j];
}
break;
}
}
}
}
if(count1!=n)
{
printf("\n---------------IT'S AN UNSAFE STATE---------------");
break;
}
}while(count1!=n);
if(count1==n)
{
printf("\n *******************IT'S A SAFE STATE*******************");
printf("\n The safe sequence is....\n");
for(i=0;i<n;i++)
printf("\t P%d",sq[i]);
printf("\n");
printf("\n The available matrix is now : ");
for(i=0;i<r;i++)

St.Joseph’s College of Engineering 49


CS8461-Operating Systems Lab Department of CSE 2020-2021

printf("\t %d",available[i]);
}
}

OUTPUT

St.Joseph’s College of Engineering 50


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT:
Thus the program was executed successfully.
Ex.No:9 IMPLEMENTATION OF DEAD LOCK DETECTION

St.Joseph’s College of Engineering 51


CS8461-Operating Systems Lab Department of CSE 2020-2021

AIM:

To implement deadlock detection by using Banker’s Algorithm.

ALGORITHM:

1. Start the program.


2. Get the values of resources and processes.
3. Get the allocation and request matrix value.
4. Get the available instances for each resources
5. Check whether its possible to allocate using below condition.
6. If Need<=Work update work as work=work +allocation
7. Else proceed with next process.
8. If it is possible to allocate then the system is in safe state else system is not in safe state.

PROGRAM
#include<stdio.h>
int main()
{
int alloc[10][10],req[10][10],ins[10],avail[10],tp,tr,i,j;
int tmp[10]={0},count=0;
int finish[10]={0},flag;
printf("Enter total no of processes:");
scanf("%d",&tp);
printf("Enter total no of resources:");
scanf("%d",&tr);
printf("Enter the Allocation Matrix:\n");
for(i=0;i<tp;i++)
{
for(j=0;j<tr;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the Request Matrix:\n");
for(i=0;i<tp;i++)
{
for(j=0;j<tr;j++)
{
scanf("%d",&req[i][j]);
}
}
printf("Enter the resource instance vector:\n");
for(i=0;i<tr;i++)
{
scanf("%d",&ins[i]);
}
//To calculate resource availability vector
for(i=0;i<tp;i++)
{
for(j=0;j<tr;j++)
{
tmp[i]+=alloc[j][i]; //sum calculated columnwise for allocation matrix

St.Joseph’s College of Engineering 52


CS8461-Operating Systems Lab Department of CSE 2020-2021

}
}
printf("\nCalculated Availability Vector:\n");
for(i=0;i<tp;i++)
{
avail[i]=ins[i]-tmp[i];
printf("\t%d",avail[i]);
}
while(count!=tp)
{ //if finish array has all true's(all processes to running state)
//deadlock not detected and loop stops!
for(i=0;i<tp;i++)
{
count=0;
//To check whether resources can be allocated any to blocked process
if(finish[i]==0)
{
for(j=0;j<tr;j++)
{
if(req[i][j]<=avail[j])
{
count++;
}
}
flag=0;
if(count==tr)
{
for(j=0;j<tr;j++)
{
avail[j]+=alloc[i][j]; //allocated reources are released and added to available!
}
finish[i]=1;
printf("\nProcess %d is transferred to running state and assumed finished",i+1);
}
else
flag=1;
}
}
count=0;
for(j=0;j<tr;j++)
{
if(finish[j]==1)
{
count++;
}
}
}
for(i=0;i<tp;i++)
{
if(finish[i]==0)
{
printf("\n Oops! Deadlock detected and causing process is:process(%d)\n",i+1);
break;

St.Joseph’s College of Engineering 53


CS8461-Operating Systems Lab Department of CSE 2020-2021

}
}
i=i-1;
if(finish[i]==1)
printf("\nHurray! Deadlock not detected:-)\n");
return 0;
}

OUTPUT:

St.Joseph’s College of Engineering 54


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT:
Thus the program was executed successfully.

Ex.No:10 IMPLEMENTATION OF THREADING & SYNCHRONIZATION

AIM
To implement POSIX thread operations and synchronize using mutex variable.

ALGORITHIM

1. Start the process

2. Intialize mutex lock ,thread id and required other variables

3. Create POSIX threads using pthread_create

4. Define thread task by creating new function.

5. Synchronize threads using mutex lock and unlock variables

6. Using pthread_join allow threads to wait until parent process completes

7. Destroy the synchronization variable mutex using pthread_mutex_destroy

St.Joseph’s College of Engineering 55


CS8461-Operating Systems Lab Department of CSE 2020-2021

PROGRAM
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
pthread_t tid[2];
int counter;
pthread_mutex_t lock;
void* doSomeThing(void *arg)
{
pthread_mutex_lock(&lock);
unsigned long i = 0;
counter += 1;
printf("\n Job %d started\n", counter);

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

printf("\n Job %d finished\n", counter);

pthread_mutex_unlock(&lock);

return NULL;
}

int main(void)
{
int i = 0;
int err;

if (pthread_mutex_init(&lock, NULL) != 0)
{
printf("\n mutex init failed\n");
return 1;
}
while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
i++;
}

pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
pthread_mutex_destroy(&lock);
return 0;
}

St.Joseph’s College of Engineering 56


CS8461-Operating Systems Lab Department of CSE 2020-2021

OUTPUT

St.Joseph’s College of Engineering 57


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT:
Thus the program was executed successfully.
Ex. No : 11(a) FIRST FIT MEMORY ALLOCATION

AIM
To write a C program to implement First fit Memory allocation technique

ALGORITHM
1. Enter the number of blocks and number of files.
2. Enter the size of the blocks.
3. Enter the size of the files.
4. First fit chooses the first available block that is large enough.
5. Display File number, File size, Occupied block number and size.

PROGRAM
#include<stdio.h>
#define max 25
void main()
{

St.Joseph’s College of Engineering 58


CS8461-Operating Systems Lab Department of CSE 2020-2021

int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
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]);
}

St.Joseph’s College of Engineering 59


CS8461-Operating Systems Lab Department of CSE 2020-2021

OUTPUT

St.Joseph’s College of Engineering 60


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT
Thus First fit Memory allocation technique was executed successfully.

Ex. No : 11(b) WORST FIT MEMORY ALLOCATION

AIM
To write a C program to implement Worst fit Memory allocation technique

ALGORITHM
1. Enter the number of blocks and number of files.
2. Enter the size of the blocks.
3. Enter the size of the files.
4. Worst fit occupies the largest available block.
5. Display File number, File size, occupied block number and size.

PROGRAM
#include<stdio.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];
printf("\n\tMemory Management Scheme - Worst Fit");

St.Joseph’s College of Engineering 61


CS8461-Operating Systems Lab Department of CSE 2020-2021

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]);
}

St.Joseph’s College of Engineering 62


CS8461-Operating Systems Lab Department of CSE 2020-2021

OUTPUT

St.Joseph’s College of Engineering 63


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT
Thus Worst fit Memory allocation technique was executed and verified successfully.

Ex. No : 11(c) BEST FIT MEMORY ALLOCATION

AIM
To write a C program to implement Best fit Memory allocation technique

ALGORITHM
1. Enter the number of blocks and number of files.
2. Enter the size of the blocks.
3. Enter the size of the files.
4. Best fit chooses the block that is closest in size to the request.
5. Display File number, File size, occupied block number and size.

PROGRAM
#include<stdio.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];
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);

St.Joseph’s College of Engineering 64


CS8461-Operating Systems Lab Department of CSE 2020-2021

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]);
}

St.Joseph’s College of Engineering 65


CS8461-Operating Systems Lab Department of CSE 2020-2021

OUTPUT

St.Joseph’s College of Engineering 66


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT
Thus Best fit Memory allocation technique was executed and verified successfully.

Ex.No:12 IMPLEMENTATION OF PAGING TECHNIQUE

AIM:
To implement the Memory management policy- Paging.

ALGORITHM:
1. Read all the necessary input from the keyboard.
2. Pages - Logical memory is broken into fixed - sized blocks.
3. Frames – Physical memory is broken into fixed – sized blocks.
4. Calculate the physical address using the following
Physical address = ( Frame number * Frame size ) + offset
5. Display the physical address.

PROGRAM
#include <stdio.h>

struct pstruct
{
int fno;
int pbit;
}ptable[10];

int pmsize,lmsize,psize,frame,page,ftable[20],frameno;

void info()
{
printf("\n\nMEMORY MANAGEMENT USING PAGING\n\n");
printf("\n\nEnter the Size of Physical memory: ");
scanf("%d",&pmsize);
printf("\n\nEnter the size of Logical memory: ");
scanf("%d",&lmsize);

St.Joseph’s College of Engineering 67


CS8461-Operating Systems Lab Department of CSE 2020-2021

printf("\n\nEnter the partition size: ");


scanf("%d",&psize);
frame = (int) pmsize/psize;
page = (int) lmsize/psize;
printf("\nThe physical memory is divided into %d no.of frames\n",frame);
printf("\nThe Logical memory is divided into %d no.of pages",page);
}

void assign()
{
int i;
for (i=0;i<page;i++)
{
ptable[i].fno = -1;
ptable[i].pbit= -1;
}
for(i=0; i<frame;i++)
ftable[i] = 32555;
for (i=0;i<page;i++)
{
printf("\n\nEnter the Frame number where page %d must be placed: ",i);
scanf("%d",&frameno);
ftable[frameno] = i;
if(ptable[i].pbit == -1)
{
ptable[i].fno = frameno;
ptable[i].pbit = 1;
}
}

printf("\n\nPAGE TABLE\n\n");
printf("PageAddress FrameNo. PresenceBit\n\n");
for (i=0;i<page;i++)
printf("%d\t\t%d\t\t%d\n",i,ptable[i].fno,ptable[i].pbit);
printf("\n\n\n\tFRAME TABLE\n\n");
printf("FrameAddress PageNo\n\n");
for(i=0;i<frame;i++)
printf("%d\t\t%d\n",i,ftable[i]);
}
void cphyaddr()
{
int laddr,paddr,disp,phyaddr,baddr;

// clrscr();
printf("\n\n\n\tProcess to create the Physical Address\n\n");
printf("\nEnter the Base Address: ");
scanf("%d",&baddr);
printf("\nEnter theLogical Address: ");
scanf("%d",&laddr);

paddr = laddr / psize;


disp = laddr % psize;

St.Joseph’s College of Engineering 68


CS8461-Operating Systems Lab Department of CSE 2020-2021

if(ptable[paddr].pbit == 1 )
phyaddr = baddr + (ptable[paddr].fno*psize) + disp;
printf("\nThe Physical Address where the instruction present: %d",phyaddr);
}
void main()
{

info();
assign();
cphyaddr();

OUTPUT

St.Joseph’s College of Engineering 69


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT: Thus the program was executed successfully.

Ex.No:13(a) FIFO PAGE REPLACEMENT ALGORITHM

AIM
To write a c program to implement FIFO page replacement algorithm

ALGORITHM
1. Start the process
2. Declare the size with respect to page length
3. Check the need of replacement from the page to memory
4. Check the need of replacement from old page to new page in memory
5. Forma queue to hold all pages
6. Insert the page require memory into the queue
7. Check for bad replacement and page fault
8. Get the number of processes to be inserted
9. Display the values
10. Stop the process

PROGRAM:

#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
            printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
            printf("\n ENTER THE PAGE NUMBER :\n");
            for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
            printf("\n ENTER THE NUMBER OF FRAMES :");
            scanf("%d",&no);

St.Joseph’s College of Engineering 70


CS8461-Operating Systems Lab Department of CSE 2020-2021

for(i=0;i<no;i++)
            frame[i]= -1;
                        j=0;
                        printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
                        {
                                    printf("%d\t\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("Page Fault Is %d",count);
                        return 0;
}

St.Joseph’s College of Engineering 71


CS8461-Operating Systems Lab Department of CSE 2020-2021

OUTPUT

St.Joseph’s College of Engineering 72


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT: Thus the program was executed successfully.

Ex No:13(b) LRU PAGE REPLACEMENT ALGORITHM

AIM:

To implement LRU page replacement technique.

ALGORITHM:

1. Start the process


2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least recently used page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process

PROGRAM
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];

printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];

St.Joseph’s College of Engineering 73


CS8461-Operating Systems Lab Department of CSE 2020-2021

printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
            {
                        c1=0;
                        for(j=0;j<f;j++)
                        {
                                    if(p[i]!=q[j])
                                    c1++;
                        }
                        if(c1==f)
                        {
                                    c++;
                                    if(k<f)
                                    {
                                                q[k]=p[i];
                                                k++;
                                                for(j=0;j<k;j++)
                                                printf("\t%d",q[j]);
                                                printf("\n");
                                    }
                                    else
                                    {
                                                for(r=0;r<f;r++)
                                                {
                                                            c2[r]=0;
                                                           for(j=i-1;j<n;j--)
                                                            {
                                                            if(q[r]!=p[j])
                                                            c2[r]++;
                                                            else
                                                            break;
                                                }
                                    }
                                    for(r=0;r<f;r++)
                                     b[r]=c2[r];
                                    for(r=0;r<f;r++)
                                    {
                                                for(j=r;j<f;j++)
                                                {
                                                            if(b[r]<b[j])
                                                            {
                                                                   t=b[r];
                                                                   b[r]=b[j];
                                                                   b[j]=t;
                                                            }
                                                }
                                    }
                                    for(r=0;r<f;r++)
                                    {
                                                if(c2[r]==b[0])
                                                q[r]=p[i];

St.Joseph’s College of Engineering 74


CS8461-Operating Systems Lab Department of CSE 2020-2021

                                                printf("\t%d",q[r]);
                                    }
                                    printf("\n");
                        }
            }
}
printf("\nThe no of page faults is %d",c);

return 0;
}

OUTPUT

St.Joseph’s College of Engineering 75


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT:
Thus the program was executed successfully.

Ex No:13 (c ) LFU PAGE REPLACEMENT ALGORITHM

AIM:
To implement LFU page replacement technique.

ALGORITHM:
1. Read Number Of Pages And Frames
2. Read Each Page Value
3. Search For Page In The Frames
4. If Not Available Allocate Free Frame
5. If No Frames Is Free Repalce The Page With The Page That Is Least frequently used.
6. Print Page Number Of Page Faults

PROGRAM
#include<stdio.h>
void main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf(“Enter no of pages:”);
scanf(“%d”,&n);
printf(“Enter the reference string:”);
for(i=0;i<n;i++)
scanf(“%d”,&p[i]);
printf(“Enter no of frames:”);
scanf(“%d”,&f);
q[k]=p[k];
printf(“\n\t%d\n”,q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;

St.Joseph’s College of Engineering 76


CS8461-Operating Systems Lab Department of CSE 2020-2021

for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf(“\t%d”,q[j]);
printf(“\n”);
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j–)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf(“\t%d”,q[r]);
}
printf(“\n”);
}
}
}

St.Joseph’s College of Engineering 77


CS8461-Operating Systems Lab Department of CSE 2020-2021

printf(“\nThe no of page faults is %d”,c);


}
/*

OUTPUT:

St.Joseph’s College of Engineering 78


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT:
Thus the program was executed successfully.

St.Joseph’s College of Engineering 79


CS8461-Operating Systems Lab Department of CSE 2020-2021

Ex.No: 14 (a) SINGLE LEVEL DIRECTORY

AIM
To write a c program to simulate Single level directory structure.

ALGORITHM

Start the program.


Get the number of main directories to be created.
Get the name of the directory and size of the directory.
Simulate the directory level and display the structure.
Stop the program.

PROGRAM
#include<stdio.h>
main()
{
int master,s[20];
char f[20][20][20];
char d[20][20];
int i,j;
printf("Enter number of directorios:");
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 file names :");
for(i=0;i<master;i++)
for(j=0;j<s[i];j++)
scanf("%s",&f[i][j]);
printf("\n");
printf(" directory\tsize\tfilenames\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\t",f[i][j]);
printf("\n"); }
printf("\t\n");
}

OUTPUT

St.Joseph’s College of Engineering 80


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT: Thus the program was executed successfully.

St.Joseph’s College of Engineering 81


CS8461-Operating Systems Lab Department of CSE 2020-2021

Ex.No: 14(b) TWO LEVEL DIRECTORY

AIM
To write a C program to simulate Two Level directory structures.

ALGORITHM
1. Start the program.
2. Get the number of main directories to be created, name of the directory and size of the
directory.
3. Get the number of sub directories to be created, name of the directory and size of the
directory.
4. Simulate the directory level and display the structure.
5. Stop the program.
PROGRAM
#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 names:",i+1);
scanf("%s",&dir[i].dname);
printf("Enter size of directories:");
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 name:");
scanf("%s",&dir[i].fname[j][k]);
} } }
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++)

St.Joseph’s College of Engineering 82


CS8461-Operating Systems Lab Department of CSE 2020-2021

{
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

St.Joseph’s College of Engineering 83


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT: Thus the program was executed successfully.

St.Joseph’s College of Engineering 84


CS8461-Operating Systems Lab Department of CSE 2020-2021

Ex.No:14(c) HIERARCHICAL & DAG

AIM
To write a C program to implement Hierarchical and DAG.

ALGORITHM
1. Start the program.
2. Get the number of main directories to be created, name of the directory and size of the directory.
3. Get the number of sub directories to be created, name of the directory and size of the directory.
4. Get the name of the file to be shared between directories.
5. Simulate the directed acyclic graph and display the structure.
6. Stop the program.

PROGRAM

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define MIN_PER_RANK 1

#define MAX_PER_RANK 5

#define MIN_RANKS 3

#define MAX_RANKS 5

#define PERCENT 30

int main (void)

int i, j, k,nodes = 0;

srand (time (NULL));

int ranks = MIN_RANKS

+ (rand () % (MAX_RANKS - MIN_RANKS + 1));

printf ("digraph {\n");

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

int new_nodes = MIN_PER_RANK

St.Joseph’s College of Engineering 85


CS8461-Operating Systems Lab Department of CSE 2020-2021

+ (rand () % (MAX_PER_RANK - MIN_PER_RANK + 1));

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

for (k = 0; k < new_nodes; k++)

if ( (rand () % 100) < PERCENT)

printf (" %d -> %d;\n", j, k + nodes);

nodes += new_nodes;

printf ("}\n");

return 0;

OUTPUT

St.Joseph’s College of Engineering 86


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT: Thus the program was executed successfully.

Ex No: 15 (a) SEQUENTIAL FILE ALLOCATION TECHNIQUE

AIM:
To write a C program to implementation sequential file allocation techniques.

ALGORITHM:
Step 1: Memory before allocation is displayed.
Step 2: The file Id is entered by the user.
Step 3: the number of blocks memory required and the starting address of the file is given by the user..
Step 4: If starting address and length of block is greater than 20 print error message.
Else , the memory address is replaced by the id.
Step 5 : Display memory after allocation.

PROGRAM:
#include<stdio.h>
#include<conio.h>
int a[20],num=0;
int fid[10],length[10],start[10];
void filedescriptor();
void display();
void filedescriptor()
{
int i;
printf("\n file id \t starting address \t length \n");
for(i=0;i<num;i++)
printf("%d\t\t\t %d\t\t%d\n",fid[i],start[i],length[i]);
}
void display()
{
int i;
for(i=0;i<20;i++)
printf("%2d",i);
printf("\n");

St.Joseph’s College of Engineering 87


CS8461-Operating Systems Lab Department of CSE 2020-2021

for(i=0;i<20;i++)
printf("%2d", a[i]);
}
void main()
{
int i,n,k,temp,st,l,id,flag=0,cho;
for(i=0;i<20;i++)
a[i]=0;
clrscr();
printf("\n memory before allocation:\n");
display();
while(1)
{
printf("\n enter the file id:");
scanf("%d",&id);
printf("\n enter the number of blocks the file occupies:");
scanf("%d", &l);
fid[num]=id;
length[num]=l;
l:printf("\n enter the starting address:");
scanf("%d", &st);
flag=0;
if((st+1)>20)
{
printf("\n sorry the given memory goes out of space:");
goto l; }
for(i=st;i<(st+1);i++)
{
if(a[i]!=0)
{
flag=1;
break;
}}
if(flag==0)
{
start[num]=st;
for(i=st;i<(st+1);i++)
a[i]=id;
}
else
{
printf("\n sorry the given blocks are already occupied: Enter new starting
address");
goto l;
}
flag=0;
num++;
filedescriptor();
printf("\n memory after allocation \n");
display();
printf("\n want to continue? \n1.yes \n2.no");
scanf("%d",&cho);
if(cho==2)

St.Joseph’s College of Engineering 88


CS8461-Operating Systems Lab Department of CSE 2020-2021

exit(0);
}}
OUTPUT:

St.Joseph’s College of Engineering 89


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT: Thus the program was executed successfully.

Ex No: 15 (b ) INDEXED FILE ALLOCATION TECHNIQUE

AIM:
To write a C program to implementation indexed file allocation techniques.

ALGORITHM:
Step 1: Start.
Step 2: Get the index block number and number of files in the index block as input from user.
Step 3: Get the file numbers (i.e referred block numbers holding file) as input .
Step 4: Check whether that the input block number is already allocated if so print block allocated
Else increase the count and allocate the file.
Step 5: Continue the loop to enter another index block.

PROGRAM
#include<stdio.h>

int main()
{
int f[50],indblk,i,k,j,index[50],n,c,count=0;

for(i=0;i<50;i++)
f[i]=0;
X:
printf("enter index block");
scanf("%d",&indblk);
if(f[indblk]!=1)
{
f[indblk]=1;
printf("enter no of files on index");
scanf("%d",&n);
}
y:
for(i=0;i<n;i++)
{
scanf("%d",&index[i]);
if(f[index[i]]==0)
{
count++;
}
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("\nallocated");
printf("\n file indexed");
for(k=0;k<n;k++)
printf("\n%d->%d:%d",indblk,index[k],f[index[k]]);
}
else

St.Joseph’s College of Engineering 90


CS8461-Operating Systems Lab Department of CSE 2020-2021

{
printf("\n file in the index already allocation");
printf("\nenter another file indexed");
goto y;
}
printf("\n index is already allocated");
count=0;
printf("\n if u enter one more block(1/0)");
scanf("%d",&c);
if(c==1)
goto X;

St.Joseph’s College of Engineering 91


CS8461-Operating Systems Lab Department of CSE 2020-2021

OUTPUT:

RESULT:
Thus the program was executed successfully.

St.Joseph’s College of Engineering 92


CS8461-Operating Systems Lab Department of CSE 2020-2021

Ex No: 15(c) LINKED LIST FILE ALLOCATION TECHNIQUE

AIM:
To write a C program to implementation linked list file allocation techniques.

ALGORITHM:
Step 1: Start.
Step 2: Get the number of blocks already allocated and its block numbers as input from user.
Step 3: Add that block numbers to a list as allocated block number list and mark that block numbers are
allocated.
Step 4: To perform allocation .Get the starting block number and length of the input file
Step 5: From the starting block check whether each block is in allocated block number list
Step 6: If so Print block is already allocated. Else allocate file to the block number.
Step 7: Continue the loop if the processes want to be repeated.

PROGRAM
#include<stdio.h>

void main()
{
int f[50],p,i,j,k,a,st,len,n,c;

for(i=0;i<50;i++)
f[i]=0;
printf("enter how many blocks already allocated");
scanf("%d",&p);
printf("\nenter the blocks nos");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("enter index sarting block & length");
scanf("%d%d",&st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("\n %d->file is already allocated",j);
k++;
}
}

St.Joseph’s College of Engineering 93


CS8461-Operating Systems Lab Department of CSE 2020-2021

}
else
printf("\nif u enter one more (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();

St.Joseph’s College of Engineering 94


CS8461-Operating Systems Lab Department of CSE 2020-2021

OUTPUT:

RESULT: Thus the program was executed successfully

St.Joseph’s College of Engineering 95


CS8461-Operating Systems Lab Department of CSE 2020-2021

Ex.No:16 MEMORY MANAGEMENT USING SEGMENTATION

AIM
To implement Segmentation Memory Management scheme using C

ALGORITHM
1. Get Segment number, base and limit address.
2. Define insert function to create segments and add segmentation details for each.
3. Define find function to search and get the segment details for the given segment number.
4. To display the segment details get segment number and offset.
5. Segment details are displayed when offset is less than limit of the given segment number
6. calculate physical memory address as phy addr=base+offset
7. Display the physical address in the segment of the given offset.

PROGRAM

#include<stdio.h>
struct list
{
int seg;
int base;
int limit;
struct list *next;
}*p;
void insert(struct list *q,int base,int limit,int seg)
{
if(p==NULL)
{
p=malloc(sizeof(struct list));
p->limit=limit;
p->base=base;
p->seg=seg;
p->next=NULL;
}
else
{
while(q->next!=NULL)
{
q=q->next;
printf("yes");
}
q->next=malloc(sizeof(struct list));
q->next->limit=limit;
q->next->base=base;
q->next->seg=seg;
q->next->next=NULL;
}
}
int find(struct list *q,int seg)
{
while(q->seg!=seg)

St.Joseph’s College of Engineering 96


CS8461-Operating Systems Lab Department of CSE 2020-2021

{
q=q->next;
}
return q->limit;
}
int search(struct list *q,int seg)
{
while(q->seg!=seg)
{
q=q->next;
}
return q->base;
}
main()
{
p=NULL;
int seg,offset,limit,base,c,s,physical;
printf("Enter segment table/n");
printf("Enter -1 as segment value for termination\n");
do
{
printf("Enter segment number");
scanf("%d",&seg);
if(seg!=-1)
{
printf("Enter base value:");
scanf("%d",&base);
printf("Enter value for limit:");
scanf("%d",&limit);
insert(p,base,limit,seg);
}
}while(seg!=-1);
printf("Enter offset:");
scanf("%d",&offset);
printf("Enter bsegmentation number:");
scanf("%d",&seg);
c=find(p,seg);
s=search(p,seg);
if(offset<c)
{
physical=s+offset;
printf("Address in physical memory %d\n",physical);
}
else
{
printf("error");
}
}

OUTPUT

St.Joseph’s College of Engineering 97


CS8461-Operating Systems Lab Department of CSE 2020-2021

RESULT
Thus the program to simulate Segmentation Memory Management is executed successful.

St.Joseph’s College of Engineering 98

You might also like