Professional Documents
Culture Documents
Oslabmanualanu1
Oslabmanualanu1
You Choose, We Do it
St. JOSEPH’S COLLEGE OF ENGINEERING
(An Autonomous Institution)
Name: KAARTHIK N
You Choose, We Do it
BONAFIDE CERTIFICATE
LAB IN – CHARGE
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
12
IMPLEMENTATION OF PAGING TECHNIQUE 06-05-21 10-05-21
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
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.
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.
5.Copy a file:
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
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
$ diff f1 f2
3c2
< I am
---
> IT
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.
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
Ex. No: 2(a)Implementation of UNIX system calls : fork, exec, getpid, exit, wait, close.
AIM
To write a program for implementing process management using the following system calls
of UNIX operating system: fork, exec, getpid, exit, wait, close.
ALGORITHM
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;
}
OUTPUT
RESULT
Thus the program for implementation of various UNIX system calls was executed successfully
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>
OUTPUT
RESULT
Thus the program for implementing directory system calls was successfully executed.
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
#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);
}
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);
}
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;
}
}
}
}
OUTPUT:
RESULT
Thus the C program to simulate UNIX commands like ls,cp,grep was executed successfully.
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"
OUTPUT:
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:
RESULT:
Thus the program was executed successfully.
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:
RESULT:
Thus the program was executed successfully.
EXP-4(d)
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:
RESULT:
Thus the program was executed successfully.
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++;
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];
}
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]);
} 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:
RESULT:
Thus the program was executed successfully.
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
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);
}
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);
}
OUTPUT
RESULT:
Thus the program was executed successfully.
Ex No: 5(d) PRIORITY SCHEDULING
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];
}
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
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:
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
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);
return 0;
}
OUTPUT
RESULT
Thus the program for Interprocess communication using Shared Memory system calls was
executed successfully.
AIM:
ALGORITHM:
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]);
}
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++)
printf("\t %d",available[i]);
}
}
OUTPUT
RESULT:
Thus the program was executed successfully.
Ex.No:9 IMPLEMENTATION OF DEAD LOCK DETECTION
AIM:
ALGORITHM:
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
}
}
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;
}
}
i=i-1;
if(finish[i]==1)
printf("\nHurray! Deadlock not detected:-)\n");
return 0;
}
OUTPUT:
RESULT:
Thus the program was executed successfully.
AIM
To implement POSIX thread operations and synchronize using mutex variable.
ALGORITHIM
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++);
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;
}
OUTPUT
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()
{
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]);
}
OUTPUT
RESULT
Thus First fit Memory allocation technique was executed successfully.
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");
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]);
}
OUTPUT
RESULT
Thus Worst fit Memory allocation technique was executed and verified successfully.
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);
OUTPUT
RESULT
Thus Best fit Memory allocation technique was executed and verified successfully.
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);
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);
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
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);
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;
}
OUTPUT
AIM:
ALGORITHM:
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];
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];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
return 0;
}
OUTPUT
RESULT:
Thus the program was executed successfully.
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;
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”);
}
}
}
OUTPUT:
RESULT:
Thus the program was executed successfully.
AIM
To write a c program to simulate Single level directory structure.
ALGORITHM
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
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++)
{
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
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 i, j, k,nodes = 0;
nodes += new_nodes;
printf ("}\n");
return 0;
OUTPUT
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");
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)
exit(0);
}}
OUTPUT:
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
{
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;
OUTPUT:
RESULT:
Thus the program was executed successfully.
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++;
}
}
}
else
printf("\nif u enter one more (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
OUTPUT:
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)
{
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
RESULT
Thus the program to simulate Segmentation Memory Management is executed successful.