0% found this document useful (0 votes)
7 views41 pages

Name Atharva Tatkare Class TE Div B Roll No:307b006

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)
7 views41 pages

Name Atharva Tatkare Class TE Div B Roll No:307b006

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

Name: Atharva Tatkare

Class: TE Div: B
Roll no:307B006
` Experiment :
A. Study of Basic Linux Commands: echo, ls, read, cat, touch, test, loops, arithmetic comparison,
conditional loops, grep, sed etc.

B. Write a program to implement an address book with options given below: a) Create address

book. b) View address book. c) Insert a record. d) Delete a record. e) Modify a

record. f) Exit
`

opt=1
while [ "$opt" -lt 6 ] do

echo -e "Choose one of the Following\n1. Create a New Address Book\n2. View Records\n3.
Insert new Record\n4. Delete a Record\n5. Modify a Record\n6. Exit"

read opt
case $opt in

1) echo "Enter filename"


read fileName
if [ -e $fileName ] ; then # -e to check if file exists, if exits remove the file rm
$fileName
fi
cont=1
echo "NAME\tNUMBER\t\tADDRESS\
n===============================\n" | cat >> $fileName
while [ "$cont" -gt 0 ] do
echo "\nEnter Name" read
name echo "Enter Phone Number of $name"
read number
echo "Enter Address of $name"
read address echo "$name\t$number\t\t$address" |
cat >> $fileName echo "Enter 0 to Stop, 1 to Enter next"
read cont
done
;;
2) cat $fileName
;;
3) echo "\nEnter Name" read name echo "Enter Phone
Number of
$name" read number echo "Enter
Address of $name" read address
echo "$name\t$number\t\t$address" | cat >> $fileName
;;
4) echo "Delete record\nEnter
Name/Phone Number"
read pattern temp="temp"
grep -v $pattern $fileName | cat >>
$temp rm $fileName cat $temp | cat >>
$fileName
rm $temp
;;
5) echo "Modify record\nEnter Name/Phone Number"
read pattern temp="temp" grep -v $pattern $fileName
| cat >> $temp rm $fileName cat $temp | cat >>
$fileName rm $temp echo "Enter Name" read name
echo "Enter Phone Number of
$name" read number echo "Enter
Address of $name" read address
echo -e "$name\t$number\t$address" | cat >> $fileName
;;
esac done
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006
Experiment 2 :
A. Implement the C program in which main program accepts the integers to be sorted.
Mainprogram uses the FORK system call to create a new process called a child process. Parent
process sorts the integers using sorting algorithm and waits for child process using WAIT system
call to sort the integers using any sorting algorithm. Also, demonstrate zombie and orphan
states.

B. Implement the C program in which main program accepts an array. Main program uses
theFORK system call to create a new process called a child process. Parent process sorts an
array and passes the sorted array to child process through the command line arguments of
EXECVE system call. The child process uses EXECVE system call to load new program that display
array in reverse order.

Code -

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

void bubbleSort(int arr[], int n) {


int i, j, temp; for (i = 0; i <
n-1; i++) { for (j = 0; j < ni-1;
j++) { if (arr[j] > arr[j+1]) {
temp =
arr[j]; arr[j] =
arr[j+1];
arr[j+1] = temp;
}
}
}
}

int main() {
int n, i;
printf("Enter the number of integers to be sorted:
"); scanf("%d", &n); int arr[n];
printf("Enter the integers to be sorted: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
pid_t pid = fork();
if (pid == 0) { //
Child process printf("Child process sorting the integers using bubble sort
algorithm...\n");
bubbleSort(arr, n);
printf("Sorted integers by child process: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
system("ps -x");
printf("\n"); } else
if (pid > 0) { //
Parent process printf("Parent process waiting for child process to
complete...\n");
wait(NULL); // Wait for the child process to complete printf("Sorted
integers by parent process: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n"); } else {
// fork failed
printf("Fork failed!\n");
return 1;
}
return 0;
}
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006

Experiment 2 :
A. Implement the C program in which main program accepts the integers to be sorted.
Mainprogram uses the FORK system call to create a new process called a child process. Parent
process sorts the integers using sorting algorithm and waits for child process using WAIT system
call to sort the integers using any sorting algorithm. Also, demonstrate zombie and orphan
states.

B. Implement the C program in which main program accepts an array. Main program uses
theFORK system call to create a new process called a child process. Parent process sorts an
array and passes the sorted array to child process through the command line arguments of
EXECVE system call. The child process uses EXECVE system call to load new program that display
array in reverse order.
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006

Experiment 2 :
A. Implement the C program in which main program accepts the integers to be sorted.
Mainprogram uses the FORK system call to create a new process called a child process. Parent
process sorts the integers using sorting algorithm and waits for child process using WAIT system
call to sort the integers using any sorting algorithm. Also, demonstrate zombie and orphan
states.

B. Implement the C program in which main program accepts an array. Main program uses
theFORK system call to create a new process called a child process. Parent process sorts an
array and passes the sorted array to child process through the command line arguments of
EXECVE system call. The child process uses EXECVE system call to load new program that display
array in reverse order.
Code - Parent

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

void bubbleSort(int arr[], int n) {


int i, j, temp; for (i = 0; i <
n-1; i++) { for (j = 0; j < ni-1;
j++) { if (arr[j] > arr[j+1]) {
temp =
arr[j]; arr[j] =
arr[j+1];
arr[j+1] = temp;
}
}
}
}

int main() {
int n, i; printf("Enter the number of integers to
be sorted:
"); scanf("%d", &n); int arr[n];
printf("Enter the integers to be sorted: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

pid_t pid = fork();


if (pid < 0)
{
printf("Fork failed!\n");
return 1; } else
if (pid == 0) { char
*args[n+2];
args[0] = "./child";

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


*arg = (char *)malloc(20);
sprintf(arg, "%d", arr[i]);
args[i+1] = arg;
}
args[n+1] = NULL;

execve(args[0], args, NULL);

perror("execve failed");
return 1; } else { printf("Parent process (PID: %d) sorting the
integers...\n", getpid());
bubbleSort(arr, n);

printf("Parent process (PID: %d) sorted integers: ", getpid());


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

wait(NULL);
}
return 0;
}
Child -

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

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


int n = argc - 1;
int arr[n];

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


arr[i] = atoi(argv[i+1]);
}

printf("Child process (PID: %d) displaying integers in reverse order: ", getpid());
for (int i = n-1; i >= 0; i--) { printf("%d ", arr[i]);
}
printf("\n");

return 0;
}
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006

Experiment 2 :
A. Implement the C program in which main program accepts the integers to be sorted.
Mainprogram uses the FORK system call to create a new process called a child process. Parent
process sorts the integers using sorting algorithm and waits for child process using WAIT system
call to sort the integers using any sorting algorithm. Also, demonstrate zombie and orphan
states.

B. Implement the C program in which main program accepts an array. Main program uses
theFORK system call to create a new process called a child process. Parent process sorts an
array and passes the sorted array to child process through the command line arguments of
EXECVE system call. The child process uses EXECVE system call to load new program that display
array in reverse order.
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006

Experiment - Implement the C program for CPU Scheduling Algorithms: Shortest Job First

(Preemptive) and Round Robin with different arrival time.

Code -

#include <stdio.h> #include


<stdbool.h>

struct Process
{
int pid;
int at; int
bt;
int ct, tt, wt, rt, st;
};

int main()
{ int size = 0; printf("Enter number of
processes: "); scanf("%d", &size);
struct Process ps[size];

printf("\nEnter process Details: \n"); for


(int i = 0; i < size; ++i)
{
printf("Enter %dth process details: \n", i + 1);
ps[i].pid = i + 1;

printf("\tEnter Arrival Time: "); scanf("%d",


&ps[i].at);

printf("\tEnter Burst Time: ");


scanf("%d", &ps[i].bt);
}
printf("\
n===================================================================
=================\n\n");
printf("PID\tAT \t BT\n"); for
(int i = 0; i < size; i++)
{
printf("%d \t %d \t %d \n", ps[i].pid, ps[i].at, ps[i].bt);
}
printf("\n\
n===================================================================
=================\n\n"); // Input Done

int n = size; int completed = 0; int


currentTime = 0; int burstTimeR[4]; bool
iscompleted[4] = {false}; float avgWT =
0, avgTT = 0, avgRT = 0;

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


{
burstTimeR[i] = ps[i].bt; //Sets the remaining burst times to the initial
burst times and marks all processes as not completed.
}

while (completed != n)
{

int minimum = 99999;


int miniI = -1;
for (int i = 0; i < n; i++)
{
if ((ps[i].at <= currentTime) && (iscompleted[i] == false)) //Finds
the process with the smallest remaining burst time that has arrived and is not yet completed.
If there are multiple processes with the same burst time, the one with the earliest arrival time
is chosen
{ if (burstTimeR[i] < minimum)
{ minimum = burstTimeR[i];
miniI = i;
}
if (burstTimeR[i] == minimum)
{
if (ps[i].at < ps[miniI].at)
{ minimum = burstTimeR[i];
miniI = i;
}
}
}
}

if (miniI == -1)
{ currentTime++;
}
else
{

if (burstTimeR[miniI] == ps[miniI].bt)
{ ps[miniI].st = currentTime;
}

burstTimeR[miniI] -= 1; currentTime++;

if (burstTimeR[miniI] == 0)
{

ps[miniI].ct = currentTime; ps[miniI].tt =


ps[miniI].ct - ps[miniI].at; ps[miniI].wt =
ps[miniI].tt - ps[miniI].bt; ps[miniI].rt = ps[miniI].st
- ps[miniI].at;

avgWT += ps[miniI].wt;
avgTT += ps[miniI].tt; avgRT
+= ps[miniI].rt;

completed++;
iscompleted[miniI] = true;
}
}
}
printf("PID \t AT \t BT \t CT \t TT \t WT \t RT \t\n"); for
(int i = 0; i < n; i++)
{
printf("%d \t %d \t %d \t %d \t %d \t %d \t %d \t\n", ps[i].pid, ps[i].at, ps[i].bt,
ps[i].ct, ps[i].tt, ps[i].wt, ps[i].rt);
}
printf("\n\
n===================================================================
=================\n");

printf("\n\n AVG WT: %f", avgWT / n);


printf("\n\n AVG TT: %f", avgTT / n);
printf("\n\n AVG RT: %f", avgRT / n); printf("\n\
n===================================================================
=================\n");
}
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006

Experiment - Implement the C program for CPU Scheduling Algorithms: Shortest Job First
(Preemptive) and Round Robin with different arrival time.
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006

Experiment - Implement the C program for CPU Scheduling Algorithms: Shortest Job First
(Preemptive) and Round Robin with different arrival time.

Code -

#include<stdio.h>

int main()
{

int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];


float avg_wt, avg_tat;
printf(" Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP;

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


{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t"); scanf("%d", &at[i]); printf(" \nBurst time is:
\t"); scanf("%d", &bt[i]); temp[i] = bt[i];
}

printf("Enter the Time Quantum for the process: \t");


scanf("%d", &quant);

printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0)
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{ y--
;
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-
bt[i]); wt = wt+sum-at[i]-bt[i]; tat = tat+sum-at[i]; count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++; }
else {
i=0;
}
}

avg_wt = wt * 1.0/NOP; avg_tat = tat * 1.0/NOP;


printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat); }
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006

Experiment - Implement the C program for CPU Scheduling Algorithms: Shortest Job First
(Preemptive) and Round Robin with different arrival time.
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006

Experiment - A. Thread synchronization using counting semaphores. Application to


demonstrate: producer- consumer problem with counting
semaphores and mutex.

B. Thread synchronization and mutual exclusion using mutex.

Application to demonstrate: Reader- Writer problem with reader


priority.
Code-

#include<stdio.h>
#include<sys/types.h>
#include<semaphore.h>
#include<pthread.h>
#include<unistd.h>
char avbuff[10]; sem_t
mutex,empty,full;

void *produce(void *arg)


{ int i;
printf("\n Inside Producer"); for(i=0;i<10;i++)
{ sem_wait(&empty);
sem_wait(&mutex)
; avbuff[i]=i; printf("\nItem produced is
%d",avbuff[i]);
sem_post(&mutex);
sem_post(&full);
sleep(1);
}
pthread_exit(nullptr);
}
void *consumer(void *arg)
{ int j;
printf("\nInside consumer");
for(j=0;j<10;j++)
{ sem_wait(&full);
sem_wait(&mutex)
; j=avbuff[j];
printf("\n Item consumed is
%d",j); sem_post(&mutex);
sem_post(&empty); sleep(5); }
pthread_exit(nullptr);
}

int main()
{ pthread_t tid1,tid2;
sem_init(&empty,0,10);
sem_init(&full,0,0);
sem_init(&mutex,1,1);
void *status;
pthread_create(&tid1,NULL,produce,NULL);
pthread_create(&tid2,NULL,consumer,NULL);
pthread_join(tid1,&status);
printf("\nThe exited status %s \n",(char *)status);
pthread_join(tid2,&status);
printf("\nThe exited status %s \n",(char *)status);
return 1;
}
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006

Experiment - A. Thread synchronization using counting semaphores. Application to


demonstrate: producer- consumer problem with counting
semaphores and mutex.

B. Thread synchronization and mutual exclusion using mutex.


Application to demonstrate: Reader- Writer problem with reader
priority.
Output -
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006

Experiment - A. Thread synchronization using counting semaphores. Application


to demonstrate: producer- consumer problem with counting
semaphores and mutex.

B. Thread synchronization and mutual exclusion using mutex.


Application to demonstrate: Reader- Writer problem with reader
priority.

Code-

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include<stdlib.h>
#define READERS 3
#define WRITERS 2

pthread_mutex_t
mutex; sem_t
rw_mutex; int
readers_count = 0;
int shared_resource = 0;

void *reader(void *arg) { int id =


*((int *) arg); while (1) {
sleep(rand() % 3);
pthread_mutex_lock(&mutex);
readers_count++; if
(readers_count == 1) {
sem_wait(&rw_mutex); }
pthread_mutex_unlock(&mutex);
printf("Reader %d is reading: %d\n", id, shared_resource);
pthread_mutex_lock(&mutex);
readers_count--; if
(readers_count == 0) {
sem_post(&rw_mutex);
}
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}

void *writer(void *arg) {


int id = *((int *) arg);
while (1) { sleep(rand()
% 3);
sem_wait(&rw_mutex);
shared_resource++;
printf("Writer %d is writing: %d\n", id, shared_resource);
sem_post(&rw_mutex);
}
pthread_exit(NULL);
}

int main() { pthread_t reader_threads[READERS],


writer_threads[WRITERS]; int reader_ids[READERS],
writer_ids[WRITERS];

pthread_mutex_init(&mutex, NULL);
sem_init(&rw_mutex, 0, 1);

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


reader_ids[i] = i + 1;
pthread_create(&reader_threads[i], NULL, reader, &reader_ids[i]);
}

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


writer_ids[i] = i + 1;
pthread_create(&writer_threads[i], NULL, writer, &writer_ids[i]);
}

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


pthread_join(reader_threads[i], NULL);
}
for (int i = 0; i < WRITERS; i++) {
pthread_join(writer_threads[i], NULL);
}

pthread_mutex_destroy(&mutex);
sem_destroy(&rw_mutex);

return 0;
}
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006

Experiment - A. Thread synchronization using counting semaphores. Application to


demonstrate: producer- consumer problem with counting
semaphores and mutex.

B. Thread synchronization and mutual exclusion using mutex.


Application to demonstrate: Reader- Writer problem with reader
priority.

Output -
Name: Atharva Tatkare
Class: TE Div: B

Roll no:307B006
`

Experiment : Implement the C program for Deadlock Avoidance Algorithm: Bankers


Algorithm.

`
Code - #include<stdio.h>

struct process{ int max[10],allocate[10],need[10];


}p[10];

int n,m; void input(int[]); void


display(); int
isSafestate(int[],int[]); int safetyalgorithm(int[],int[]);

int main(){
int i;
printf("\nEnter No of processes: ");
scanf("%d",&n);
printf("Enter no of resources: ");
scanf("%d",&m);
int available[m]; int safesequence[n];
printf("\n*****Enter details of process*****");
input(available); display(); if(isSafestate(available,safesequence))
{ printf("\n\tSYSTEM IS IN SAFE STATE...");
printf("\nsafesequence is: "); for(i=0;i<n;i++)
printf("P%d -> ",safesequence[i]);
}
else
printf("\nSYSTEM IS IN UNSAFE STATE!!!");
return 0;
}
void input (int available[]){

int i,j;
for(i=0;i<n;i++){

printf("\nEnter the details of process P%d: ",i);


printf("\n\tEnter the allocates resources: "); for(j=0;j<m;j++)
{ scanf("%d",&p[i].allocate[j]);
}
printf("\tEnter the max resourcess: "); for(j=0;j<m;j++)
{ scanf("%d",&p[i].max[j]); p[i].need[j]=p[i].max[j]-
p[i].allocate[j];
}
}
printf("\nEnter the allvailable resources: "); for(j=0;j<m;j++)
{ scanf("%d",&available[j]);
}}
void
display()
{ int i,j;
printf("\n\tPID\tALLOCATE\tMAX\t\tNEED\n");
for(i=0;i<n;i++)
{
printf("\tP%d\t",i);
for(j=0;j<m;j++) printf("%d
",p[i].allocate[j]);
printf("\t\t");
for(j=0;j<m;j++) printf("%d
",p[i].max[j]); printf("\t\t");
for(j=0;j<m;j++) printf("%d
",p[i].need[j]); printf("\n");
}
}

int isSafestate(int available[],int safesequence[])


{
if(safetyalgorithm(available,safesequence)==1)
return 1;
return 0;
}

int safetyalgorithm(int available[],int safesequence[])


{ int
i,j;

int work[m],finish[n]; for(j=0;j<m;j++)


work[j]=available[j];

for(i=0;i<n;i++)
finish[i]=0;

int proceed=1,k=0;
while(proceed)
{ proceed=0;
for(i=0;i<n;i++)
{
int flag=1; if(finish[i]==0)
{ for(j=0;j<m;j++)
{ if(p[i].need[j]<=work[j])
{
continue;
}
else
{ flag=0;
break;
}
} if(flag==0)
continue;

for(j=0;j<m;j++)
{
work[j]+=p[i].allocate[j];
}
finish[i]=1;
safesequence[k++]=i;
proceed=1;
}
}
}
for(i=0;i<n&&finish[i]==1;i++)
continue;
if(i==n)
return 1;
return 0;
}
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006

`
Experiment : Implement the C program for Deadlock Avoidance Algorithm: Bankers
Algorithm.
`
Output -
Name: Atharva Tatkare

Class: TE Div: B
Roll no:307B006

Experiment 6 : Implement the C program for Page Replacement Algorithms: FCFS, LRU, and Optimal for
frame size as minimum three.

Code -

#include<stdio.h>
typedef struct {

char
data[20][2]; int
end; }queue;

void enqueue(queue *q,char data,int position);


char dequeue(queue *q,int position); void
fifo(char string[],int frameSize,int count); void
optimal(char string[],int frameSize,int count); void
lru(char string[],int frameSize,int count); int
main() {

int frameSize,count,cnt,ch;
char string[50];
printf("Enter the string: ");
count=0; do{
scanf("%c",&string[count]);
count++;

}
while(string[count-1]!='\n'); count-
-; printf("\nEnter the size of the frame:
"); scanf("%d",&frameSize);
do{
printf("\nMENU\n====\n1.FIFO\n2.Least Recently Used (LRU)\n3.Optimal\n4.Exit\ n\nYour
Choice:"); scanf("%d",&ch); switch(ch){ case 1:
fifo(string,frameSize,count);
break;
case 2:
lru(string,frameSize,count);
break; case 3:
optimal(string,frameSize,count);
break; case 4:
//exit(0);
break;

default:
printf("\nInvalid choice! Please try again!");
continue;

}
while(ch!=4);

void enqueue(queue *q,char data,int position) { q-

>data[position][0]=data;

}
char dequeue(queue *q,int position){
char value;
value=q->data[position][0];
return(value);
}

void fifo(char string[],int frameSize,int count) {


int cnt,cnt2,flag,faults=0;
queue q; int firstin=-1;
q.end=0;
printf("\nData Requested\tFrame contents\t Page Fault\
n==============================================");
for(cnt=0;cnt<count;cnt+=2) {
printf("\n\n\t%c",string[cnt]);
flag=0;
for(cnt2=0;cnt2<q.end;cnt2++) {
if(string[cnt]==q.data[cnt2][0]){
flag=1;
break; }

}
if(flag==0){
faults++;
if(q.end<frameSize) {
enqueue(&q,string[cnt],q.end);
q.end++;

}
else{

dequeue(&q,firstin);
firstin=(firstin+1)%(q.end);
enqueue(&q,string[cnt],firstin);

} printf("\t ");
for(cnt2=0;cnt2<q.end;cnt2++) {
printf("%c ",q.data[cnt2][0]);

}
printf("\t\tY");

}
else{
printf("\t ");
for(cnt2=0;cnt2<q.end;cnt2++) {
printf("%c ",q.data[cnt2][0]);
}

printf("\t\tN");

}
} printf("\n\n==============================================\n");
printf("\nTotal no. of Page Faults: %d\n\n",faults);

}
void optimal(char string[],int frameSize,int count){
int
cnt,cnt2,selector,flag,max,faults=0;
int distance[20]; queue q;
q.end=0;
printf("\nData Requested\tFrame contents\t Page Fault\
n==============================================");
for(cnt=0;cnt<count;cnt+=2){ printf("\n\n\t%c",string[cnt]);
flag=0; for(cnt2=0;cnt2<q.end;cnt2++) {

if(string[cnt]==q.data[cnt2][0]) {
flag=1; break;

if(flag==0) {
faults++;
if(q.end<frameSize) {
enqueue(&q,string[cnt],q.end);
q.data[q.end][1]=cnt;
q.end++;
} else {
for(cnt2=0;cnt2<q.end;cnt2++) {
distance[cnt2]=0;
}
for(selector=0;selector<q.end;selector++) {
for(cnt2=cnt;cnt2<count;cnt2+=2){
if(string[cnt2]==q.data[selector][0]) {
distance[selector]=cnt2/2;
break;
}
if(distance[selector]==0) {
distance[selector]=99-q.data[selector][1]; }
} }
max=0;
for(cnt2=0;cnt2<q.end;cnt2++) {
if(distance[cnt2]>max) {
max=distance[cnt2];
selector=cnt2;
}
}
dequeue(&q,selector);
enqueue(&q,string[cnt],selector);
q.data[selector][1]=cnt;
} printf("\t ");
for(cnt2=0;cnt2<q.end;cnt2++) {
printf("%c ",q.data[cnt2][0]);
}
printf("\t\tY");
}
else {
printf("\t ");
for(cnt2=0;cnt2<q.end;cnt2++) {
printf("%c ",q.data[cnt2][0]);
}
printf("\t\tN");
}
}
printf("\n\n==============================================\n");
printf("\nTotal no. of Page Faults: %d\n\n",faults);

void lru(char string[],int frameSize,int count) {


int cnt,cnt2,selector,flag,min,faults=0;
queue q;
q.end=0;
printf("\nData Requested\tFrame contents\t
Page Fault\ n==============================================");
for(cnt=0;cnt<count;cnt+=2) {
printf("\n\n\t%c",string[cnt]); flag=0;
for(cnt2=0;cnt2<q.end;cnt2++) {
if(string[cnt]==q.data[cnt2][0]) {
q.data[cnt2][1]=(cnt/2)+1;
flag=1;
break; }

} if(flag==0) {
faults++; if(q.end<frameSize) {
enqueue(&q,string[cnt],q.end);
q.data[q.end][1]=(cnt/2)+1;
q.end++;
} else {

min=99;

for(cnt2=0;cnt2<q.end;cnt2++) {
if(q.data[cnt2][1]<min) {
min=q.data[cnt2][1]; selector=cnt2;
}
}
dequeue(&q,selector);
enqueue(&q,string[cnt],selector);
q.data[selector][1]=(cnt/2)+1;
}
printf("\t ");
for(cnt2=0;cnt2<q.end;cnt2++) {
printf("%c ",q.data[cnt2][0]);
}
printf("\t\tY");

else {
printf("\t ");
for(cnt2=0;cnt2<q.end;cnt2++) { printf("%c
",q.data[cnt2][0]);
}
printf("\t\tN");
}
}
printf("\n\n==============================================\n");
printf("\nTotal no. of Page Faults: %d\n\n",faults);

}
Name: Atharva Tatkare
Class: TE Div: B

Roll no:307B006

Experiment 6 : Implement the C program for Page Replacement Algorithms: FCFS, LRU, and
Optimal for frame size as minimum three.

Output -

You might also like