Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views29 pages

OS (Operating System) File

The document is a practical file for the Operating Systems Lab submitted by Lakshraj, detailing various scheduling algorithms implemented in C programming. It includes programs for FCFS, SJF, Round Robin, Priority Scheduling, and Deadlock detection using resource allocation graphs, along with their respective codes and outputs. The document is affiliated with the Panipat Institute of Engineering & Technology and Kurukshetra University for the academic year 2022-2023.

Uploaded by

Lv Chn
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)
21 views29 pages

OS (Operating System) File

The document is a practical file for the Operating Systems Lab submitted by Lakshraj, detailing various scheduling algorithms implemented in C programming. It includes programs for FCFS, SJF, Round Robin, Priority Scheduling, and Deadlock detection using resource allocation graphs, along with their respective codes and outputs. The document is affiliated with the Panipat Institute of Engineering & Technology and Kurukshetra University for the academic year 2022-2023.

Uploaded by

Lv Chn
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/ 29

Lakshraj

2821145

Panipat Institute of Engineering & Technology

Samalkha, Panipat
Computer Science & EngineeringDepartment

Practical File- OS Lab


( PC-CS212AL )
2022-2023

Submitted To:- Submitted By:-Lakshraj


Ms. Dipti Dhingra Rollno -2821145
(Asst. Professor, CSE) Group : A ( A2 )

AFFILIATED TO

KURUKSHETRA UNIVERSITY, KURUKSHETRA, INDIA

1|P a g e
Lakshraj
2821145

Program-1
Aim:- FCFS SCHEDULING
CODE:-
#include <stdio.h>
int main()
{
printf("LAKSHRAJ 2821145\n");
int pid[15];
int bt[15];
int n;
printf("Enter the number of processes: ");
scanf("%d",&n);
printf("Enter process id of all the processes: ");
for(int i=0;i<n;i++)
{
scanf("%d",&pid[i]);
}
printf("Enter burst time of all the processes: ");
for(int i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
int i, wt[n];
wt[0]=0;

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


{
wt[i]= bt[i-1]+ wt[i-1];
}

2|P a g e
Lakshraj
2821145

printf("Process ID Burst Time Waiting Time TurnAround Time\n");


float twt=0.0;
float tat= 0.0;
for(i=0; i<n; i++)
{
printf("%d\t\t", pid[i]);
printf("%d\t\t", bt[i]);
printf("%d\t\t", wt[i]);

printf("%d\t\t", bt[i]+wt[i]);
printf("\n");

twt += wt[i];

tat += (wt[i]+bt[i]);
}
float att,awt;

awt = twt/n;

att = tat/n;
printf("Avg. waiting time= %f\n",awt);
printf("Avg. turnaround time= %f",att);}

3|P a g e
Lakshraj
2821145

OUTPUT

4|P a g e
Lakshraj
2821145

Program-2
Aim:- SRJF SCHEDULING
CODE:-
#include<stdio.h>
int main()
{
printf("\n");
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("Enter Burst Time:");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
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];

5|P a g e
Lakshraj
2821145

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;
printf("\nProcess\t 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",avg_tat);
}

6|P a g e
Lakshraj
2821145

OUTPUT

7|P a g e
Lakshraj
2821145

Program-3
AIM: Round Robin scheduling

CODE:-
#include<stdio.h>
void main()
{
printf("LAKSHRAJ 2821145\n");
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];
float avg_wt, avg_tat;
printf(" Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP;

for(i=0; i<NOP; i++)


{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t");
scanf("%d", &at[i]);
printf(" \nBurst time is: \t");
scanf("%d", &bt[i]);
temp[i] = bt[i];
}

printf("Enter the Time Quantum for the process: \t");


scanf("%d", &quant);

printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )

8|P a g e
Lakshraj
2821145

{
if(temp[i] <= quant && temp[i] > 0)
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--;
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-
bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else

9|P a g e
Lakshraj
2821145

{
i=0;
}
}

avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat);

10 | P a g e
Lakshraj
2821145

OUTPUT

11 | P a g e
Lakshraj
2821145

Program-4
AIM: Priority Scheduling
CODE:-
#include <stdio.h>

void swap(int *a,int *b)


{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
printf("LAKSHRAJ 2821145\n");
int n;
printf("Enter Number of Processes: ");
scanf("%d",&n);

int burst[n],priority[n],index[n];
for(int i=0;i<n;i++)
{
printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
scanf("%d %d",&burst[i],&priority[i]);
index[i]=i+1;
}
for(int i=0;i<n;i++)
{
int temp=priority[i],m=i;

12 | P a g e
Lakshraj
2821145

for(int j=i;j<n;j++)
{
if(priority[j] > temp)
{
temp=priority[j];
m=j;
}
}

swap(&priority[i], &priority[m]);
swap(&burst[i], &burst[m]);
swap(&index[i],&index[m]);
}

int t=0;

printf("Order of process Execution is\n");


for(int i=0;i<n;i++)
{
printf("P%d is executed from %d to %d\n",index[i],t,t+burst[i]);
t+=burst[i];
}
printf("\n");
printf("Process Id\tBurst Time\tWait Time\n");
int wait_time=0;
int total_wait_time = 0;
for(int i=0;i<n;i++)
{
printf("P%d\t\t%d\t\t%d\n",index[i],burst[i],wait_time);
total_wait_time += wait_time;

13 | P a g e
Lakshraj
2821145

wait_time += burst[i];
}

float avg_wait_time = (float) total_wait_time / n;


printf("Average waiting time is %f\n", avg_wait_time);

int total_Turn_Around = 0;
for(int i=0; i < n; i++){
total_Turn_Around += burst[i];
}
float avg_Turn_Around = (float) total_Turn_Around / n;
printf("Average TurnAround Time is %f",avg_Turn_Around);
return 0;
}

14 | P a g e
Lakshraj
2821145
OUTPUT

15 | P a g e
Lakshraj
Program-5 2821145

AIM: Write a program to detect deadlock using


resource allocation graph

CODE:-
#include<bits/stdc++.h>
using namespace std;
int proc, res, i, j, flag = 0;
static int pro[3][3], req[3][3], st_req[3][3], st_pro[3][3],
avail[3]; int main() {
cout << "\nLakshraj\n";
cout << "2821145\n";
cout << "********** Resource Allocation Graph ************\n";
cout<<"\nEnter the number of Processes:";
cin>>proc;
cout<<"\nEnter the number of
Resources:";cin>>res;

cout<<"\nEnter the Process


Matrix:"; for (i = 0; i < proc; i++)
for (j = 0; j < res; j++)
cin>>pro[i][j];

cout<<"\nEnter the Request


Matrix:"; for (i = 0; i < res; i++)
for (j = 0; j < proc; j++)
cin>>req[i][j];
for (i = 0; i < res; i++) {
avail[i] = 1;
}
for (i = 0; i < proc; i++) {

16 | P a g e
Lakshraj
2821145

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


if (pro[i][j] == 1 && st_pro[i][j] == 0)
{ bool can_allocate = true;
for (int k = 0; k < res; k++) {
if (req[k][i] == 1 && st_req[k][i] == 0 && avail[k]
== 0) { can_allocate = false;
break;
}
}
if (can_allocate) {
// Allocate resource
st_pro[i][j] = 1;
avail[j] = 0;
// Release request if it was allocated
earlier st_req[j][i] = 0;
flag =
1;
break;
}
}
}
if (flag == 0) {
// Deadlock detected
cout<<"\nDeadlock
Occured";return 0;
}
flag = 0;
}
cout<<"\nNo Deadlock
Detected";return 0;
}
17 | P a g e
Lakshraj
2821145

OUTPUT

18 | P a g e
Lakshraj
2821145

Program-6
AIM: Write a program to detect deadlock using
resource allocation graph CODE:-
#include<iostream>
using namespace
std; int main() {
int n, m, count, ssq = 0;
cout << "Ish Kumar 2821050 ";
cout << "Number of processes:
";cin >> n;
cout << "Number of
resources: ";cin >> m;
int ss[n];
int max[n][m], allocation[n][m],
need[n][m]; int available[m];
bool satisfy[n], flag;
cout << "\nAllocation
graph:\n"; for (int i = 0; i < n;
i++) {
for (int j = 0; j < m; j++) {
cin >> allocation[i][j];
}
}
cout << "\nMaximum
graph:\n"; for (int i = 0; i <
n; i++) {
for (int j = 0; j < m; j++) {
cin >> max[i][j];
}
}
cout << "\nAvailable
resources:\n"; for (int i = 0; i <
m; i++) {
cin >> available[i];
}
for (int i = 0; i < n;
i++) { satisfy[i] =
false;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m;
j++) {
19 | P a g e
Lakshraj
need[i][j] = max[i][j] - allocation[i][j]; 2821145
}
}
count = n;

while (count) {
for (int i = 0; i < n; i++) {
flag = true;
for (int j = 0; j < m; j++) {
if (need[i][j] > available[j]) {
flag = false;
break;
}
}
if (flag == true && satisfy[i] ==
false) { satisfy[i] = true;
for (int k = 0; k < m; k++) {
available[k] += allocation[i][k];
}
ss[ssq++] = i;
break;
}
}
count--;
}
if (ssq == n) {
cout << "\nSafe sequence is:
"; for (int i = 0; i < n; i++) {
if (i == n - 1) {
cout << "P" << ss[i];
}
else {
cout << "P" << ss[i] << " -> ";
}
}
cout << endl;
}
else {
cout << "\nNo safe sequence found.\n";
}
return 0;
}

20 | P a g e
Lakshraj
2821145
OUTPUT

21 | P a g e
Lakshraj
2821145

Program – 7
AIM: -: TO CREATE A PROGRAM TO IMPLEMENT DISK
SCHEDULING ALGORITHM
CODE:

#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Lakshraj 2821145
\n"); int queue[20],
n,
head
, i,
j,
k,
seek =
0, max,
diff,
temp,
queue1[20
],
queue2[20
], temp1 =
0,
temp2 =
0; float
avg;
printf("Enter the max range of disk\n");scanf("%d", &
max); printf("Enter the initial head
position\n");scanf("%d", & head);printf("Enter the size of
queue request\n");scanf("%d", & n); printf("Enter the
queue of disk positions to be read\n");
for (i = 1; i <= n; i++)
{ scanf("%d", &
temp); if (temp >=
head) {
queue1[temp1] = temp;
temp1++;
} else {
queue2[temp2] = temp;
temp2++;
22 | P a g e
Lakshraj
} 2821145
}
for (i = 0; i < temp1 - 1; i++)
{ for (j = i + 1; j < temp1;
j++) { if (queue1[i] >
queue1[j]) {

temp = queue1[i];
queue1[i] =
queue1[j];queue1[j]
= temp;
}
}
}
for (i = 0; i < temp2 - 1; i++)
{ for (j = i + 1; j < temp2;
j++) { if (queue2[i] >
queue2[j]) {
temp = queue2[i];
queue2[i] = queue2[j];
queue2[j] = temp;
}
}
}
for (i = 1, j = 0; j < temp1; i++, j++) queue[i] = queue1[j];queue[i] = max;queue[i +
1] = 0; for (i = temp1 + 3, j = 0; j < temp2; i++, j++) queue[i] = queue2[j];
queue[0] = head;
for (j = 0; j <= n + 1; j++) {
diff = abs(queue[j + 1] -
queue[j]);seek += diff;
printf("Disk head moves from %d to %d with seek %d\n", queue[j], queue[j + 1],
diff);
}
printf("Total seek time is %d\n",
seek);avg = seek / (float) n;
printf("Average seek time is %f\n", avg);
return 0;
}

23 | P a g e
Lakshraj
2821145

OUTPUT

24 | P a g e
Lakshraj
2821145

Program-8

AIM:- : TO CREATE A PROGRAM TO IMPLEMENT PAGE


REPLACEMENT ALGORITHM.

CODE:

#include<stdio.h>
int main()
{
int incomingStream[]={4,1,2 , 4 , 5};int pageFaults = 0;

int frames = 3;

int m, n, s, pages;

pages=sizeof(incomingStrems)/sizeof(incomingStream[0]);

printf(" Incoming \ t Frame 1 \ t Frame 2 \ t Frame 3 ");


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(incomingStream[m] == temp[n])

{s++;pageFaults--
;}}
pageFaults++;
if((pageFaults <=
frames) && (s == 0)){
temp[m] = incomingStream[m];}else if(s
== 0){ temp[(pageFaults - 1) % frames] =
incomingStream[m];}printf("\n");
printf("%d\t\t\t",incomingStre
25 | P a g e
Lakshraj
am[m]);for(n = 0; n < frames; 2821145

{if(temp[n] != -
1)
printf("%d\t\t\t",
temp[n]);else
printf(" -
\t\t\t");}}
printf("\nTotal Page Faults:\t%d\n",
pageFaults);
return 0;}

26 | P a g e
Lakshraj
2821145
OUTPUT

27 | P a g e
Lakshraj
2821145

Program-9
AIM:- : TO CREATE A PROGRAM FOR FILE OPERATIONS.

CODE:-
#include <stdio.h> int
main() {
printf("Lakshraj 2821145\n");
FILE *fptr;
char str[100];
fptr = fopen("ABC.TXT", "r"); if
(fptr == NULL) {
printf("Error opening the file for reading.\n");
return 1;
}
while (fscanf(fptr, "%s", str) == 1) {
printf("%s ", str);
}
fclose(fptr);
fptr = fopen("ABC.TXT", "w"); if
(fptr == NULL) {
printf("Error opening the file for writing.\n");
return 1;
}
fprintf(fptr, "Code is running perfectly");
fclose(fptr);
return 0;
}

28 | P a g e
Lakshraj
2821145

OUTPUT

29 | P a g e

You might also like