Os Lab Manual1
Os Lab Manual1
SYSTEM
OPERATING SYSTEM
LABORATORY
MANUAL BCS303
For
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.
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());
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");
}
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);
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 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);
DEPARTMENT OF Page 8
OPERATING
SYSTEM
min = 1000;
ct[i] = ct[i - 1] + bt[i];
}
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];
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;
}
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;
};
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>
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
DEPARTMENT OF Page
OPERATING
SYSTEM
Enter the value: 8
Buffer: 4 <5> 6 7 8 {0} 0 0 0 0
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];
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;
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]);
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
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;
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");
}
}
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");
}
}
int main() {
int numBlocks, i, choice, requestSize;
printf("Enter the number of memory blocks: ");
scanf("%d", &numBlocks);
Block blocks[numBlocks];
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>
int main() {
int choice, n;
int pages[100]; // Assume a maximum of 100 pages for simplicity
int capacity = MAX_FRAMES;
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;
if (!pageFound) {
if (rear < capacity - 1) {
rear++;
} else {
rear = 0;
}
frame[rear] = pages[i];
pageFaults++;
}
}
return pageFaults;
}
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
DEPARTMENT OF Page
OPERATING
SYSTEM
Program 8:
Simulate following File Organization Techniques: a) Single level directory b) Two 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);
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:
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;
};
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);
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);
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:
DEPARTMENT OF Page
OPERATING
SYSTEM
File created.
DEPARTMENT OF Page
OPERATING
SYSTEM
Directory Files
d1 f1
d2 f1
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;
};
if (file->head == NULL)
{ file->head =
newBlock;
} else {
struct Block* current = file->head;
while (current->next != NULL) {
current = current->next;
}
current->next = newBlock;
}
}
int main() {
struct File files[50];
DEPARTMENT OF Page
OPERATING
SYSTEM
int p;
char data[50];
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
DEPARTMENT OF Page
OPERATING
SYSTEM
Program 10:
Develop a C program to simulate SCAN disk scheduling algorithm.
#include<stdio.h>
#include<stdlib.h>
DEPARTMENT OF Page
OPERATING
SYSTEM
printf("Disk head moves from %d to %d with seek %d\n", head, queue[i], diff);
head = queue[i];
}
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
DEPARTMENT OF Page