Os Lab Manul-1
Os Lab Manul-1
CLASS:SE SEMESTER:IV
LABORATORY MANUAL
(CSL403)
INSTITUTE VISION
INSTITUTE MISSION
M1-To teach students basic concepts of science and fundamental engineering principles by encouraging inter
disciplinary approach for comprehensive broad-based learning.
M2-To provide hands-on training by executing project-based learning, industrial internships and value-added
programs.
M3-To train professional thinking and attitude building to be sensitive to needs of the society.
DEPARTMENTVISION
To become a meritorious branch of Computer Engineering for imparting high quality education and technical
skills necessary for industrial needs and society
DEPARTMENTMISSION
M1-To enhance career opportunities through project based learning and value added programs.
M2-To develop graduates for competitive examinations and inculcate entrepreneurial skills to acquire higher
goals of excellence.
M3-To include professional behavior, ethical values and inter disciplinary research capabilities.
Program Outcomes
PEO
Program Educational Objectives Statements
Code
To prepare the Learner with a sound foundation in the mathematical, scientific and
PEO.1
Engineering fundamentals.
To motivate the Learner in the art of self-learning and to use modern tools for
PEO.2
solving real life problems.
To equip the Learner with broad education necessary to understand the impact of
PEO.3
Computer Science and Engineering in a global and social context.
LO.2 Implement various process scheduling algorithms and evaluate their performance.
Name of Student:
ARMIETPIN:
Roll No.
Exam Seat No.
INDEX
Sr.No. Name of Experiment
1.1 Explore usage of basic Linux Commands and system calls for file,
directory and process management.
1
For eg (mkdir, chdir, cat, ls, chown. chmod, chgp,ps etc.
systemcalls:open,read,write,close,getpid,setpid, getuid, getgid, getegid.geteuid,
sort, grep, awk, etc.) [LO1]
e. Display current shell, home directory, operating system type, current path
setting, current working directory.[L01]
Linux- API
3.1 Implement anyone basic commands of Linux like is, cp, my and others using
3
kernel APIs.[LO1]
Linux-Process
a. Create a child process in Linux using the fork system call. From the child
process obtain the process ID of both child and parent by using get pid and get
4 pid system call.
1
6. Task: To display current system time.
Syntax: Date+%H
Explanation: This command displays the current system time on the screen.
Conclusion: In this experiment we have run all the basic linux commands. Through this experiment, you
have become familiar with basic unix commands. These commands are essential for learning of unix
command (date, mkdir, chdir, cat, echo,cal).Experiment with these commands further to gain confidence
and proficiency in unix command.
2
EXPERIMENTNO:2
AIM: Write shell scripts to do the following:
uname-a;
echo"Releasenumberis."un
ame-1:
Output
root@rajesh-optiplex-3020:--Ssh exp5.sh
OS Name is:
Linux
3
Release number is:
GNU/Linux Release
numberis:4.4.0-81-
generic
root@rajesh-optiplex-3020:-
psaxl|head-n10
Output
root@rajesh-optiplex-3020:~$sh expl.sh
Top 10
processes in decending order is:
4
FUID PID PPIDPRI NI VSZRSSWCHAN STATE TTYTIMEScommand
1 0 2 0 20 0 0 0 - S ? 0:00 [kthreadd]
1 0 3 2 20 0 0 0 - S ? 0:00 [ksoftinqd/0]
1 0 7 2 20 0 0 0 - S ? 0:44 [reu_sched]
1 0 8 2 20 0 0 0 - S ? 0:00[reu_bh]
1 0 9 2 -100- 0 0 - S ? 0:00[migration/0]
5 0 10 2-100 - 0 0 - S ? 0:00[watchdog/0]
5 0 11 2-100 - 0 0 - S ? 0:00[watchdog/1]
5
C. Display processes with high est memory usage.
Program
echo"Displayprocesseswithhighestmemoryusage";ps
-eopid,ppid.cmd,%mem,%cpu--sort=-%memhead
Output
root@rajesh-optiplex3020:~$
6
D. Displaycurrent logged in userandlogname.
PROGRAM
echo"LOGGEDINUSERSARE:-\n";
who -u
echo"NUMBEROFLOGGEDINUSERSARE:-\n";
who-u|we-]
Output
root@rajesh-optiplex-3020:~$sh exp3.sh
LOGGED IN USERS ARE:-
E. Display current shell, home directory, operating system type, current path setting,
current working directory.
Program
echo“Currenthomedirectoryis:":
whoami
echo "Current operatingsystemtype
is:"uname
echo “Curent current workingdirectoryis:”
pwd
Output
root@rajesh-optiplex-3020:-shexp4.nh
Current home directory is:
root
Currentoperatingsystem typeis:
Linux
7
Currentcurrentworkingdirectoryis:
/home/root
root@majesh-optiplex-3020:-$
Conclusion: In this experiment we have successfully perform top 10 process in descending order, processes
with highest memory usage and current login user name.
8
EXPERIMENTNO: 3
AIM: Implement any one basic commands of Linux like is, cp, my and others using kernel APIs.
DIR*dp:
struct dirent*sd:
dp = opendir(argv[1]);
while((sd=readdir(dp))!=NULL)
{
printf("%s\t",sd->d_name);
closedir(dp):
9
Output:
Conclusion: Here we have studied and perform basic Linux commands cp, my and others using kernel APIs.
To learn how kernel work
10
EXPERIMENTNO:4
AIM: To write the program to implement the system calls getpid() and getppid().
PROGRAMCODING:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>int
main( )
{
int pid;
pid=fork();
if(pid==-1)
{
perror(“forkfailed”);
exit(0);
}
if(pid==0)
printf(“\nChildprocessisunder execution”);
11
else
printf(“\nParentprocessisunderexecution”);
printf(“\nProcessidof theparentprocessis%d”,getpid());
return(0);
OUTPUT:
12
EXPERIMENTNO:5(A)
intbt[20],wt[20],tat[20],i,n; float
wtavg, tatavg;
clrscr();
printf("\nEnterthenumberofprocesses -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
13
= tat[i-1]+bt[i];wtavg
=wtavg+wt[i];
tatavg=tatavg+ tat[i];}
printf("\t PROCESS\tBURSTTIME\tWAITINGTIME\tTURNAROUNDTIME\n");
for(i=0;i<n;i++)
OUTPUT:
P0 24 0 24
P1 3 24 27
P2 3 27 30
14
EXPERIMENT NO: 5(B)
ROUNDROBIN:
SOURCECODE
#include<stdio.h>
main()
{
inti,j,n,bu[10],wa[10],tat[10],t,ct[10],max;float
awt=0,att=0,temp=0;
clrscr();
printf("Entertheno of processes--"); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnterBurst Time forprocess %d --", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnterthe size oftimeslice -- ");
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];for(j=0;j<(max/t)+1;j++)for(i=0;i<n;i++)if(bu[i]!=0)
if(bu[i]<=t) {
15
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
}
else {
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++){
wa[i]=tat[i]-
ct[i];att+=tat[i];
awt+=wa[i];}
printf("\nTheAverage Turnaround timeis --%f",att/n);
printf("\nThe Average Waiting time is -- %f ",awt/n);
printf("\n\tPROCESS\tBURSTTIME\tWAITINGTIME\tTURNAROUNDTIME\n");
for(i=0;i<n;i++)
16
OUTPUT:
0 24 6 30
1 3 4 7
2 3 7 10
Conclusion: Here we have successfully studied and perform the non-premptive process scheduling and
preemptive scheduling algorithm. Some time preemptive scheduling algorithm is give better response and vice
versa. It all depends on how a scheduling algorithm increases CPU utilization while decreasing average
process waiting time.
17
EXPERIMENTNO:6
AIM: Write a C program to implement solution of Producer consumer problem through Semaphore.
ALGORITHM:
2. If there is empty space in the buffer check the mutex value for enter into the critical section.
3. If the mutex value is 0, allow the producer to add value in thetemporaryvariable to the
buffer.
ii) If there is anyitem in the buffer check for mutex value, ifthe mutex==0, removeitem from
buffer
iii) Signal the mute x value and reduce the empty value by 1.
18
PROGRAMCODING:
#include<stdio.h>
intmutex=1,full=0,empty=3,x=0;
main()
{
intn;
void producer();
void consumer();
int wait(int);
intsignal(int);
printf("\n1.producer\n2.consumer\n3.exit\n");while(1)
{
case 1:
if((mutex==1)&&(empty!=0))
producer();
else
printf("bufferisfull\n");
break;
case2:if((mutex==1)&&(full!=0))
consumer();
else
printf("bufferisempty");
break;
19
case 3:
exit(0);
break;
}
}
}
intwait(ints)
{
return(--s);
}
intsignal(ints)
{
return(++s);
}
voidproducer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\n producerproducestheitems %d",x);
mutex=signal(mutex);
}
voidconsumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nconsumerconsumestheitem%d",x);x--);
mutex=signal(mutex);
}
20
RESULT : Thus the program was executed successfully
OUTPUT :
[cse6@localhost Pgm]$ccex11.c-oex11.out
[cse6@localhost Pgm]$ ./ex11.out
Produced element a
Consumedelementa
Produced element b
Consumedelementb
Produced element c
Consumedelementc
Produced element d
Consumedelementd
Produced element e
Consumedelemente
Produced element f
Consumed element f
Produced element g
Consumedelementg
Produced element h
Consumedelementh
21
EXPERIMENTNO:7(A)
AIM:A)Write a program to demonstrate the concept of deadlock avoidance through Banker's Algorithm
ALGORITHM:
Step1:StarttheProgram
Step 2: Obtain the required data through char and in data types.
Step 3: Enter the filename, index block.
Step4:Printthefilenameindexloop.
Step5:Fileisallocatedto theunusedindexblocks
intfound,flag,l,p[4][5],tp,tr,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;clrscr();
printf("Enter total no of processes");
scanf("%d",&tp);
printf("Entertotalnoofresources");
scanf("%d",&tr);
printf("Enterclaim(Max. Need)matrix\n");
for(i=1;i<=tp;i++)
22
{
printf("process%d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&c[i][j]);
}
printf("Enterallocationmatrix\n");
for(i=1;i<=tp;i++)
{
printf("process%d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&p[i][j]);
}
printf("Enter resource vector (Total resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&r[i]);
}
printf("Enter availability vector (available resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=1;i<=tp;i++)
{
sum=0;
for(j=1;j<=tr;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
23
k++;
}
}
for(i=1;i<=tp;i++)
{
for(l=1;l<k;l++)
if(i!=m[l])
{
flag=1;
for(j=1;j<=tr;j++)
if(c[i][j]<temp[j])
{
flag=0;
break;
}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j<=tr;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are:");
for(j=1;j<=tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
24
printf("%d\t",j);
}
getch();
}
OUTPUT:
25
EXPERIMENTNO:7(B)
{
inti; clrscr();
printf("\n\nDININGPHILOSOPHERPROBLEM");
printf("\nEnterthetotalno. ofphilosophers: ");
scanf("%d",&tph);
for(i=0;i<tph;i++)
{
philname[i]=(i+1);status[i]=1;
}
printf("Howmanyarehungry: ");
scanf("%d", &howhung);
if(howhung==tph)
{
printf(“\n All arehungry..\nDeadlock stage will occur”);
printf(\n”Exiting\n”);
else{
for(i=0;i<howhung;i++){
printf(“Enterphilosopher%dposition:”,(i+1));
scanf(“%d”,&hu[i]);
status[hu[i]]=2;
}
do
26
{
printf("1.Onecaneatatatime\t2.Twocan eatatatime
\t3.Exit\nEnter your choice:");
scanf("%d",&cho);switch(cho)
{
case1:one();
break;
case2:two();
break;
case3:exit(0);
default: printf("\nInvalidoption..");
}
}
while(1);
}
}
one()
{
intpos=0,x,i;
printf("\nAllow one philosopherto eat at anytime\n");
for(i=0;i<howhung; i++, pos++)
{
printf("\nP %d is granted to eat", philname[hu[pos]]);
for(x=pos;x<howhung;x++)
printf("\nP %d iswaiting",philname[hu[x]]);
}
}
two()
{
int i,j,s=0, t, r, x;
printf("\nAllowtwophilosopherstoeatatsame
27
time\n");for(i=0;i<howhung;i++)
{
for(j=i+1;j<howhung;j++)
{
if(abs(hu[i]-hu[j])>=1&&abs(hu[i]-hu[j])!=4)
{
printf("\n\ncombination%d\n",(s+1));
t=hu[i];
r=hu[j];s++;
printf("\nP %d and P %d are granted to eat", philname[hu[i]],
philname[hu[j]]);
for(x=0;x<howhung;x++)
{
if((hu[x]!=t)&&(hu[x]!=r))
printf("\nP %d iswaiting",philname[hu[x]]);
}
}
INPUT:
Enterphilosopher2position:4
combination1
P3andP5aregrantedtoeatP0 is
waiting
combination2
P3andP0aregrantedtoeatP5 is
waiting
combination3
P5andP0aregrantedtoeatP3 is
waiting
1. One can eatatatime2.Twocan
eatatatime3.ExitEnteryourchoice:3
Conclusion: In this experiment we have perform deadlock detection using Banker’s algorithm and Dining
Philospher's Problem. Hence successfully perform Banker’s algorithm and Dining Philospher's Problem.
29
EXPERIMENTNO:8(A)
AIM:A)Write a program to demonstrate the concept of MVT and MFT memory management
techniques.
PROGRAM(MVT):
#include<stdio.h>
#include<conio.h>
main()
{
int ms, bs, nob, ef,n,
mp[10],tif=0;inti,p=0;
clrscr();
printf("Enterthetotal memoryavailable (in Bytes) --");
scanf("%d",&ms);
printf("Entertheblocksize (in Bytes)-- "); scanf("%d",
&bs);
nob=ms/bs;ef=ms
-nob*bs;
printf("\nEnterthenumberofprocesses -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter memoryrequired for process %d (in Bytes)-- ",i+1);
scanf("%d",&mp[i]);
}
30
printf("\nNo. of Blocks available in memory--%d",nob);
printf("\n\nPROCESS\tMEMORYREQUIRED\tALLOCATED\tINTERNALFRAGMENTATION");
for(i=0;i<n &&p<nob;i++)
{
printf("\n%d\t\t%d",i+1,mp[i]);
if(mp[i] > bs)
printf("\t\tNO\t\t---");
else
{
printf("\t\tYES\t%d",bs-mp[i]);
tif = tif + bs-mp[i];
p++;
}
}
if(i<n)
printf("\nMemoryis Full, Remaining Processes cannot be accomodated");
printf("\n\nTotal Internal Fragmentation is %d",tif);
printf("\nTotal External Fragmentationis %d",ef);
getch();
}
INPUT
31
Enter memory required for process 1 (in Bytes) -- 275
Enter memory required for process 2 (in Bytes)--400
OUTPUT
PROGRAM(MFT):
#include<stdio.h>
#include<conio.h>
main()
{
int ms,mp[10],i,
temp,n=0;charch='y';
clrscr();
32
printf("\nEnterthetotal memoryavailable (in Bytes)-- ");
scanf("%d",&ms);
temp=ms;
for(i=0;ch=='y';i++,n++)
{
printf("\nEnter memoryrequired for process %d (in Bytes) -- ",i+1);
scanf("%d",&mp[i]);
if(mp[i]<=temp)
{
printf("\nMemoryis allocated for Process %d ",i+1);
temp = temp - mp[i];
}
else
{
printf("\nMemoryisFull");break;
}
printf("\nDo you wantto continue(y/n)--");
scanf(" %c", &ch);
}
printf("\n\nTotal Memory Available -- %d", ms);
printf("\n\n\tPROCESS\t\t MEMORY ALLOCATED ");
for(i=0;i<n;i++)
printf("\n \t%d\t\t%d",i+1,mp[i]);
printf("\n\nTotal MemoryAllocated is %d",ms-temp);
printf("\nTotal External Fragmentation is %d",temp);
getch();
}
33
OUTPUT:
Enter the total memory available (in Bytes) – 1000
Enter memory required for process 1 (in Bytes)– 400
Memory is allocated for Process 1
PROCESS MEMORY
ALLOCATED
1 400
2 275
Total Memory Allocated is 675 Total
External Fragmentation is 325
Conclusion: Here we have learned and perform memory management technique. Here we also calculate
Internal fragmentation of process.
34
EXPERIMENTNO:8(B)
AIM: B) Write a program to demonstrate the concept of dynamic partitioningplacement algorithms i.e. Best
Fit, First Fit, Worst-Fit etc.
PROGRAM
WORST-FIT
#include<stdio.h>
#include<conio.h>#def
inemax25void main()
{
intfrag[max],b[max],f[max],i,j,nb,nf,t
emp; static int bf[max],ff[max];
clrscr();
printf("\n\tMemoryManagementScheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enterthenumber offiles:");
scanf("%d",&nf);
printf("\nEnterthe sizeoftheblocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block%d:",i);
scanf("%d",&b[i]);
}
printf("Enterthesizeofthefiles:-\n");
35
for(i=1;i<=nf;i++)
{
printf("File%d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
36
INPUT
Enter the number of blocks: 3
Enter the number of files: 2
Enter the size of the blocks:-
Block 1: 5
Block2:2
Block3:7
Enterthesizeofthefiles:-
File 1: 1
File 2: 4
OUTPUT
37
BEST-FIT
#include<stdio.h>
#include<conio.h>#def
inemax25void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;static
int bf[max],ff[max];
clrscr();
printf("\nEnterthenumberof blocks:");
scanf("%d",&nb);
printf("Enterthenumber offiles:");
scanf("%d",&nf);
printf("\nEnterthe sizeoftheblocks:-\n");
for(i=1;i<=nb;i++)
printf("Block%d:",i);
scanf("%d",&b[i]);
38
for(j=1;j<=nb;j++)
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;bf[ff[i]]=1;lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf&& ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
INPUT
Enterthenumberofblocks: 3
Enter the number of files: 2
Enter the size of the blocks:-
Block 1: 5
Block2:2
Block3:7
39
Enter the size of the files:-
File 1: 1
File2:4
OUTPUT
40
FIRST-FIT
#include<stdio.h>
#include<conio.h>#def
inemax25void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
scanf("%d",&b[i]);
printf("Enterthesizeofthefiles:-\n");for(i=1;i<=nf;i++)
{
printf("File%d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
41
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)//ifbf[j]isnotallocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
}
}
frag[i]=highest;bf[ff[i]]=1;highest=0;
}
ff[i]=j; highest=temp;
}
printf("\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
INPUT
Enter the number ofblocks:3
Block3:7
42
Enterthesizeofthefiles:-
File 1: 1
File2:4
OUTPUT
Conclusion: Here we have perform different type of memory management technique like Best Fit, Worst Fit,
First Fit.and successfully calculate the entire algorithm.
43
EXPERIMENT NO: 9
A) FIRSTINFIRSTOUT
SOURCE CODE :
#include<stdio.h>
#include<conio.h>i
nt fr[3]; void
main()
{
voiddisplay();
int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2};
intflag1=0,flag2=0,pf=0,frsize=3,top=0;
clrscr();
for(i=0;i<3;i++){
fr[i]=
-1;
}
for(j=0;j<12;j++)
{
flag1=0;flag2=0; for(i=0;i<12;i++){ if(fr[i]==page[j])
{
flag1=1;flag2=1;break;}}
if(flag1==0) {
for(i=0;i<frsize;i++) {
if(fr[i]==-1)
{
44
fr[i]=page[j];flag2=1;break;}}}
if(flag2==0) {
fr[top]=page[j];
top++;
pf++;if(top>=frsize)
top=0;
}
display();
}
voiddisplay()
{
int i; printf("\n");
for(i=0;i<3;i++)
printf("%d\t",fr[i]);
}
OUTPUT:
2 -1 -1
23-1
23-1
231
531
45
521
524
524
324
324
354
352
46
B) LEASTRECENTLYUSED
SOURCE CODE :
#include<stdio.h>
#include<conio.h>i
nt fr[3]; void
main()
{
voiddisplay();
int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
intindex,k,l,flag1=0,flag2=0,pf=0,frsize=3;
clrscr();
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0,flag2=0;
for(i=0;i<3;i++)
{
if(fr[i]==p[j])
{
flag1=1;flag2=1;
break;
}
}
if(flag1==0)
47
{
for(i=0;i<3;i++){
if(fr[i]== -1)
{
fr[i]=p[j];flag2=1;
break; }}}
if(flag2==0) {
for(i=0;i<3;i++)
fs[i]=0;
for(k=j-1,l=1;l<=frsize-1;l++,k--
)
{
for(i=0;i<3;i++) {
if(fr[i]==p[k])fs[i]=1;
}
}
for(i=0;i<3;i++){
if(fs[i]==0)
index=i; }
fr[index]=p[j];
pf++; }
display();}
printf("
\nnoofpage faults:%d",pf+frsize);
getch(); }
void display() {
inti;printf("\n");
for(i=0;i<3;i++)
printf("\t%d",fr[i
]);
}
48
OUTPUT:
2-1 -1
23-1
23-1
231
251
251
254
254
354
352
352
352
No of page faults : 7
Conclusion: Here we have perform different type of Page replacement algorithm (FIFO,LRU) and FIFO
have 9 page fault and LRU have 7 page fault. Thus in this process LRU give the better result than FIFO.
49
EXPERIMENT NO: 10(A)
AIM: Write a C program to simulate File allocation strategies typically sequential, indexed and
linked files.
A. SEQUENTIAL:
SOURCECODE:
#include<stdio.h>
main()
{
intf[50],i,st,j,len,c,k;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
X:
f[j]=1;
printf("\n%d->%d",j,f[j]);
50
else
{
printf("Block already allocated");
break;
}
if(j==(st+len))
printf("\n the file is allocated to disk");
printf("\n if u want to enter more files?(y-1/n-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch();
}
OUTPUT:
Enter the starting block & length offile 4 10
4->1
5->1
6->1
7->1
8->1
9->1
10->1
11->1
12->1
13->1
The file is allocated to disk.
51
B. INDEXED:
SOURCECODE:
#include<stdio.h>
intf[50],i,k,j,inde[50],n,c,count=0,p;
main(){
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x: printf("enterindex block\t");
scanf("%d",&p);if(f[p]==0)
{
f[p]=1;
printf("enterno offiles on index\t");
scanf("%d",&n); }else {
printf("Block already allocated\n");
gotox;
}
for(i=0;i<n;i++)
scanf("%d",&inde[i]);
for(i=0;i<n;i++)
if(f[inde[i]]==1)
{
printf("Blockalreadyallocated");
goto x;
}
for(j=0;j<n;j++)
f[inde[j]]=1;
52
printf("\n allocated " );
printf("\n fileindexed");
for(k=0;k<n;k++)
printf("\n%d->%d:%d",p,inde[k],f[inde[k]]);
Enternooffilesonindex 31 2 3
Allocated
Fileindexed
9->1:1
9->2;1
53
C. LINKED:
SOURCECODE:
#include<stdio.h>
main()
{
intf[50],p,i,j,k,a,st,len,n,c;
clrscr();
for(i=0;i<50;i++)f[i]=0;
X:
printf("Enter the starting index block &
length");scanf("%d%d",&st,&len);k=len;
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
54
else
{
printf("\n%d->file is already allocated" ,j);
k++;
}
}
printf("\n Ifuwanttoenterone
more file? (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto
X;
else
exit();
getch();
}
OUTPUT:
Enter how many blocks that are already allocated 3 Enter the blocks no.s
that are already allocated 4 7 Enter the starting index block &length 3 7 9
3->1
4->1fileisalreadyallocated 5-
>1
6->1
7->1fileisalreadyallocated 8-
>1
9->1file isalreadyallocated
10->1
11->1
12->1
55
EXPERIMENT NO: 10(B)
#include<stdio.h>
#include<graphics.h>s
truct tree_element
{
charname[20];
int x,y,ftype,lx,rx,nc,level;
structtree_element*link[5];
};
typedeftructtree_element
node; void main()
{
intgd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"null",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\bgi");
display(root);
getch();
closegraph();
}
create(node**root,intlev,char*dname,intlx,intrx,intx)
56
{
int i,gap;
if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
printf("entername of dir/file(under %s):",dname); fflush(stdin);
gets((*root)->name);
if(lev==0||lev==1)
(*root)->ftype=1; else
(*root)->ftype=2;
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
if(lev==0||lev==1)
{
if((*root)->level==0)
printf("Howmanyusers");
else
printf("hoe many files");
printf("(for%s):",(*root)->name);
scanf("%d",&(*root)->nc);
}
else(*root)->nc=0;
57
if((*root)->nc==0) gap=rx-lx;
else gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)- >nc;i++)
create(&((*root)>link[i]),lev+1,(*root)>name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
else(*root)->nc=0;
display(node *root)
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root!=NULL)
{
for(i=0;i<root->nc;i++)
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
if(root->ftype==1) bar3d(root->x-20,root->y-10,root->x+20,roo>y+10,0,0);
else
58
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
display(root->link[i]);
RESULT:
59
EXPERIMENT NO: 10(C)
main()
{
int t[20],n, I, j, tohm[20],tot=0; float avhm;
clrscr();
printf(“entertheno.oftracks”);
scanf(“%d”,&n);
printf(“enterthetracksto betraversed”);
for(i=2;i<n+2;i++)
scanf(“%d”,&t*i+);
for(i=1;i<n+1;i++)
{
tohm[i]=t[i+1]-t[i];
if(tohm[i]<0)
tohm[i]=tohm[i]*(-1);
}
for(i=1;i<n+1;i++)
tot+=tohm[i];
avhm=(float)tot/n;
printf(“Tracks traversed\tDifference between tracks\n”);
for(i=1;i<n+1;i++)printf(“%d\t\t\t%d\n”,t*i+,tohm*i+);
60
printf("\nAverage header movements:%f",avhm);
getch();
}
INPUT
Enterno.oftracks:9
55 45
58 3
60 2
70 10
18 52
90 72
150 60
160 10
184 24
61
B) SCAN DISK SCHEDULING ALGORITHM
#include<stdio.h>
main()
{
printf("enterthetracks");
for(i=2;i<n+2;i++)
scanf("%d",&t[i]);
for(i=0;i<n+2;i++)
{
for(j=0;j<(n+2)-i-1;j++)
{
if(t[j]>t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
}
}
}
62
for(i=0;i<n+2;i++)
if(t[i]==h)
j=i;k=i;
p=0;
while(t[j]!=0)
{
atr[p]=t[j];j--;
p++;
}
atr[p]=t[j];
for(p=k+1;p<n+2;p++,k++)
atr[p]=t[k+1];
for(j=0;j<n+1;j++)
{
if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];
}
printf("\nAverage header movements:%f",(float)sum/n);
getch();
}
INPUT
Enterno.oftracks:9
Entertrackposition:55586070 1890150160 184
63
OUTPUT
150 50
160 10
184 24
90 94
70 20
60 10
58 2
55 3
18 37
64
C) C-SCAN DISK SCHEDULING ALGORITHM
#include<stdio.h>
main()
{
int t[20], d[20], h,i, j, n,temp, k,atr[20], tot, p, sum=0; clrscr();
printf("enterthe no oftracks to betraveresed");
scanf("%d'",&n);
printf("entertheposition ofhead");
scanf("%d",&h);
t[0]=0;t[1]=h;
printf("entertotaltracks");
scanf("%d",&tot);
t[2]=tot1;
if(t[j]>t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp
}
for(i=0;i<=n+2;i++)
if(t[i]==h);j=i;break;
p=0;
65
while(t[j]!=tot-1)
{
atr[p]=t[j];
j++;
p++;
}
atr[p]=t[j];
p++;
i=0;
while(p!=(n+3)&&t[i]!=t[h])
{
atr[p]=t[i];i++;
p++;
}
Page67for(j=0;j<n+2;j++)
{
if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];}
printf("total header movements%d",sum);
printf("avg is %f",(float)sum/n); getch();}
INPUT
66
OUTPUT
Tracks traversed Difference Between tracks
150 50
160 10
184 24
18 240
55 37
58 3
60 2
70 10
90 20
Averageseektime:35.7777779
Conclusion: In this experiment we have studied and successfully perform various types of File management
techniques and calculate the seek time of all the techniques.
THANKYOU!
67