0% found this document useful (0 votes)
81 views49 pages

Os Lab Manual1

The document provides details about operating system laboratory experiments for an undergraduate computer science course. It includes program code examples for implementing process scheduling algorithms like FCFS, SJF, and round robin as well as examples for processes, memory management, file systems, and more.

Uploaded by

akshatha s.a
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)
81 views49 pages

Os Lab Manual1

The document provides details about operating system laboratory experiments for an undergraduate computer science course. It includes program code examples for implementing process scheduling algorithms like FCFS, SJF, and round robin as well as examples for processes, memory management, file systems, and more.

Uploaded by

akshatha s.a
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/ 49

OPERATING

SYSTEM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

OPERATING SYSTEM
LABORATORY
MANUAL BCS303

For

B.E IN COMPUTER SCIENCE AND


ENGINEERING (CSE)

DEPARTMENT OF Page 1
OPERATING
SYSTEM
PRACTICAL COMPONENT OF IPCC:

Sl.NO Experiments

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

2 Simulate the following CPU scheduling algorithms to find turnaround time and waiting
time a) FCFS
b) SJF c) Round Robin d) Priority.
3 Develop a C program to simulate producer-consumer problem using semaphores.

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.
5
Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance.
6 Develop a C program to simulate the following contiguous memory allocation Techniques:
a) Worst fit b) Best fit c) First fit.
7 Develop a C program to simulate page replacement algorithms:
a) FIFO b) LRU
8 Simulate following File Organization Techniques
a) Single level directory b) Two level directory
9 Develop a C program to simulate the Linked file allocation strategies.

10 Develop a C program to simulate SCAN disk scheduling algorithm.

Course outcomes (Course Skill Set):


At the end of the course, the student will be able to:
CO 1. Explain the structure and functionality of operating system
CO 2. Apply appropriate CPU scheduling algorithms for the given problem.
CO 3. Analyse the various techniques for process synchronization and
deadlock handling.CO 4. Apply the various techniques for memory
management
CO 5. Explain file and secondary storage management
strategies.CO 6. Describe the need for information
protection mechanisms

DEPARTMENT OF Page 2
OPERATING
SYSTEM
Program 1:
Develop a c program to implement the Process system calls (fork (), exec(), wait(), create
process,terminate process).

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

int main() {
pid_t child_pid;
int status;
// Create a child process using fork()
child_pid = fork();

if (child_pid == -1) {
perror("Fork failed");
exit(EXIT_FAILURE);
}

if (child_pid == 0) {
// This code will be executed by the child process
printf("Child process: PID = %d\n", getpid());

// Execute a new program using exec()


char *args[] = {"/bin/ls", "-l", NULL};
if (execvp("/bin/ls", args) == -1) {
perror("Exec failed");
exit(EXIT_FAILURE)
;
}
} else {
// This code will be executed by the parent process

// Wait for the child process to finish using wait()


wait(&status);

if (WIFEXITED(status)) {
printf("Parent process: Child process terminated with exit status %d\n",
WEXITSTATUS(status));
} else {
printf("Parent process: Child process did not terminate normally\n");
}

printf("Parent process: PID = %d\n", getpid());


}

return 0;
}

DEPARTMENT OF Page 3
OPERATING
SYSTEM
OUTPUT:
Child process: PID = 6739
total 20
-rwxr-xr-x 1 runner12 runner12 16304 Feb 18 01:51 a.out
-rwxrwxrwx 1 root root 1183 Feb 18 01:51 main.c
Parent process: Child process terminated with exit status 0
Parent process: PID = 6735

DEPARTMENT OF Page 4
OPERATING
SYSTEM
Program 2:
Simulate the following CPU scheduling algorithms to find turnaround time and waiting
time a) FCFS b) SJF c) Round Robin d) Priority.

a) FCFS

#include <stdio.h>
int main() {
int i, n, sum = 0, wt, tat, twt = 0, ttat = 0;
int t[10], arrival[10];
float awt, atat;
// Input the number of processes
printf("Enter number of processors:\n");
scanf("%d", &n);

// Input burst times and arrival times for each process


for (i = 0; i < n; i++) {
printf("\nEnter the Arrival Time of process %d: ", i + 1);
scanf("%d", &arrival[i]);
printf("Enter the Burst Time of process %d: ", i + 1);
scanf("%d", &t[i]);
}

// Display table headers


printf("\n\nFIRST COME FIRST SERVE SCHEDULING ALGORITHM\n");
printf("\nProcess ID\tArrival Time\tBurst Time\tWaiting Time\tTurn Around Time\n");

// Display waiting time and turnaround time for each


process for (i = 0; i < n; i++) {
wt = sum - arrival[i];
if (wt < 0) {
wt = 0;
}

DEPARTMENT OF Page 5
OPERATING
SYSTEM
tat = wt + t[i];
sum += t[i];
twt += wt;
ttat += tat;
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, arrival[i], t[i], wt, tat);
}

// Calculate and display average waiting time and average turnaround time
awt = (float)twt / n;
atat = (float)ttat / n;
printf("\nAverage Waiting Time: %4.2f", awt);
printf("\nAverage Turnaround Time: %4.2f\n", atat);
return 0;
}

OUTPUT:

DEPARTMENT OF Page 6
OPERATING
SYSTEM
b) SJF

#include<stdio.h>
#define MAX_PROCESS 20

void swap(int *x, int *y) {


int temp = *x;
*x = *y;
*y = temp;
}

void sortat(int p[], int at[], int bt[], int n) {


int i, j;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (at[i] > at[j] || (at[i] == at[j] && bt[i] > bt[j])) {
swap(&p[i], &p[j]);
swap(&at[i], &at[j]);
swap(&bt[i], &bt[j]);
}
}
}
}

void tatwt(int ct[], int at[], int bt[], int tat[], int wt[], int n) {
int i;
for (i = 0; i < n; i++) {
tat[i] = ct[i] - at[i];
wt[i] = tat[i] - bt[i];
}
}
int main() {

DEPARTMENT OF Page 7
OPERATING
SYSTEM
int p[MAX_PROCESS], at[MAX_PROCESS], bt[MAX_PROCESS],
ct[MAX_PROCESS], wt[MAX_PROCESS], tat[MAX_PROCESS];
int pos, i, j, min = 1000, n;
float awt = 0, atat = 0;
printf("Enter total number of processes (maximum %d): ", MAX_PROCESS);
scanf("%d", &n);

if (n <= 0 || n > MAX_PROCESS) {


printf("Invalid number of processes. Exiting...\n");
return 1;
}
printf("\nEnter Process Burst Time and Arrival Time:\n");
for (i = 0; i < n; i++) {
printf("P[%d] - Burst Time: ", i + 1);
scanf("%d", &bt[i]);
printf("P[%d] - Arrival Time: ", i + 1);
scanf("%d", &at[i]);
p[i] = i + 1; // Assigning process IDs
}

sortat(p, at, bt, n);


ct[0] = at[0] + bt[0];
for (i = 1; i < n; i++) {
for (j = i; j < n; j++) {
if (at[j] <= ct[i - 1] && bt[j] < min) {
min = bt[j];
pos = j;
}
}
swap(&p[i], &p[pos]);
swap(&at[i], &at[pos]);
swap(&bt[i], &bt[pos]);

DEPARTMENT OF Page 8
OPERATING
SYSTEM
min = 1000;
ct[i] = ct[i - 1] + bt[i];
}

tatwt(ct, at, bt, tat, wt, n);


printf("\nProcess Burst Time Arrival Time Waiting Time Turnaround Time\n");
for (i = 0; i < n; i++) {
printf("P[%d]\t %d\t\t%d\t\t%d\t\t%d\n", p[i], bt[i], at[i], wt[i], tat[i]);
awt += wt[i];
atat += tat[i];
}
printf("\nAverage Waiting Time: %.2f", awt / n);
printf("\nAverage Turnaround Time: %.2f\n", atat / n);
return 0;
}

OUTPUT:

DEPARTMENT OF Page 9
OPERATING
SYSTEM
c) Round robin

#include<stdio.h>
#include<limits.h>
#include<stdbool.h>

struct P{
int AT,BT,ST[20],WT,FT,TAT,pos;
};

int quant;
int main(){
int n,i,j;
// Taking Input
printf("Enter the no. of processes :");
scanf("%d",&n);
struct P p[n];
printf("Enter the quantum \n");
scanf("%d",&quant);
printf("Enter the process numbers \n");
for(i=0;i<n;i++)
scanf("%d",&(p[i].pos));
printf("Enter the Arrival time of processes \n");
for(i=0;i<n;i++)
scanf("%d",&(p[i].AT));
printf("Enter the Burst time of processes \n");
for(i=0;i<n;i++)
scanf("%d",&(p[i].BT));
// Declaring variables
int c=n,s[n][20];
float time=0,mini=INT_MAX,b[n],a[n];

// Initializing burst and arrival time arrays

DEPARTMENT OF Page
OPERATING
SYSTEM
int index=-1;
for(i=0;i<n;i++){
b[i]=p[i].BT;
a[i]=p[i].AT;
for(j=0;j<20;j++){
s[i][j]=-1;
}
}

int tot_wt,tot_tat;
tot_wt=0;
tot_tat=0;
bool flag=false;
while(c!=0){
mini=INT_MAX;
flag=false;

for(i=0;i<n;i++){
float p=time+0.1;
if(a[i]<=p && mini>a[i] && b[i]>0){
index=i;
mini=a[i];
flag=true;
}
}
// if at =1 then loop gets out hence set flag to false
if(!flag){
time++;
continue;
}
//calculating start time
j=0;

DEPARTMENT OF Page
OPERATING
SYSTEM
while(s[index][j]!=-1){
j++;
}

if(s[index][j]==-1){
s[index][j]=time;
p[index].ST[j]=time;
}

if(b[index]<=quant){
time+=b[index];
b[index]=0;
}
else{ time+=qua
nt; b[index]-
=quant;
}

if(b[index]>0){
a[index]=time+0.1;
}

// calculating arrival,burst,final times


if(b[index]==0){
c--;
p[index].FT=time;
p[index].WT=p[index].FT-p[index].AT-p[index].BT;
tot_wt+=p[index].WT;
p[index].TAT=p[index].BT+p[index].WT;
tot_tat+=p[index].TAT;
}
} // end of while loop

DEPARTMENT OF Page
OPERATING
SYSTEM
// Printing output
printf("Process number ");
printf("Arrival time ");
printf("Burst time ");
printf("\tStart time");
j=0;

while(j!=10){
j+=1;
printf(" ");
}
printf("\t\tFinal time"); printf("\
tWait Time "); printf("\
tTurnAround Time \n");

for(i=0;i<n;i++){
printf("%d \t\t",p[i].pos);
printf("%d \t\t",p[i].AT);
printf("%d \t",p[i].BT);
j=0;
int v=0; while(s[i]
[j]!=-1){
printf("%d ",p[i].ST[j]);
j++;
v+=3;
}
while(v!=40){
printf(" ");
v+=1;
}
printf("%d \t\t",p[i].FT);
printf("%d \t\t",p[i].WT);

DEPARTMENT OF Page
OPERATING
SYSTEM
printf("%d \n",p[i].TAT);
}
//Calculating average wait time and turnaround time
double avg_wt,avg_tat;
avg_wt=tot_wt/(float)n;
avg_tat=tot_tat/(float)n;
//Printing average wait time and turnaround time
printf("The average wait time is : %lf\n",avg_wt);
printf("The average TurnAround time is : %lf\n",avg_tat);
return 0;
}

OUTPUT:
Enter the no. of processes :5
Enter the quantum
2
Enter the process numbers
12345
Enter the Arrival time of processes
01234
Enter the Burst time of processes
53123
Process number Arrival time Burst time Start time Final time Wait Time TurnAround Time
1 0 5 0 5 12 13 8 13
2 1 3 2 11 12 8 11
3 2 1 4 5 2 3
4 3 2 7 9 4 6
5 4 3 9 13 14 7 10
The average wait time is : 5.800000
The average TurnAround time is : 8.600000

DEPARTMENT OF Page
OPERATING
SYSTEM
d) Priority

#include <stdio.h>
struct process {
int WT, AT, BT, TAT, PT;
};

struct process a[10];

int main() {
int n, temp[10], t, count = 0, short_p, i;
float total_WT = 0, total_TAT = 0, Avg_WT, Avg_TAT;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Input the arrival time, burst time, and priority of each process (one per line):\n");
for (i = 0; i < n; i++) {
printf("Process %d:\n", i + 1);
scanf("%d%d%d", &a[i].AT, &a[i].BT, &a[i].PT);
temp[i] = a[i].BT; // Store the initial burst time
}
for (t = 0; count != n; t++) {
short_p = -1;
for (i = 0; i < n; i++) {
if (a[i].AT <= t && a[i].BT > 0) {
if (short_p == -1 || a[short_p].PT > a[i].PT) {
short_p = i;
}
}
}

if (short_p != -1) {
a[short_p].BT = a[short_p].BT - 1;

if (a[short_p].BT == 0) {

DEPARTMENT OF Page
OPERATING
SYSTEM
count++;
a[short_p].WT = t + 1 - a[short_p].AT - temp[short_p];
a[short_p].TAT = t + 1 - a[short_p].AT;
total_WT += a[short_p].WT;
total_TAT += a[short_p].TAT;
}
}
}
Avg_WT = total_WT / n;
Avg_TAT = total_TAT / n;
// Printing the answer printf("ID\
tAT\tBT\tWT\tTAT\n"); for (i = 0; i
< n; i++) {
printf("%d\t%d\t%d\t%d\t%d\n", i + 1, a[i].AT, temp[i], a[i].WT, a[i].TAT);
}
printf("Avg waiting time is %f\n", Avg_WT);
printf("Avg turn around time is %f\n", Avg_TAT);
return 0;
}

OUTPUT:
Enter the number of processes: 5
Input the arrival time, burst time, and priority of each process (one per line):
Process 1:
0 11 2
Process 2:
5 28 0
Process 3:
12 2 3
Process 4:
2 10 1
Process 5:

DEPARTMENT OF Page
OPERATING
SYSTEM
9 16 4
ID AT BT WT TAT
1 0 11 38 49
2 5 28 0 28
3 12 2 37 39
4 2 10 28 38
5 9 16 42 58
Avg waiting time is 29.000000
Avg turn around time is 42.400002

DEPARTMENT OF Page
OPERATING
SYSTEM
Program 3:
Develop a C program to simulate producer-consumer problem using semaphores.

#include <stdio.h>

void displayBuffer(int buffer[], int in, int out, int bufsize) {


printf("Buffer: ");
for (int i = 0; i < bufsize; i++) {
if (i == out && i == in) {
printf("[%d] ", buffer[i]); // Highlight both in and out indices
} else if (i == out) {
printf("<%d> ", buffer[i]); // Highlight out index
} else if (i == in) {
printf("{%d} ", buffer[i]); // Highlight in index
} else {
printf("%d ", buffer[i]);
}
}
printf("\n");
}

int main(void) {
int buffer[10], bufsize, in, out, produce, consume, choice = 0;
in = 0;
out = 0;
bufsize = 10;

while (choice != 3) {
printf("\n1. Produce \t 2. Consume \t 3. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:

DEPARTMENT OF Page
OPERATING
SYSTEM
if ((in + 1) % bufsize == out)
printf("\nBuffer is Full");
else {
printf("\nEnter the value: ");
scanf("%d", &produce);
buffer[in] = produce;
in = (in + 1) % bufsize;
}
displayBuffer(buffer, in, out, bufsize);
break;

case 2:
if (in == out)
printf("\nBuffer is Empty");
else {
consume = buffer[out];
printf("\nThe consumed value is %d\n", consume);
out = (out + 1) % bufsize;
}
displayBuffer(buffer, in, out, bufsize);
break;

case 3:
break;

default:
printf("\nInvalid choice. Please enter 1, 2, or 3.");
}
}
return 0;
}

DEPARTMENT OF Page
OPERATING
SYSTEM
OUTPUT:
1. Produce 2. Consume 3. Exit
Enter your choice: 2
Buffer is Empty Buffer: [0] 0 0 0 0 0 0 0 0 0

1. Produce 2. Consume 3. Exit


Enter your choice: 1
Enter the value: 4
Buffer: <4> {0} 0 0 0 0 0 0 0 0

1. Produce 2. Consume 3. Exit


Enter your choice: 1
Enter the value: 5
Buffer: <4> 5 {0} 0 0 0 0 0 0 0

1. Produce 2. Consume 3. Exit


Enter your choice: 2
The consumed value is 4
Buffer: 4 <5> {0} 0 0 0 0 0 0 0

1. Produce 2. Consume 3. Exit


Enter your choice: 1
Enter the value: 6
Buffer: 4 <5> 6 {0} 0 0 0 0 0 0

1. Produce 2. Consume 3. Exit


Enter your choice: 1
Enter the value: 7
Buffer: 4 <5> 6 7 {0} 0 0 0 0 0

1. Produce 2. Consume 3. Exit


Enter your choice: 1

DEPARTMENT OF Page
OPERATING
SYSTEM
Enter the value: 8
Buffer: 4 <5> 6 7 8 {0} 0 0 0 0

1. Produce 2. Consume 3. Exit


Enter your choice: 1
Enter the value: 9
Buffer: 4 <5> 6 7 8 9 {0} 0 0 0

1. Produce 2. Consume 3. Exit


Enter your choice: 1
Enter the value: 10
Buffer: 4 <5> 6 7 8 9 10 {0} 0 0

1. Produce 2. Consume 3. Exit


Enter your choice: 1
Enter the value: 12
Buffer: 4 <5> 6 7 8 9 10 12 {0} 0

1. Produce 2. Consume 3. Exit


Enter your choice: 1
Enter the value: 3
Buffer: 4 <5> 6 7 8 9 10 12 3 {0}

1. Produce 2. Consume 3. Exit


Enter your choice: 1
Enter the value: 14
Buffer: {4} <5> 6 7 8 9 10 12 3 14

1. Produce 2. Consume 3. Exit


Enter your choice: 1
Buffer is Full Buffer: {4} <5> 6 7 8 9 10 12 3 14

DEPARTMENT OF Page
OPERATING
SYSTEM
1. Produce 2. Consume 3. Exit
Enter your choice: 3

DEPARTMENT OF Page
OPERATING
SYSTEM
Program 4:
Write 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.

/*Writer Process*/

#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
int fd;
char buf[1024];

/* create the FIFO (named pipe) */


char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
printf("Run Reader process to read the FIFO File\n");
fd = open(myfifo, O_WRONLY);
write(fd,"Hi", sizeof("Hi"));

/* write "Hi" to the FIFO */


close(fd);
unlink(myfifo); /* remove the FIFO */
return 0;
}

DEPARTMENT OF Page
OPERATING
SYSTEM

/* Reader Process */
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#define MAX_BUF 1024
int main()
{
int fd;

/* A temp FIFO file is not created in reader */


char *myfifo = "/tmp/myfifo";
char buf[MAX_BUF];

/* open, read, and display the message from the FIFO


*/ fd = open(myfifo, O_RDONLY);
read(fd, buf, MAX_BUF);
printf("Writer: %s\n", buf);
close(fd);
return 0;
}

OUTPUT:
Hi

DEPARTMENT OF Page
OPERATING
SYSTEM
Program 5:
Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance.

#include<stdio.h>

int main() {
/* array will store at most 5 process with 3 resoures if your process or
resources is greater than 5 and 3 then increase the size of array */
int p, c, count = 0, i, j, alc[5][3], max[5][3], need[5][3], safe[5], available[3], done[5],
terminate = 0;
printf("Enter the number of process and resources");
scanf("%d %d", & p, & c);
// p is process and c is diffrent resources
printf("enter allocation of resource of all process %dx%d matrix", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", & alc[i][j]);
}
}
printf("enter the max resource process required %dx%d matrix", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", & max[i][j]);
}
}
printf("enter the available resource");
for (i = 0; i < c; i++)
scanf("%d", & available[i]);

printf("\n need resources matrix are\n");


for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
need[i][j] = max[i][j] - alc[i][j];
printf("%d\t", need[i][j]);

DEPARTMENT OF Page
OPERATING
SYSTEM
}
printf("\n");
}
/* once process execute variable done will stop them for again execution */
for (i = 0; i < p; i++) {
done[i] = 0;
}
while (count < p) {
for (i = 0; i < p; i++) {
if (done[i] == 0) {
for (j = 0; j < c; j++) {
if (need[i][j] > available[j])
break;
}
//when need matrix is not greater then available matrix then if j==c will true
if (j == c) {
safe[count] = i;
done[i] = 1;
/* now process get execute release the resources and add them in available resources */
for (j = 0; j < c; j++) {
available[j] += alc[i][j];
}
count++;
terminate = 0;
} else {
terminate++;
}
}
}
if (terminate == (p - 1)) {
printf("safe sequence does not exist");
break;

DEPARTMENT OF Page
OPERATING
SYSTEM
}
}

if (terminate != (p - 1)) {
printf("\n available resource after completion\n");
for (i = 0; i < c; i++) {
printf("%d\t", available[i]);
}
printf("\n safe sequence are\n");
for (i = 0; i < p; i++) { printf("p
%d\t", safe[i]);
}
}
return 0;
}

OUTPUT:
Enter the number of process and resources: 5 3
enter allocation of resource of all process 5x3 matrix:0 1 0
200
302
211
002
enter the max resource process required 5x3 matrix:7 5 3
322
902
222
433
enter the available resource:3 3 2

need resources matrix are:


7 4 3

DEPARTMENT OF Page
OPERATING
SYSTEM
1 2 2
6 0 0
0 1 1
4 3 1
available resource after completion
10 5 7
safe sequence are
p1 p3 p4 p0 p2

DEPARTMENT OF Page
OPERATING
SYSTEM
Program 6:
Develop a C program to simulate the following contiguous memory allocation
Techniques: a) Worst fit b) Best fit c) First fit.

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

typedef struct {
int id;
int size;
int allocated;
} Block;

void worstFit(Block blocks[], int numBlocks, int requestSize) {


int i, worstFitIdx = -1;
for (i = 0; i < numBlocks; i++) {
if (blocks[i].allocated == 0 && blocks[i].size >= requestSize) {
if (worstFitIdx == -1 || blocks[i].size > blocks[worstFitIdx].size) {
worstFitIdx = i;
}
}
}

if (worstFitIdx != -1) {
blocks[worstFitIdx].allocated = 1;
printf("Memory allocated using Worst Fit at Block %d\n", worstFitIdx);
} else {
printf("No suitable block found for Worst Fit allocation\n");
}
}

void bestFit(Block blocks[], int numBlocks, int requestSize) {


int i, bestFitIdx = -1;
for (i = 0; i < numBlocks; i++) {
if (blocks[i].allocated == 0 && blocks[i].size >= requestSize) {
if (bestFitIdx == -1 || blocks[i].size < blocks[bestFitIdx].size) {
bestFitIdx = i;
}
}
}

if (bestFitIdx != -1) {
blocks[bestFitIdx].allocated = 1;

DEPARTMENT OF Page
OPERATING
SYSTEM
printf("Memory allocated using Best Fit at Block %d\n", bestFitIdx);
} else {
printf("No suitable block found for Best Fit allocation\n");
}
}

void firstFit(Block blocks[], int numBlocks, int requestSize) {


int i;
for (i = 0; i < numBlocks; i++) {
if (blocks[i].allocated == 0 && blocks[i].size >= requestSize) {
blocks[i].allocated = 1;
printf("Memory allocated using First Fit at Block %d\n", i);
return;
}
}
printf("No suitable block found for First Fit allocation\n");
}

int main() {
int numBlocks, i, choice, requestSize;
printf("Enter the number of memory blocks: ");
scanf("%d", &numBlocks);

Block blocks[numBlocks];

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


blocks[i].id = i;
blocks[i].allocated = 0;
printf("Enter the size of Block %d: ", i);
scanf("%d", &blocks[i].size);
}

while (1) { printf("\


nMenu:\n");
printf("1. Worst Fit Allocation\n");
printf("2. Best Fit Allocation\n");
printf("3. First Fit Allocation\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the size of the memory request: ");

DEPARTMENT OF Page
OPERATING
SYSTEM
scanf("%d", &requestSize);
worstFit(blocks, numBlocks, requestSize);
break;
case 2:
printf("Enter the size of the memory request: ");
scanf("%d", &requestSize);
bestFit(blocks, numBlocks, requestSize);
break;
case 3:
printf("Enter the size of the memory request: ");
scanf("%d", &requestSize);
firstFit(blocks, numBlocks, requestSize);
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}

OUTPUT:
Enter the number of memory blocks:
4 Enter the size of Block 0: 90
Enter the size of Block 1: 20
Enter the size of Block 2: 50
Enter the size of Block 3: 200

Menu:
1. Worst Fit Allocation
2. Best Fit Allocation
3. First Fit Allocation
4. Exit
Enter your choice: 1
Enter the size of the memory request: 20
Memory allocated using Worst Fit at Block 3

Menu:
1. Worst Fit Allocation
2. Best Fit Allocation
3. First Fit Allocation
4. Exit
Enter your choice: 2

DEPARTMENT OF Page
OPERATING
SYSTEM
Enter the size of the memory request: 20
Memory allocated using Best Fit at Block 1

Menu:
1. Worst Fit Allocation
2. Best Fit Allocation
3. First Fit Allocation
4. Exit
Enter your choice: 3
Enter the size of the memory request: 20
Memory allocated using First Fit at Block 0

Menu:
1. Worst Fit Allocation
2. Best Fit Allocation
3. First Fit Allocation
4. Exit
Enter your choice: 1
Enter the size of the memory request: 60
No suitable block found for Worst Fit allocation

Menu:
1. Worst Fit Allocation
2. Best Fit Allocation
3. First Fit Allocation
4. Exit
Enter your choice: 6
Invalid choice. Please try again.

Menu:
1. Worst Fit Allocation
2. Best Fit Allocation
3. First Fit Allocation
4. Exit
Enter your choice: 4

DEPARTMENT OF Page
OPERATING
SYSTEM
Program 7:
Develop a C program to simulate page replacement algorithms: a) FIFO b) LRU

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

#define MAX_FRAMES 3 // Number of frames

int fifo(int pages[], int n, int capacity);


int lru(int pages[], int n, int capacity);

int main() {
int choice, n;
int pages[100]; // Assume a maximum of 100 pages for simplicity
int capacity = MAX_FRAMES;

printf("Enter the number of pages: ");


scanf("%d", &n);

printf("Enter the sequence of pages (separated by space): ");


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

while (1) {
printf("\nPage Replacement Algorithms Menu:\n");
printf("1. FIFO (First-In-First-Out)\n");
printf("2. LRU (Least Recently Used)\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Page Faults using FIFO: %d\n", fifo(pages, n, capacity));
break;
case 2:
printf("Page Faults using LRU: %d\n", lru(pages, n, capacity));
break;
case 3:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}
int fifo(int pages[], int n, int capacity) {

DEPARTMENT OF Page
OPERATING
SYSTEM
int frame[capacity];
int pageFaults = 0;
int rear = -1;

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


int pageFound = 0;
for (int j = 0; j < capacity; j++) {
if (frame[j] == pages[i]) {
pageFound = 1;
break;
}
}

if (!pageFound) {
if (rear < capacity - 1) {
rear++;
} else {
rear = 0;
}
frame[rear] = pages[i];
pageFaults++;
}
}

return pageFaults;
}

int lru(int pages[], int n, int capacity) {


int frame[capacity];
int pageFaults = 0;
int
counter[capacity];
for (int i = 0; i < capacity; i++) {
frame[i] = -1;
counter[i] = 0;
}

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


int pageFound = 0;
for (int j = 0; j < capacity; j++) {
if (frame[j] == pages[i]) {
pageFound = 1;
counter[j] = i;
break;
}
}

if (!pageFound) {
int lruPage = 0;
for (int j = 1; j < capacity; j++) {
if (counter[j] < counter[lruPage]) {
DEPARTMENT OF Page
OPERATING
SYSTEM
lruPage = j;
}
}
frame[lruPage] = pages[i];
counter[lruPage] = i;
pageFaults++;
}
}

return pageFaults;
}

OUTPUT:
Enter the number of pages: 20
Enter the sequence of pages (separated by space): 1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6
Page Replacement Algorithms Menu:
1. FIFO (First-In-First-Out)
2. LRU (Least Recently Used)
3. Exit
Enter your choice: 1
Page Faults using FIFO: 16

Page Replacement Algorithms Menu:


1. FIFO (First-In-First-Out)
2. LRU (Least Recently Used)
3. Exit
Enter your choice: 2
Page Faults using LRU: 15

Page Replacement Algorithms Menu:


1. FIFO (First-In-First-Out)
2. LRU (Least Recently Used)
3. Exit
Enter your choice: 3

DEPARTMENT OF Page
OPERATING
SYSTEM
Program 8:
Simulate following File Organization Techniques: a) Single level directory b) Two level
directory

a) Single level directory

#include <stdio.h>
#include <string.h>
#define MAX_FILES 10
#define MAX_FILENAME_LENGTH 20
#define MAX_DIRNAME_LENGTH 20

int main() {
int nf = 0, i = 0, j = 0, ch;
char mdname[MAX_DIRNAME_LENGTH],
fname[MAX_FILES][MAX_FILENAME_LENGTH],
name[MAX_FILENAME_LENGTH];
printf("Enter the directory name:");
scanf("%s", mdname);
printf("Enter the number of files:");
scanf("%d", &nf);

do {
if (j >= nf) {
printf("Maximum number of files reached.\n");
break;
}
printf("Enter file name to be created:");
scanf("%s", name);

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


if (!strcmp(name, fname[i]))
break;
}

DEPARTMENT OF Page
OPERATING
SYSTEM
if (i == j) { strcpy(fname[j+
+], name);
} else {
printf("There is already %s\n", name);
}
printf("Do you want to enter another file (yes - 1 or no - 0):");
scanf("%d", &ch);
} while (ch == 1);
printf("Directory name is: %s\n", mdname);
printf("Files names are:");
for (i = 0; i < j; i++)
printf("\n%s", fname[i]);
return 0;
}

OUTPUT:

Enter the directory name:D1


Enter the number of files:3
Enter file name to be created:file1
Do you want to enter another file (yes - 1 or no - 0):1
Enter file name to be created:file2
Do you want to enter another file (yes - 1 or no - 0):1
Enter file name to be created:file3
Do you want to enter another file (yes - 1 or no - 0):1
Maximum number of files reached.
Directory name is: D1
Files names are:
file1
file2
file3

DEPARTMENT OF Page
OPERATING
SYSTEM
b) Two level directory

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DIRS 10
#define MAX_FILES_PER_DIR 10
#define MAX_NAME_LENGTH 30

struct Directory {
char dname[MAX_NAME_LENGTH];
char fname[MAX_FILES_PER_DIR][MAX_NAME_LENGTH];
int fcnt;
};

struct Directory dir[MAX_DIRS];


int dcnt = 0;

void createDirectory() {
if (dcnt >= MAX_DIRS) {
printf("Maximum number of directories reached.\n");
return;
}
printf("Enter name of the directory: ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt = 0;
dcnt++;
printf("Directory created.\n");
}

void createFile() {
char d[MAX_NAME_LENGTH];

DEPARTMENT OF Page
OPERATING
SYSTEM
int i;
printf("Enter name of the directory: ");
scanf("%s", d);
for (i = 0; i < dcnt; i++) {
if (strcmp(d, dir[i].dname) == 0) {
if (dir[i].fcnt >= MAX_FILES_PER_DIR) {
printf("Maximum number of files reached for this directory.\n");
return;
}
printf("Enter name of the file: ");
scanf("%s", dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created.\n");
return;
}
}
printf("Directory %s not found.\n", d);
}

void deleteFile() {
char d[MAX_NAME_LENGTH], f[MAX_NAME_LENGTH];
int i, k;
printf("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", f);

for (k = 0; k < dir[i].fcnt; k++) {


if (strcmp(f, dir[i].fname[k]) == 0) {

DEPARTMENT OF Page
OPERATING
SYSTEM
printf("File %s is deleted.\n", f);
dir[i].fcnt--;
strcpy(dir[i].fname[k], dir[i].fname[dir[i].fcnt]);
return;
}
}
printf("File %s not found.\n", f);
return;
}
}
printf("Directory %s not found.\n", d);
}

void searchFile() {
char d[MAX_NAME_LENGTH], f[MAX_NAME_LENGTH];
int i, k;
printf("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", f);

for (k = 0; k < dir[i].fcnt; k++) {


if (strcmp(f, dir[i].fname[k]) == 0) {
printf("File %s is found.\n", f);
return;
}
}
printf("File %s not found.\n", f);
return;

DEPARTMENT OF Page
OPERATING
SYSTEM
}
}
printf("Directory %s not found.\n", d);
}

void displayDirectories() {
if (dcnt == 0) {
printf("No directories.\n");
} else {
printf("Directory\tFiles\n");
for (int i = 0; i < dcnt; i++) {
printf("%s\t\t", dir[i].dname);
for (int k = 0; k < dir[i].fcnt; k++)
printf("%s\t", dir[i].fname[k]);
printf("\n");
}
}
}

int main() {
int choice;
while (1) {
printf("\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\n4. Search File\t\t5. Display\t6. Exit\nEnter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
createDirectory();
break;
case 2:
createFile();

DEPARTMENT OF Page
OPERATING
SYSTEM
break;
case 3:
deleteFile();
break;
case 4:
searchFile();
break;
case 5:
displayDirectories();
break;
case 6:
exit(0);
default:
printf("Invalid choice. Please enter a valid option.\n");
}
}
return 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 the directory: d1
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: d1
Enter name of the file: f1

DEPARTMENT OF Page
OPERATING
SYSTEM
File created.

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


4. Search File 5. Display 6. Exit
Enter your choice: 1
Enter name of the directory: d2
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: d2
Enter name of the file: f1
File created.

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: d1
Enter name of the file: f2
File f2 not found.

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: d1
Enter name of the file: f1
File f1 is found.

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


4. Search File 5. Display 6. Exit
Enter your choice: 5

DEPARTMENT OF Page
OPERATING
SYSTEM
Directory Files
d1 f1
d2 f1

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


4. Search File 5. Display 6. Exit
Enter your choice: 6

DEPARTMENT OF Page
OPERATING
SYSTEM
Program 9:
Develop a C program to simulate the Linked file allocation strategies.

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

struct Block {
char data[50]; // Assuming each block can store 50 characters of data
struct Block* next;
};

struct File {
char filename[20];
struct Block* head;
};

void appendBlock(struct File* file, const char data[]) {


struct Block* newBlock = (struct Block*)malloc(sizeof(struct Block));
if (newBlock == NULL) {
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
snprintf(newBlock->data, sizeof(newBlock->data), "%s", data);
newBlock->next = NULL;

if (file->head == NULL)
{ file->head =
newBlock;
} else {
struct Block* current = file->head;
while (current->next != NULL) {
current = current->next;
}
current->next = newBlock;
}
}

void displayFile(const struct File* file) {


printf("File %s contents: ", file->filename);
struct Block* current = file->head;
while (current != NULL) {
printf("%s -> ", current->data);
current = current->next;
}
printf("NULL\n");
}

int main() {
struct File files[50];

DEPARTMENT OF Page
OPERATING
SYSTEM
int p;
char data[50];

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


files[i].head = NULL;
printf("Enter how many files to allocate: ");

// Check if the input is successful


if (scanf("%d", &p) != 1) {
printf("Error: Please enter a valid integer.\n");
return 1; // Exit with an error code
}

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


printf("Enter the filename for File %d: ", i + 1);
scanf("%s", files[i].filename);
printf("Enter data for blocks in %s (enter 'end' to stop): ", files[i].filename);
scanf("%s", data);
while (strcmp(data, "end") != 0) {
appendBlock(&files[i], data);
scanf("%s", data);
}
}
printf("\nFile allocation details:\n");
for (int i = 0; i < p; i++) {
displayFile(&files[i]);
}
return 0;
}

OUTPUT:
Enter how many files to allocate: 2
Enter the filename for File 1: file1
Enter data for blocks in file1 (enter 'end' to stop): Data Hello World
end
Enter the filename for File 2: file2
Enter data for blocks in file2 (enter 'end' to stop): Computer
end

File allocation details:


File file1 contents: Hello -> World -> NULL
File file2 contents: Computer -> NULL

DEPARTMENT OF Page
OPERATING
SYSTEM
Program 10:
Develop a C program to simulate SCAN disk scheduling algorithm.

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

void scanDisk(int queue[], int head, int n, int max) {


int seek = 0;
int diff;
printf("\nSCAN Disk Scheduling:\n");
// Sorting the queue in ascending order
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (queue[j] > queue[j + 1]) {
int temp = queue[j];
queue[j] = queue[j + 1];
queue[j + 1] = temp;
}
}
}
// Finding the index where the head is located in the sorted queue
int index;
for (index = 0; index < n; index++) {
if (queue[index] >= head) {
break;
}
}
// Moving towards the right
for (int i = index; i < n; i++) {
diff = abs(queue[i] - head);
seek += diff;

DEPARTMENT OF Page
OPERATING
SYSTEM
printf("Disk head moves from %d to %d with seek %d\n", head, queue[i], diff);
head = queue[i];
}

// Moving towards the left


for (int i = index - 1; i >= 0; i--) {
diff = abs(queue[i] - head);
seek += diff;
printf("Disk head moves from %d to %d with seek %d\n", head, queue[i], diff);
head = queue[i];
}
printf("\nTotal seek time is %d\n", seek);
float avg = (float)seek / n;
printf("Average seek time is %f\n", avg);
}

int main() {
int n, head, max;
printf("Enter the max range of disk: ");
scanf("%d", &max);
printf("Enter the initial head position: ");
scanf("%d", &head);
printf("Enter the size of the queue request: ");
scanf("%d", &n);
int *queue = (int *)malloc(n * sizeof(int));

if (queue == NULL) {
printf("Memory allocation failed.\n");
return 1;
}

DEPARTMENT OF Page
OPERATING
SYSTEM
printf("Enter the queue of disk positions to be read:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &queue[i]);
}
scanDisk(queue, head, n, max);
// Free dynamically allocated memory
free(queue);
return 0;
}

OUTPUT:
Enter the max range of disk: 200
Enter the initial head position: 50
Enter the size of the queue request: 5
Enter the queue of disk positions to be read:
120 90 35 10 180

SCAN Disk Scheduling:


Disk head moves from 50 to 90 with seek 40
Disk head moves from 90 to 120 with seek 30
Disk head moves from 120 to 180 with seek 60
Disk head moves from 180 to 35 with seek 145
Disk head moves from 35 to 10 with seek 25

Total seek time is 300


Average seek time is 60.000000

DEPARTMENT OF Page

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