Skip to content

Commit 74da732

Browse files
author
=
committed
Preemptive coding aded
1 parent 944b101 commit 74da732

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

Lab4/a.out

8.59 KB
Binary file not shown.

Lab4/activityselection.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include<stdio.h>
2+
#include<time.h>
3+
int main()
4+
{
5+
int n,i,j,temp1,temp2,temp3,lastindex;
6+
clock_t s,end;
7+
float t;
8+
printf("Enter the number of activities to be performed: ");
9+
scanf("%d",&n);
10+
int anum[n],start[n],final[n];
11+
for(i=0;i<n;i++)
12+
{
13+
printf("Enter the Activity number, start time and final time for %d process\n",i+1);
14+
scanf("%d",&anum[i]);
15+
scanf("%d",&start[i]);
16+
scanf("%d",&final[i]);
17+
}
18+
s=clock();
19+
for(i=0;i<n-1;i++)
20+
{
21+
for(j=i+1;j<n;j++)
22+
{
23+
if(final[j]<final[i])
24+
{
25+
temp1 = final[i];
26+
final[i] = final[j];
27+
final[j] = temp1;
28+
temp2= anum[i];
29+
anum[i]=anum[j];
30+
anum[j]=temp2;
31+
temp3=start[i];
32+
start[i]=start[j];
33+
start[j]=temp3;
34+
}
35+
}
36+
}
37+
//sorted successfully
38+
printf("Activities sorted by final time are:\n");
39+
for(i=0;i<n;i++)
40+
{
41+
printf("%d\t%d\t%d\n",anum[i],start[i],final[i]);
42+
}
43+
printf("%d Activity is performed\n",anum[0]);
44+
lastindex=0;
45+
for(i=1;i<n;i++)
46+
{
47+
if(start[i]>=final[lastindex])
48+
{
49+
printf("%d Activity is performed\n",anum[i]);
50+
lastindex=i;
51+
}
52+
}
53+
end=clock();
54+
t=(float)(end-s)/CLOCKS_PER_SEC;
55+
printf("Time taken for processing is: %f\n",t);
56+
return 0;
57+
}

Lab4/preemptivepriorityscheduling.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include<stdio.h>
2+
int main ()
3+
{
4+
int n,i,j,temp;
5+
printf("Enter the number of processes: ");
6+
scanf("%d",&n);
7+
int pnum[n],start[n],burst[n],turn[n];
8+
for(i=0;i<n;i++)
9+
{
10+
printf("\n\nEnter the Details of %d process\n",i+1);
11+
printf("Enter the process number: ");
12+
scanf("%d",&pnum[i]);
13+
printf("Enter the start time: ");
14+
scanf("%d",&start[i]);
15+
printf("Enter the end time: ");
16+
scanf("%d",&burst[i]);
17+
turn[i]=0;
18+
}
19+
printf("Sorting by the start time\n");
20+
for(i=0;i<n;i++)
21+
{
22+
for(j=i+1;j<n;j++)
23+
{
24+
if(start[j]<start[i])
25+
{
26+
temp=start[j];
27+
start[j]=start[i];
28+
start[i]=temp;
29+
temp=pnum[i];
30+
pnum[i]=pnum[j];
31+
pnum[j]=temp;
32+
temp=burst[i];
33+
burst[i]=burst[j];
34+
burst[j]=temp;
35+
}
36+
}
37+
}
38+
printf("Printing the sorted process\n");
39+
printf("Process number Start time End time\n");
40+
for(i=0;i<n;i++)
41+
{
42+
printf("%d %d %d\n",pnum[i],start[i],burst[i]);
43+
}
44+
//////////////////////////////////////////
45+
46+
//////////////////////////////////////////
47+
return 0;
48+
}

0 commit comments

Comments
 (0)