0% found this document useful (0 votes)
12 views42 pages

lab manual

The document outlines a series of experiments for an Operating Systems course at Dayananda Sagar University, focusing on various programming tasks related to process management, synchronization, memory management, and CPU scheduling algorithms using C. Each experiment includes instructions for implementation, system calls used, and example code. The document emphasizes the use of Ubuntu OS and specific commands for compiling and executing the programs.

Uploaded by

eng22cs0424
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views42 pages

lab manual

The document outlines a series of experiments for an Operating Systems course at Dayananda Sagar University, focusing on various programming tasks related to process management, synchronization, memory management, and CPU scheduling algorithms using C. Each experiment includes instructions for implementation, system calls used, and example code. The document emphasizes the use of Ubuntu OS and specific commands for compiling and executing the programs.

Uploaded by

eng22cs0424
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Dayananda Sagar University

School of Engineering
Department of Computer Science & Engineering
List of Experiments
Course: Operating System Code: 16CS372/16CT371

Exp. Division of
List of Experiments
No Experiments
Write a C program to create a new process that exec a new
1 program using system calls fork(), execlp() & wait()

System Calls Write a C program to display PID and PPID using system calls
2
getpid () & getppid ()
Write a C program using I/O system calls open(), read() &
3 write() to copy contents of one file to another file
Write a C program to implement multithreaded program using
4
pthreads
Process
Management Write C program to simulate the following CPU scheduling
5 algorithms
a) FCFS b) SJF c) Priority d) Round Robin
Process Write a C program to simulate producer-consumer problem
6
synchronization using semaphores
Write a C program to simulate Bankers algorithm for the
7
Deadlock purpose of deadlock avoidance.
8 Write a C program to simulate deadlock detection.
Write a C program to simulate paging technique of memory
9
Memory management
Management Write a C program to simulate page replacement algorithms
10
a) FIFO b) LRU c) LFU
Write a C program to simulate the following file organization
11 techniques
a) Single level directory b) Two level directory
I/O System
Write a C program to simulate the following file allocation
12 strategies.
a) Sequential b) Indexed

Instructions
1. Use Ubuntu OS
2. Use cc or gcc compiler for compiling C program
3. Type the program in gedit text editor. Save the program with .c file extension in
current directory
4. Compile the program at the terminal using the command
cc filename.c
5. Execute the program using the command
./a.out
Give instructions on
1. What is a process? How to create a process
2. What are system calls?

1. Write a C program to create a new process that exec a new program using system calls
fork(), execlp() & wait()
Program description:
C program (or, process, at run time) needs to create a process which would execute a program.
Here, two system calls are of interest, fork and exec. The fork system call in Unix creates a new
process. The new process (the child process) is an exact copy of the calling process (the parent
process). however, you want the new process to run a new program. When, for example, you
type "date" on the unix command line, the command line interpreter (the so-called "shell") forks
so that momentarily 2 shells are running, then the code in the child process is replaced by the
code of the "date" program by using one of the family of exec system calls execlp(). The parent
process waits for the child to exit.
System calls used
1. fork ( )

fork() is used to create new processes. The new process consists of a copy of the address space
of the original process. Upon successful completion, fork() returns 0 to the child process
and returns the process ID of the child process to the parent process.
Syntax:
#include <unistd.h>
int fork( );
2. execlp ( )
Used after the fork () system call by one of the two processes to replace the process memory
space with a new program. It loads a binary file into memory destroying the memory image
of the program containing the execlp system call and starts its execution. The child process
overlays its address space with the UNIX command /bin/ls using the execlp system call.

Syntax:
#include <unistd.h>
int execlp(const char *file, const char *arg, ...);
The initial argument is the name of a file that is to be executed. The next arguments describe a
list of one or more pointers to null-terminated strings that represent the argument list available to
the executed program. The list of arguments mustbe terminated by a NULL pointer,
3. wait ( )
The parent waits for the child process to complete using the wait system call. The wait
system call returns the process identifier of a terminated child, so that the parent can tell
which of its possibly many children has terminated.

Syntax:
#include <sys/wait.h>

pid_t wait( int* status );

4. exit( )
A process terminates when it finishes executing its final statement and asks the operating
system to delete it by using the exit system call. At that point, the process may return data
(output) to its parent process (via the wait system call).
Syntax:
#include <std1ib.h>
void exit(int status);

PROGRAM CODE:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
void main(intargc,char *arg[])
{
int pid;
pid=fork();
if(pid<0)
{
printf("fork failed");
exit(1);
}
else if(pid==0)
{
execlp("date","ls",NULL );
exit(0);
}
else
{

printf("\n Process id is -%d\n",getpid());


wait(NULL);
exit(0);
}
}

Expt. 2. Write a C program to display PID and PPID using system


calls getpid () & getppid ()
Program Description:
Using fork(), create a child process. The child process prints its own process id and id of the
parent and exits. The parent process waits for the child to finish and prints its own process id and
the id of the child process and exits.
SYSTEM CALLS USED:
getpid( ): returns the process id of the current process
Syntax():
#include<unistd.h>
pid_t getpid();
getppid( ): returns the process id of the parent of the current process
Syntax():
#include<unistd.h>
pid_t getppid();

Program Code:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void main()
{
int pid;
pid=fork();
if(!pid)
{
printf("Child process...");
printf("\n\nChild PID : %d",getpid());
printf("\nParent PID : %d",getppid());
printf("\n\nFinished with child\n");
}

else
{
wait(NULL);
printf("\nParent process");
printf("\nPARENT PID : %d",getpid());
printf("\nChild PID : %d",pid);
}
}

Expt. 3. Write a C program using I/O system calls open(), read() &
write() to copy contents of one file to another file
Program Description: Assume that one.txt is well defined. What we want is to create a new file
called two.txt and copy over all the contents of one.txt.
SYSTEM CALLS USED:
open(): allows you to open or create file for reading and/or writing
Syntax:
#include <fcntl.h>
int open (const char* Path, int flags [, int mode ]);
Returns: file descriptor if OK, -1 on error
The pathname is the name of the file to open or create.
Then a flag in the second parameter determine the method in which the file is to be opened:
O_RDONLY, O_WRONLY or O_RDWR.
The third argument determine the permissions of the file if it is created: S_IRUSR (Set read
rights for the owner to true), S_IWUSR (Set write rights for the owner to true)
read(): used to read data into a buffer.
syntax:
#include <unistd.h>
size_t read (int fd, void* buf, size_t cnt);
Returns the number of bytes that were read.
fd: The file descriptor of where to read the input
buf: A character array where the read content will be stored.
cnt: The number of bytes to read
write(): used to write data out of a buffer.
Syntax:
#include <unistd.h>
size_t write (int fd, void* buf, size_t cnt);
Returns: the number of bytes that were written
fd: The file descriptor of where to write the output.
Buf: A pointer to a buffer which will be written to the file.
cnt: The number of bytes to write.

Program code:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include <fcntl.h>
void main()
{
char buff;
int fd,fd1;
fd=open("one.txt",O_RDONLY);
fd1=open("two.txt",O_WRONLY|O_CREAT);
while(read(fd,&buff,1))
write(fd1,&buff,1);
printf("The copy of a file is successed");
close(fd);
close(fd1);
}

4. Write a C program to implement multithreaded program using


pthreads
Program Description:

Program creates 5 threads with the pthread_create() routine. Each thread prints a Hello World
message and then terminates with a call to pthread_exit().

System Calls Used:


pthread_create(): creates a new thread and makes it executable. This routine can be called any
number of times from anywhere within your code.
Syntax:
pthread_create (thread, attr, start_routine, arg)
pthread_create arguments:
● thread: unique identifier for the new thread returned by the subroutine.

● attr: attribute object that may be used to set thread attributes. You can specify a thread
attributes object, or NULL for the default values.
● start_routine: the C routine that the thread will execute once it is created.
● arg: A single argument that may be passed to start_routine. It must be passed by
reference as a pointer cast of type void. NULL may be used if no argument is to be
passed.
pthread_exit(): terminate the calling thread
Syntax():
pthread_exit(status)

Program Code:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5

void *PrintHello(void *threadid)


{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}

int main(int argc, char *argv[])


{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0;t<NUM_THREADS;t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}

/* Last thing that main() should do */


pthread_exit(NULL);
}

OUTPUT:

$ gcc 4a.c -lpthread


$./a.out
In main: creating thread 0
In main: creating thread 1
Hello World! It's me, thread #0!
In main: creating thread 2
Hello World! It's me, thread #1!
In main: creating thread 3
Hello World! It's me, thread #2!
In main: creating thread 4
Hello World! It's me, thread #3!
Hello World! It's me, thread #4!

EXP 5. CPU scheduling Algorithms

1. FCFS Scheduling
#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);

printf("\nEnter Process Burst Time\n");


for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}

wt[0]=0; //waiting time for first process is 0

//calculating waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]= wt[i]+bt[j];
}

printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");

//calculating turnaround time


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}

avwt/=n;
avtat/=n;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);

return 0;
}
2. SJF Scheduling
#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0, total1=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);

printf("\nEnter Burst Time:\n");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}

//sorting burst time in ascending order using selection sort


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0; //waiting time for first process will be zero

//calculate waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_wt=(float)total/n; //average waiting time
avg_tat=(float)total1/n; //average turnaround time
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}
3. Priority Scheduling

#include<stdio.h>

int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,
total1=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);

printf("\nEnter Burst Time and Priority\n");


for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}

//sorting burst time, priority and process number in ascending order using
selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}

temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0; //waiting time for first process is zero


//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total1+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_wt=total/n; //average waiting time


avg_tat=total1/n; //average turnaround time
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);

return 0;
}
4. Round Robin

#include<stdio.h>
int main()
{
int i,tbt=0,nop,ts=0,flag[20],rem[20];
int from,wt[20],tt[20],b[20],twt=0,ttt=0;
int dur;
float awt,att;

printf("Enter no.of Processes:");


scanf("%d",&nop);

printf("Enter the time slice:");


scanf("%d",&ts);

printf("Enter the Burst times..\n");


for(i=0;i<nop;i++)
{
wt[i]=tt[i]=0;
printf("P%d\t:",i+1);
scanf("%d",&b[i]); // Reading Burst Time
rem[i]=b[i]; // Store the Burst Time in rem array
tbt+=b[i]; // Total Burst Time
flag[i]=0; // used to check whether the process has remaining burst time
or not
}
from=0;
i=0;
printf("\n\tGantt Chart");
printf("\nProcessID \t From Time\tTo Time\n");

while(from<tbt)
{
if(!flag[i]) //true only when process has burst time
{
if(rem[i]<=ts) //burst time should be equal or less than time slice
{
dur=rem[i];
flag[i]=1; // make the value false
tt[i]=dur+from;
wt[i]=tt[i]-b[i]; // subtract
}
else
dur=ts;
printf("%7d%15d%15d\n",i+1,from,from+dur);
rem[i]= rem[i]-dur;
from = from+dur;
}
i=(i+1)%nop;
}
for(i=0;i<nop;i++)
{
twt+=wt[i];
ttt+=tt[i];
}

printf("\n\nProcess ID\tWaiting Time\tTurn Around Time");


for(i=0;i<nop;i++)
{
printf("\n\t%d\t\t%d\t\t%d",i+1,wt[i],tt[i]);
}
awt=(float)twt/(float)nop;
att=(float)ttt/(float)nop;
printf("\nTotal Waiting Time:%d",twt);
printf("\nTotal TurnAround Time:%d",ttt);
printf("\nAverage Waiting Time:%.2f",awt);
printf("\nAverage Turn Around Time:%.2f\n",att);
return 0;
}
EXP 6. Semaphore

#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;

void producer();
void consumer();

int wait(int);
int signal(int);

int main()
{
int n;

do
{
printf("\n1.producer\n2.consumer\n3.exit\n");
printf("\nenter ur choice");
scanf("%d",&n);
switch(n)
{
case 1:
if((mutex==1)&&(empty!=0))
producer();
else
printf("buffer is full\n");
break;
case 2:
if((mutex==1)&&(full!=0))
consumer();
else
printf("buffer is empty");
break;
case 3:
exit(0);
break;
}
}while(n!=3);
return 0;
}

int wait(int s)
{
return(--s);
}

int signal(int s)
{
return(++s);
}

void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nproducer produces the items%d",x);
mutex=signal(mutex);
}

void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nconsumerconsumes the item %d",x);
x--;
mutex=signal(mutex);
}
Expt 7. Bankers Algorithm
#include<stdio.h>

void main()
{
int k=0,output[10],d=0,t=0,ins[5],i,avail[5],allocated[10][5],need[10]
[5],MAX[10][5],pno,P[10],j,rz, count=0;

printf("\n Enter the number of resources : ");


scanf("%d", &rz);

printf("\n enter the max instances of each resources\n");


for(i=0;i<rz;i++)
{
avail[i]=0;
printf("%c= ",(i+97));
scanf("%d",&ins[i]);
}

printf("\n Enter the number of processes : ");


scanf("%d", &pno);

printf("\n Enter the allocation matrix \n ");


for(i=0;i<rz;i++)
printf(" %c",(i+97));

printf("\n");
for(i=0;i <pno;i++)
{
P[i]=i;
printf("P[%d] ",P[i]);

for(j=0;j<rz;j++)
{
scanf("%d",&allocated[i][j]);
avail[j]+=allocated[i][j];
}
}

printf("\nEnter the MAX matrix \n ");


for(i=0;i<rz;i++)
{
printf(" %c",(i+97));
avail[i]=ins[i]-avail[i];
}

printf("\n");

for(i=0;i <pno;i++)
{
printf("P[%d] ",i);

for(j=0;j<rz;j++)
scanf("%d", &MAX[i][j]);
}

printf("\n");

A: d=-1;

for(i=0;i <pno;i++)
{
count=0; t=P[i];
for(j=0;j<rz;j++)
{
need[t][j] = MAX[t][j]-allocated[t][j];
if(need[t][j]<=avail[j])
count++;
}
if(count==rz)
{
output[k++]=P[i];
for(j=0;j<rz;j++)
avail[j]+=allocated[t][j];
}
else
P[++d]=P[i];
}

if(d!=-1)
{
pno=d+1;
goto A;
}
printf("\t <");
for(i=0;i<k;i++)
printf(" P[%d] ",output[i]);
printf(">");
}
Exp. 8 Deadlock Detection
#include<stdio.h>
int main()
{
int alloc[10][10], request[10][10], avail[10], work[10], n, m, i, j,k, true=1, false=0, finish[n];
printf("Enter number of processes: \n");
scanf("%d",&n);

printf("Enter number of resources: \n");


scanf("%d",&m);

printf("Enter allocation: \n");


for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
scanf("%d", &alloc[i][j]);
}
}

printf("Enter request: \n");


for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
scanf("%d", &request[i][j]);
}
}

printf("Enter available: \n");


for(i=0; i<m; i++)
{
scanf("%d", &avail[i]);
}
for(i=0; i<n; i++)
{
finish[i]=false;
}

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


{
work[i]=avail[i];
}

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


{
for(i=0; i<n; i++)
{
k=0;
if(finish[i]==false && request[i][k]<=work[k] && request[i][k+1]<=work[k+1] && request[i]
[k+2]<=work[k+2])
{
work[k]+=alloc[i][k];
work[k+1]+=alloc[i][k+1];
work[k+2]+=alloc[i][k+2];
finish[i]=true;
}
}
}

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


{
if(finish[i]==false)
{
printf("Deadlock detected! \n");
}
else
{
printf("No deadlock! \n");
}
}
}
9. Write a C program to simulate paging technique of memory management
#include<stdio.h>
int main()
{
int ms, ps, nop, np, rempages, i, j, procno, pgno, pa, offset;
int s[10], fno[10][20];

printf("\nEnter the memory size: ");


scanf("%d",&ms);

printf("\nEnter the page size: ");


scanf("%d",&ps);

nop = ms/ps;
printf("\nThe no. of pages available in memory are:%d ",nop);

printf("\nEnter number of processes: ");


scanf("%d",&np);
rempages = nop;
for(i=1;i<=np;i++)
{

printf("\nEnter no. of pages required for p[%d]: ",i);


scanf("%d",&s[i]);
if(s[i] >rempages)
{
printf("\nMemory is Full");
break;
}
rempages = rempages - s[i];
printf("\nEnter pagetable entry for p[%d]: ",i);
for(j=1;j<=s[i];j++)
scanf("%d",&fno[i][j]);
}

printf("\nEnter process no. and pagenumber and offset: ");


scanf("%d %d %d",&procno,&pgno, &offset);
if(procno> np || pgno>= s[i] || offset>=ps)
printf("\nInvalid Process or Page Number or offset");
else
{
pa=fno[procno][pgno]*ps+offset;
printf("\nThe Physical Address is: %d",pa);

}
return 0;
}
OUTPUT:
Enter the memory size: 1000
Enter the page size: 100
The no. of pages available in memory are:10
Enter number of processes:3
Enter no. of pages required for p[1]:4
Enter page table entry for p[1]: 8 6 9 5
Enter no. of pages required for p[2]: 5
Enter pagetable entry for p[2]: 1 4 5 7 3
Enter no. of pages required for p[3]: 5
Memory is Full
Enter process no. and page number and offset:
2
3
60
The Physical Address is: 560

Expt. 10 Page replacement Algorithms


1. FIFO page replacement
#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("\tpage number\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;
}

10. Write a C program to simulate page replacement algorithms


b) LRU

#include<stdio.h>
int findLRU(int time[], int n){
int i, minimum = time[0], pos = 0;

for(i = 1; i< n; ++i){


if(time[i] <minimum){
minimum = time[i];
pos = i;
}
}
return pos;
}

int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j, pos,
faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);

printf("Enter number of pages: ");


scanf("%d", &no_of_pages);

printf("Enter reference string: ");

for(i = 0; i<no_of_pages; ++i){


scanf("%d", &pages[i]);
}

for(i = 0; i<no_of_frames; ++i){


frames[i] = -1;
}

for(i = 0; i<no_of_pages; ++i){


flag1 = flag2 = 0;

for(j = 0; j <no_of_frames; ++j){


if(frames[j] == pages[i]){
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}

if(flag1 == 0){
for(j = 0; j <no_of_frames; ++j){
if(frames[j] == -1){
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
for(j = 0; j <no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}

Output
Enter number of frames: 3
Enter number of pages: 6
Enter reference string: 5 7 5 6 7 3
5 -1 -1
5 7 -1
5 7 -1
576
576
376
Total Page Faults = 4

10. Write a C program to simulate page replacement algorithms


c) LFU
#include<stdio.h>
int main()
{
int rs[50],i,j, k,m,f, cntr[20],a[20],min,pf=0;
//clrscr();
printf("\nEnter number of page references -- ");
scanf("%d",&m);
printf("\nEnter the reference string--");
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
printf("\nEnter the available no. of frames--");
scanf("%d",&f);
for(i=0;i<f;i++)
{
cntr[i]=0;
a[i]=-1;
}
printf("\nThe Page Replacement Process is–\n");
for(i=0;i<m;i++)
{
for(j=0;j<f;j++)
if(rs[i]==a[j])
{
cntr[j]++;
break;
}
if(j==f)
{
min = 0;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min])
min=k;
a[min]=rs[i];
cntr[min]=1;
pf++;
}
printf("\n");
for(j=0;j<f;j++)
printf("\t %d",a[j]);
if(j==f)
printf("\t PF No. %d",pf);

}
printf("\n\nTotal number of page faults--%d",pf);
return 0;
}

OUTPUT:
Enter number of page references -- 12
Enter the reference string—1 2 3 4 5 2 5 2 5 1 4 3
Enter the available no. of frames-- 3
The Page Replacement Process is–

1 -1 -1 PF No. 1
1 2 -1 PF No. 2
1 2 3 PF No. 3
4 2 3 PF No. 4
5 2 3 PF No. 5
5 2 3 PF No. 5
5 2 3 PF No. 5
5 2 3 PF No. 5
5 2 3 PF No. 5
5 2 1 PF No. 6
5 2 4 PF No. 7
5 2 3 PF No. 8

Total number of page faults--8

Expt. 11 File Organization Techniques.

a. File Organization Using Single Level Directory

#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\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter your choice -- ");
scanf("%d",&ch);

switch(ch)
{
case 1: printf("\n Enter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;

case 2: printf("\n Enter 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("\n Enter 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("\n Directory Empty");
else
{
printf("\n The Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
}

OUTPUT:

Enter name of directory -- CSE

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 1

Enter the name of the file -- A

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 1

Enter the name of the file -- B

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 1

Enter the name of the file -- C

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 4

The Files are -- A B C

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 3

Enter the name of the file – ABC

File ABC not found


1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 2

Enter the name of the file – B


File B is deleted
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 5

b. File Organization Using Two Level Technique

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];

void main()
{
int i,ch,dcnt,k;
char f[30], d[30];

dcnt=0;
while(1)
{
printf("\n\n 1. Create Directory\t 2. Create File\t 3. Delete File");
printf("\n 4. Search File \t \t 5. Display \t 6. Exit \t Enter your choice -- ");
scanf("%d",&ch);

switch(ch)
{
case 1: printf("\n Enter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;

case 2: printf("\n Enter 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;
}
if(i==dcnt)
printf("Directory %s not found",d);
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)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;

case 5: if(dcnt==0)
printf("\nNo Directory's ");
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
}

OUTPUT:
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 1

Enter name of directory -- DIR1


Directory created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 1

Enter name of directory -- DIR2

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 – DIR1


Enter name of the file -- A1
File 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 – DIR1


Enter name of the file -- A2
File 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 – DIR2


Enter name of the file -- B1
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 5
Directory Files
DIR1 A1 A2
DIR2 B1

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 4
Enter name of the directory – DIR
Directory not found

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 – DIR1
Enter name of the file -- A2

File A2 is deleted
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice – 6

12. Write a C program to simulate the following file allocation strategies.


a) Sequential
#include<stdio.h>
#include<string.h>
struct fileTable
{
char name[20]; int sb, nob;
}ft[30];

int main()
{
int i, j, n; char s[20];
printf("\nEnter no of files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter file name %d :",i+1);
scanf("%s",ft[i].name);
printf("\nEnter starting block of file %d :",i+1);
scanf("%d",&ft[i].sb);
printf("\nEnter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
}
printf("\nEnter the file name to be searched: ");
scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
break;
if(i==n)
printf("\nFile Not Found");
else
{
printf("\nFILE NAME \tSTART BLOCK \tNO OF BLOCKS \tBLOCKS OCCUPIED\n");
printf("\n%s\t\t%d\t\t%d\t",ft[i].name,ft[i].sb,ft[i].nob);
for(j=0;j<ft[i].nob;j++)
printf("%d, ",ft[i].sb+j);
}
return 0;
}
OUTPUT:

Enter no of files :3
Enter file name 1 :AAA
Enter starting block of file 1 :85
Enter no of blocks in file 1 :6
Enter file name 2 :BBB
Enter starting block of file 2 :102
Enter no of blocks in file 2 :4
Enter file name 3 :CCC
Enter starting block of file 3 :60
Enter no of blocks in file 3 :4
Enter the file name to be searched: BBB

FILENAME STARTBLOCK NO OF BLOCKS BLOCKS OCCUPIED


BBB 102 4 102,103,104,105

12. Write a C program to simulate the following file allocation strategies.


b) Indexed
#include<stdio.h>
#include<stdlib.h>
int main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
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("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0): ");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
return 0;
}

OUTPUT:
Enter the index block: 5
Enter no of blocks needed and no of files for the index 5 on the disk :
4
1234
Allocated
File Indexed
5-------->1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0): 1
Enter the index block: 4
4 index is already allocated
Enter the index block: 6
Enter no of blocks needed and no of files for the index 6 on the disk :
2
78
Allocated
File Indexed
6-------->7 : 1
6-------->8 : 1
Do you want to enter more file(Yes - 1/No - 0): 0

***************************************************************

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy