Staff Manual: Dr.M.G.R. University Department of Computer Science and Engineering & Information Technology
Staff Manual: Dr.M.G.R. University Department of Computer Science and Engineering & Information Technology
STAFF MANUAL
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Table of Contents
2 Shell Programming
3 System Calls
7 Bankers Algorithm
11 Paging
12 Segmentation
13 File Systems
2
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Ex.No:1
UNIX INTRODUCTION
AIM:
UNIX Commands
General Commands:
2. who: Gives the details of the user who have logged into the system.
$ who
abc tty() oct 15 11:17
Xyz tty4 oct 15 11:30
3. whoami: Gives the details regarding the login time and system’s name for the
connection being used.
$ whoami.
user 1 ttya Oct 15 12:20.
4. man: It displays the manual page of our terminal with the command ‘man’
command name.
$ man who
5. head and tail: ‘head’ is used to display the initial part of the text file and ‘tail’
is used to display the last part of the file.
$ head [-count] [filename]
$ tail [-count] [filename]
6. pwd: It displays the full path name for the current directory we are working in.
$ pwd
/usr/tmp.
11.cat : This command helps us to specify the contents of the file we specify.
$ cat [option….[file….]]
13.mv: This command is used to rename and move ordinary and directory files.
$ mv file1 file2
$ mv directory1 directory2
15. rm: This command is used to remove one or more files from the
directory.This can be used to delete all files as well as directory.
$ rm [option…..] file
[permission] r = read
w =write
x =execute
Ex.:: $ chmod 754 prog1.
4
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
18. wc:counts and displays the lines,words and characters in the files specified.
$ wc
$ wc
$ wc
$ wc
Ex:: $ wc prog2.
3 9 60 prog2.
Ex :: $ ls – l | sort –n
Result
Thus the Basics of Unix operating system , commands and vi have been studied
successfully.
5
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Ex.No:2
SHELL PROGRAMMING
AIM:
Program
echo " enter first no "
read a
echo " enter second no "
read b
c= expr $a + $b
INPUT
[student@localhost student]$ sh sum
enter first no
3
enter second no
4
OUTPUT:
7
b. FIBONACCI SERIES
Aim:
To find the Fibonacci series of the given number.
Program:
echo "enter a number"
read n
i=0
a=0
b=1
c=0
echo "fibonacci series is"
echo "$a"
echo "$b"
n1=` expr $n - 2`
while [ $i -lt $n1 ]
do
6
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
INPUT:
[student@localhost student]$ sh fibo
enter a number
5
OUTPUT:
fibonacci series is
0
1
1
2
3
c. POSITIVE OR NEGATIVE
Aim:
To find whether the given number is positive or negative.
Program:
INPUT:
[student@localhost student]$ sh positive
enter a number
4
OUTPUT:
no is positive
INPUT:
[student14@localhost student14]$ sh positive
7
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
enter a number
-5
OUTPUT:
no is negative
INPUT:
[student14@localhost student14]$ sh positive
enter a number
0
OUTPUT:
no is zero
OUTPUT:
9 is greater
e. ARMSTRONG NUMBER
Aim:
To find whether the given number is an Armstrong number or not.
Program:
echo "enter the number"
read num
ans=0
n=$num
8
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
while [ $n -gt 0 ]
do
q=` expr $n % 10`
ans=` expr $ans + $q \* $q \* $q`
n=` expr $n / 10`
done
if [ $ans -eq $num ]
then
echo "number is armstrong"
else
echo "number is not armstrong"
fi
INPUT:
[student@localhost student]$ sh armstrong
enter the number
153
OUTPUT:
number is Armstrong
INPUT:
[student@localhost student]$ sh armstrong
enter the number
243
OUTPUT:
number is not armstrong
f. FACTORIAL NUMBER
Aim : To write a program to generate the factorial of given number.
Program:
n=0
fact=1
g=0
y=1
echo "enter no to find the factorial:"
read n
g=$n
while [ $n -ge $y ]
do
fact=` expr $fact \* $n`
n=` expr $n - 1 `
done
echo "factorial for $g is $fact"
INPUT:
[student@localhost student]$ sh fact
enter no to find the factorial:
5
OUTPUT:
factorial for 5 is 120
RESULT: Thus the shell Programs were implemented and output was verified successfully.
9
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Ex.No:3
PROCESS CREATION
Aim:
To write a C++ program to create a process using the fork system calls.
Program :
#include<iostream.h>
#include<sys/types.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
pid_t child;
cout<<"pid:"<<getpid()<<"patent:"<<getppid()<<endl;
switch(child=fork())
{
case (pid_t) -1: perror ("fork");
break;
case (pid_t) 0: cout<<"child created:pid:"<<getpid()<<"patent:"<<getppid()<<endl;
exit(0);
default:cout<<"parent after fork pid:"<<"child pid:"<<child<<endl;
}
return 0;
}
INPUT:
[student@localhost student]$ cc proc.c
OUTPUT:
pid:26482parent:26292
parent after fork pid:child pid:26483
child created:pid:26483parent:1
RESULT: Thus the Program was implemented and output was verified successfully
10
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Ex.No:4
CPU SCHEDULING POLICIES
AIM:
Ex No.4 (a)
FIRST COME FIRST SERVED CPU SCHEDULING
Program:
#include<stdio.h>
main()
{
int i,n,w[10],e[10],b[10];
float wa=0,ea=0;
printf("\nEnter the no of jobs: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the burst time of job %d :",i+1);
scanf("%d",&b[i]);
if(i==0)
{
w[0]=0;
e[0]=b[0];
}
else
{
e[i]=e[i-1]+b[i];
w[i]=e[i-1];
}
}
printf("\n\n\tJobs\tWaiting time \tBursttime\tExecution time\n");
printf("\t-------------------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("\t%d\t\t%d\t\t%d\t\t%d\n",i+1,w[i],b[i],e[i]);
wa+=w[i];
ea+=e[i];
}
wa=wa/n;
ea=ea/n;
printf("\n\nAverage waiting time is :%2.2f ms\n",wa);
11
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
INPUT:
RESULT: Thus the Program was implemented and output was verified successfully.
Ex No.4 (b)
}
for(i=0;i<n;i++)
{
printf("Enter the process name: \n");
scanf("%s",&proc[i].pname);
printf("Enter the Burst time:\n");
scanf("%d",&proc[i].btime);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(proc[i].btime<proc[j].btime)
{
a=proc[i];
proc[i]=proc[j];
proc[j]=a;
}
}
}
printf("-------------------- CPU SCHEDULING ALGORITHM - SJFS -------------------------- ");
printf("\n\tprocess name \tBurst time \twaiting time \tturnaround time\n");
temp=0;
for(i=0;i<n;i++)
{
temp=temp1+temp+proc[i].btime;
temp1=temp1+proc[i].btime;
temp2=temp1-proc[i].btime;
printf("\n\t %s \t %d ms \t %d ms \t %d ms\n",proc[i].pname,proc[i].btime,temp2,temp1);
}
printf("-------------------------------------------------------------------------------");
awt=(temp-temp1)/n;
tt=temp/n;
printf("\nThe Average Waiting time is %4.2f milliseconds\n",awt);
printf("\nThe Average Turnaround time is %4.2f",tt);
}
13
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
INPUT:
Enter the number of processes:
4
Enter the process name:
p1
Enter the Burst time:
5
Enter the process name:
p2
Enter the Burst time:
5
Enter the process name:
p3
Enter the Burst time:
6
Enter the process name:
p4
Enter the Burst time:
2
OUTPUT:
-------------------- CPU SCHEDULING ALGORITHM - SJFS --------------------------
Process name Burst time waiting time turnaround time
p4 2 ms 0ms 2 ms
p1 5 ms 2 ms 7 ms
p2 5 ms 7 ms 12 ms
p3 6 ms 12 ms 18 ms
-----------------------------------------------------------------------------------------------------
The Average Waiting time is 5.00 milliseconds
RESULT: Thus the Program was implemented and output was verified successfully.
14
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Program:
#include<stdio.h>
#include<malloc.h>
void line(int i)
{
int j;
for(j=1; j<=i; j++)
printf("-");
printf("\n");
}
struct process
{
int p_id;
int etime, wtime, tatime;
};
struct process *p, *tmp;
int i, j, k, l, n, time_slice, ctime;
float awtime=0, atatime=0;
int main()
{
printf("Process Scheduling - Round Robin \n");
line(29);
printf("Enter the no.of processes : ");
scanf("%d", &n);
printf("Enter the time slice : ");
scanf("%d", &time_slice);
printf("Enter the context switch time : ");
scanf("%d", &ctime);
p=(struct process*) calloc(n+1, sizeof(struct process));
tmp=(struct process*) calloc(n+1, sizeof(struct process));
for(i=1; i<=n; i++)
{
printf("Enter the execution time of process %d : ", i-1);
scanf("%d", &p[i].etime);
p[i].wtime=(time_slice+ctime)*i-1;
awtime += p[i].wtime;
p[i].p_id=i-1;
tmp[i]=p[i];
}
i=0; j=1; k=0;
while(i<n)
{
for(j=1; j<=n; j++)
{
if(tmp[j].etime <= time_slice && tmp[j].etime!=0)
{
k=k+tmp[j].etime;
15
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
tmp[j].etime=0;
p[j].tatime=k;
atatime += p[j].tatime;
k=k+ctime;
i++;
}
if(tmp[j].etime>time_slice && tmp[j].etime!=0)
{
k=k+time_slice+ctime;
tmp[j].etime -= time_slice;
}
}
}
awtime=awtime/n;
atatime=atatime/n;
printf("\nShedule \n");
line(60);
printf("Process\t\tExecution\tWait\t\tTurnaround\n");
printf("Id No\t\ttime\t\ttime\t\ttime\n");
line(60);
for(i=1;i<=n;i++)
printf("%7d\t%14d\t%8d\t%14d \n", p[i].p_id, p[i].etime, p[i].wtime,
p[i].tatime);
line(60);
printf("Avg waiting time :\t%2f \n",awtime);
printf("Avg turn around time :\t%2f\n",atatime);
line(60);
return(0);
}
Input :
Round Robin Scheduling
Enter the no of processes : 3
Enter the time slice : 4
Enter the context switch time : 5
Enter the execution time of process 0 : 6
Enter the execution time of process 1 : 6
Enter the execution time of process 2 : 5
Output:
Schedule
------------------------------------------------------------
Process Execution Wait Turnaround
Id No time time time
------------------------------------------------------------
0 6 8 29
1 6 17 36
2 5 26 42
------------------------------------------------------------
Avg waiting time : 17.000000
Avg turn around time : 35.666668
------------------------------------------------------------
RESULT: Thus the Program was implemented and output was verified successfully.
16
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Ex No.4 (d)
PRIORITY CPU SCHEDULING
Program:
#include<stdio.h>
#include<malloc.h>
void line(int i)
{
int j;
for(j=1;j<=i;j++)
printf("-");
printf("\n");
}
struct process
{
int p_id,priority;
int etime,wtime,tatime;
};
struct process *p,temp;
int i,j,k,l,n;
float awtime=0,atatime=0;
int main()
{
printf("Priority Scheduling\n");
line(29);
printf("Enter the no of processes : ");
scanf("%d",&n);
p=(struct process *) calloc(n+1,sizeof(struct process));
p[0].wtime=0;
p[0].tatime=0;
for(i=1;i<=n;i++)
{
printf("Enter the execution time of process %d:",i-1);
scanf("%d",&p[i].etime);
printf("Enter the priority of process %d:",i-1);
scanf("%d",&p[i].priority);
p[i].p_id=i;
}
for(i=1;i<=n;i++)
for(j=1;j<=n-i;j++)
if(p[i].priority>p[j+1].priority)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
for(i=1;i<=n;i++)
{
p[i].wtime=p[i-1].tatime;
p[i].tatime=p[i-1].tatime+p[i].etime;
17
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
awtime+=p[i].wtime;
atatime+=p[i].tatime;
}
awtime=awtime/n;
awtime=atatime/n;
printf("\nSchedule\n");
line(60);
printf("Process\t\texection\twait\t\tturnaround\n");
printf("Id No\t\t time\t\ttime\t\ttime\n");
line(60);
for(i=1;i<=n;i++)
printf("%7d\t%14d\t%8d\t%14d\n",p[i].p_id,p[i].etime,p[i].tatime);
line(60);
printf("Avg waiting time:\t %2f\n",awtime);
printf("Avg turnaround time:\t %2f\n",atatime);
line(60);
return(0);
}
Input:
Priority Scheduling
-------------------------------------------------------
Enter the no of processes : 3
RESULT: Thus the Program was implemented and output was verified successfully
18
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Ex.No: 5
INTERPROCESS COMMUNICATION USING SHARED MEMORY
AIM:
To implement Inter Process Communication using Message queue.
Program:
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#define SHMKEY 75
#define K 1024
int main()
{
int shmid;
char *addr1;
printf("\n\t\tSENDING---USING SHARED MEMORY\n");
printf("\t\t`````````````````````````````````");
shmid=shmget(SHMKEY,128*K,IPC_CREAT|0777);
addr1=shmat(shmid,0,0);
printf("\n the address is:0x%x\n",addr1);
printf("\n enter the message to send:");
scanf("%s",addr1);
return(0);
}
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#define SHMKEY 75
#define K 1024
int main()
{
int shmid;
char *addr1;
printf("\n\t\treceiving--USING SHARED MEMORY\n");
printf("\t\t`````````````````````````````````");
shmid=shmget(SHMKEY,128*K,IPC_CREAT|0777);
addr1=shmat(shmid,0,0);
printf("\n the address is:0x%x\n",addr1);
printf("\n the received message is:%s\n",addr1);
return(0);
}
19
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
OUTPUT:
RESULT: The IPC using Shared memory segment is implemented and verified successfully.
20
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Ex.No: 6
IMPLEMENTATION OF BANKER’S ALGORITHM
Input:
22
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Output:
The Need is
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1
The Available is 3 3 2
System is in safe mode
Result: The Banker’s Algorithm was implemented and verified successfully.
23
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Ex.No: 7
IMPLEMENTAION OF DINING PHILOSOPHER’S PROBLEM
int main()
{
int ch,a,n,i;
printf("\t\t Dining Philosopher Problem\n");
for(i=0;i<5;i++)
{
state[i]='t';
self[i]='s';
spoon[i]='s';
}
printf("\t\t Initial State of Each Philosopher\n");
printf("\n\t Philosopher No.\t Think/Eat \tStatus \tspoon");
for(i=0;i<5;i++)
{
24
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
printf("\n\t\t %d\t\t%c\t\t%c\t%c\n",i+1,state[i],self[i],spoon[i]);
}
printf("\n 1.Exit \n 2.Hungry\n3.Thinking\n");
printf("\n Enter your choice\n");
printf("\t\t");
scanf("%d",&ch);
while(ch!=1)
{
switch(ch)
{
case 2:
{
printf("\n\t Enter which philosopher is hungry\n");
printf("\t\t");
scanf("%d",&n);
n=n-1;
pickup(n);
break;
}
case 3:
{
printf("\n\t Enter which philosopher is thinking\n");
printf("\t\t");
scanf("%d",&n);
n=n-1;
putdown(n);
break;
}
}
printf("\n\t State of Each philosopher\n\n");
printf("\n\t Philosoper No.\t Thinking\t Hungry");
for(i=0;i<5;i++)
{
printf("\n\t\t %d\t\t%c\t\t%c\t%c\n",i+1,state[i],self[i],spoon[i]);
}
printf("\n 1.Exit\n 2.Hungry\n 3.Thinking\n");
printf("\n Enter your choice\n");
printf("\t\t");
scanf("%d",&ch);
}
}
Output:
25
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
1.Exit
2.Hungry
3.Thinking
Enter your choice: 2
Enter which philosopher is hungry : 4
State of Each philosopher
Philosopher No. Think/Eat Status spoon
1 t s s
2 t s s
3 t s n
4 e s n
5 t s s
1.Exit
2.Hungry
3.Thinking
26
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Ex.No:8(a)
FIRST FIT MEMORY ALLOCATION
Aim: To write a C++ program to implement First Fit Memory allocation technique.
Program:
#include<stdio.h>
#include<string.h>
main()
{
int n,j,i,size[10],sub[10],f[10],m,x,ch,t;
int cho;
printf("\t\t MEMORY MANAGEMENT \n");
printf("\t\t =================\n");
printf("\tEnter the total no of blocks: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter the size of blocks: ");
scanf("%d",&size[i]);
}
cho=0;
while(cho==0)
{
printf("\n Enter the size of the file: ");
scanf("%d",&m);
x=0;
for(i=1;i<=n;i++)
{
if(size[i]>=m)
{
printf("\n size can occupy %d",size[i]);
size[i]-=m;
x=i;
break;
}
}
if(x==0)
{
printf("\n\nBlock can't occupy\n\n");
}
printf("\n\nSNO\t\tAvailable block list\n") ;
for(i=1;i<=n;i++)
printf("\n\n%d\t\t\t%d",i,size[i]);
printf("\n\n Do u want to continue.....(0-->yes/1-->no): ");
scanf("%d",&cho);
}
}
27
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Input:
Output:
size can occupy 50
SNO Available block list
1 25
2 20
3 30
4 40
Do u want to continue.....(0-->yes/1-->no): 0
Enter the size of the file: 28
size can occupy 30
SNO Available block list
1 25
2 20
3 2
4 40
Do u want to continue.....(0-->yes/1-->no):0
Enter the size of the file: 32
size can occupy 40
SNO Available block list
1 25
2 20
3 2
4 8
Do u want to continue.....(0-->yes/1-->no): 0
RESULT:
Thus the memory allocation techniques first fit was implemented.
28
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Ex.No. 8(b)
BEST FIT MEMORY ALLOCATION
#include<stdio.h>
main()
{
int n,j,i,size[10],sub[10],f[10],m,x,ch,t;
int cho;
}
}
if(x==0)
{
printf("block can't occupy");
}
printf("\n\nSNO\t\t Available Block size\n") ;
for(i=1;i<=n;i++)
printf("\n%d\t\t%d",i,size[i]);
printf("\n\n Do u want to continue.....(0-->yes\t/1-->no)");
scanf("%d",&cho);
}
Input:
Output:
size can occupy 100 :
SNO SIZE
1 50
2 5
3 200
4 150
Do u want to continue.....(0-->yes /1-->no)0
30
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
RESULT:
Thus the best fit memory allocation technique was implemented.
31
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Ex.No. 8(c)
WORST FIT MEMORY ALLOCATION
Program:
#include<stdio.h>
int p[10]={0},m=0,x=0,b[10]={0},a[10]={0};
main()
{
int j=0,n=0,pra[10]={0},pro[10]={0},z[10],ch,flag,flag2,sum=0,sum2=0;
int c=0,i=0,k=0;
printf("\n\t\tMEMORY MANAGEMENT POLICIES\n");
printf("\n enter the no of process:\t");
scanf("%d",&n);
printf("\n enter the no of partition:\t");
scanf("%d",&m);
printf("\nprocess information\n");
for(i=0;i<n;i++)
{
printf("\n enter the memory required for process P%d:",i+1);
scanf("\t%d",&a[i]);
pro[i]=a[i];
}
printf("\n memory partition information\n");
for(j=0;j<m;j++)
{
printf("\n enter the block size of block B%d:",j+1);
scanf("\t%d",&p[j]);
pra[j]=p[j];
}
arrange();
printf("\n process partition\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i]<p[j])
{
printf("%d\t%d\n",a[i],p[j]);
p[j]=0;
flag=i;
break;
}
}
if(flag!=i)
printf("%d\t%s\n",a[i],"waiting");
arrange();
32
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
}
}
arrange()
{
int i,j,t;
for(i=0;i<m-1;i++)
for(j=0;j<m-i-1;j++)
{
if(p[j]<p[j+1])
{
t=p[j+1];
p[j+1]=p[j];
p[j]=t;
}
}
}
Input:
Output:
Process partition
212 600
417 500
112 400
321 waiting
460 waiting
RESULT:
Thus the Worst fit memory allocation technique was implemented.
33
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Ex.No: 9(a)
FIRST IN FIRST OUT PAGE REPLACEMENT
Program:
#include<stdio.h>
main()
{
int main_mem,cur=0,i=0,j,fault=0;
static int page[100],page_mem[100],flag,num;
for(i=0;i<100;i++)
page_mem[i]=-2;
printf("\n\t\t\t\t paging->fifo\n");
printf("\n\n Enter the number of pages in main memory ");
scanf("%d",&main_mem);
printf("\n enter no of page references");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\n Enter page reference:");
scanf("%d",&page[i]);
}
printf("\n\t\t\t\t Fifo-> paging \n\n\n");
for(i=0;i<main_mem;i++)
printf("\t page %d",i+1);
for(i=0;i<num;i++)
{
for(j=0;j<main_mem;j++)
if(page[i]==page_mem[j])
{
flag=1;
break;
}
if(!flag)
{
page_mem[cur]=page[i];
fault++;
}
printf("\n\n");
for(j=0;j<main_mem;j++)
printf("\t%d",page_mem[j]);
if(!flag&&cur<main_mem-1)
cur++;
else if(!flag)
34
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
cur=0;
flag=0;
}
printf("\n\n-2 refers to empty blocks\n\n")
printf("\n\n No of page faults:%d\n",fault);
}
Input:
Enter the number of pages in main memory: 3
enter no of page references: 8
Enter page reference: 2
Enter page reference: 0
Enter page reference: 3
Enter page reference: 0
Enter page reference: 2
Enter page reference: 3
Enter page reference: 5
Enter page reference: 9
Output:
FIFO - PAGING
page 1 page 2 page 3
2 -2 -2
2 0 -2
2 0 3
2 0 3
2 0 3
2 0 3
5 0 3
5 9 3
No of page faults: 5
35
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
RESULT:
Thus the FIFO page replacement algorithm was implemented.
36
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Ex.No: 9(b)
LEAST RECENTLY USED PAGE REPLACEMENT
Programs:
#include<stdio.h>
#include<stdlib.h>
#define max 100
int frame[10],count[10],cstr[max],tot,nof,fault;
main()
{
getdata();
push();
}
getdata()
{
int pno,i=0;
printf("\n\t\tL R U - Page Replacement Algorithm\n");
printf("\nEnter No. of Pages in main memory:");
scanf("%d",&nof);
printf("\nEnter the no of page references:\n");
scanf("%d",&pno);
for(i=0;i<pno;i++)
{printf("Enter page reference%d:",i);
scanf("%d",&cstr[i]);
}
tot=i;
for(i=0;i<nof;i++)
printf("\tpage%d\t",i);
}
push()
{
int x,i,j,k,flag=0,fault=0,nc=0,mark=0,maximum,maxpos=-1;
for(i=0;i<nof;i++)
{
frame[i]=-1;
count[i]=mark--;
}
for(i=0;i<tot;i++)
{
flag=0;
x=cstr[i];
nc++;
for(j=0; j<nof; j++)
{
for(k=0; k<nof;k++)
count[k]++;
37
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
if(frame[j]==x)
{
flag=1;
count[j]=1;
break;
}
}
if(flag==0)
{
maximum = 0;
for(k=0;k<nof;k++)
{
if(count[k]>maximum && nc>nof)
{
maximum=count[k];
maxpos = k;
}
}
if(nc>nof)
{
frame[maxpos]=x;
count[maxpos]=1;
}
else
frame[nc-1]=x;
fault++;
dis();
}
}
printf("\nTotal Page Faults :%d",fault);
}
dis()
{
int i=0;
printf("\n\n");
while(i<nof)
{
printf("\t%d\t",frame[i]);
i++;
}
}
Input:
Output:
Page 0 page 1 page 2
0 -1 -1
0 1 -1
0 1 2
5 1 2
5 0 2
5 0 1
Result:
Thus the page replacement algorithm LRU was implemented.
39
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
AIM:
To implement paging concept.
PROGRAM:
#include<stdio.h>
Structptable
{
Intpno, staddr;
} p[50];
Structpcount
{
Intpnum;
Char data[50];
}pg[50][50];
Main()
{
Intn,psize,I,j,k,st,pagenum,disp,phyaddr,laddr,found=0;
Printf(“\n Memory in formation ”);
Printf(“\n Enter the no of pages: ”);
Scanf(“%d”,&n);
Printf(“\n Enter page size: ”);
Scanf(“%d”,&psize);
For(i=1;i<=n;i++)
{
Printf(“\n Enter the starting address for page[%d] ”,i);
Scanf(“%d”,&st);
For(j=1;j<=psize;j++)
{
Printf(“page[%d] \t data”,st);
Scanf(“%s”,&pg[i][j],data);
Pg[i][j].pnum=st;
St=st+1;
}
}
Printf(“\n memory \n”);
For(i=1;i<=n;i++)
{
For(j=1;j<=psize;j++)
{
Printf(“\n page[%d] \t data %s”,pg[i][j].pnum,pg[i][j].data);
}
}
Printf(“\n Page table details \n”);
Printf(“\n Enter the starting address for the page:”);
For(k=1;k<=n;k++)
{
Printf(“page[%d] \t starting address:\n”,k);
P(i<pno)=k;
Scanf(“%d”,&p(k),staddr);
40
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
}
Printf(“\n page number \t starting address \n”);
For(k=1;k<=n;k++)
{
Printf(“\n %d \t \t %d \n”,p(k).pno,p(k).staddr);
}
Printf(“\n Enter the logical address:”);
Scanf(“%d”,&laddr);
Page num=laddr\100;
Disp=laddr%100;
Printf(“\n pagenum; %d \t disp; %d ”,pagenum,disp);
For(k=1;k<=n;k++)
If(p(k).pno=pagenum)
{
Phyaddr=p(k).staddr+disp;
Found=1;
Break; }
If(found==1)
{
Printf(“\n physical address:%d ”,phyaddr);
For(j=1;j<=psize;j++)
{
If(pg[pagenum][j].pnum==phyaddr)
Printf(“\n data %s \n”,pg[pagenum][j].data);
}}
Else
Printf(“\n not found \n”); }
41
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
OUTPUT:
Memory in formation
Enter the no of pages:2
Enter page size:2
Enter the starting address for page(1):100
Page(100)data:df
Page(101)data:dsf
Enter the starting address for page(2):200
Page(200)data:dskf
Page(201)data:maan
Memory
Page(100)data:df
Page(101)data:dsf
Page(200)data:dskf
Page(201)data:maan
Page table details
Enter the starting address for the page:
Page[1] starting address:100
Page[2] starting address:200
Page number Starting address
1 100
2 200
RESULT:
42
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
AIM:
To implement segmentation concept .
PROGRAM:
#include<stdio.h>
Structptable
{
Intpno, staddr;
} p[50];
Structpcount
{
Intpnum;
Char data[50];
}pg[50][50];
Main()
{
Intn,psize,I,j,k,st,pagenum,disp,phyaddr,laddr,found=0;
Printf(“\n Memory in formation ”);
Printf(“\n Enter the no of pages: ”);
Scanf(“%d”,&n);
Printf(“\n Enter page size: ”);
Scanf(“%d”,&psize);
For(i=1;i<=n;i++)
{
Printf(“\n Enter the starting address for page[%d] ”,i);
Scanf(“%d”,&st);
For(j=1;j<=psize;j++)
{
Printf(“page[%d] \t data”,st);
Scanf(“%s”,&pg[i][j],data);
Pg[i][j].pnum=st;
St=st+1;
}
}
Printf(“\n memory \n”);
For(i=1;i<=n;i++)
{
For(j=1;j<=psize;j++)
{
Printf(“\n page[%d] \t data %s”,pg[i][j].pnum,pg[i][j].data);
}
}
Printf(“\n Page table details \n”);
Printf(“\n Enter the starting address for the page:”);
For(k=1;k<=n;k++)
{
Printf(“page[%d] \t starting address:\n”,k);
P(i<pno)=k;
Scanf(“%d”,&p(k),staddr);
43
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
}
Printf(“\n page number \t starting address \n”);
For(k=1;k<=n;k++)
{
Printf(“\n %d \t \t %d \n”,p(k).pno,p(k).staddr);
}
Printf(“\n Enter the logical address:”);
Scanf(“%d”,&laddr);
Page num=laddr\100;
Disp=laddr%100;
Printf(“\n pagenum; %d \t disp; %d ”,pagenum,disp);
For(k=1;k<=n;k++)
If(p(k).pno=pagenum)
{
Phyaddr=p(k).staddr+disp;
Found=1;
Break; }
If(found==1)
{
Printf(“\n physical address:%d ”,phyaddr);
For(j=1;j<=psize;j++)
{
If(pg[pagenum][j].pnum==phyaddr)
Printf(“\n data %s \n”,pg[pagenum][j].data);
}}
Else
Printf(“\n not found \n”); }
44
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
OUTPUT:
memory information
----------------------------
Enter the no of segments:2
Enter starting address and limit of the segment(1): 100 2
Segment(100) data: maan
Segment(101) data:ford
Enter starting address and limit of the segment(2): 200 3
Segment(200) data: raj
Segment(201) data: srini
Segment(202) data: data
Memory address data
100 maan
101 ford
200 raj
201 srini
202 data
Segmentation table details
----------------------------------
Segment starting address length
1 100 2
2 200 3
Enter logical address:100
Segment no:1
Disp 0
RESULT:
45
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Aim
To implement file allocation on free disk space in a contiguous manner.
Program
/* Contiguous Allocation - cntalloc.c */
#include <stdio.h>
#include <string.h>
int num=0, length[10], start[10];
char fid[20][4], a[20][4];
void directory()
{
int i;
printf("\nFile Start Length\n");
for(i=0; i<num; i++)
printf("%-4s %3d %6d\n",fid[i],start[i],length[i]);
}
void display()
{
int i;
for(i=0; i<20; i++)
printf("%4d",i);
printf("\n");
for(i=0;i<20;i++)
printf("%4s", a[i]);
}
main()
{
int i,n,k,temp,st,l,ch,flag;
char id[4];
for(i=0; i<20; i++)
strcpy(a[i],"");
printf("Disk space before allocation:\n");
display();
do
{
printf("\nEnter File name (max 3 char) : ");
scanf("%s", id);
printf("Enter start block : ");
scanf("%d", &st);
printf("Enter no. of blocks : ");
scanf("%d", &l);
46
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
strcpy(fid[num],id);
length[num]=l;
flag = 0;
CS2257 Operating System Lab
if((st+l) > 20)
{
printf("Requirement exceeds range\n");
continue;
}
for(i=st; i<(st+l); i++)
if(strcmp(a[i],"") != 0)
flag = 1;
if (flag == 1)
{
printf("Contiguous allocation not possible.\n");
continue;
}
start[num] = st;
for(i=st; i<(st+l); i++)
strcpy(a[i],id);;
printf("Allocation done\n");
num++;
printf("\nAny more allocation (1. yes / 2. no)? : ");
scanf("%d",&ch);
} while (ch == 1);
printf("\n\t\t\tContiguous Allocation\n");
printf("Directory:");
directory();
printf("\nDisk space after allocation:\n");
display();
printf("\n");
}
Output
$ gcc cntalloc.c
$ ./a.out
Disk space before allocation:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Enter File name (max 3 char) : ls
Enter start block : 3
Enter no. of blocks : 4
Allocation done
Any more allocation (1. yes / 2. no)? : 1
Enter File name (max 3 char) : cp
Enter start block : 14
Enter no. of blocks : 3
Allocation done
Any more allocation (1. yes / 2. no)? : 1
Enter File name (max 3 char) : tr
47
B.Tech CSE Operating Systems Lab (Staff Manual) -2013 BCSL1204
Result
Thus contiguous allocation is done for files with the available free blocks.
48