Operating Systems Lab Record
Operating Systems Lab Record
INSTITUTE OF TECHNOLOGY
(AUTONOMOUS)
(Approved by AICTE New Delhi, Permanently Affiliated to JNTUK
Kakinada, Accredited by NAAC & NBA)
NH-5, Chowdavaram, Guntur-522019
KHIT
OPERATING SYSTEMS
LAB RECORD
Name:………………………………………………………………..…….
.
Roll No:……………..…... Year & Semester:…………..………..
Course:…………………. Section:…………………………........
KALLAM HARANADHAREDDY
INSTITUTE OF TECHNOLOGY
(AUTONOMOUS)
(APPROVED BY AICTE NEW DELHI, PERMANENTLY AFFLIATED TO
JNTUK, KAKINADA) CHOWDAVARAM, GUNTUR-19
Roll No:
CERTIFICATE
This is to Certify that the Bonafide Record of the Laboratory Work done by
Mr/Ms…………………………………………………………………………………………..
experiments in ……………………………………………………….………………………..
.
EX. PAGE
NO DATE NAME OF THE EXPERIMENT FROM TO MARKS SIGNATURE
.
EX. PAGE
NO DATE NAME OF THE EXPERIMENT FROM TO MARKS SIGNATURE
.
Exercise:
Date: Roll No:
Exercise 1
a) Implementing FCFS CPU Scheduling Algorithm
Program:
#include<stdio.h>
int main()
{
int p[10],at[10],bt[10],ct[10],tat[10],wt[10],i,j,temp=0,n;
float awt=0,atat=0;
printf("enter no of proccess you want:");
scanf("%d",&n);
printf("enter %d process:",n);
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
printf("enter %d arrival time:",n);
for(i=0;i<n;i++)
{
scanf("%d",&at[i]);
}
printf("enter %d burst time:",n);
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
// sorting at,bt, and process according to at
for(i=0;i<n;i++)
{
for(j=0;j<(n-i);j++)
{
if(at[j]>at[j+1])
{
temp=p[j+1];
p[j+1]=p[j];
p[j]=temp;
temp=at[j+1];
at[j+1]=at[j];
at[j]=temp;
temp=bt[j+1];
bt[j+1]=bt[j];
bt[j]=temp;
}
}
}
/* calculating 1st ct */
ct[0]=at[0]+bt[0];
/* calculating 2 to n ct */
for(i=1;i<n;i++)
{
//when proess is ideal in between i and i+1
temp=0;
if(ct[i-1]<at[i])
{
temp=at[i]-ct[i-1];
}
ct[i]=ct[i-1]+bt[i]+temp;
}
/* calculating tat and wt */
printf("\np\t A.T\t B.T\t C.T\t TAT\t WT");
for(i=0;i<n;i++)
{
tat[i]=ct[i]-at[i];
wt[i]=tat[i]-bt[i];
atat+=tat[i];
awt+=wt[i];
}
atat=atat/n;
awt=awt/n;
for(i=0;i<n;i++)
{
printf("\nP%d\t %d\t %d\t %d \t %d \t %d",p[i],at[i],bt[i],ct[i],tat[i],wt[i]);
}
printf("\naverage turnaround time is %f",atat);
printf("\naverage wating timme is %f",awt);
return 0;
}
Program:
#include<stdio.h>
int main()
{
int p[10], at[10], bt[10], ct[10], tat[10], wt[10],i, j, temp = 0, n;
float awt = 0, atat = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the process IDs: ");
for (i = 0; i < n; i++)
{
scanf("%d", &p[i]);
}
printf("Enter the arrival times: ");
for (i = 0; i < n; i++)
{
scanf("%d", &at[i]);
}
printf("Enter the burst times: ");
for (i = 0; i < n; i++)
{
scanf("%d", &bt[i]);
}
// Sorting based on Burst Time (SJF)
for (i = 0; i < n; i++)
{
for (j = 0; j < (n - i); j++)
{
if (bt[j] > bt[j + 1])
{
temp = p[j + 1];
p[j + 1] = p[j];
p[j] = temp;
temp = at[j + 1];
at[j + 1] = at[j];
at[j] = temp;
temp = bt[j + 1];
bt[j + 1] = bt[j];
bt[j] = temp;
}
}
}
// Calculating Completion Time (CT)
ct[0] = at[0] + bt[0];
Program:
#include<stdio.h>
int main()
{
int p[10], at[10], bt[10], priority[10], ct[10], tat[10], wt[10], i, j, temp = 0, n;
float awt = 0, atat = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the process IDs: ");
for (i = 0; i < n; i++)
{
scanf("%d", &p[i]);
}
printf("Enter the arrival times: ");
for (i = 0; i < n; i++)
{
scanf("%d", &at[i]);
}
printf("Enter the burst times: ");
for (i = 0; i < n; i++)
{
scanf("%d", &bt[i]);
}
printf("Enter the priorities: ");
for (i = 0; i < n; i++)
{
scanf("%d", &priority[i]);
}
// Sorting based on Priority
for (i = 0; i < n; i++)
{
for (j = 0; j < (n - i); j++)
{
if (priority[j] < priority[j + 1])
{
temp = p[j + 1];
p[j + 1] = p[j];
p[j] = temp;
temp = at[j + 1];
at[j + 1] = at[j];
at[j] = temp;
temp = bt[j + 1];
bt[j + 1] = bt[j];
bt[j] = temp;
Program:
#include<stdio.h>
int main()
{
int p[10], at[10], bt[10], ct[10], tat[10], wt[10], remaining[10], quantum, i, time, n;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the process IDs: ");
for (i = 0; i < n; i++)
{
scanf("%d", &p[i]);
}
printf("Enter the arrival times: ");
for (i = 0; i < n; i++)
{
scanf("%d", &at[i]);
}
printf("Enter the process burdt time: ");
for (i = 0; i < n; i++)
{
scanf("%d", &bt[i]);
remaining[i] = bt[i];
}
printf("Enter the time quantum: ");
scanf("%d", &quantum);
time = 0;
while (1)
{
int flag = 0; // Flag to check if there is any remaining process
for (i = 0; i < n; i++)
{
if (remaining[i] > 0)
{
flag = 1; // There is a remaining process
if (remaining[i] > quantum)
{
time += quantum;
remaining[i] -= quantum;
}
else
{
time += remaining[i];
remaining[i] = 0;
ct[i] = time;
}
if (flag == 0)
{
// All processes are completed
break;
}
}
// Calculating Turnaround Time (TAT) and Waiting Time (WT)
printf("\nProcess\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time");
for (i = 0; i < n; i++)
{
tat[i] = ct[i] - at[i];
wt[i] = tat[i] - bt[i];
printf("\n%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d", p[i], at[i], bt[i], ct[i], tat[i], wt[i]);
}
return 0;
}
Exercise - 2
a) Implementation of fork() System call
Program:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void forkexample()
{
if (fork() == 0)
printf("Hello from Child!\n");
else
printf("Hello from Parent!\n");
}
int main()
{
forkexample();
return 0;
}
Program:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
int ret = 1;
int status;
pid = fork();
if (pid == -1)
{
printf("can't fork, error occured\n");
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
printf("child process, pid = %u\n",getpid());
execv("ls",argv_list);
exit(0);
}
else
{
printf("parent process, pid = %u\n",getppid());
if (waitpid(pid, &status, 0) > 0)
{
if (WIFEXITED(status) && !WEXITSTATUS(status))
printf("program execution successfull\n");
else if (WIFEXITED(status) && WEXITSTATUS(status))
{
if (WEXITSTATUS(status) == 127)
{
printf("execv failed\n");
}
else
printf("program terminated normally, but returned a non-zero status\n");
}
else
printf("program didn't terminate normally\n");
}
else
{
KALLAM HARANADHAREDDY INSTITUTE OF TECHNOLOGY, CHOWDAVARAM
GUNTUR, (AUTONOMOUS)
Exercise:
Date: Roll No:
printf("waitpid() failed\n");
}
exit(0);
}
return 0;
}
Program:
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
int main()
{
pid_t cpid;
if (fork()== 0)
exit(0);
else
cpid = wait(NULL);
printf("Parent pid = %d\n", getpid());
printf("Child pid = %d\n", cpid);
return 0;
}
Program:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("START");
exit(0);
printf("End of program");
}
Exercise - 3
a) Multiprogramming with A Fixed Number Of Tasks (MFT)
Aim: To write a C program to simulate the Multiprogramming With a Fixed Number of Tasks.
Program:
#include<stdio.h>
int main()
{
int ms, bs, nob, ef, n, mp[10], tif = 0;
int i, p = 0;
printf("Enter the total memory available (in Bytes) -- ");
scanf("%d", &ms);
printf("Enter the block size (in Bytes) -- ");
scanf("%d", &bs);
nob = ms / bs;
ef = ms - nob * bs;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter memory required for process %d (in Bytes)-- ", i + 1);
scanf("%d", &mp[i]);
}
printf("\nNo. of Blocks available in memory -- %d", nob);
printf("\n\nPROCESS\tMEMORY REQUIRED\t ALLOCATED\tINTERNAL FRAGMENTATION");
for (i = 0; i < n && p < nob; i++)
{
printf("\n %d\t\t%d", i + 1, mp[i]);
if (mp[i] > bs)
printf("\t\tNO\t\t---");
else
{
printf("\t\tYES\t%d", bs - mp[i]);
tif = tif + bs - mp[i];
p++;
}
}
if (i < n)
printf("\nMemory is Full, Remaining Processes cannot be accommodated");
printf("\n\nTotal Internal Fragmentation is %d", tif);
printf("\nTotal External Fragmentation is %d", ef);
return 0;
}
Aim: To write a C program to simulate the Multiprogramming With a Variable Number of Tasks.
Program:
#include <stdio.h>
int main()
{
int ms, mp[10], i, temp, n = 0;
char ch = 'y';
printf("\nEnter the total memory available (in Bytes)-- ");
scanf("%d", &ms);
temp = ms;
for (i = 0; ch == 'y'; i++, n++)
{
printf("\nEnter memory required for process %d (in Bytes) -- ", i + 1);
scanf("%d", &mp[i]);
if (mp[i] <= temp)
{
printf("\nMemory is allocated for Process %d", i + 1);
temp = temp - mp[i];
}
else
{
printf("\nMemory is Full");
break;
}
printf("\nDo you want to continue (y/n) -- ");
scanf(" %c", &ch);
}
printf("\n\nTotal Memory Available -- %d", ms);
printf("\n\n\tPROCESS\t\t MEMORY ALLOCATED ");
for (i = 0; i < n; i++)
printf("\n \t%d\t\t%d", i + 1, mp[i]);
printf("\n\nTotal Memory Allocated is %d", ms - temp);
printf("\nTotal External Fragmentation is %d", temp);
return 0;
}
Exercise - 4
Implementation of first fit, best fit and worst fit algorithm
Aim: To write a C program to implement first fit, best fit and worst fit algorithm for memory
management
#include<stdio.h>
void firstFit(int blockSize[], int m, int processSize[], int n)
{
int i, j;
int allocation[n];
for(i = 0; i < n; i++)
{
allocation[i] = -1;
}
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};
KALLAM HARANADHAREDDY INSTITUTE OF TECHNOLOGY, CHOWDAVARAM
GUNTUR, (AUTONOMOUS)
Exercise:
Date: Roll No:
m = sizeof(blockSize) / sizeof(blockSize[0]);
n = sizeof(processSize) / sizeof(processSize[0]);
firstFit(blockSize, m, processSize, n);
return 0 ;
}
#include<stdio.h>
void bestFit(int blockSize[], int m, int processSize[], int n)
{
int allocation[n];
for (int i = 0; i < n; i++)
allocation[i] = -1;
for (int i = 0; i < n; i++)
{
int bestIdx = -1;
for (int j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (bestIdx == -1)
bestIdx = j;
else if (blockSize[bestIdx] > blockSize[j])
bestIdx = j;
}
}
if (bestIdx != -1)
{
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++)
{
printf(" %d\t\t%d\t\t", i+1, processSize[i]);
if (allocation[i] != -1)
printf("%d", allocation[i] + 1);
else
printf("Not Allocated");
printf("\n");
}
}
int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);
KALLAM HARANADHAREDDY INSTITUTE OF TECHNOLOGY, CHOWDAVARAM
GUNTUR, (AUTONOMOUS)
Exercise:
Date: Roll No:
#include <stdio.h>
#include <string.h>
// Function to allocate memory to blocks as per worst fit algorithm
void worstFit(int blockSize[], int m, int processSize[], int n)
{
// Stores block id of the block allocated to a process
int allocation[n];
// Initially no block is assigned to any process
memset(allocation, -1, sizeof(allocation));
// pick each process and find suitable blocks
// according to its size ad assign to it
for (int i = 0; i < n; i++)
{
// Find the worst fit block for the current process
int wstIdx = -1;
for (int j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (wstIdx == -1)
wstIdx = j;
else if (blockSize[wstIdx] < blockSize[j])
wstIdx = j;
}
}
// If we could find a block for the current process
if (wstIdx != -1)
{
// allocate block j to p[i] process
allocation[i] = wstIdx;
// Reduce available memory in this block.
blockSize[wstIdx] -= processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++)
{
printf(" %d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d", allocation[i] + 1);
else
printf("Not Allocated");
printf("\n");
}
}
int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);
worstFit(blockSize, m, processSize, n);
return 0;
}
Exercise - 5
Simulation of Bankers Algorithm for Dead Lock Avoidance
Aim: To write a C program to Simulate Bankers Algorithm for Dead Lock Avoidance
Programs:
#include <stdio.h>
int main()
{
int n, m, i, j, k;
n = 5;
m = 3;
int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4
int max[5][3] = { { 7, 5, 3 }, // P0 // MAX Matrix
{ 3, 2, 2 }, // P1
{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // 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;
KALLAM HARANADHAREDDY INSTITUTE OF TECHNOLOGY, CHOWDAVARAM
GUNTUR, (AUTONOMOUS)
Exercise:
Date: Roll No:
}
}
if (flag == 0)
{
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}
int flag = 1;
for(int 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);
}
Exercise - 6
Simulation of Bankers Algorithm for Dead Lock Prevention
Aim: To write a C program to Simulate Bankers Algorithm for Dead Lock Prevention
Program:
#include <stdio.h>
#define MAX_PROCESSES 10
#define MAX_RESOURCES 10
int processes, resources;
int max[MAX_PROCESSES][MAX_RESOURCES];
int allocation[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
int available[MAX_RESOURCES];
// Function to initialize data structures
void initialize()
{
printf("Enter the number of processes: ");
scanf("%d", &processes);
printf("Enter the number of resources: ");
scanf("%d", &resources);
printf("Enter the Maximum matrix:\n");
for (int i = 0; i < processes; i++)
for (int j = 0; j < resources; j++)
scanf("%d", &max[i][j]);
printf("Enter the Allocation matrix:\n");
for (int i = 0; i < processes; i++)
for (int j = 0; j < resources; j++)
scanf("%d", &allocation[i][j]);
printf("Enter the Available matrix:\n");
for (int i = 0; i < resources; i++)
scanf("%d", &available[i]);
// Calculate Need matrix
for (int i = 0; i < processes; i++)
for (int j = 0; j < resources; j++)
need[i][j] = max[i][j] - allocation[i][j];
}
// Function to check if the system is in a safe state
int isSafe()
{
int work[MAX_RESOURCES];
int finish[MAX_PROCESSES];
// Initialize work and finish arrays
for (int i = 0; i < resources; i++)
work[i] = available[i];
for (int i = 0; i < processes; i++)
finish[i] = 0;
int count = 0;
KALLAM HARANADHAREDDY INSTITUTE OF TECHNOLOGY, CHOWDAVARAM
GUNTUR, (AUTONOMOUS)
Exercise:
Date: Roll No:
// Main function
int main()
{
initialize();
if (isSafe())
{
printf("The initial state is safe.\n");
}
else
{
printf("The initial state is unsafe.\n");
return 1;
}
// Simulate resource request
simulateRequest();
// Simulate resource release
simulateRelease();
return 0;
}
Exercise - 7
a) Implementation of FIFO page replacement technique
Program:
#include <stdio.h>
void display(int fr[], int frsize)
{
int i;
printf("\n");
for (i = 0; i < frsize; i++)
printf("%d\t", fr[i]);
}
int main()
{
int i, j, page[12] = {2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2};
int flag1 = 0, flag2 = 0, pf = 0, frsize = 3, top = 0;
int fr[3];
for (i = 0; i < 3; i++)
{
fr[i] = -1;
}
for (j = 0; j < 12; j++)
{
flag1 = 0;
flag2 = 0;
for (i = 0; i < frsize; i++)
{
if (fr[i] == page[j])
{
flag1 = 1;
flag2 = 1;
break;
}
}
if (flag1 == 0)
{
for (i = 0; i < frsize; i++)
{
if (fr[i] == -1)
{
fr[i] = page[j];
flag2 = 1;
break;
}
}
}
KALLAM HARANADHAREDDY INSTITUTE OF TECHNOLOGY, CHOWDAVARAM
GUNTUR, (AUTONOMOUS)
Exercise:
Date: Roll No:
if (flag2 == 0)
{
fr[top] = page[j];
top = (top + 1) % frsize;
pf++;
display(fr, frsize);
}
}
printf("Number of page faults: %d\n", pf + frsize);
return 0;
}
Program:
#include<stdio.h>
int fr[3];
void display()
{
int i;
printf("\n");
for (i = 0; i < 3; i++)
printf("\t%d", fr[i]);
}
int main()
{
int p[12] = {2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2};
int i, j, fs[3];
int index, k, l, flag1 = 0, flag2 = 0, pf = 0, frsize = 3;
for (i = 0; i < 3; i++)
{
fr[i] = -1;
}
for (j = 0; j < 12; j++)
{
flag1 = 0, flag2 = 0;
for (i = 0; i < 3; i++)
{
if (fr[i] == p[j])
{
flag1 = 1;
flag2 = 1;
break;
}
}
if (flag1 == 0)
{
for (i = 0; i < 3; i++)
{
if (fr[i] == -1)
{
fr[i] = p[j];
flag2 = 1;
break;
}
}
}
if (flag2 == 0)
{
KALLAM HARANADHAREDDY INSTITUTE OF TECHNOLOGY, CHOWDAVARAM
GUNTUR, (AUTONOMOUS)
Exercise:
Date: Roll No:
Program:
#include<stdio.h>
int fr[3];
int fs[3]; // Added an array to store frequency
int top = 0; // To keep track of the top of the stack
void display()
{
int i;
printf("\n");
for (i = 0; i < 3; i++)
printf("\t%d", fr[i]);
}
int findLFU(int fs[])
{
int minIndex = 0;
for (int i = 1; i < 3; i++)
{
if (fs[i] < fs[minIndex])
{
minIndex = i;
}
}
return minIndex;
}
int isPageInFrames(int page)
{
for (int i = 0; i < 3; i++)
{
if (fr[i] == page)
{
return 1; // Page is in frames
}
}
return 0; // Page is not in frames
}
int main()
{
int p[12] = {2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2};
int pf = 0;
for (int i = 0; i < 3; i++)
{
fr[i] = -1;
fs[i] = 0;
}
Exercise – 8
a) Simulation of Single level directory file organization technique
Aim: To write a C program to simulate Single level directory file organization technique.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Directory
{
char dname[10];
char fname[10][10];
int fcnt;
};
int main()
{
struct Directory dir;
int i, ch;
char f[30];
system("cls");
// Use system("clear") for Unix-like systems
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while (1)
{
("\n\n1. Create File\t2. Delete File\t3. Search File\n4. Display Files\t5. Exit\nEnter your choice -- ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter the name of the file -- ");
scanf("%s", dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2:
printf("\nEnter the name of the file -- ");
scanf("%s", f);
for (i = 0; i < dir.fcnt; i++)
{
if (strcmp(f, dir.fname[i]) == 0)
{
printf("File %s is deleted\n", f);
strcpy(dir.fname[i], dir.fname[dir.fcnt - 1]);
dir.fcnt--;
break;
}
KALLAM HARANADHAREDDY INSTITUTE OF TECHNOLOGY, CHOWDAVARAM
GUNTUR, (AUTONOMOUS)
Exercise:
Date: Roll No:
}
if (i == dir.fcnt)
printf("File %s not found\n", f);
break;
case 3:
printf("\nEnter the name of the file -- ");
scanf("%s", f);
for (i = 0; i < dir.fcnt; i++)
{
if (strcmp(f, dir.fname[i]) == 0)
{
printf("File %s is found\n", f);
break;
}
}
if (i == dir.fcnt)
printf("File %s not found\n", f);
break;
case 4:
if (dir.fcnt == 0)
printf("\nDirectory Empty\n");
else
{
printf("\nThe Files are -- ");
for (i = 0; i < dir.fcnt; i++)
printf("\t%s", dir.fname[i]);
printf("\n");
}
break;
default:
exit(0);
}
}
return 0;
}
Aim: To write a C program to simulate Single level directory file organization technique.
Program:
#include<stdio.h>
#include<string.h>
struct Directory
{
char dname[10];
char fname[10][10];
int fcnt;
};
int main()
{
struct Directory dir[10];
int i, ch, dcnt, k;
char f[30], d[30];
dcnt = 0;
while (1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\n4. Search File\t\t5. Display\t6. Exit\nEnter your choice -- ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt = 0;
dcnt++;
printf("Directory created\n");
break;
case 2:
printf("\nEnter name of the directory -- ");
scanf("%s", d);
for (i = 0; i < dcnt; i++)
{
if (strcmp(d, dir[i].dname) == 0)
{
printf("Enter name of the file -- ");
scanf("%s", dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created\n");
break;
}
}
if (i == dcnt)
printf("Directory %s not found\n", d);
KALLAM HARANADHAREDDY INSTITUTE OF TECHNOLOGY, CHOWDAVARAM
GUNTUR, (AUTONOMOUS)
Exercise:
Date: Roll No:
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\n", f);
dir[i].fcnt--;
strcpy(dir[i].fname[k], dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found\n", f);
goto jmp;
}
}
printf("Directory %s not found\n", 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\n", f);
goto jmp1;
}
}
printf("File %s not found\n", f);
goto jmp1;
}
}
printf("Directory %s not found\n", d);
jmp1: break;
case 5:
if (dcnt == 0)
printf("\nNo Directories\n");
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:
return 0;
}
}
return 0;
}
Exercise – 9
a) Implementation of Sequenced File allocation strategies
Program:
#include <stdio.h>
#define MAX_BLOCKS 50
int fileAllocation(int f[], int st, int len)
{
int count = 0;
for (int k = st; k < st + len; k++)
{
if (f[k] == 0)
{
count++;
}
else
{
printf("Block %d is already allocated. Cannot allocate the file.\n", k);
return 0;
}
}
if (count == len)
{
for (int j = st; j < st + len; j++)
{
f[j] = 1;
printf("Block %d\t%d\n", j, f[j]);
}
printf("The file is allocated to disk.\n");
return 1;
}
else
{
printf("Not enough contiguous free blocks to allocate the file.\n");
return 0;
}
}
int main()
{
int f[MAX_BLOCKS] = {0};
int st, len, c;
printf("Files Allocated are:\n");
do
{
printf("Enter starting block and length of files: ");
scanf("%d%d", &st, &len);
KALLAM HARANADHAREDDY INSTITUTE OF TECHNOLOGY, CHOWDAVARAM
GUNTUR, (AUTONOMOUS)
Exercise:
Date: Roll No:
Program:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int f[50], index[50], i, n, st, len, j, c, k, ind, count = 0;
for (i = 0; i < 50; i++)
f[i] = 0;
x:
printf("Enter the index block: ");
scanf("%d", &ind);
if (f[ind] != 1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk: \n", ind);
scanf("%d", &n);
}
else
{
printf("%d index is already allocated\n", ind);
goto x;
}
y:
count = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &index[i]);
if (f[index[i]] == 0)
count++;
}
if (count == n)
{
for (j = 0; j < n; j++)
f[index[j]] = 1;
printf("Allocated\n");
printf("File Indexed\n");
for (k = 0; k < n; k++)
printf("%d-------->%d : %d\n", ind, index[k], f[index[k]]);
}
else
{
printf("File in the index is already allocated\n");
printf("Enter another file indexed\n");
goto y;
}
printf("Do you want to enter more file (Yes - 1/No - 0): ");
scanf("%d", &c);
if (c == 1)
goto x;
else
exit(0);
}
Program:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int f[50], p, i, st, len, j, c, k, a;
for (i = 0; i < 50; i++)
f[i] = 0;
printf("Enter how many blocks already allocated: ");
scanf("%d", &p);
printf("Enter blocks already allocated: ");
for (i = 0; i < p; i++)
{
scanf("%d", &a);
f[a] = 1;
}
x:
printf("Enter index starting block and length: ");
scanf("%d%d", &st, &len);
k = len;
if (f[st] == 0)
{
for (j = st; j < (st + k); j++)
{
if (f[j] == 0)
{
f[j] = 1;
printf("%d-------->%d\n", j, f[j]);
}
else
{
printf("%d Block is already allocated\n", j);
k++;
}
}
}
else
printf("%d starting block is already allocated\n", st);
printf("Do you want to enter more file (Yes - 1/No - 0): ");
scanf("%d", &c);
if (c == 1)
goto x;
else
exit(0);
}
Exercise – 10
Implementation of two processes communication using shared memory
Aim: To write a C program to implement two processes communication using shared memory.
Program:
#include <windows.h>
#include <stdio.h>
struct shared_data
{
int value;
char message[256];
};
int main()
{
HANDLE hMapFile;
LPVOID lpMapAddress;
struct shared_data* shared_memory;
hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
sizeof(struct shared_data), (LPCSTR)L"MySharedMemory");
if (hMapFile == NULL)
{
printf("Could not create file mapping object (%d)\n", GetLastError());
return 1;
}
lpMapAddress = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if (lpMapAddress == NULL)
{
printf("Could not map view of file (%d)\n", GetLastError());
CloseHandle(hMapFile);
return 1;
}
shared_memory = (struct shared_data*)lpMapAddress;
// Fork a child process
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (CreateProcess(NULL, // No module name (use command line)
"child_process.exe", // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
Exercise – 11
Implementation of producer and consumer problem using semaphores
Aim: To write a C program to implement producer and consumer problem using semaphores.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#define BUFFER_SIZE 5
#define MAX_ITEMS 20
int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int produced_count = 0;
int consumed_count = 0;
HANDLE mutex;
HANDLE full;
HANDLE empty;
DWORD WINAPI producer(LPVOID arg)
{
int item = 1;
while (produced_count < MAX_ITEMS)
{
WaitForSingleObject(empty, INFINITE);
WaitForSingleObject(mutex, INFINITE);
buffer[in] = item;
printf("Produced: %d\n", item);
item++;
in = (in + 1) % BUFFER_SIZE;
produced_count++;
ReleaseMutex(mutex);
ReleaseSemaphore(full, 1, NULL);
}
return 0;
}
DWORD WINAPI consumer(LPVOID arg)
{
while (consumed_count < MAX_ITEMS)
{
WaitForSingleObject(full, INFINITE);
WaitForSingleObject(mutex, INFINITE);
int item = buffer[out];
printf("Consumed: %d\n", item);
out = (out + 1) % BUFFER_SIZE;
consumed_count++;
ReleaseMutex(mutex);
ReleaseSemaphore(empty, 1, NULL);
}
return 0;
}
int main()
{
HANDLE producerThread, consumerThread;
mutex = CreateMutex(NULL, FALSE, NULL);
full = CreateSemaphore(NULL, 0, BUFFER_SIZE, NULL);
empty = CreateSemaphore(NULL, BUFFER_SIZE, BUFFER_SIZE, NULL);
producerThread = CreateThread(NULL, 0, producer, NULL, 0, NULL);
consumerThread = CreateThread(NULL, 0, consumer, NULL, 0, NULL);
WaitForSingleObject(producerThread, INFINITE);
WaitForSingleObject(consumerThread, INFINITE);
CloseHandle(mutex);
CloseHandle(full);
CloseHandle(empty);
return 0;
}
Exercise – 12
a) Implementation of FCFS disk scheduling algorithm
Program:
#include <stdio.h>
#include <stdlib.h>
void calculateHeadMovement(int requests[], int n) {
int totalHeadMovement = 0;
printf("Order of disk access: %d", requests[0]);
for (int i = 1; i < n; i++) {
totalHeadMovement += abs(requests[i] - requests[i - 1]);
printf(" -> %d", requests[i]);
}
printf("\nTotal head movement: %d\n", totalHeadMovement);
}
int main() {
int n;
printf("Enter the number of disk requests: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid number of disk requests.\n");
return 1;
}
int *requests = (int *)malloc(n * sizeof(int));
if (requests == NULL) {
printf("Memory allocation error.\n");
return 1;
}
printf("Enter the disk requests:\n");
for (int i = 0; i < n; i++) {
printf("Request %d: ", i + 1);
scanf("%d", &requests[i]);
}
calculateHeadMovement(requests, n);
free(requests);
return 0;
}
Program:
#include <stdio.h>
#include <stdlib.h>
void calculateHeadMovement(int requests[], int n, int initialPosition)
{
int totalHeadMovement = 0;
printf("Order of disk access: ");
// Sorting the requests
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (requests[j] > requests[j + 1])
{
int temp = requests[j];
requests[j] = requests[j + 1];
requests[j + 1] = temp;
}
}
}
int direction;
// Determine the direction based on the initial position
if (initialPosition < requests[0])
{
direction = 1; // Moving towards the higher track numbers
}
else
{
direction = -1; // Moving towards the lower track numbers
}
int index = 0;
// Find the index where the initial position is located in the sorted requests
while (index < n && requests[index] <= initialPosition)
{
index++;
}
// Move towards higher track numbers
for (int i = index; i < n; i++)
{
totalHeadMovement += abs(requests[i] - initialPosition);
printf("%d -> ", requests[i]);
initialPosition = requests[i];
}
// Move towards lower track numbers
Program:
#include <stdio.h>
#include <stdlib.h>
void calculateHeadMovement(int requests[], int n, int initialPosition)
{
int totalHeadMovement = 0;
printf("Order of disk access: ");
// Sorting the requests
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (requests[j] > requests[j + 1])
{
int temp = requests[j];
requests[j] = requests[j + 1];
requests[j + 1] = temp;
}
}
}
int direction = 1; // Moving towards the higher track numbers
int index = 0;
// Find the index where the initial position is located in the sorted requests
while (index < n && requests[index] <= initialPosition)
{
index++;
}
// Move towards higher track numbers
for (int i = index; i < n; i++)
{
totalHeadMovement += abs(requests[i] - initialPosition);
printf("%d -> ", requests[i]);
initialPosition = requests[i];
}
// Move to the highest track
totalHeadMovement += abs(requests[n - 1] - initialPosition);
printf("%d -> ", requests[n - 1]);
initialPosition = requests[n - 1];
// Move towards lower track numbers
for (int i = 0; i < index; i++)
{
totalHeadMovement += abs(requests[i] - initialPosition);
printf("%d -> ", requests[i]);
initialPosition = requests[i];
}
KALLAM HARANADHAREDDY INSTITUTE OF TECHNOLOGY, CHOWDAVARAM
GUNTUR, (AUTONOMOUS)
Exercise:
Date: Roll No: