0% found this document useful (0 votes)
4 views

OS Manual

Uploaded by

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

OS Manual

Uploaded by

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

Operating Systems Laboratory (BCS303)

1. Develop a c program to implement the Process system calls (fork (), exec(), wait(), create
process, terminate process)

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/wait.h>

int main()

{pid_t parent = getpid();

pid_t pid = fork();

if (pid == -1)

// error, failed to fork()

printf("fork failed\n");

else if (pid > 0)

int status;

printf("Parent id: %d\n will be waiting for child to complete\n",getpid());

waitpid(pid, &status, 0);

else

// we are the child

printf("child process1 id: %d\n",(int)pid);

Department of Artificial Intelligence & Machine Learning, BIT Page 1


int flag = 0;

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

if (need[i][j] > avail[j]){

flag = 1;

break;

if (flag == 0) {

ans[ind++] = i;

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

avail[y] += alloc[i][y];

f[i] = 1;

int flag = 1;

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

if(f[i]==0)

flag=0;

printf("The following system is not safe");

break;

if(flag==1)

printf("Following is the SAFE Sequence\n");

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

printf(" P%d ->", ans[i]);


printf(" P%d", ans[n - 1]);

return (0);

}
6. Develop a C program to simulate the following contiguous memory allocation Techniques:

a) Worst fit b) Best fit c) First fit.

a) Worst fit

#include <stdio.h>

void implimentWorstFit(int blockSize[], int blocks, int processSize[], int processes)

// This will store the block id of the allocated block to a process

int allocation[processes];

int occupied[blocks], i, j;

// initially assigning -1 to all allocation index means nothing is allocated currently

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

allocation[i] = -1;

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

occupied[i] = 0;

// pick each process and find suitable blocks according to its size ad assign to it

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

int indexPlaced = -1;

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

// if not occupied and block size is large enough

if(blockSize[j] >= processSize[i] && !occupied[j])

// place it at the first block fit to accomodate process

if (indexPlaced == -1)

indexPlaced = j;

// if any future block is larger than the current block where

// process is placed, change the block and thus indexPlaced

else if (blockSize[indexPlaced] < blockSize[j])

indexPlaced = j;
}

// If we were successfully able to find block for the process

if (indexPlaced != -1)

// allocate this block j to process p[i]

allocation[i] = indexPlaced;

// make the status of the block as occupied

occupied[indexPlaced] = 1;

// Reduce available memory for the block

blockSize[indexPlaced] -= processSize[i];

printf("\nProcess No.\tProcess Size\tBlock no.\n");

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

printf("%d \t\t\t %d \t\t\t", i+1, processSize[i]);

if (allocation[i] != -1)

printf("%d\n",allocation[i] + 1);

else

printf("Not Allocated\n");

int main()

int blockSize[] = {100, 50, 30, 120, 35};

int processSize[] = {40, 10, 30, 60};

int blocks = sizeof(blockSize)/sizeof(blockSize[0]);

int processes = sizeof(processSize)/sizeof(processSize[0]);

implimentWorstFit(blockSize, blocks, processSize, processes);

return 0;

}
b) Best fit

#include <stdio.h>

void implimentBestFit(int blockSize[], int blocks, int processSize[], int proccesses)

// This will store the block id of the allocated block to a process

int allocation[proccesses];

int occupied[blocks];

// initially assigning -1 to all allocation indexes means nothing is allocated currently

for(int i = 0; i < proccesses; i++){

allocation[i] = -1;

for(int i = 0; i < blocks; i++){

occupied[i] = 0;

// pick each process and find suitable blocks according to its size ad assign to it

for (int i = 0; i < proccesses; i++)

int indexPlaced = -1;

for (int j = 0; j < blocks; j++) {

if (blockSize[j] >= processSize[i] && !occupied[j])

// place it at the first block fit to accomodate process

if (indexPlaced == -1)

indexPlaced = j;

// if any future block is smalller than the current block where

// process is placed, change the block and thus indexPlaced

// this reduces the wastage thus best fit

else if (blockSize[j] < blockSize[indexPlaced])

indexPlaced = j;

}
// If we were successfully able to find block for the process

if (indexPlaced != -1)

// allocate this block j to process p[i]

allocation[i] = indexPlaced;

// make the status of the block as occupied

occupied[indexPlaced] = 1;

printf("\nProcess No.\tProcess Size\tBlock no.\n");

for (int i = 0; i < proccesses; i++)

printf("%d \t\t\t %d \t\t\t", i+1, processSize[i]);

if (allocation[i] != -1)

printf("%d\n",allocation[i] + 1);

else

printf("Not Allocated\n");

int main()

int blockSize[] = {100, 50, 30, 120, 35};

int processSize[] = {40, 10, 30, 60};

int blocks = sizeof(blockSize)/sizeof(blockSize[0]);

int proccesses = sizeof(processSize)/sizeof(processSize[0]);

implimentBestFit(blockSize, blocks, processSize, proccesses);

return 0 ;

}
c) First fit

#include<stdio.h>

// Function to allocate memory to blocks as per First fit algorithm

void firstFit(int blockSize[], int m, int processSize[], int n)

int i, j;

// Stores block id of the block allocated to a process

int allocation[n];

// Initially no block is assigned to any process

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

allocation[i] = -1;

// pick each process and find suitable blocks according to its size ad assign to it

for (i = 0; i < n; i++) //here, n -> number of processes

for (j = 0; j < m; j++) //here, m -> number of blocks

if (blockSize[j] >= processSize[i])

// allocating block j to the ith process

allocation[i] = j;

// Reduce available memory in this block.

blockSize[j] -= processSize[i];

break; //go to the next process in the queue

printf("\nProcess No.\tProcess Size\tBlock no.\n");

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

printf(" %i\t\t\t", i+1);


printf("%i\t\t\t\t", processSize[i]);

if (allocation[i] != -1)

printf("%i", allocation[i] + 1);

else

printf("Not Allocated");

printf("\n");

int main()

int m; //number of blocks in the memory

int n; //number of processes in the input queue

int blockSize[] = {100, 500, 200, 300, 600};

int processSize[] = {212, 417, 112, 426};

m = sizeof(blockSize) / sizeof(blockSize[0]);

n = sizeof(processSize) / sizeof(processSize[0]);

firstFit(blockSize, m, processSize, n);

return 0 ;

}
Operating Systems Laboratory (BCS303)

printf("parent process1 id: %d\n",getppid());

//execl("/bin/ls", "ls", "-S", "-l","-h", NULL);

execl("/usr/bin/echo", "echo", NULL);

//execl("/bin/cal", "cal", NULL);

_exit(EXIT_FAILURE); // exec never returns

Department of Artificial Intelligence & Machine Learning, BIT Page 2


Operating Systems Laboratory (BCS303)

2. Simulate the following CPU scheduling algorithms to find turnaround time and waiting
time
a) FCFS b) SJF c) Round Robin d) Priority.
FIRST COME FIRST SERVE (FCFS)
#include<stdio.h>
#include<conio.h>
main()
{
int bt[20], wt[20], tat[20], i, n;
float wtavg, tatavg;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
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("\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", i, bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
}

Department of Artificial Intelligence & Machine Learning, BIT Page 3


Operating Systems Laboratory (BCS303)

SHORTEST JOB FIRST (SJF)

#include<stdio.h>
#include<conio.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];
}

Department of Artificial Intelligence & Machine Learning, BIT Page 4


Operating Systems Laboratory (BCS303)

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);
}

Department of Artificial Intelligence & Machine Learning, BIT Page 5


Operating Systems Laboratory (BCS303)

ROUND ROBIN
#include<stdio.h>

int main()

int count,j,n,time,remain,flag=0,time_quantum;

int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];

printf("Enter Total Process:\t ");

scanf("%d",&n);

remain=n;

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

printf("Enter Arrival Time and Burst Time for Process ProcessNumber %d


:",count+1);

scanf("%d",&at[count]);

scanf("%d",&bt[count]);

rt[count]=bt[count];

printf("Enter Time Quantum:\t");

scanf("%d",&time_quantum);

printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");

for(time=0,count=0;remain!=0;)

if(rt[count]<=time_quantum && rt[count]>0)

time+=rt[count];

rt[count]=0;

flag=1;

Department of Artificial Intelligence & Machine Learning, BIT Page 6


Operating Systems Laboratory (BCS303)

else if(rt[count]>0)

rt[count]-=time_quantum;

time+=time_quantum;

if(rt[count]==0 && flag==1)

remain--;

printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],

time-at[count]-bt[count]);

wait_time+=time-at[count]-bt[count];

turnaround_time+=time-at[count];

flag=0;

if(count==n-1)

count=0;

else if(at[count+1]<=time)

count++;

else

count=0;

printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);

printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);

return 0;

Department of Artificial Intelligence & Machine Learning, BIT Page 7


Operating Systems Laboratory (BCS303)

PRIORITY
#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++)

Department of Artificial Intelligence & Machine Learning, BIT Page 8


Operating Systems Laboratory (BCS303)

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);
}

Department of Artificial Intelligence & Machine Learning, BIT Page 9


Operating Systems Laboratory (BCS303)

3. Develop a C program to simulate producer-consumer problem using semaphores.


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

int mutex = 1;
int full = 0;
int empty = 10, x = 0;

// Function to produce an item and add it to the buffer


void producer()
{
--mutex;
++full;
--empty;

// Item produced
x++;
printf("\nProducer produces" "item %d", x);
// Increase mutex value by 1
++mutex;
}

// Function to consume an item and remove it from buffer


void consumer()
{
--mutex;
--full;
++empty;
printf("\nConsumer consumes " "item %d", x);
x--;
// Increase mutex value by 1
++mutex;
}
int main()
{
int n, i;
printf("\n1.For Producer" "\n2.For Consumer" "\n3.For Exit");
for (i = 1; i > 0; i++) {

printf("\nEnter your choice:");


scanf("%d", &n);

// Switch Cases
switch (n) {
case 1:
if ((mutex == 1) && (empty != 0))
{
Department of Artificial Intelligence & Machine Learning, BIT Page 10
Operating Systems Laboratory (BCS303)

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;
}
}
}

Department of Artificial Intelligence & Machine Learning, BIT Page 11


Operating Systems Laboratory (BCS303)

4. Develop a C program which demonstrates interprocess communication between a reader process and a writer
process. Use mkfifo, open, read, write and close APIs in your program.

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <string.h>

#define FIFO_NAME "myfifo"

void writerProcess() {

int fd;

char *message = "Hello, reader!";

mkfifo(FIFO_NAME, 0666);

fd = open(FIFO_NAME, O_WRONLY);

write(fd, message, strlen(message) + 1);

close(fd);

printf("Data sent to reader successfully!\n");

void readerProcess() {

int fd;

char readMessage[100];

fd = open(FIFO_NAME, O_RDONLY);

Department of Artificial Intelligence & Machine Learning, BIT Page 12


Operating Systems Laboratory (BCS303)

read(fd, readMessage, sizeof(readMessage));

printf("Received message: %s\n", readMessage);

close(fd);

int main() {

pid_t pid;

// Create a child process

pid = fork();

if (pid < 0) {

fprintf(stderr, "Fork failed!\n");

return 1;

} else if (pid > 0) {

// Parent process (Writer)

writerProcess();

} else {

// Child process (Reader)

readerProcess();

return 0;

Department of Artificial Intelligence & Machine Learning, BIT Page 13


5. Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance.

#include <stdio.h>

int main()

// P0, P1, P2, P3, P4 are the Process names here

int n, m, i, j, k;

n = 5; // Number of processes

m = 3; // Number of resources

int alloc[5][3] = { { 1, 1, 2 }, // P0 // Allocation Matrix

{ 2, 1, 2 }, // P1

{ 4, 0, 1 }, // P2

{ 0, 2, 0 }, // P3

{ 1, 1, 2 } }; // P4

int max[5][3] = { { 4, 3, 3 }, // P0 // MAX Matrix

{ 3, 2, 2 }, // P1

{ 9, 0, 2 }, // P2

{ 7, 5, 3 }, // P3

{ 1, 1, 2 } }; // P4

int avail[3] = { 3, 3, 2 }; // Available Resources

int f[n], ans[n], ind = 0;

for (k = 0; k < n; k++) {

f[k] = 0;

int need[n][m];

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

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

need[i][j] = max[i][j] - alloc[i][j];

int y = 0;

for (k = 0; k < 5; k++) {

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

if (f[i] == 0) {
int flag = 0;

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

if (need[i][j] > avail[j]){

flag = 1;

break;

if (flag == 0) {

ans[ind++] = i;

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

avail[y] += alloc[i][y];

f[i] = 1;

int flag = 1;

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

if(f[i]==0)

flag=0;

printf("The following system is not safe");

break;

if(flag==1)

printf("Following is the SAFE Sequence\n");

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

printf(" P%d ->", ans[i]);


printf(" P%d", ans[n - 1]);

return (0);

}
6. Develop a C program to simulate the following contiguous memory allocation Techniques:

a) Worst fit b) Best fit c) First fit.

a) Worst fit

#include <stdio.h>

void implimentWorstFit(int blockSize[], int blocks, int processSize[], int processes)

// This will store the block id of the allocated block to a process

int allocation[processes];

int occupied[blocks], i, j;

// initially assigning -1 to all allocation index means nothing is allocated currently

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

allocation[i] = -1;

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

occupied[i] = 0;

// pick each process and find suitable blocks according to its size ad assign to it

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

int indexPlaced = -1;

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

// if not occupied and block size is large enough

if(blockSize[j] >= processSize[i] && !occupied[j])

// place it at the first block fit to accomodate process

if (indexPlaced == -1)

indexPlaced = j;

// if any future block is larger than the current block where

// process is placed, change the block and thus indexPlaced

else if (blockSize[indexPlaced] < blockSize[j])

indexPlaced = j;
}

// If we were successfully able to find block for the process

if (indexPlaced != -1)

// allocate this block j to process p[i]

allocation[i] = indexPlaced;

// make the status of the block as occupied

occupied[indexPlaced] = 1;

// Reduce available memory for the block

blockSize[indexPlaced] -= processSize[i];

printf("\nProcess No.\tProcess Size\tBlock no.\n");

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

printf("%d \t\t\t %d \t\t\t", i+1, processSize[i]);

if (allocation[i] != -1)

printf("%d\n",allocation[i] + 1);

else

printf("Not Allocated\n");

int main()

int blockSize[] = {100, 50, 30, 120, 35};

int processSize[] = {40, 10, 30, 60};

int blocks = sizeof(blockSize)/sizeof(blockSize[0]);

int processes = sizeof(processSize)/sizeof(processSize[0]);

implimentWorstFit(blockSize, blocks, processSize, processes);

return 0;

}
b) Best fit

#include <stdio.h>

void implimentBestFit(int blockSize[], int blocks, int processSize[], int proccesses)

// This will store the block id of the allocated block to a process

int allocation[proccesses];

int occupied[blocks];

// initially assigning -1 to all allocation indexes means nothing is allocated currently

for(int i = 0; i < proccesses; i++){

allocation[i] = -1;

for(int i = 0; i < blocks; i++){

occupied[i] = 0;

// pick each process and find suitable blocks according to its size ad assign to it

for (int i = 0; i < proccesses; i++)

int indexPlaced = -1;

for (int j = 0; j < blocks; j++) {

if (blockSize[j] >= processSize[i] && !occupied[j])

// place it at the first block fit to accomodate process

if (indexPlaced == -1)

indexPlaced = j;

// if any future block is smalller than the current block where

// process is placed, change the block and thus indexPlaced

// this reduces the wastage thus best fit

else if (blockSize[j] < blockSize[indexPlaced])

indexPlaced = j;

}
// If we were successfully able to find block for the process

if (indexPlaced != -1)

// allocate this block j to process p[i]

allocation[i] = indexPlaced;

// make the status of the block as occupied

occupied[indexPlaced] = 1;

printf("\nProcess No.\tProcess Size\tBlock no.\n");

for (int i = 0; i < proccesses; i++)

printf("%d \t\t\t %d \t\t\t", i+1, processSize[i]);

if (allocation[i] != -1)

printf("%d\n",allocation[i] + 1);

else

printf("Not Allocated\n");

int main()

int blockSize[] = {100, 50, 30, 120, 35};

int processSize[] = {40, 10, 30, 60};

int blocks = sizeof(blockSize)/sizeof(blockSize[0]);

int proccesses = sizeof(processSize)/sizeof(processSize[0]);

implimentBestFit(blockSize, blocks, processSize, proccesses);

return 0 ;

}
c) First fit

#include<stdio.h>

// Function to allocate memory to blocks as per First fit algorithm

void firstFit(int blockSize[], int m, int processSize[], int n)

int i, j;

// Stores block id of the block allocated to a process

int allocation[n];

// Initially no block is assigned to any process

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

allocation[i] = -1;

// pick each process and find suitable blocks according to its size ad assign to it

for (i = 0; i < n; i++) //here, n -> number of processes

for (j = 0; j < m; j++) //here, m -> number of blocks

if (blockSize[j] >= processSize[i])

// allocating block j to the ith process

allocation[i] = j;

// Reduce available memory in this block.

blockSize[j] -= processSize[i];

break; //go to the next process in the queue

printf("\nProcess No.\tProcess Size\tBlock no.\n");

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

printf(" %i\t\t\t", i+1);


printf("%i\t\t\t\t", processSize[i]);

if (allocation[i] != -1)

printf("%i", allocation[i] + 1);

else

printf("Not Allocated");

printf("\n");

int main()

int m; //number of blocks in the memory

int n; //number of processes in the input queue

int blockSize[] = {100, 500, 200, 300, 600};

int processSize[] = {212, 417, 112, 426};

m = sizeof(blockSize) / sizeof(blockSize[0]);

n = sizeof(processSize) / sizeof(processSize[0]);

firstFit(blockSize, m, processSize, n);

return 0 ;

}
7. Develop a C program to simulate page replacement algorithms: a) FIFO b) LRU
a) FIFO
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE LENGTH OF REFERENCE STRING \n");
scanf("%d",&n);
printf("\n ENTER THE REFERENCE STRING:\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("\tref string\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;
}
b) LRU
#include<stdio.h>
main()
{
int i, j , k, min, rs[25], m[10], count[10], flag[25], n, f, pf=0, next=1;
printf("Enter the length of reference string -- ");
scanf("%d",&n);
printf("Enter the reference string -- ");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]);
flag[i]=0;
}
printf("Enter the number of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0;
m[i]=-1;
}
printf("\nThe Page Replacement process is -- \n");
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
flag[i]=1;
count[j]=next;
next++;
}
}
if(flag[i]==0)
{
if(i<f)
{ m[i]=rs[i];
count[i]=next;
next++;
}
else
{ min=0;
for(j=1;j<f;j++)
if(count[min] > count[j])
min=j;
m[min]=rs[i];
count[min]=next;
next++;
}
pf++;
}
for(j=0;j<f;j++)
printf("%d\t", m[j]);
if(flag[i]==0)
printf("PF No. -- %d" , pf);
printf("\n");
}
printf("\nThe number of page faults using LRU are %d",pf);
}
8. Simulate following File Organization Techniques a) Single level directory b) Two level
directory
a) 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);
}
}
}
b) Two level directory
#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);
}
}
}
9. Develop a C program to simulate the Linked file allocation strategies
#include<stdio.h>

struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
}
10. Develop a C program to simulate SCAN disk scheduling algorithm.
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20], temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
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",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]<queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}

for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
for(i=temp1+2,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[i]=0;
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;
}

You might also like