OS Record
OS Record
Aim:
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<process.h>
int main()
char*path;
clrscr();
path = getenv("PATH");
printf("the PATH = %s",path);
getch();
Output:
#include<stdio.h>
int main()
{
int n, bt[10],wt[10],tat[10],avwt=0, avtat=0, i,j;
clrscr();
printf("enter the total number of process:");
scanf("%d",&n);
printf("enter the process burst time:");
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] = bt[i-1] + wt[i-1];
}
for (i = 0; i < n ; i++)
{
tat[i] = bt[i] + wt[i];
}
printf("\n process\t burst time\t waiting time \t turnaround time");
for(i=0; i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\n p[%d]\t\t",i+1);
printf("%d\t\t",bt[i]);
printf("%d\t\t",wt[i]);
printf("%d\t\t",tat[i]);
}
avwt /=i;
avtat /=i;
printf("\n Average waiting time:%d",avwt);
printf("\n Average turnaround time:%d",avtat);
getch();
return 0;
}
output:
p[2]: 5
p[3]:9
p[4]:15
p[1] 10 0 10
p[2] 5 10 15
p[3] 9 15 24
p[4] 15 24 39
Aim:
To write the Program to Implement First come first serve in C.
Algorithm:
STEP 1: Start the process.
· STEP 2:Input the number of processes required to be scheduled using
FCFS, burst time for each process and its arrival time.
·
· STEP 3 : Using enhanced bubble sort technique, sort the all given
processes in ascending order according to arrival time in a ready queue.
·
· STEP 4 : Calculate the Finish Time, Turn Around Time and Waiting
Time for each process which in turn help to calculate Average Waiting Time
and Average Turn Around Time required by CPU to schedule given set of
process using FCFS.
·
· STEP5 : Process with less arrival time comes first and gets scheduled first
by the CPU.
·
· STEP6 : Calculate the Average Waiting Time and Average Turn Around
Time. ·
· STEP 7 : Stop the process and execute the result.
Program
#include<stdio.h>
#include<conio.h>
#define max 30
void main()
{
int i, j, n, bt[max], at[max], wt[max], tat[max],temp[max];
float avwt=0,avtat=0;
clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
printf("Enter the burst time of the process:");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
printf("Enter the arrival time of the process:");
for(i=0;i<n;i++)
scanf("%d",&at[i]);
temp[0]=0;
printf("process burst time arrival time waiting time turnaroud time:\n");
for(i=0;i<n;i++)
{
wt[i]=0;
tat[i]=0;
temp[i+1]=temp[i]+bt[i];
wt[i]=temp[i]-at[i];
tat[i]=wt[i]+bt[i];
avwt=avwt+wt[i];
avtat=avtat+at[i];
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\n",i+1, bt[i],at[i],wt[i],tat[i]);
}
avwt=avwt/n;
avtat=avtat/n;
printf("Average waiting time=%f\n",avwt);
printf("Average turnaround time=%f ",avtat);
getch();
}
output:
Enter the number of process:4
Enter the burst time of the process:5 4 1 3
Enter the arrival time of the process:0 1 2 3
process burst time arrival time waiting time turnaround time
1 5 0 0 5
2 4 1 4 8
3 1 2 7 8
4 3 3 7 10
Average waiting time = 4.500000
Average turnaround time = 7.750000
Aim:
To write the Program to Implement Shortest job first in C.
Algorithm:
STEP 1: Start the process.
STEP 2:Sort all the processes according to their arrival time.
STEP 3:Select the process with minimum arrival time as well as minimum
burst time.
STEP 4:After completion of the process, select from the ready queue the
process which has the minimum burst time.
STEP 5:Repeat above processes untill all processes have finished their
execution.
Program
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process: ");
scanf("%d",&n);
printf("\nEnter Burst Time: \n");
for(i=0;i<n;i++)
{
printf("p[%d]:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
//sorting of burst times
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("\nProcesst Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\np[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("\n\nAverage Waiting Time = %f",avg_wt);
printf("\nAverage Turnaround Time = %f\n",avg_tat);
}
output:
Enter the number of process: 5
Enter the burst time :
p1:4
p2:3
p3:7
p4:1
p5:2
process burst time waiting time turnaround time
p4 1 0 1
p5 2 1 3
p2 3 3 6
p1 4 6 10
p3 7 10 17
Average waiting time : 4.000000
Average turnaround time: 7.400000
Algorithm:
STEP 1: Start the process.
STEP 2: The producer is to either go to sleep or discard data if the buffer is
full.
STEP 3:The next time the consumer removes an item from the buffer, it
notifies the producer, who starts to fill the buffer again.
STEP 4:In the same manner, the consumer can go to sleep if it finds the
buffer to be empty.
STEP 5:The next time the producer puts data into the buffer, it wakes up
the sleeping consumer.
Coding
#include<stdio.h>
int main()
{
int buffer[4], bufsize=4, in=0, out=0, produce, consume, choice=0;
while (choice!=3)
{
printf("\n 1. Produce \n 2. Consume \n 3. Exit");
printf("Enter your Choice");
scanf("%d", &choice);
switch(choice)
{
case 1:
if((in+1)%bufsize==out)
printf("\n Buffer is full");
else
{
printf("Enter the Data produced by producer");
scanf("%d", &produce);
buffer[in]=produce;
in=(in+1)%bufsize;
}
break;
case 2:
if(in==out)
printf("\n Buffer is empty");
else
{
consume=buffer[out];
printf("Consumed element is %d", consume);
out=(out+1)%bufsize;
break;
}
}
}
return 0;
}
Output:
1. Produce
2. Consume
3. ExitEnter your Choice1
Enter the Data produced by producer1
1. Produce
2. Consume
3. ExitEnter your Choice1
Enter the Data produced by producer2
1. Produce
2. Consume
3. ExitEnter your Choice1
Enter the Data produced by producer3
1. Produce
2. Consume
3. ExitEnter your Choice1
Buffer is full
1. Produce
2. Consume
3. ExitEnter your Choice2
Consumed element is 1
1. Produce
2. Consume
3. ExitEnter your Choice2
Consumed element is 2
1. Produce
2. Consume
3. ExitEnter your Choice2
Consumed element is 3
1. Produce
2. Consume
3. ExitEnter your Choice2
Buffer is empty
1. Produce
2. Consume
3. ExitEnter your Choice1
Enter the Data produced by producer1
1. Produce
2. Consume
3. ExitEnter your Choice2
Consumed element is 1
1. Produce
2. Consume
3. ExitEnter your Choice2
Buffer is empty
1. Produce
2. Consume
3. ExitEnter your Choice1
Enter the Data produced by producer2
1. Produce
2. Consume
3. ExitEnter your Choice1
Enter the Data produced by producer3
1. Produce
2. Consume
3. ExitEnter your Choice1
Enter the Data produced by producer4
1. Produce
2. Consume
3. ExitEnter your Choice1
Buffer is full
1. Produce
2. Consume
3. ExitEnter your Choice2
Consumed element is 2
1. Produce
2. Consume
3. ExitEnter your Choice2
Consumed element is 3
1. Produce
2. Consume
3. ExitEnter your Choice2
Consumed element is 4
1. Produce
2. Consume
3. ExitEnter your Choice
OUTPUT:
1. Producer
2.Consumer
3.Exit
Enter your choice:2
Buffer is empty!!
Enter your choice :1
Producer Produces item 1
Enter your choice :1
Producer Produces item 2
Enter your choice :1
Producer Produces item 3
Enter your choice :2
Consumer consumes item 3
Enter your choice :1
Consumer consumes item 3
Enter your choice :2
Consumer consumes item 3
Enter your choice :2
Consumer consumes item 2
Enter yur choice :2
Consumer consumes item 1
Enter your choice :2
Buffer is empty!!
Enter your choice :3
Algorithm:
STEP 1:Start the process.
STEP 2:As the processes enter the system, they must predict the maximum
number of resources needed which is not impractical to determine.
STEP 3:In this algorithm, the number of processes remain fixed which is not
possible in interactive systems.
· STEP 4: This algorithm requires that there should be a fixed
number of resources to allocate. If a device breaks and becomes suddenly
unavailable the algorithm would not work.
·
· STEP 5:Overhead cost incurred by the algorithm can be high
when there are many processes and resources because it has to be invoked
for every processes.
Coding
#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the Process names here
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:
following is the SAFE sequence
P1 -> P3 -> P4 -> P0 -> P2
Aim:
Algorithm:
STEP 1:Start the process.
STEP 2: Read all the necessary input from the keyboard.
Coding
#include<stdio.h>
#include<conio.h>
void main()
{
int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;
int s[10], fno[10][20];
clrscr();
printf("\nEnter the memory size -- ");
scanf("%d",&ms);
printf("\nEnter the page size -- ");
scanf("%d",&ps);
nop = ms/ps;
printf("\nThe no. of pages available in memory are -- %d ",nop);
printf("\nEnter number of processes -- ");
scanf("%d",&np);
rempages = nop;
for(i=1;i<=np;i++)
{
printf("\nEnter no. of pages required for p[%d]-- ",i);
scanf("%d",&s[i]);
if(s[i] >rempages)
{
printf("\nMemory is Full");
break;
}
rempages = rempages - s[i];
printf("\nEnter pagetable for p[%d] --- ",i);
for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);
}
printf("\nEnter Logical Address to find Physical Address ");
printf("\nEnter process no. and pagenumber and offset -- ");
scanf("%d %d %d",&x,&y, &offset);
if(x>np || y>=s[i] || offset>=ps)
printf("\nInvalid Process or Page Number or offset");
else
{
pa=fno[x][y]*ps+offset;
printf("\nThe Physical Address is -- %d",pa);
}
getch();
}
output:
Enter the memory size – 1000
Enter the page size -- 100
The no. of pages available in memory are -- 10
Enter number of processes -- 3
Enter no. of pages required for p[1]-- 4
Enter pagetable for p[1] --- 8 6
9
5
Enter no. of pages required for p[2]-- 5
Enter pagetable for p[2] --- 1 4 5 7 3
Enter no. of pages required for p[3]-- 5
Memory is Full
Enter Logical Address to find Physical Address Enter process no. and
pagenumber and offset -- 2
3
60
The Physical Address is -- 760
9. WRITE A C PROGRAM FOR FIRST FIT MEMORY
MANAGEMENT TECHNIQUE
Aim:
Algorithm:
STEP 4: Now Display the processes with blocks and allocate to respective
process.
Algorithm:
STEP 1: Start the process.
STEP 2:Let Request array represents an array storing indexes of tracks that
have been requested in ascending order of their time of arrival. ‘head’ is the
position of disk head.
STEP 3:Let us one by one take the tracks in default order and calculate the
absolute distance of the track from the head.
STEP 6:Go to step 2 until all tracks in request array have not been serviced.
#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;
}
output:
Enter the number of Requests
5
Enter the Requests Sequence
113
213
222
12
123
Enter initial hrad position
113
total head moment is 330