Skip to content

Commit d2c6cda

Browse files
authored
Merge pull request #271 from dk172923/main
Added programs for Operating systems
2 parents 4ba0cee + 9f9eaf0 commit d2c6cda

File tree

10 files changed

+503
-0
lines changed

10 files changed

+503
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <stdio.h>
2+
int main()
3+
{
4+
char pn[10][10];
5+
int arr[10], bur[10], star[10], finish[10], tat[10], wt[10], i, n;
6+
int totwt=0, tottat=0;
7+
printf("Enter the number of processes: ");
8+
scanf("%d",&n);
9+
for(i=0;i<n;i++)
10+
{
11+
printf("Enter the Process Name, Arrival Time & Burst Time: ");
12+
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
13+
}
14+
for(i=0;i<n;i++) {
15+
if(i==0)
16+
{
17+
star[i]=arr[i];
18+
wt[i]=star[i]-arr[i];
19+
finish[i]=star[i]+bur[i];
20+
tat[i]=finish[i]-arr[i];
21+
}
22+
else
23+
{
24+
star[i]=finish[i-1];
25+
wt[i]=star[i]-arr[i];
26+
finish[i]=star[i]+bur[i];
27+
tat[i]=finish[i]-arr[i];
28+
}
29+
}
30+
printf("\nPName\tArrtime\t\tBurtime\t\t Start\t\t TAT\t\tFinish");
31+
for(i=0;i<n;i++)
32+
{
33+
printf("\n%s\t%6d\t\t%6d\t\t%6d\t\t%6d\t\t%6d",pn[i],arr[i],bur[i],star[i],tat[i],finish[i]);
34+
totwt+=wt[i];
35+
tottat+=tat[i];
36+
}
37+
printf("\nAverage Waiting time:%f", (float)totwt/3);
38+
printf("\nAverage Turn Around Time:%f\n", (float)tottat/3);
39+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#define MAX_PROCESSES 10
4+
typedef struct
5+
{
6+
int pid; // process ID
7+
int burst_time; // burst time (in milliseconds)
8+
int priority; // priority (higher number means higher priority)
9+
int waiting_time; // waiting time (in milliseconds)
10+
int turnaround_time; // turnaround time (in milliseconds)
11+
} Process;
12+
int main()
13+
{
14+
Process processes [MAX_PROCESSES];
15+
int num_processes, i, j;
16+
printf("Enter the number of processes: ");
17+
scanf("%d", &num_processes);
18+
for (i = 0; i < num_processes; i++) {
19+
printf("\nEnter the burst time for process %d (in milliseconds): ", i+1);
20+
scanf("%d", &processes[i].burst_time);
21+
printf("Enter the priority for process %d (higher number means higher priority): ", i+1);
22+
scanf("%d", &processes[i].priority);
23+
processes[i].pid = i+1;
24+
}
25+
for (i = 0; i < num_processes - 1; i++)
26+
{
27+
for (j = i+1; j < num_processes; j++)
28+
{
29+
if (processes[i].priority < processes[j].priority)
30+
{
31+
Process temp = processes[i];
32+
processes[i] = processes[j];
33+
processes[j] = temp;
34+
}
35+
}
36+
}
37+
int total_waiting_time = 0, total_turnaround_time = 0;
38+
for (i = 0; i < num_processes; i++)
39+
{
40+
if (i == 0)
41+
{
42+
processes[i].waiting_time = 0;
43+
}
44+
else
45+
{
46+
processes[i].waiting_time = processes[i-1].turnaround_time;
47+
}
48+
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
49+
total_waiting_time += processes[i].waiting_time;
50+
total_turnaround_time += processes[i].turnaround_time;
51+
}
52+
printf("\nPID\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n");
53+
for (i = 0; i < num_processes; i++)
54+
{
55+
printf("%d\t%d ms\t\t%d\t\t%d ms\t\t%d ms\n", processes[i].pid, processes[i].burst_time, processes[i].priority, processes[i].waiting_time, processes[i].turnaround_time);
56+
}
57+
printf("\nAverage waiting time: %.2f ms", (float)total_waiting_time/num_processes);
58+
printf("\nAverage turnaround time: %.2f ms\n",
59+
(float)total_turnaround_time/num_processes);
60+
return 0;
61+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include<stdio.h>
2+
struct process {
3+
int burst, wait, comp, f;
4+
} p[20]={0,0};
5+
int main( ) {
6+
int n, i, j, totalwait=0, totalturn=0, quantum,flag=1, time=0;
7+
printf("\nEnter the no. of processes: ");
8+
scanf("%d",&n);
9+
printf("\nEnter the Quantum time (in ms) : ");
10+
scanf("%d",&quantum);
11+
for(i=0;i<n;i++) {
12+
printf("Enter the Burst Time (in ms) For Process #%2d : ",i+1);
13+
scanf("%d",&p[i].burst);
14+
p[i].f=1;
15+
}
16+
printf("\nOrder of Execution \n");
17+
printf("\nProcess Starting Ending Remaining");
18+
printf("\n\t\tTime\t Time \t\t Time");
19+
while(flag==1)
20+
{
21+
flag=0;
22+
for(i=0;i<n;i++)
23+
{
24+
if(p[i].f==1)
25+
{
26+
flag=1;
27+
j=quantum;
28+
if((p[i].burst-p[i].comp)>quantum)
29+
{
30+
p[i].comp+=quantum;
31+
}
32+
else
33+
{
34+
p[i].wait=time-p[i].comp;
35+
j=p[i].burst-p[i].comp;
36+
p[i].comp=p[i].burst;
37+
p[i].f=0;
38+
}
39+
printf("\nProcess # %-3d %-10d %-10d %-10d", i+1, time,
40+
time+j, p[i].burst-p[i].comp);
41+
time+=j;
42+
}
43+
}
44+
}
45+
printf("\n\n--------------------");
46+
printf("\nProcess \t Waiting Time TurnAround Time ");
47+
for(i=0;i<n;i++)
48+
{
49+
printf("\nProcess # %-12d%-15d%-15d",i+1,p[i].wait,p[i].wait+p[i].burst);
50+
totalwait=totalwait+p[i].wait;
51+
totalturn=totalturn+p[i].wait+p[i].burst;
52+
}
53+
printf("\n\nAverage\n-------------------");
54+
printf("\nWaiting Time: %fms",totalwait/(float)n);
55+
printf("\nTurnAround Time : %fms\n\n",totalturn/(float)n);
56+
return 0;
57+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include<stdio.h>
2+
#include<string.h>
3+
int main()
4+
{
5+
int i=0, pno[10], bt[10], n, wt[10], temp=0, j, tt[10];
6+
float sum,at;
7+
printf("\nEnter the no. of process: ");
8+
scanf("\n %d",&n);
9+
printf("\nEnter the burst time of each process: \n");
10+
for(i=0;i<n;i++)
11+
{
12+
printf("P%d ",i);
13+
scanf("%d",&bt[i]);
14+
}
15+
for(i=0;i<n-1;i++)
16+
{
17+
for(j=i+1;j<n;j++)
18+
{
19+
if(bt[i]>bt[j])
20+
{
21+
temp=bt[i];
22+
bt[i]=bt[j];
23+
bt[j]=temp;
24+
temp=pno[i];
25+
pno[i]=pno[j];
26+
pno[j]=temp;
27+
}
28+
}
29+
}
30+
wt[0]=0;
31+
for(i=1;i<n;i++)
32+
{
33+
wt[i]=bt[i-1]+wt[i-1];
34+
sum=sum+wt[i];
35+
}
36+
printf("\nProcess No \t Burst Time\t Waiting time \t Turn around time\n");
37+
for(i=0;i<n;i++)
38+
{
39+
tt[i]=bt[i]+wt[i];
40+
at+=tt[i];
41+
printf("\n P%d\t\t %d\t\t %d\t\t %d",i,bt[i],wt[i],tt[i]);
42+
}
43+
printf("\n\nAverage waiting time: %f\nAverage turn around time: %f\n", sum/n, at/n);
44+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include<stdio.h>
2+
void main() {
3+
int a[20], p[20], i, j, n, m, temp, b[20], temp1, temp2, c[20];
4+
printf("\nMEMORY MANAGEMENT SCHEME - BEST FIT \n");
5+
printf("Enter No. of Blocks: ");
6+
scanf("%d",&n);
7+
for(i=0;i<n;i++) {
8+
printf("Enter the %dst block size: ",i);
9+
scanf("%d",&a[i]);
10+
}
11+
printf("Enter No. of Process: ");
12+
scanf("%d",&m);
13+
for(i=0;i<m;i++)
14+
{
15+
printf("Enter the size of %dst process: ",i);
16+
scanf("%d",&p[i]);
17+
}
18+
for(i=0;i<n;i++)
19+
{
20+
for(j=0;j<m;j++)
21+
{
22+
if(a[i]>a[j])
23+
{
24+
temp=a[i];
25+
a[i]=a[j];
26+
a[j]=temp;
27+
}
28+
if(p[i]>p[j])
29+
{
30+
temp=p[i];
31+
p[i]=p[j];
32+
p[j]=temp;
33+
}
34+
}
35+
}
36+
printf("\nProcess\tBlock Size\n");
37+
for(i=0;i<n;i++)
38+
printf("%d\t%d\n",p[i],a[i]);
39+
printf("\n\n");
40+
for(i=0;i<n;i++) {
41+
for(j=0;j<m;j++) {
42+
if(p[j]<=a[i]) {
43+
printf("The process %d [size %d] allocated to block %d\n", j, p[j], a[i]);
44+
p[j]=10000;
45+
break;
46+
}
47+
}
48+
}
49+
for(j=0;j<m;j++)
50+
if(p[j]!=10000)
51+
printf("The process %d is not allocated\n",p[j]);
52+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<stdio.h>
2+
void main()
3+
{
4+
int a[20], p[20], i, j, n, m, temp, b[20], temp1, temp2, c[20];
5+
printf("\nMEMORY MANAGEMENT SCHEME - FIRST FIT \n");
6+
printf("Enter No. of Blocks: ");
7+
scanf("%d",&n);
8+
for(i=0;i<n;i++)
9+
{
10+
printf("Enter the %dst block size: ",i);
11+
scanf("%d",&a[i]);
12+
}
13+
printf("Enter No. of Process: ");
14+
scanf("%d",&m);
15+
for(i=0;i<m;i++)
16+
{
17+
printf("Enter the size of %dst process: ",i);
18+
scanf("%d",&p[i]);
19+
}
20+
printf("\nProcess\tBlock Size\n");
21+
for(i=0;i<n;i++)
22+
printf("%d\t\t%d\n",p[i],a[i]);
23+
printf("\n\n");
24+
for(i=0;i<n;i++)
25+
{
26+
for(j=0;j<m;j++)
27+
{
28+
if(p[j]<=a[i])
29+
{
30+
printf("The process %d [size %d] allocated to block %d\n",j,p[j],a[i]);
31+
p[j]=10000; break;
32+
}
33+
}
34+
}
35+
for(j=0;j<m;j++)
36+
if(p[j]!=10000)
37+
printf("The process %d is not allocated\n",j);
38+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include<stdio.h>
2+
void main()
3+
{
4+
int a[20], p[20], i, j, n, m, temp, b[20], temp1, temp2, c[20];
5+
printf("\nMEMORY MANAGEMENT SCHEME - WORST FIT \n");
6+
printf("Enter No. of Blocks: ");
7+
scanf("%d",&n);
8+
for(i=0;i<n;i++)
9+
{
10+
printf("Enter the %dst block size: ",i);
11+
scanf("%d",&a[i]);
12+
}
13+
printf("Enter No. of Process: ");
14+
scanf("%d",&m);
15+
for(i=0;i<m;i++)
16+
{
17+
printf("Enter the size of %dst process: ",i);
18+
scanf("%d",&p[i]);
19+
}
20+
for(i=0;i<n;i++)
21+
{
22+
for(j=0;j<m;j++)
23+
{
24+
if(a[i]>a[j])
25+
{
26+
temp=a[i];
27+
a[i]=a[j];
28+
a[j]=temp;
29+
}
30+
if(p[i]<p[j])
31+
{
32+
temp=p[i];
33+
p[i]=p[j];
34+
p[j]=temp;
35+
}
36+
}
37+
}
38+
printf("\nProcess\tBlock Size\n");
39+
for(i=0;i<n;i++)
40+
printf("%d\t%d\n",p[i],a[i]);
41+
printf("\n\n");
42+
for(i=0;i<n;i++) {
43+
for(j=0;j<m;j++) {
44+
if(p[j]<=a[i]) {
45+
printf("The process %d [size %d] allocated to block %d\n",j,p[j],a[i]);
46+
p[j]=10000;
47+
break;
48+
}
49+
}
50+
}
51+
for(j=0;j<m;j++)
52+
if(p[j]!=10000)
53+
printf("The process %d is not allocated\n",p[j]);
54+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include<stdio.h>
2+
void main() {
3+
int a[5], b[20], n, p=0, q=0, m=0, h, k, i, q1=1;
4+
char f='F';
5+
printf("Enter the Number of Pages: ");
6+
scanf("%d",&n);
7+
printf("Enter %d Page Numbers: ",n);
8+
for(i=0;i<n;i++)
9+
scanf("%d",&b[i]);
10+
for(i=0;i<n;i++) {
11+
if(p==0) {
12+
if(q>=3)
13+
q=0;
14+
a[q]=b[i]; q++;
15+
if(q1<3) {
16+
q1=q;
17+
}
18+
}
19+
printf("\n%d",b[i]);
20+
printf("\t");
21+
for(h=0;h<q1;h++)
22+
printf("%d",a[h]);
23+
if((p==0)&&(q<=3)) {
24+
printf("-->%c",f);
25+
m++;
26+
}
27+
p=0;
28+
for(k=0;k<q1;k++) {
29+
if(b[i+1]==a[k])
30+
p=1;
31+
}
32+
}
33+
printf("\nNo of faults: %d\n",m);
34+
}

0 commit comments

Comments
 (0)