Skip to content

Commit 5c3985d

Browse files
committed
[OS] Added Scheduling Algorithms
1 parent 8e230fd commit 5c3985d

File tree

10 files changed

+549
-128
lines changed

10 files changed

+549
-128
lines changed

DBMS/Lab-2/Week-1.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ mysql> select * from Student;
308308
-- | 11 | Jacky | Dacky | Czech Republic | 1995-01-09 | 30 | 987261 | 94 |
309309
-- +--------------------+-----------+----------+----------------+------------+------+--------------+-------+
310310

311-
--Question15
311+
**--Question15
312312
mysql> alter table Student Change DoB DOB date;
313313
Query OK, 0 rows affected (0.50 sec)
314314
Records: 0 Duplicates: 0 Warnings: 0
@@ -352,6 +352,6 @@ Query OK, 10 rows affected (0.05 sec)
352352
mysql> select * from Student;
353353
Empty set (0.00 sec)
354354

355-
--Question18
355+
**--Question18
356356
mysql> drop table Student;
357357
Query OK, 0 rows affected (0.17 sec)
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,56 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
3-
4-
#define N 100
5-
struct process{
6-
int waiting_time;
7-
int turn_around_time;
8-
int pid;
9-
int arrival_time;
10-
int burst_time;
11-
};
12-
13-
struct process p[N];
14-
int n;
15-
void sort()
16-
{
17-
//Sort according to arrival time
18-
int i,j;
19-
for(i=0;i<n;i++)
20-
{
21-
for(j=i+1;j<n;j++)
22-
{
23-
if(p[i].arrival_time > p[j].arrival_time)
24-
{
25-
int temp_pid=p[i].pid;
26-
int temp_arrival_time=p[i].arrival_time;
27-
int temp_burst_time=p[i].burst_time;
28-
p[i].pid = p[j].pid;
29-
p[i].arrival_time = p[j].arrival_time;
30-
p[i].burst_time = p[j].burst_time;
31-
p[j].pid = temp_pid;
32-
p[j].arrival_time = temp_arrival_time;
33-
p[j].burst_time = temp_burst_time;
34-
}
35-
}
36-
}
37-
}
38-
void FCFS()
39-
{
40-
int i,j;
41-
//First assign waiting_time[p0] = 0
42-
p[0].waiting_time = 0;
43-
//Next, lets calculate wt[i] = summation of bt[0...i-1] - at[i]
44-
for(i=1;i<n;i++)
45-
{ int temp_summation=0;
46-
for(j=0;j<i;j++)
47-
{
48-
temp_summation+=p[j].burst_time;
49-
}
50-
p[i].waiting_time = temp_summation - p[i].arrival_time;
51-
}
52-
//Next, TAT = WT + BT for each process
53-
for(i=0;i<n;i++)
54-
{
55-
p[i].turn_around_time = p[i].waiting_time+p[i].burst_time;
56-
}
57-
58-
}
1+
#include<stdio.h>
592
int main()
603
{
61-
int i,j;
62-
printf("Enter the number of Processes\n");
63-
scanf("%d", &n);
64-
printf("Enter the arrival time of all the processes:\n");
65-
for(i=0;i<n;i++)
66-
{
67-
scanf("%d", &p[i].arrival_time);
68-
p[i].pid = i+1;
69-
}
70-
printf("Enter the burst time of all the processes:\n");
71-
for(i=0;i<n;i++)
72-
{
73-
scanf("%d", &p[i].burst_time);
74-
//p[i].pid = i+1;
75-
}
76-
sort();
77-
FCFS();
78-
printf("\n");
79-
printf("\n Process \t Arrival Time \t Burst Time \t Waiting Time \t Turn Around Time\n");
80-
for(i=0;i<n;i++)
81-
{
82-
printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d\n", p[i].pid ,p[i].arrival_time, p[i].burst_time, p[i].waiting_time, p[i].turn_around_time);
83-
}
84-
printf("\nNumber of Context switches is %d\n", n-1);
85-
int total_end_time = p[n-1].waiting_time + p[n-1].burst_time+p[n-1].arrival_time;
86-
int total_burst_time=0;
87-
for(i=0;i<n;i++)
88-
{
89-
total_burst_time+=p[i].burst_time;
90-
}
91-
int cpu_idle_time = total_end_time-total_burst_time;
92-
printf("Total CPU Idle Time is %d\n", cpu_idle_time);
4+
int n,at[10],bt[10],rt[10],wt[10],tat[10];
5+
printf("Enter no. of processes\n");
6+
scanf("%d",&n);
7+
for(int i=0;i<n;i++)
8+
{
9+
printf("Enter arrival time process %d:",i+1);
10+
scanf("%d",&at[i]);
11+
printf("Enter burst time process %d:",i+1);
12+
scanf("%d",&bt[i]);
13+
//rt[i]=bt[i];
14+
}
15+
int time=0;
16+
int idt=0;
17+
int endtime=0;
18+
int remain=0;
19+
int finished[10]={0};
20+
while(remain!=n)
21+
{
22+
int smallarr=99999;
23+
int smallest;
24+
for(int i=0;i<n;i++)
25+
{
26+
if(at[i]<=smallarr && finished[i]==0)
27+
{
28+
smallarr=at[i];
29+
smallest=i;
30+
}
31+
}
32+
if(smallarr > time)
33+
{
34+
idt++;
35+
time+=1;
36+
}
37+
else
38+
{
39+
printf("Process %d from %d to %d\n",smallest+1,time,time+bt[smallest]);
40+
remain++;
41+
finished[smallest]=1;
42+
endtime=time+bt[smallest];
43+
tat[smallest]=endtime-at[smallest];
44+
wt[smallest]=endtime-at[smallest]-bt[smallest];
45+
time+=bt[smallest];
46+
}
9347

94-
}
48+
}
49+
printf("\n\nProcess\tTurnaround Time\tWaiting Time\n\n");
50+
for(int i=0;i<n;i++)
51+
{
52+
printf("%d\t%d\t%d\n",i+1,tat[i],wt[i]);
53+
}
54+
printf("CPU Idle Time is %d\n",idt);
55+
printf("Number of Context Swiches is %d\n",n-1);
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#define N 100
5+
struct process{
6+
int waiting_time;
7+
int turn_around_time;
8+
int pid;
9+
int arrival_time;
10+
int burst_time;
11+
};
12+
13+
struct process p[N];
14+
int n;
15+
void sort()
16+
{
17+
//Sort according to arrival time
18+
int i,j;
19+
for(i=0;i<n;i++)
20+
{
21+
for(j=i+1;j<n;j++)
22+
{
23+
if(p[i].arrival_time > p[j].arrival_time)
24+
{
25+
int temp_pid=p[i].pid;
26+
int temp_arrival_time=p[i].arrival_time;
27+
int temp_burst_time=p[i].burst_time;
28+
p[i].pid = p[j].pid;
29+
p[i].arrival_time = p[j].arrival_time;
30+
p[i].burst_time = p[j].burst_time;
31+
p[j].pid = temp_pid;
32+
p[j].arrival_time = temp_arrival_time;
33+
p[j].burst_time = temp_burst_time;
34+
}
35+
}
36+
}
37+
}
38+
void FCFS()
39+
{
40+
int i,j;
41+
//First assign waiting_time[p0] = 0
42+
p[0].waiting_time = 0;
43+
//Next, lets calculate wt[i] = summation of bt[0...i-1] - at[i]
44+
for(i=1;i<n;i++)
45+
{ int temp_summation=0;
46+
for(j=0;j<i;j++)
47+
{
48+
temp_summation+=p[j].burst_time;
49+
}
50+
p[i].waiting_time = temp_summation - p[i].arrival_time;
51+
}
52+
//Next, TAT = WT + BT for each process
53+
for(i=0;i<n;i++)
54+
{
55+
p[i].turn_around_time = p[i].waiting_time+p[i].burst_time;
56+
}
57+
58+
}
59+
int main()
60+
{
61+
int i,j;
62+
printf("Enter the number of Processes\n");
63+
scanf("%d", &n);
64+
printf("Enter the arrival time of all the processes:\n");
65+
for(i=0;i<n;i++)
66+
{
67+
scanf("%d", &p[i].arrival_time);
68+
p[i].pid = i+1;
69+
}
70+
printf("Enter the burst time of all the processes:\n");
71+
for(i=0;i<n;i++)
72+
{
73+
scanf("%d", &p[i].burst_time);
74+
//p[i].pid = i+1;
75+
}
76+
sort();
77+
FCFS();
78+
printf("\n");
79+
printf("\n Process \t Arrival Time \t Burst Time \t Waiting Time \t Turn Around Time\n");
80+
for(i=0;i<n;i++)
81+
{
82+
printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d\n", p[i].pid ,p[i].arrival_time, p[i].burst_time, p[i].waiting_time, p[i].turn_around_time);
83+
}
84+
printf("\nNumber of Context switches is %d\n", n-1);
85+
int total_end_time = p[n-1].waiting_time + p[n-1].burst_time+p[n-1].arrival_time;
86+
int total_burst_time=0;
87+
for(i=0;i<n;i++)
88+
{
89+
total_burst_time+=p[i].burst_time;
90+
}
91+
int cpu_idle_time = total_end_time-total_burst_time;
92+
printf("Total CPU Idle Time is %d\n", cpu_idle_time);
93+
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include<stdio.h>
2+
int main()
3+
{
4+
int n,at[10],bt[10],rt[10],wt[10],tat[10];
5+
printf("Enter no. of processes\n");
6+
scanf("%d",&n);
7+
for(int i=0;i<n;i++)
8+
{
9+
printf("Enter arrival time process %d:",i+1);
10+
scanf("%d",&at[i]);
11+
printf("Enter burst time process %d:",i+1);
12+
scanf("%d",&bt[i]);
13+
rt[i]=bt[i];
14+
}
15+
rt[9]=-1;
16+
int time=0;
17+
int idt=0;
18+
int endtime=0;
19+
int remain=0;
20+
int cntx=0;
21+
int prev;
22+
while(remain!=n)
23+
{
24+
int largest=9;
25+
for(int i=0;i<n;i++)
26+
{
27+
if(at[i]<=time && rt[i]>rt[largest] && rt[i]>0)
28+
{
29+
largest=i;
30+
}
31+
}
32+
if(time>=1 && rt[largest]==rt[prev])
33+
largest=prev;
34+
if(time>=1 && largest!=prev)
35+
cntx++;
36+
prev=largest;
37+
if(largest==9)
38+
idt++;
39+
else
40+
{
41+
rt[largest]--;
42+
printf("Process %d from %d to %d\n",largest+1,time,time+1);
43+
if(rt[largest]==0)
44+
{
45+
remain++;
46+
endtime=time+1;
47+
tat[largest]=endtime-at[largest];
48+
wt[largest]=endtime-at[largest]-bt[largest];
49+
}
50+
}
51+
time+=1;
52+
}
53+
printf("\n\nProcess\tTurnaround Time\tWaiting Time\n\n");
54+
for(int i=0;i<n;i++)
55+
{
56+
printf("%d\t%d\t%d\n",i+1,tat[i],wt[i]);
57+
}
58+
printf("CPU Idle Time is %d\n",idt);
59+
printf("Number of Context Swiches is %d\n",cntx);
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include<stdio.h>
2+
int main()
3+
{
4+
int n,at[10],bt[10],rt[10],wt[10],tat[10],prio[10];
5+
printf("Enter no. of processes\n");
6+
scanf("%d",&n);
7+
for(int i=0;i<n;i++)
8+
{
9+
printf("Enter arrival time of process %d:",i+1);
10+
scanf("%d",&at[i]);
11+
printf("Enter burst time of process %d:",i+1);
12+
scanf("%d",&bt[i]);
13+
printf("Enter priority of process %d:",i+1);
14+
scanf("%d",&prio[i]);
15+
}
16+
prio[9]=9999;
17+
int time=0;
18+
int idt=0;
19+
int endtime=0;
20+
int remain=0;
21+
int finished[10]={0};
22+
while(remain!=n)
23+
{
24+
25+
int smallest=9;
26+
for(int i=0;i<n;i++)
27+
{
28+
if(at[i]<=time && prio[i]<prio[smallest] && finished[i]==0)
29+
{
30+
smallest=i;
31+
}
32+
}
33+
if(smallest==9)
34+
{
35+
idt++;
36+
time+=1;
37+
}
38+
else
39+
{
40+
printf("Process %d from %d to %d\n",smallest+1,time,time+bt[smallest]);
41+
remain++;
42+
finished[smallest]=1;
43+
endtime=time+bt[smallest];
44+
tat[smallest]=endtime-at[smallest];
45+
wt[smallest]=endtime-at[smallest]-bt[smallest];
46+
time+=bt[smallest];
47+
}
48+
49+
}
50+
printf("\n\nProcess\tTurnaround Time\tWaiting Time\n\n");
51+
for(int i=0;i<n;i++)
52+
{
53+
printf("%d\t%d\t%d\n",i+1,tat[i],wt[i]);
54+
}
55+
printf("CPU Idle Time is %d\n",idt);
56+
printf("Number of Context Swiches is %d\n",n-1);
57+
}

0 commit comments

Comments
 (0)