OS Lab Manual 2023-24
OS Lab Manual 2023-24
H.K.E. Society’s
H
Sir M.. Visvesvaraya College
e of Engineering
Department. of Computer Science & Engineering
(Affiliated to VTU - Belagavi, Approved by AICTE – New Delhi, Accredited by NAAC)
Yeramarus Camp, Raichur – 584135
VISION
MISSION
To provide young computing aspirants with
best practices in teaching
teaching-learning
learning with
highly experienced faculty
To team up with industries and provide
experience on latest tools to excel in
promising technologies.
To prepare students with required
fundamentals practical exposure, social and
professional morals
Programme Outcomes (POs)
PO1: Apply knowledge of computing fundamentals, computing
specialization, mathematics and domain knowledge to provide IT
solutions.
PO2: Identify, analyze and solve IT problems using fundamental
principles of mathematics and computing sciences.
PO3: Design, Develop and evaluate software solutions to meet societal
and environmental concerns.
PO4: Conduct investigations of complex problems using research
based knowledge and methods to provide valid conclusions.
PO5: Select and apply appropriate techniques and modern tools for
complex computing activities.
PO6: Understand professional ethics, cyber regulations and
responsibilities.
PO7: Involve in life-long learning for continual development as an IT
professional.
PO8: Apply and demonstrate computing and management principles
to manage projects in multidisciplinary environments by involving in
different roles
PO9: Comprehend& write effective reports and make quality
presentations.
PO10: Understand and assess the impact of IT solutions on socio-
environmental Issues.
PO11: Work collaboratively as a member or leader in
multidisciplinary teams.
PO12: Identify potential business opportunities and innovate to
create value to the society and seize that opportunity.
CONTENTS
List of problems for which student should develop program and execute in the Laboratory.
Implement all the programs in “C ” Programming Language and Linux OS.
15 marks for the conduction of the experiment and preparation of laboratory record, and 10
marks for the test to be conducted after the completion of all the laboratory sessions.
The CIE marks awarded in the case of the Practical component shall be based on the
continuous evaluation of the laboratory report. Each experiment report can be evaluated for
10 marks. Marks of all experiments write-ups are added and scaled down to 15 marks.
The laboratory test (duration 02/03 hours) after completion of all the experiments shall be
conducted for 50 marks and scaled down to 10 marks.
Scaled-down marks of write-up evaluations and tests added will be CIE marks for the
laboratory component of IPCC for 25 marks.
The student has to secure 40% of 25 marks to qualify in the CIE of the practical component of
the IPCC.
● Change of experiment is allowed only once and marks allotted for procedure to be
made zero of the changed part only.
OPERATING SYSTEMS LAB MANUAL (BCS303)
Program 1:
Description:
i. Fork( ): fork( ) is used to create a new process known as a child process which
runs concurrently with the process that made the fork( ) call.
The child process is an exact copy of parent process it has its own unique
process ID and its parents ID is set to the ID of the parent process. In the parent
process fork returns the process ID of the newly created child process. In the
child process fork returns a zero.
ii. Wait( ): the primary function of the wait( ) is for a parent process to wait() for
its child’s process to compete execution. When a parent process calls a wait it
gets blocked until one of its child process exits or signal is received.
Upon the successful completion of the child process wait( ) returns the process
ID of the child. If there is an error it returns -1.
iii. Exec( ): exec replaces the current process image with the new process image.
This is typically used in conjunction with the fork( ). A process would fork( ) a
child and the child process then uses exec to run a new program.
On success, exec( ) does not return to the original program, instead the new
programs main function is called. If there is an error it returns -1.
iv. Exit( ): it is used to end up process explicitly when a process calls a exit( ) it
signals the OS to release all the resources associated with that process and
terminate it. Before termination exit( ) ensures the standard IO buffers are
flushed, files opened by the process are closed and temporary files are deleted.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
int main(int argc, char **argv)
{
pid_t pid;
pid = fork();
if(pid==0)
{
printf("It is the child process and pid is %d\n",getpid());
exit(0);
}
else if(pid> 0)
{
printf("It is the parent process and pid is %d\n",getpid());
}
else
{
printf("Error while forking\n");
exit(EXIT_FAILURE);
}
return 0;
}
OUTPUT:
[arjunshanka@localhost ~]$ cc pg1a.c
[arjunshanka@localhost ~]$ ./a.out
It is the child process and pid is 3324
It is the parent process and pid is 3323
Program 1b:EXEC( )
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
main(void)
{
pid_t pid = 0;
int status;
pid = fork( );
if (pid == 0)
{
printf("I am the child.");
execl("/bin/ls", "ls", "-l", "/home/arjunshanka/", (char *) 0);
perror("In exec( ): ");
}
if (pid> 0)
{
printf("I am the parent, and the child is %d.\n", pid);
pid = wait(&status);
printf("End of process %d: ", pid);
if (WIFEXITED(status))
{
printf("The process ended with exit(%d).\n", WEXITSTATUS(status));
}
if (WIFSIGNALED(status))
{
printf("The process ended with kill -%d.\n", WTERMSIG(status));
}
}
if (pid< 0)
{
perror("In fork():");
}
exit(0);
}
OUTPUT:
[arjunshanka@localhost ~]$ cc pg1b.c
[arjunshanka@localhost ~]$ ./a.out
total 2032
-rw-rw-r-- 1 arjunshankaarjunshanka 292 2012-05-26 17:07 1a1.l
-rw-rw-r-- 1 arjunshankaarjunshanka 294 2022-07-04 14:58 1a.l
..
..
..
rwxr-xr-x 2 arjunshankaarjunshanka 4096 2012-02-15 03:01 Templates
-rw-rw-r-- 1 arjunshankaarjunshanka 810 2023-02-20 16:24 test2.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
int main(int argc, char **argv)
{
pid_t pid;
pid = fork( );
if(pid==0)
{
printf("It is the child process and pid is %d\n",getpid( ));
int i=0;
for(i=0;i<8;i++)
{
printf("%d\n",i);
}
exit(0);
}
else if(pid>0)
{
printf("It is the parent process and pid is %d\n", getpid( ));
int status;
wait(&status);
printf("Child is repeated\n");
}
else
{
printf("Error in forking..\n");
exit(EXIT_FAILURE);
}
return 0;
}
OUTPUT:
[arjunshanka@localhost ~]$ cc pg1c.c
[arjunshanka@localhost ~]$ ./a.out
It is the child process and pid is 3380
0 1 2 3 4 5 6 7
It is the parent process and pid is 3379
Child is repeated
#include <stdio.h>
#include <stdlib.h>
void exitfunc( )
{
printf("Called cleanup function - exitfunc( )\n");
return;
}
int main( )
{
atexit(exitfunc);
printf("Hello, World!\n");
exit(0);
}
OUTPUT:
[arjunshanka@localhost ~]$ cc pg1d.c
[arjunshanka@localhost ~]$ ./a.out
Hello, World!
Called cleanup function - exitfunc( )
Program 2:
AIM: Simulate the following CPU scheduling algorithms to find turnaround time and
waiting time
DESCRIPTION: To calculate the average waiting time using the FCFS algorithm first
the waiting time of the first process is kept zero and the waiting time of the second
process is the burst time of the first process and the waiting time of the third process
is the sum of the burst times of the first and the second process and so on. After
calculating all the waiting times the average waiting time is calculated as the average
of all waiting time.
OUTPUT:
[arjunshanka@localhost ~]$ cc pg2a.c
[arjunshanka@localhost ~]$ ./a.out
Enter the number of processes: 3
Enter Burst Time for Process 0:24
Enter Burst Time for Process 1:3
Enter Burst Time for Process 2:3
PROCESS BURST TIME WAITING TIME TURNAROUND TIME
P0 24 0 24
P1 3 24 27
P2 3 27 30
Average Waiting Time -- 17.000000
Average Turnaround Time -- 27.000000
#include<stdio.h>
main( )
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp;
float wtavg, tatavg;
printf("\nEnter the number of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
}
OUTPUT:
[arjunshanka@localhost ~]$ cc pg2b.c
[arjunshanka@localhost ~]$ ./a.out
Enter the number of processes -- 4
Enter Burst Time for Process 0 -- 6
Enter Burst Time for Process 1 -- 8
Enter Burst Time for Process 2 -- 7
Enter Burst Time for Process 3 -- 3
PROCESS BURST TIME WAITING TIME TURNAROUND TIME
P3 3 0 3
P0 6 3 9
P2 7 9 16
P1 8 16 24
Average Waiting Time -- 7.000000
#include<stdio.h>
main( )
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
printf("Enter the no of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
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)
{
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("\nThe Average Turnaround time is -- %f",att/n);
printf("\nThe Average Waiting time is -- %f ",awt/n);
printf("\nPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
}
OUTPUT:
[arjunshanka@localhost ~]$ cc pg2c.c
[arjunshanka@localhost ~]$ ./a.out
#include<stdio.h>
main( )
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
printf("Enter the number of processes --- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i] = i;
printf("Enter the Burst Time & Priority of Process %d --- ",i);
scanf("%d %d",&bt[i], &pri[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(pri[i] >pri[k])
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;
}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\tTURNAROUND
TIME");
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],pri[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is --- %f",wtavg/n);
printf("\nAverage Turnaround Time is --- %f",tatavg/n);
}
OUTPUT:
[arjunshanka@localhost ~]$ cc pg2d.c
[arjunshanka@localhost ~]$ ./a.out
Program 3:
AIM: Develop a C program to simulate producer-consumer problem using
semaphores.
#include<stdio.h>
void main( )
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;
in = 0;
out = 0;
bufsize = 10;
while(choice!=3)
{
printf("\n1.Producer\t 2.Consumer\t 3.Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: if((in+1)%bufsize==out)
printf("\nBuffer is Full");
else
{
printf("\nEnter the value: ");
scanf("%d", &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
}
break;
case 2: if(in == out)
printf("\nBuffer is Empty");
else
{
consume = buffer[out];
printf("\nThe consumed value is %d", consume);
out = (out+1)%bufsize;
}
break;
}
}
}
OUTPUT:
[arjunshanka@localhost ~] cc pg3.c
[arjunshanka@localhost ~]$ ./a.out
1.Producer 2.Consumer 3.Exit
Enter your choice: 2
Buffer is Empty
1.Producer 2.Consumer 3.Exit
Enter your choice: 1
Enter the value: 100
1.Producer 2.Consumer 3.Exit
Enter your choice: 2
The consumed value is 100
1.Producer 2.Consumer 3.Exit
Enter your choice: 3
Program 4:
AIM: Develop a C program which demonstrates inter process communication between
a reader process and a writer process. Use mkfifo, open, read, write and close APIs in
your program.
Writer process
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include <unistd.h>
int main( )
int fd;
char buf[1024];
mkfifo(myfifo, 0666);
fd = open(myfifo, O_WRONLY);
write(fd,"Hi", sizeof("Hi"));
close(fd);
unlink(myfifo);
return 0;
Reader process
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include <unistd.h>
int main()
int fd;
char buf[MAX_BUF];
fd = open(myfifo, O_RDONLY);
close(fd);
return 0;
OUTPUT:
Writer: Hi
Program 5:
AIM: Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance.
include<stdio.h>
#include<string.h>
void main( )
{
int alloc[10][10],max[10][10];
int avail[10],work[10],total[10];
int i,j,k,n,need[10][10];
int m;
int count=0,c=0;
char finish[10];
printf("Enter the no. of processes and resources:");
scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)
finish[i]='n';
printf("Enter the claim matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&max[i][j]);
printf("Enter the allocation matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&alloc[i][j]);
printf("Resource vector:");
for(i=0;i<m;i++)
scanf("%d",&total[i]);
for(i=0;i<m;i++)
avail[i]=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
avail[j]+=alloc[i][j];
for(i=0;i<m;i++)
work[i]=avail[i];
for(j=0;j<m;j++)
work[j]=total[j]-work[j];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
need[i][j]=max[i][j]-alloc[i][j];
A:
for(i=0;i<n;i++)
{
c=0;
for(j=0;j<m;j++)
if((need[i][j]<=work[j])&&(finish[i]=='n')) c++;
if(c==m)
{
printf("All the resources can be allocated to Process %d", i+1);
printf("\n\nAvailable resources are:");
for(k=0;k<m;k++)
{
work[k]+=alloc[i][k];
printf("%4d",work[k]);
}
printf("\n");
finish[i]='y';
printf("\nProcess %d executed?:%c \n",i+1,finish[i]);
count++;
}
}
if(count!=n) goto A;
else
printf("\n System is in safe mode");
printf("\n The given state is safe state");
}
OUTPUT:
[arjunshanka@localhost ~]$ cc pg5.c
[arjunshanka@localhost ~]$ ./a.out
Program 6:
AIM: Develop a C program to simulate the following contiguous memory allocation
Techniques.
OUTPUT:
[arjunshanka@localhost ~]$ cc pg6.c
[arjunshanka@localhost ~]$ ./a.out
2 4 3 7 3
#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);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\t File Size\t Block No\t Block Size\t Fragment");
for(i=1;i<=nf&&ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d\n",i,f[i],ff[i],b[ff[i]],frag[i]);
}
OUTPUT:
[arjunshanka@localhost ~]$ cc pg6b.c
[arjunshanka@localhost ~]$ ./a.out
2 4 1 5 1
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\n",i,f[i],ff[i],b[ff[i]],frag[i]);
}
OUTPUT:
[arjunshanka@localhost ~]$ cc pg6c.c
[arjunshanka@localhost ~]$ ./a.out
2 4 1 5 1
Program 7:
AIM: Develop a C program to simulate page replacement algorithms.
FIFO-This is the simplest page replacement algorithm. In this algorithm, the operating
system keeps track of all pages in the memory in a queue, the oldest page is in the
front of the queue. When a page needs to be replaced page in the front of the queue is
selected for removal.
LRU-In this algorithm page will be replaced which is least recently used.
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<frsize;i++)
{
if(fr[i]==-1)
{
fr[i]=page[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
fr[top]=page[j];
top++;
pf++;
if(top>=frsize)
top=0;
}
display( );
}
printf("\n Number of page faults : %d\n ",pf+frsize);
}
void display( )
{
int i;
printf("\n");
for(i=0;i<3;i++)
printf("%d\t",fr[i]);
}
OUTPUT:
[arjunshanka@localhost ~]$ cc pg7a.c
[arjunshanka@localhost ~]$ ./a.out
2 -1 -1
2 3 -1
2 3 -1
2 3 1
5 3 1
5 2 1
5 2 4
5 2 4
3 2 4
3 2 4
3 5 4
3 5 2
Number of page faults: 9
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("\n no of page faults :%d",pf+frsize);
}
void display( )
{
int i;
printf("\n");
for(i=0;i<3;i++)
printf("\t%d",fr[i]);
}
OUTPUT:
[arjunshanka@localhost ~]$ cc pg7b.c
[arjunshanka@localhost ~]$ ./a.out
2 -1 -1
2 3 -1
2 3 -1
2 3 1
2 5 1
2 5 1
2 5 4
2 5 4
3 5 4
3 5 2
3 5 2
3 5 2
No of page faults: 7
Program 8:
AIM: Simulate following File Organization Techniques.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main( )
{
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory--");
scanf("%s", dir.dname);
while(1)
{
printf(“\n\n 1. Create File\t 2. Delete File\t 3. Search File \n 4. Display Files\t5.
Exit\n“);
printf(“Enter your choice – “);
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter the name of the file -- ");
scanf("%s", dir.fname[dir.fcnt]);
dir.fcnt++; break;
case 2:printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3:printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4:if(dir.fcnt==0)
printf("\nDirectory Empty");
else
{
printf("\nThe Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
break;
}
default: exit(0);
}
}
}
OUTPUT:
[arjunshanka@localhost ~]$ ./a.out
Enter name of directory-- CSE
1. Create File 2. Delete File3. Search File 4. Display Files 5. Exit
Enter your choice -- 1
Enter the name of the file -- cse
1. Create File 2. Delete File3. Search File 4. Display Files 5. Exit
Enter your choice -- 1
Enter the name of the file -- ise
1. Create File 2. Delete File3. Search File 4. Display Files 5. Exit
Enter your choice -- 4
The Files are -- cse ise
1. Create File 2. Delete File3. Search File 4. Display Files 5. Exit
Enter your choice -- 3
Enter the name of the file -- cse
File cse is found
1. Create File 2. Delete File3. Search File 4. Display Files 5. Exit
Enter your choice -- 3
Enter the name of the file -- ISE
File ISE not found
1. Create File 2. Delete File3. Search File 4. Display Files 5. Exit
Enter your choice -- 2
Enter the name of the file -- ise
File ise is deleted
1. Create File 2. Delete File3. Search File
4. Display Files 5. Exit
Enter your choice – 5
Program 8b:
Description: In the two-level directory system, each user has own user file directory
(UFD). The system maintains a master block that has one entry for each user. This
master block contains the addresses of the directory of the users. When a user job
starts or a user logs in, the system's master file directory (MFD) is searched. When a
user refers to a particular file, only his own UFD is searched.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct
{
chardname[10],fname[10][10];
intfcnt;
}dir[10];
void main( )
{
int i,ch,dcnt,k;
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\n4. Search File\t\t5. Display\t6. Exit\t Enter your choice --");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter name of directory -- ");
scanf("%s",dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file --");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++; printf("File created");
}
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f); for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
OUTPUT:
[arjunshanka@localhost ~]$ cc pg8a.c
[arjunshanka@localhost ~]$ ./a.out
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice – 1
Enter name of directory -- CSE
Directory created
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice --2
Enter name of the directory -- CSE
Enter name of the file-- SLN
File created
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 5
Directory Files
CSE SLN
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice –1
Enter name of directory -- ISE
Directory created
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 2
Enter name of the directory – ISE
Enter name of the file --SLNCE
File created
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 5
Directory Files
CSE SLN
ISE SLNCE
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 3
Enter name of the directory -- CSE
Enter name of the file -- SLN
File SLN is deleted
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 5
Directory Files
CSE
ISE SLNCE
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter name of the directory -- ISE
Enter the name of the file -- SLN
File SLN not found
Program 9:
AIM: Develop a C program to simulate the Linked file allocation strategies.
DESCRIPTION: In the chained method file allocation table contains a field which
points to starting block of memory. From it for each bloc a pointer is kept to next
successive block. Hence, there is no external fragmentation.
#include<stdio.h>
#include<stdlib.h>
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 that are already allocated");
scanf("%d",&p);
printf("\nEnter the blocks no.s that are already allocated");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:printf("Enter the starting index 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("\n If u want to enter one more file? (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto X;
}
else exit(0);
}
OUTPUT:
[arjunshanka@localhost ~]$ cc pg9.c
1->1
2->1
3->1
4->1
7->1
8->1
9->1
10->1
Program 10:
AIM: Develop a C program to simulate SCAN disk scheduling algorithm.
DESCRIPTION: In the SCAN algorithm, the disk arm starts at one end, and moves
towards the other end, servicing requests as it reaches each cylinder, until it gets to
the other end of the disk. At the other end, the direction of head movement is reversed,
and servicing continues. The head continuously scans back and forth across the disk.
#include <stdio.h>
int request[50];
int SIZE;
int head;
int uptrack;
int downtrack;
struct max{
int up;
int down;
} kate[50];
void sort(int n)
{
int i, j;
for (i = 0; i < n - 1; i++){
for (j = 0; j < n - i - 1; j++){
if (request[j] > request[j + 1]){
int temp = request[j];
request[j] = request[j + 1];
request[j + 1] = temp;
}
}
}
j = 0;
i = 0;
while (request[i] != head)
{
kate[j].down = request[i];
j++;
i++;
}
downtrack = j;
i++;
j = 0;
while (i < n) {
kate[j].up = request[i];
j++;
i++;
}
uptrack = j;
}
void scan(int n){
int i;
printf("SEEK SEQUENCE = ");
sort(n);
for (i = downtrack - 1; i > 0; i--){
printf("%d ", head);
head = kate[i].down;
}
for (i = 0; i <uptrack - 1; i++){
printf("%d ", head);
head = kate[i].up;
}
}
int main( ){
int n, i;
printf("ENTER THE DISK SIZE :\n");
scanf("%d", &SIZE);
printf("ENTER THE NO OF REQUEST SEQUENCE :\n");
scanf("%d", &n);
printf("ENTER THE REQUEST SEQUENCE :\n");
for (i = 0; i < n; i++)
scanf("%d", &request[i]);
printf("ENTER THE CURRENT HEAD :\n");
scanf("%d", &head);
request[n] = head;
request[n + 1] = SIZE - 1;
request[n + 2] = 0;
scan(n + 3);
}
OUTPUT:
170
150
135
10
20