0% found this document useful (0 votes)
17 views30 pages

OS Record

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)
17 views30 pages

OS Record

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/ 30

1.

Program to Get Process I'd and Path

Aim:

To write the Program to get process I'd and path in C.

Algorithm:

STEP 1:Start the process

STEP 2:Input the process I'd Path details

STEP 3:Get the result

STEP 4: Print the output

STEP 5:Stop the process

Program:

#include<stdio.h>

#include<conio.h>

#include<process.h>

int main()

char*path;

clrscr();

printf("\n process ID : %u",getpid());

path = getenv("PATH");
printf("the PATH = %s",path);

getch();

Output:

U= processID : 36298 the path = z:1

D = processID :-29238 the path= z:1

2. Program to implement first come first serve scheduling


without arrival time

1. Start the program


2. Get the number of processes and their burst time
3. Find the waiting time for each of the processes and turn around time.
4. Print the process with burst time, waiting time and turnaround time.
5. Stop the process.
Program

#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:

Enter the total number of process :4

Enter the burst time p[1]:10

p[2]: 5

p[3]:9

p[4]:15

process burst time waiting time turnaround time

p[1] 10 0 10

p[2] 5 10 15

p[3] 9 15 24

p[4] 15 24 39

Average waiting time : 12

Average turnaround time : 22

3. Program to Implement First come first serve

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

4. Program to Implement Shortest job first

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.

STEP 6 : Stop the process and execute the result.

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

5. Program to Implement Producer &Consumer Problem


6. Aim:
To write the Program to Implement Producer & Consumer Problem
in C.

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.

STEP 6:Stop the process and execute the result.

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

6. IMPLEMENTING C PROGRAM TO SOLVE PRODUCER AND


CONSUMER USING WITH SEMAPHORES

1. Start the program


2. Initialize the semaphore variable as mutex.
3. If empty is 0, then buffer is full, if full is not zero, then print buffer is
empty.
4. When producer produces, keep mutex as 0 and print the produced value,
then release the mutex as 1.
5. When consume consumes, keep mutex as 0 and print the consumed value,
then release the mutex as 1.
6. Stop the process.
Coding
include <stdio.h>
#include <stdlib.h>
int mutex = 1;
int full = 0;
int empty = 10, x = 0;
void producer()
{
--mutex;
++full;
--empty;
x++;
printf("\nProducer produces"
"item %d",
x);
++mutex;
}
void consumer()
{
--mutex;
--full;
++empty;
printf("\nConsumer consumes "
"item %d",
x);
x--;
++mutex;
}
int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");
for (i = 1; i > 0; i++) {
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;
}
}
}

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

7. WRITE A C PROGRAM TO AVOID DEADLOCK IN BANKERS


ALGORITHM
Aim:
To write a Program to Implement Banker’s Algorithm in C.

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.

STEP 6:Stop the process and execute the result.

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

8. WRITE A C PROGRAM TO IMPLEMENT THE PAGING


TECHNIQUE

Aim:

To write a Program to Implement Paging technique in C.

Algorithm:
STEP 1:Start the process.
STEP 2: Read all the necessary input from the keyboard.

STEP 3: Pages – Logical memory is broken into fixed – sized blocks.

STEP 4: Frames – Physical memory is broken into fixed – sized blocks.


STEP 5: Calculate the physical address using the following
Physical address = ( Frame number * Frame size ) + offset

STEP 6: Display the physical address.

STEP 7: Stop the process and execute the result.

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:

To write a Program to Implement First Fit Memory Management


Technique in c.

Algorithm:

STEP 1: Start the process.

STEP 2: At first get the no of processes and blocks.

STEP 3: Allocate the process by if(size of block>=size of the process) then


allocate the process else move to the next block.

STEP 4: Now Display the processes with blocks and allocate to respective
process.

STEP 5: Stop the process and execute the result.


Coding
#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");
}
}
Output:
Enter no. of blocks : 3
Enter size of each block : 100 200 300
Enter no of processes :3
Enter size of each processes : 150 250 350
Block no . size process no . size
1 100 not allocated
2 200 1 150
3 300 2 250.
10. WRITE A C PROGRAM FOR FIRST COME FIRST SERVE DISK
SCHEDULING
Aim:

To write a Program to Implement FCFS Disk Scheduling in c.

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 4:Increment the total seek count with this distance.


STEP 5:Currently serviced track position now becomes the new head
position.

STEP 6:Go to step 2 until all tracks in request array have not been serviced.

STEP 7: Stop the process and execute the result.

#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

You might also like