CS3461 OPERATING SYSTEM LABORATORY1
CS3461 OPERATING SYSTEM LABORATORY1
LIST OF EXPERIMENTS:
1. Installation of windows operating system
2. Illustrate UNIX commands and Shell Programming
3. Process Management using System Calls : Fork, Exit, Getpid, Wait, Close
4. Write C programs to implement the various CPU Scheduling Algorithms
5. Illustrate the inter process communication strategy
6. Implement mutual exclusion by Semaphore
7. Write C programs to avoid Deadlock using Banker's Algorithm
8. Write a C program to Implement Deadlock Detection Algorithm
9. Write C program to implement Threading
10. Implement the paging Technique using C program
11. Write C programs to implement the following Memory Allocation Methods
a. First Fit b. Worst Fit c. Best Fit
12. Write C programs to implement the various Page Replacement Algorithms
13. Write C programs to Implement the various File Organization Techniques
14. Implement the following File Allocation Strategies using C programs
a. Sequential b. Indexed c. Linked
15. Write C programs for the implementation of various disk scheduling algorithms
16. Install any guest operating system like Linux using VMware.
Ex.no: 1 Installation of Operating system : Windows
Aim:
To study Installation of Operating system: Windows/ Linux.
1. Creating an Installation Disc or Drive
1Connect a blank USB flash drive or insert a blank writable DVD. You can install Windows 10 by
creating a bootable USB flash drive or DVD that contains the Windows 10 installation files. You'll
need a USB flash drive that's at least 8GB, or any blank DVD to get started.
If you already have Windows 10 installed on the PC and just want to reinstall it, it'll be
easiest to reinstall it from within Windows 10 instead of creating installation media.
If you want to upgrade from Windows 7 or Windows 8.1, you won't need to create an
installation disc or drive. However, you will need to follow most of this method to start the
upgrade.
2 Make sure you have a product key. If you bought Windows 10 through Microsoft using
your Microsoft account, your product key is already linked to your account. If you bought Windows
10 from another retailer, you'll have a 25-character product key that you'll need to have handy to
activate Windows.[2]
If you don't have a product key or you're installing Windows 10 on a new hard drive, make
sure you've linked your Windows 10 digital license to your Microsoft account before you
start the installation.[3] Head to Settings > Update & Security > Activation from the
current installation—if the activation status says Windows is activated with a digital
license, click Add an account and follow the on-screen instructions to link your Microsoft
account. If you're upgrading from an earlier version and your PC qualifies for a free upgrade, you
won't need a product key.
4Click Download tool now. This is a blue button in the middle of the page. This downloads the Media
Creation Tool, which you'll use to create your installation media (or start your upgrade).
5Double-click the downloaded file. Its name begins with "MediaCreationTool" and ends with ".exe."
You'll find it in your default download folder, which is usually called Downloads.
Click Yes when prompted to allow the installer to run.`
6Click Accept to accept the license. It's in the bottom-right corner of the window.
7 Select "Create installation media" and click OK. This option lets you create a Windows installation
disc or drive that will work on any compatible PC, not just the one you're using now.
If you're updating your PC from an earlier version of Windows, select Upgrade this PC
now instead, and then follow the on-screen instructions to install Windows 10. You're
done!
8 Select your preferences and click Next. If you're installing Windows on the current PC, you can
keep the default options. If you need to install on a different PC, make sure you choose the language
and edition for which you have a license, and select the architecture (64-bit or 32-bit) that matches
the PC you're going to install on. If you're not sure about the architecture, you can choose Both from
the menu.
9 Choose an installation type and click Next. An ISO file is a type of file that can be burned to a DVD,
so choose that option if you plan to create a DVD. Otherwise, choose the USB flash drive option.
10Create your installation media. The steps are a little different depending on what you're doing:
Flash drive: Select your flash drive from the list, click Next, and wait for the installation
files to install. When the process is complete, click Finish.
11 DVD/ISO: Click Save to save the ISO file to your computer—it may take a while because
the file is large and has to be downloaded. Once downloaded, you'll see a progress screen
that monitors the download. When the download is complete, click Open DVD burner on
the "Burn the ISO file to a DVD" screen, select your DVD burner, and then click Burn to
create your DVD.
RESULT
Thus the shell programs by using conditional, branching and looping statements was written and
executed.
Ex.no.3 Process Management using System Calls: Fork, Exec, Getpid, Exit, Wait, Close
Aim:
To Process Management using System Calls : Fork, Exec, Getpid, Exit, Wait, Close.
Algorithm:
Step 1: Declare a pid_t variable to store the process ID
Step 2: Call fork() to create a child process
Step 3: If fork() returns a negative value, print an error message and exit with an error code
Step 4: If fork() returns 0, the process is the child:
Step 4.1: Get the process ID of the child using getpid()
Step 4.2: Execute a command using execlp()
Step 5: If fork() returns a positive value, the process is the parent:
Step 5.1: Get the process ID of the parent using getpid()
Step 5.2: Call wait() to wait for the child process to complete
Step 5.3: Print a message to the console indicating that the child process has completed
Step 6: Call exit() to exit the program
Program :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid;
// Fork a child process
pid = fork();
OUTPUT:
RESULT:
Thus the C program for implementation of scheduling algorithms such as FCFS, SJF,
Priority and Round Robin Scheduling was written and executed.
AIM:
To write a C program for inter process communication.
ALGORITHM
Step 1: Start the program.
Step 2: Define the key.
Step 3: Attach the client to the shared memory created by the server. Step 4:
Read the content from the shared memory.
Step 5: Display the content on the screen.
Step 6: Stop
PROGRAM:
MESSAGE QUEUE FOR WRITER PROCESS
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX 10
struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;
int main()
{
key_t key;
int msgid;
key = ftok("progfile", 65);
msgid = msgget(key, 0666 | IPC_CREAT); message.mesg_type = 1;
printf("Write Data : ");
fgets(message.mesg_text,MAX,stdin);
msgsnd(msgid, &message, sizeof(message), 0);
printf("Data send is : %s \n", message.mesg_text); return 0;
}
MESSAGE QUEUE FOR READER PROCESS
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;
int main()
{
key_t key;
int msgid;
key = ftok("profile", 65);
msgid = msgget(key, 0666 | IPC_CREAT);
msgrcv(msgid, &message, sizeof(message), 1, 0);
printf("Data received is : %s \n", message.mesg_text);
msgctl(msgid, IPC_RMID, NULL);
return 0;
}
OUTPUT:
RESULT:
Thus the C program for inter process communication was written and
executed..
AIM:
To write a C-program to implement the producer – consumer problem using
semaphores (Mutual Exclusion).
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the required variables.
Step 3: Initialize the buffer size and get maximum item you want to
produce. Step 4: Get the option, which you want to do either producer,
consumer or exit from the operation.
Step 5: If you select the producer, check the buffer size if it is full the
producer should not produce the item or otherwise produce the item and
increase the value buffer size.
Step 6: If you select the consumer, check the buffer size if it is empty the
consumer should not consume the item or otherwise consume the item and
decrease the value of buffer size.
Step 7: If you select exit come out of the program.
Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter Your Choice:");
scanf("%d",&n);
switch(n)
{
case 1:
if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is Full!!");
break;
case 2:
if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is Empty!!");
break;
case 3:
exit(0);
break;
}
}
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 Item %d",x); mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer Consumes Item %d",x); x--;
mutex=signal(mutex);
}
OUTPUT:
RESULT:
Thus the C-program to implement the producer – consumer problem using
semaphores (Mutual Exclusion) was written and executed.
AIM:
To write a C program to implement banker’s algorithm for deadlock avoidance.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the memory for the process.
Step 3: Read the number of process, resources, allocation matrix and available
matrix.
Step 4: Compare each and every process using the banker s algorithm. ‟ Step 5: If
the process is in safe state then it is a not a deadlock process otherwise it is a
deadlock process
Step 6: Produce the result of state of process
Step 7: Stop the program
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int p, r, i, j, process, count;
int completed[20], Max[20][20], alloc[20][20], need[20][20], safeSequence[20],
avail[20];
count = 0;
printf("Enter the number of Processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the number of Resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each Process : ");
for(i = 0; i < p; i++)
{
printf("\nFor Process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the allocation for each Process : ");
for(i = 0; i < p; i++)
{
printf("\nFor Process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
for(i = 0; i < p; i++)
for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];
do
{
printf("\n Max Matrix:\tAllocation Matrix:\n"); for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
for(i = 0; i < p; i++)
{
if(completed[i] == 0)
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}
if(process != -1)
{
printf("\nProcess %d runs to Completion!", process + 1); safeSequence[count] =
process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j]; alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}
while(count != p && process != -1); if(count == p)
{
printf("\nThe system is in a Safe State!!\n"); printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]); printf(">\n");
}
else
printf("\nThe system is in an Unsafe State!!"); }
OUTPUT:
RESULT:
Thus the C program to implement banker’s algorithm for deadlock avoidance was
written and executed.
AIM:
To write a C program to implement algorithm for deadlock detection.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the memory for the process.
Step 3: Read the number of process, resources, allocation matrix and available
matrix.
Step 4: Compare each and every process using the banker s algorithm. ‟ Step 5: If
the process is in safe state then it is a not a deadlock process otherwise it is a
deadlock process
Step 6: Produce the result of state of process
Step 7: Stop the program
PROGRAM:
#include<stdio.h>
static int mark[20];
int i,j,np,nr;
int main()
{
alloc[10][10],request[10][10],avail[10],r[10],w[10];
printf("\nEnter the number of Processes: ");
scanf("%d",&np);
printf("\nEnter the number of Resources: ");
scanf("%d",&nr);
for(i=0;i<nr;i++)
{
printf("\nTotal Amount of the Resource R%d: ",i+1);
scanf("%d",&r[i]);
}
printf("\nEnter the Request Matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&request[i][j]);
printf("\nEnter the Allocation Matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&alloc[i][j]);
for(j=0;j<nr;j++)
{
avail[j]=r[j];
for(i=0;i<np;i++)
{
avail[j]-=alloc[i][j];
}
}
for(i=0;i<np;i++)
{
int count=0;
for(j=0;j<nr;j++)
{
if(alloc[i][j]==0)
count++;
else
break;
}
if(count==nr)
mark[i]=1;
}
for(j=0;j<nr;j++)
w[j]=avail[j];
for(i=0;i<np;i++)
{
int canbeprocessed=0;
if(mark[i]!=1)
{
for(j=0;j<nr;j++) {
if(request[i][j]<=w[j]) canbeprocessed=1; else
{
canbeprocessed=0; break;
}
}
if(canbeprocessed)
{
mark[i]=1;
for(j=0;j<nr;j++)
w[j]+=alloc[i][j];
}
}
}
int deadlock=0;
for(i=0;i<np;i++)
if(mark[i]!=1)
deadlock=1;
if(deadlock)
printf("\n Deadlock Detected");
else
printf("\n No Deadlock Possible");
}
OUTPUT:
RESULT: Thus the C program to implement algorithm for deadlock detection was
written and executed.
Ex.no.9 Write C program to implement Threading
AIM:
To write a c program to implement Threading and Synchronization Applications.
ALGORITHM:
Step 1: Start the process
Step 2: Declare process thread, thread-id.
Step 3: Read the process thread and thread state.
Step 4: Check the process thread equals to thread-id by using if condition. Step 5:
Check the error state of the thread.
Step 6: Display the completed thread process.
Step 7: Stop the process
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
int counter;
void* trythis(void *arg)
{
unsigned long i = 0;
counter += 1;
printf("\n Job %d has Started………..\n", counter);
for(i=0; i<(0xFFFFFFFF);i++);
printf("\n Job %d has Finished………………\n", counter);
return NULL;
}
int main(void)
{
int i = 0;
int error;
while(i < 2)
{
error = pthread_create(&(tid[i]), NULL, &trythis, NULL);
if (error != 0)
printf("\nThread can't be created : [%s]", strerror(error));
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
OUTPUT:
RESULT:
Thus the c program to implement Threading and Synchronization Applications.
AIM:
To write a c program to implement Paging technique for memory management.
ALGORITHM:
Step 1: Start the process
Step 2: Declare page number, page table, frame number and process size.
Step 3: Read the process size, total number of pages
Step 4: Read the relative address
Step 5: Calculate the physical address
Step 6: Display the address
Step 7: Stop the process
PROGRAM:
#include<stdio.h>
main()
{
int memsize=15;
int pagesize,nofpage;
int p[100];
int frameno,offset;
int logadd,phyadd;
int i;
int choice=0;
printf("\nYour Memory Size is %d ",memsize);
printf("\nEnter Page Size:");
scanf("%d",&pagesize);
nofpage=memsize/pagesize;
for(i=0;i<nofpage;i++)
{
printf("\nEnter the Frame of Page: %d",i+1);
scanf("%d",&p[i]);
}
do
{
printf("\nEnter a Logical Address:");
scanf("%d",&logadd);
frameno=logadd/pagesize;
offset=logadd%pagesize;
phyadd=(p[frameno]*pagesize)+offset;
printf("\nPhysical address is:%d",phyadd);
printf("\nDo you want to continue(1/0)?:");
scanf("%d",&choice);
}while(choice==1);
}
OUTPUT:
RESULT:
Thus the c program to implement Paging technique for memory management was
written and executed.
Ex.no.11 Write C programs to implement the following Memory Allocation
Methods a. First Fit b. Worst Fit c. Best Fit
AIM:
To Write C programs to implement the following Memory Allocation Methods
a. First Fit b. Worst Fit c. Best Fit
a. FIRST FIT
ALGORITHM:
Step 1:Define the max as 25.
Step 2: Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp , highest=0,
bf[max],ff[max] .
Step 3: Get the number of blocks,files,size of the blocks using for loop. Step 4: In
for loop check bf[j]!=1, if so temp=b[j]-f[i]
Step 5: Check highest<temp,if so assign ff[i]=j,highest=temp
Step 6: Assign frag[i]=highest, bf[ff[i]]=1,highest=0
Step 7: Repeat step 4 to step 6.
Step 8: Print file no,size,block no,size and fragment.
Step 9: Stop the program.
PROGRAM:
#include<stdio.h>
#define max 25
main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}
OUTPUT:
b.WORST FIT
AIM:
To write a C program for implementation memory allocation methods for fixed
partition using worst fit.
ALGORITHM:
Step 1:Define the max as 25.
Step 2: Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp , highest=0,
bf[max],ff[max] .
Step 3: Get the number of blocks,files,size of the blocks using for loop. Step 4: In
for loop check bf[j]!=1, if so temp=b[j]-f[i]
Step 5: Check temp>=0,if so assign ff[i]=j break the for loop.
Step 6: Assign frag[i]=temp,bf[ff[i]]=1;
Step 7: Repeat step 4 to step 6.
Step 8: Print file no,size,block no,size and fragment.
Step 9: Stop the program
PROGRAM:
#include<stdio.h>
#define max 25
main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
printf("\n\t Memory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}
OUTPUT:
c BEST FIT MEMORY ALLOCATION:
To write a C program for implementation memory allocation methods for fixed
partition using best fit.
ALGORITHM:
Step 1:Define the max as 25.
Step 2:Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp , highest=0,
bf[max],ff[max] .
Step 3: Get the number of blocks,files,size of the blocks using for loop. Step 4: In
for loop check bf[j]!=1, if so temp=b[j]-f[i]
Step 5: Check lowest>temp,if so assign ff[i]=j,highest=temp
Step 6: Assign frag[i]=lowest, bf[ff[i]]=1,lowest=10000
Step 7: Repeat step 4 to step 6.
Step 8: Print file no,size,block no,size and fragment.
Step 9: Stop the program.
PROGRAM:
#include<stdio.h>
#define max 25
main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
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]);
}
OUTPUT:
RESULT:
Thus the C program for implementation memory allocation methods for fixed
partition using best fit were written and executed.
RESULT:
Thus the c program to implement LRU page replacement algorithm was written and
executed.
Ex No :12c IMPLEMENTATION OF OPTIMAL PAGE REPLACEMENT
ALGORITHM
AIM:
To write C program to implement optimal page replacement algorithm.
ALGORITHM:
Step 1: Start the process
Step 2: Declare the size
Step 3: Get the number of pages to be inserted
Step 4: Get the value
Step 5: Declare counter and stack
Step 6: Select the least frequently used page by counter value
Step 7: Stack them according the selection.
Step 8: Display the values
Step 9: Stop the process
PROGRAM:
#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2,
flag3, i, j, k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter page 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]){
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
flag3 =0;
for(j = 0; j < no_of_frames; ++j){
temp[j] = -1;
for(k = i + 1; k < no_of_pages; ++k){
if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}
}
for(j = 0; j < no_of_frames; ++j){
if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}
if(flag3 ==0){
max = temp[0];
pos = 0;
for(j = 1; j < no_of_frames; ++j)
{
if(temp[j] > max)
{
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}
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:
RESULT:
Thus the C program to implement optimal page replacement algorithm was written
and executed.
Ex.No.13a IMPLEMENTATION OF FILE ORGANIZATION -
SINGLE LEVEL DIRECTORY
AIM:
To write C program to organize the file using single level directory.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the count, file name, graphical interface.
Step 3: Read the number of files
Step 4: Read the file name
Step 5: Declare the root directory
Step 6: Using the file eclipse function define the files in a single level
Step 7: Display the files
Step 8: Stop the program
PROGRAM:
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
main()
{
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\
n 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:
RESULT:
Thus the C program to organize the file using single level directory was written and
executed.
Ex.No.13b IMPLEMENTATION OF FILE ORGANIZATION -TWO
LEVEL DIRECTORY
AIM:
To write C program to organize the file using two level directory.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the count, file name, graphical interface.
Step 3: Read the number of files
Step 4: Read the file name
Step 5: Declare the root directory
Step 6: Using the file eclipse function define the files in a single level Step 7:
Display the files
Step 8: Stop the program
PROGRAM:
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
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;
}
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:
RESULT:
Thus the C program to organize the file using two level directory was written and
executed.
Ex.No.14a IMPLEMENTATION OF SEQUENTIAL FILE ALLOCATION
AIM:
To write a C program for sequential file allocation for the student information.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of records user want to store in the system.
Step 3: Using Standard Library function open the file to write the data into the
file.
Step 4: Store the entered information in the system.
Step 5: Using do..While statement and switch case to create the options such as 1-
DISPLAY, 2.SEARCH, 3.EXIT.
Step 6: Close the file using fclose() function.
Step 7: Process it and display the result.
Step 8: Stop the program.
PROGRAM:
#include < stdio.h>
main()
{
int f[50], i, st, len, j, c, k, count = 0;
for(i=0;i<50;i++)
f[i]=0;
printf("Files Allocated are : \n");
x: count=0;
printf(“Enter starting block and length of files: ”);
scanf("%d%d", &st,&len);
for(k=st;k<(st+len);k++)
if(f[k]==0)
count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf(” The file is allocated to disk\n");
}
else
printf(” The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit();
}
OUTPUT:
RESULT:
Thus the C program for sequential file allocation for the student information was
written and executed.
Ex.No.14b IMPLEMENTATION OF LINKED FILE ALLOCATION
AIM:
To write a C program for implementation of linked file allocation. ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of records user want to store in the system.
Step 3: Using Standard Library function open the file to write the data into the file.
Step 4: Store the entered information in the system.
Step 5: Using do..While statement and switch case to create the options such as 1-
DISPLAY, 2.SEARCH, 3.EXIT.
Step 6: Close the file using fclose() function.
Step 7: Process it and display the result.
Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
main()
{
int f[50], p,i, st, len, j, c, k, a;
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: ");
scanf("%d",&p);
printf("Enter blocks already allocated: ");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
x: printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("%d-------->%d\n",j,f[j]);
}
else
{
printf("%d Block is already allocated \n",j);
k++;
}
}
}
else
printf("%d starting block is already allocated \n",st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
}
OUTPUT:
RESULT:
Thus the C program for implementation of linked file allocation was written and
executed.
OUTPUT:
RESULT:
Thus the C program for implementation of indexed file allocation was written and
executed.
Ex.No.15 IMPLEMENTATION OF DISK SCHEDULING
ALGORITHM (FCFS)
AIM:
To write a C program for implementation of FCFS Disk Scheduling Algorithm.
ALGORITHM:
Step 1: Start the program.
Step 2: Let Request array represents an array storing indexes of tracks that have
been requested in ascending order of their time of arrival. ‘head’ is the position
of disk head.
Step 3: Let us one by one take the tracks in default order and calculate
the absolute distance of the track from the head.
Step 4: Increment the total seek count with this distance.
Step 5: Currently serviced track position now becomes the new head position.
Step 6: Go to step 3 until all tracks in request array have not been serviced.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
scanf("%d",&queue[i]);
printf("Enter the initial head position\n");
scanf("%d",&head);
queue[0]=head;
for(j=0;j<=n-1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek %d n",
queue[j],queue[j+1],diff);
}
printf("Total Seek Time is %d\n",seek);
avg=seek/(float)n;
printf("Average Seek Time is %f\n",avg);
return 0;
}
OUTPUT:
RESULT:
Thus the C program for implementation of FCFS Disk Scheduling Algorithm was
written and executed.