0% found this document useful (0 votes)
52 views10 pages

Os Practical File 2

The document describes the implementation of FCFS disk scheduling algorithm in C. It takes user input for the number of requests and the request sequence. It also takes the initial head position. It calculates the total head movement by taking the absolute difference between the current and next request position and updating the current position. It outputs the total head movement.

Uploaded by

Aditya Bansal
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)
52 views10 pages

Os Practical File 2

The document describes the implementation of FCFS disk scheduling algorithm in C. It takes user input for the number of requests and the request sequence. It also takes the initial head position. It calculates the total head movement by taking the absolute difference between the current and next request position and updating the current position. It outputs the total head movement.

Uploaded by

Aditya Bansal
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/ 10

PROGRAM – 12

OBJECTIVE - Implementation of Orphan and Zombie Process in C.

ORPHAN PROCESS –
A process whose parent process no more exists i.e., is either finished or terminated without
waiting for its child process to terminate.

SOURCE CODE –
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t p;
p=fork();
if(p==0)//child
{
printf(“I am a child having PID %d\n”,getpid());
printf(“My parent PID is %d\n”,getpid());
}
else//parent
{
printf(“I am parent having PID %d\n”,getpid());
printf(“My parent PID is %d\n”,p);
}
return 0;
}
OUTPUT –
ZOMBIE PROCESS –
A process which has finished the execution but still has entry in the process table to report to
its parent process.

SOURCE CODE –
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t t;
t=fork();
if(t==0)//child
{
printf(“Child having id %d\n”,getpid());
}
else//parent
{
printf(“Parent having id %d\n”,getpid());
sleep(15);//Parent sleeps
}
return 0;
}
OUTPUT –
PROGRAM – 13
OBJECTIVE - Implementation of Inter-process Communication using PIPE in
C.

SOURCE CODE –

#include<stdio.h>
#include<unistd.h>
int main()
{
int pipefds[2];
int returnstatus;
int pid;
char writemessages[2][20]={"Hi", "Hello"};
char readmessage[20];
returnstatus = pipe(pipefds);
if (returnstatus == -1)
{
printf("Unable to create pipe\n");
return 1;
}
pid = fork();
if (pid == 0)
{
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Child Process - Reading from pipe – Message 1 is %s\n", readmessage);
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Child Process - Reading from pipe – Message 2 is %s\n", readmessage);
}
else
{
printf("Parent Process - Writing to pipe - Message 1 is %s\n", writemessages[0]);
write(pipefds[1], writemessages[0], sizeof(writemessages[0]));
printf("Parent Process - Writing to pipe - Message 2 is %s\n", writemessages[1]);
write(pipefds[1], writemessages[1], sizeof(writemessages[1]));
}
return 0;
}

OUTPUT –
PROGRAM – 15
OBJECTIVE - Implementation of Banker’s Algorithm in C.

SOURCE CODE –
#include <stdio.h>
int main()
{
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
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;
}
}
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);
}

OUTPUT –
PROGRAM – 16
OBJECTIVE - Implementation of FCFS Page Replacement Algorithm in C.

SOURCE CODE –
#include <stdio.h>
int main()
{
int referenceString[10], pageFaults = 0, m, n, s, pages, frames;
printf("\nEnter the number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter reference string values:\n");
for( m = 0; m < pages; m++)
{
printf("Value No. [%d]:\t", m + 1);
scanf("%d", &referenceString[m]);
}
printf("\n What are the total number of frames:\t");
{
scanf("%d", &frames);
}
int temp[frames];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(referenceString[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = referenceString[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = referenceString[m];
}
printf("\n");
for(n = 0; n < frames; n++)
{
printf("%d\t", temp[n]);
}
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}

OUTPUT –
PROGRAM – 17
OBJECTIVE - Implementation of LRU Page Replacement Algorithm in C.

SOURCE CODE –
#include<stdio.h>
int findLRU(int time[], int n)
{
int i, minimum = time[0], pos = 0;
  for(i = 1; i < n; ++i)
{
if(time[i] < minimum)
{
minimum = time[i];
pos = i;
}
}
return pos;
}
int main()
{
    int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], 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)
{
     printf("%d\t", frames[j]);
     }
}
printf("\n\nTotal Page Faults = %d", faults);
    return 0;
}

OUTPUT –
PROGRAM – 19
OBJECTIVE - Implementation of FCFS Disk Scheduling Algorithm in C.

SOURCE CODE –
#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);
for(i=0;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
printf("Total head moment is %d",TotalHeadMoment);
return 0;
}

OUTPUT –

You might also like