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

os_program

The document contains multiple experiments related to operating systems, including system calls, CPU scheduling algorithms, the producer-consumer problem, and memory allocation techniques. Each experiment is implemented in C programming language, demonstrating concepts such as process creation, FIFO scheduling, priority scheduling, and various memory management strategies like worst-fit, best-fit, and first-fit. Additionally, it covers page replacement algorithms and the Banker's algorithm for deadlock avoidance.

Uploaded by

raskararav01
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)
9 views

os_program

The document contains multiple experiments related to operating systems, including system calls, CPU scheduling algorithms, the producer-consumer problem, and memory allocation techniques. Each experiment is implemented in C programming language, demonstrating concepts such as process creation, FIFO scheduling, priority scheduling, and various memory management strategies like worst-fit, best-fit, and first-fit. Additionally, it covers page replacement algorithms and the Banker's algorithm for deadlock avoidance.

Uploaded by

raskararav01
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/ 32

Experiment 3: system call

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#define MAX_COUNT 5

void ChildProcess(void);
void ParentProcess(void);
int main(void)
{
pid_t pid;
pid = fork();
if (pid == 0)
ChildProcess();
else
ParentProcess();
return 0;
}
void ChildProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf(" This line is from child, value = %d\n", i);
printf(" *** Child process is done ***\n");
}
void ParentProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value = %d\n", i);
printf("*** Parent is done ***\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{ int pid = fork();
if (pid < 0)
{printf("Fork failed\n");
exit(1);
}
else if (pid == 0)
{
execlp("whoami", "whoami", NULL);
perror("execlp failed");
exit(1);
} else
{ printf("\nProcess ID is: %d\n", getpid());
wait(NULL);
exit(0); }}
Experiment 4: FIFO PROCESS SCHEDULING
#include <stdio.h>
int main()
{
int n, bt[20], wt[20], tat[20], avwt = 0, avtat = 0, i, j;
printf("Enter total number of processes (maximum 20): ");
scanf("%d", &n);
printf("\nEnter Process Burst Time:\n");
for (i = 0; i < n; i++)
{
printf("P[%d]: ", i + 1);
scanf("%d", &bt[i]);
}
wt[0] = 0;
for (i = 1; i < n; i++)
{
wt[i] = 0;
for (j = 0; j < i; j++)
wt[i] += bt[j];
}
printf("\nProcess \tBurst_Time\tWaiting_Time\tTurnaround_Time\n");
for (i = 0; i < n; i++)
{
tat[i] = bt[i] + wt[i];
avwt += wt[i];
avtat += tat[i];
printf("P[%d]\t %d\t \t%d\t \t%d\n", i + 1, bt[i], wt[i], tat[i]);
}
avwt = avwt / n;
avtat = avtat / n;
printf("\nAverage Waiting Time: %d", avwt);
printf("\nAverage Turnaround Time: %d\n", avtat);

return 0;
}
Experiment 5:CPU scheduling algorithm
#include <stdio.h>
int main()
{
int x, n, p[10], pp[10], pt[10], w[10], t[10], awt = 0, atat = 0, i, j;
printf("Enter the number of processes: ");
scanf("%d", &n);

printf("\nEnter process burst time and priority:\n");


for (i = 0; i < n; i++)
{
printf("Process %d (Burst Time Priority): ", i + 1);
scanf("%d %d", &pt[i], &pp[i]);
p[i] = i + 1; // Store process ID
}
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (pp[i] < pp[j]) // Higher number = Higher priority
{
x = pp[i];
pp[i] = pp[j];
pp[j] = x;
x = pt[i];
pt[i] = pt[j];
pt[j] = x;
x = p[i];
p[i] = p[j];
p[j] = x;
}
}
}
w[0] = 0; // First process has no waiting time
t[0] = pt[0]; // Turnaround time for first process
awt = 0;
atat = t[0];

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


{
w[i] = t[i - 1]; // Waiting time = Turnaround time of previous process
t[i] = w[i] + pt[i]; // Turnaround time
awt += w[i];
atat += t[i];
}

printf("\nJob\tBurst_Time\tWait_Time\tTurnaround_Time \tPriority\n");
for (i = 0; i < n; i++)
{
printf("%d \t%d\t \t%d\t \t%d\t \t%d\n", p[i], pt[i], w[i], t[i], pp[i]);
}
awt = awt / n;
atat = atat / n;
printf("\nAverage Wait Time: %d", awt);
printf("\nAverage Turnaround Time: %d\n", atat);

return 0;
}
Experiment 6: Producer consumer problem
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1:
if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2:
if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
Experiment 7: Banker’s Algorithm
#include <stdio.h>
#include <stdlib.h>

#define MAX 10
int main() {
int i, j, z = 0, P, L, N, lim;
int A[MAX], R[MAX], M[MAX], S[MAX], W[MAX], MAX_LS[MAX];
int RS[MAX], LS[MAX], CP[MAX] = {0};
do {
printf("HOW MANY PROCESSES DO YOU WANT? ");
scanf("%d", &P);
if (P > MAX) {
printf("Maximum allowed processes are %d. Try again.\n", MAX);
}
} while (P > MAX);
printf("\nINSERT THE MULTIPLICITY OF THE RESOURCE: ");
scanf("%d", &N);
for (i = 0; i < P; i++) {
printf("\nHow many resources does process P%d need? ", i);
scanf("%d", &A[i]);

printf("\nHow many instances of process P%d currently hold? ", i);


scanf("%d", &R[i]);

printf("\nWhat is the max instances of a resource for Process P%d? ", i);
scanf("%d", &M[i]);

printf("\nWhat is the allocated number of instances of a resource for process P%d? ", i);
scanf("%d", &S[i]);
}
L = N;
for (i = 0; i < P; i++)
L -= A[i];
for (i = 0; i < P; i++) {
if (R[i] <= L) {
RS[i] = M[i] - A[i] - R[i];
LS[i] = L - R[i];
} else {
LS[i] = -1;
}

W[i] = M[i] - S[i];


printf("Need for P%d = %d\n", i, W[i]);
}
for (i = 0; i < P; i++)
MAX_LS[i] = -1;
for (i = 0; i < P; i++) {
if (i == 0) {
for (j = 0; j < P; j++) {
if (LS[j] >= MAX_LS[z]) {
MAX_LS[z] = LS[j];
CP[z] = j;
lim = MAX_LS[z];
}
}
} else {
for (j = 0; j < P; j++) {
if ((LS[j] >= MAX_LS[z]) && (LS[j] < lim)) {
MAX_LS[z] = LS[j];
CP[z] = j;
lim = MAX_LS[z];
}
}
}
z++;
}
printf("\nSafe sequence: ");
for (i = 0; i < P; i++) {
printf("P%d ", CP[i]);
}
printf("\n");

return 0;
}
Experiment 8: Memory allocation techniques
1. WORST-FIT
#include <stdio.h>

#define MAX 25

int main() {
int frag[MAX], b[MAX], f[MAX], i, j, nb, nf, temp;
static int bf[MAX], ff[MAX];

printf("\n\tMemory Management Scheme - Worst Fit");

printf("\nEnter the number of blocks: ");


scanf("%d", &nb);

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


scanf("%d", &nf);

printf("\nEnter the size of the blocks:\n");


for (i = 0; i < nb; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &b[i]);
}

printf("Enter the size of the files:\n");


for (i = 0; i < nf; i++) {
printf("File %d: ", i + 1);
scanf("%d", &f[i]);
}

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


int worstIdx = -1;

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


if (bf[j] == 0) {
temp = b[j] - f[i];

if (temp >= 0) {
if (worstIdx == -1 || temp > (b[worstIdx] - f[i])) {
worstIdx = j;
}
}
}
}

if (worstIdx != -1) {
ff[i] = worstIdx;
frag[i] = b[worstIdx] - f[i];
bf[worstIdx] = 1;
} else {
ff[i] = -1;
frag[i] = -1;
}
}

printf("\nFile No.\tFile Size\tBlock No.\tBlock Size\tFragment");


for (i = 0; i < nf; i++) {
if (ff[i] != -1) {
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i + 1, f[i], ff[i] + 1, b[ff[i]], frag[i]);
} else {
printf("\n%d\t\t%d\t\tNot Allocated", i + 1, f[i]);
}
}

printf("\n");
return 0;
}
2 . Best-fit
#include <stdio.h>
#define MAX 25

int main() {
int frag[MAX], b[MAX], f[MAX], i, j, nb, nf, temp, lowest;
static int bf[MAX] = {0}, ff[MAX] = {-1};

printf("\nEnter the number of blocks: ");


scanf("%d", &nb);

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


scanf("%d", &nf);

printf("\nEnter the size of the blocks:\n");


for (i = 0; i < nb; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &b[i]);
}

printf("Enter the size of the files:\n");


for (i = 0; i < nf; i++) {
printf("File %d: ", i + 1);
scanf("%d", &f[i]);
}

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


lowest = 10000;
ff[i] = -1;
for (j = 0; j < nb; j++) {
if (bf[j] == 0) {
temp = b[j] - f[i];
if (temp >= 0 && temp < lowest) {
ff[i] = j;
lowest = temp;
}
}
}

if (ff[i] != -1) {
frag[i] = lowest;
bf[ff[i]] = 1;
} else {
frag[i] = -1;
}
}

printf("\nFile No.\tFile Size\tBlock No.\tBlock Size\tFragment");


for (i = 0; i < nf; i++) {
if (ff[i] != -1) {
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i + 1, f[i], ff[i] + 1, b[ff[i]], frag[i]);
} else {
printf("\n%d\t\t%d\t\tNot Allocated", i + 1, f[i]);
}
}

printf("\n");
return 0;
}
3. First-fit
#include<stdio.h>
void main()
{
int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;
for(i = 0; i < 10; i++)
{
flags[i] = 0;
allocation[i] = -1;
}
printf("Enter no. of blocks: ");
scanf("%d", &bno);
printf("\nEnter size of each block: ");
for(i = 0; i < bno; i++)
scanf("%d", &bsize[i]);
printf("\nEnter no. of processes: ");
scanf("%d", &pno);
printf("\nEnter size of each process: ");
for(i = 0; i < pno; i++)
scanf("%d", &psize[i]);
for(i = 0; i < pno; i++) //allocation as per first fit
for(j = 0; j < bno; j++)
if(flags[j] == 0 && bsize[j] >= psize[i])
{
allocation[j] = i;
flags[j] = 1;
break;
}
//display allocation details
printf("\nBlock no.\tsize\t\tprocess no.\t\tsize");
for(i = 0; i < bno; i++)
{
printf("\n%d\t\t%d\t\t", i+1, bsize[i]);
if(flags[i] == 1)
printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]);
else
printf("Not allocated");
}
}
Experiment 9: Page Replacement Algorithms
#include <stdio.h>
#define MAX_FRAMES 10
#define MAX_PAGES 30
int findLRU(int time[], int n) {
int i, minTime = time[0], pos = 0;
for (i = 1; i < n; i++) {
if (time[i] < minTime) {
minTime = time[i];
pos = i;
}
}
return pos;
}
int main() {
int no_of_frames, no_of_pages, frames[MAX_FRAMES], pages[MAX_PAGES];
int counter = 0, time[MAX_FRAMES], flag1, flag2, i, j, pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter reference string: ");
for (i = 0; i < no_of_pages; i++) {
scanf("%d", &pages[i]);
}

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


frames[i] = -1;
}
for (i = 0; i < no_of_pages; i++) {
flag1 = flag2 = 0;
for (j = 0; j < no_of_frames; j++) {
if (frames[j] == pages[i]) {
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
if (flag1 == 0) {
for (j = 0; j < no_of_frames; j++) {
if (frames[j] == -1) {
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}

if (flag2 == 0) {
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
for (j = 0; j < no_of_frames; j++) {
if (frames[j] == -1)
printf("-\t");
else
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d\n", faults);
return 0;
}
Experiment 10: Disk scheduling
1. FCFS (First come first serve)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);

// logic for FCFS disk scheduling

for(i=0;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}

printf("Total head moment is %d",TotalHeadMoment);


return 0;

}
2. Shortest seek time first (SSTF)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial,count=0;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial); while(count!=n)
{
int min=1000,d,index;
for(i=0;i<n;i++)
{
d=abs(RQ[i]-initial);
if(min>d)
{
min=d;
index=i;
}

}
TotalHeadMoment=TotalHeadMoment+min;
initial=RQ[index];
// 1000 is for max
// you can use any number
RQ[index]=1000;
count++;
}

printf("Total head movement is %d",TotalHeadMoment);


return 0;
}
3. Scan/ Elevator Disk scheduling

#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}

}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
initial = size-1;
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
else {
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
initial =0;
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}

You might also like