Os 2
Os 2
1. Simulation ofPriorityCPUSchedulingAlgorithm.
Code:-
#include<stdio.h>
#include<stdlib.h>
intmain(void)
{
intno,temp,tot;
inti,j,jobs,pt[10],st[10],et[10],wt[10],pr[10];
floatavg;
charch;
printf(“\n\n\t\t---------PRIORITYSCHEDULINGALGORITHM--------------\n\n”);
printf'("\t\t\
tEnterthenumberofjobs:");scanf("%d",&
jobs);
//reading burst time and
priorityfor(i=0;i<jobs;i++)
{
printf("\t\t\tEnter the CPU burst time and priority of process %d: ",i+
1);scanf(“%d%d”,&pt[i],&pr[i]);
}
for(i=0;i<jobs;i++)
{
for(i=i+l;j<jobs;j++)
{
if(pr[i]>pr[j])
{
//
swappingpriorityandprocesstimetem
p=pr[i];
pr[i]=pr[j];
pr[j]=temp;
temp=pt[i];
pt[i]=pt[j];
pt[j]=temp;
}
}
//incaseoffirst
jobif(i==0)
{
st[0]=wt[0]=0;
et[0]=pt[0];
}
//
forremainingjobsel
se
{
st[i]=et[i-1];
et[i]=st[i]+pt[i];
wt[i]=st[i];
}
}
//printingframeseachtimeweenteranumber
printf("\n\n\t\tSno\tProcesstime\tStarting time\t\tEndtime\tResponsetime");printf("\n\t\t
\t \t \t\t \t \
n”);for(i=0;i<jobs;i++)
{
for(i=0;j<5;j++)
{
Switch(j)
{
case0:
no=i+1;printf("\t\t
%d",no);break;
caseI: no=pt[i];printf("\t\t
%d",no);break;
case2 :
no=st[i];
printf (“\t\t
%d",no);break;
case3:
no=et[i];
printi ("\t\t
%d",no);break;
case4:
no=wt[i];
printf("\t\t
%d”,no);break;
}
}
printf("\n");
}
for(i=0,tot=0;i<jobs;i+
+)tot+=wt[i];
avg=(float)tot/jobs;
printf(“\n\t\tTheaveragewaitingtimeis
%f",avg);for(i=0,tot=0;i<jobs;i++)
tot+=et[i];avg=(flo
at)tot/jobs;
printf("\n\t\tTheaverageturnaroundtimeis
%f",avg);for(i=0,tot=0;i<jobs;i++)
tot+=st[i];a
vg=(float)tot/jobs;
printf("\n\t\tThe average response time is %f\n\
n",avg);return0;
}
Output:-
------------PRIORITYSCHEDULINGALGORITHM--------------
Enterthenumber ofjobs:4
Enter the CPU burst time and priority of process 1:
44Enter the CPU burst time and priority of process 2: 5
2Enterthe CPU bursttime
andpriorityofprocess3:31Enterthe CPU bursttime
andpriorityofprocess4:63.
Sno Processtime Startingtime Endtime Responsetime
1 3 0 3 0
2 5 3 8 3
3 6 8 14 8
4 4 14 18 14
Theaveragewaitingtimeis6.250000
Theaverageturnaroundtimeis10.750000Th
eaverageresponsetimeis6.250000
2. Simulation ofFCFSCPUSchedulingAlgorithm.
code:-
#include<stdio.h>
#include<stdlib.h>
intmain(void)
{
int no,I,j,jobs,pt[10],st[10],et[10],
wt[10],temp,m,tot;floatavg;
charch;
printf("\n\n\t\t------FCFSSCHEDULINGALGORITHM---------\n\n”);
printf("\t\tEnterthenumber
ofjobs:");scanf("%d",&jobs);for(i=0;i
<jobs;i++)
{
printf("\n\t\tEntertheCPUburst timeofprocess
%d :",i+1);scanf("%d",&pt[i];
}
for(i=0;i<jobs;i++)
{
if(1==0)
{
St[O]=Wt[0]=0;
et[0]=pt[0];
}
else
{
st[i]=et[i-1];
et[i]=st[i]+pt[i];
wt[i]=st[i];
printf("\n\t\tSno\t\tProcesstime\t\tStartingtime\t\tEndtime\t\tWaittime\n");
Switch(j)
{
case0:
no=i+1;
printf("%d",no);
break;
case 1:
no=pt[i];
printf(" \t\t %d",no);
break;
case2 :
no=st[i];
printf("\t\t\t %d",no);
break;
case3:
no=et[i];
printf("\t\t\t
case4 :%d",no);break;
no=wt[i];
printf("\t\t\t
} %d",no);break;
//printf("%d\t\t",no);
}
printf("\n\t\t");
}
for(i=0,tot=0;i<jobs;i+
+)tot+=wt[i];
avg=(float)tot/jobs;
printf("\n\t\tTheaveragewaitingtimeis
%f",avg);for(i=0,tot=0;i<jobs;i++)
tot+=et[i];a
vg=(float)tot/jobs;
printf(“\n\t\tTheaverageturnaroundtimeis
%f",avg);for(i=0,tot=0;i<jobs;i++)
tot+=st[i];a
vg=(float)tot/jobs;
printf("\n\t\tTheaverageresponsetimeis%f\n\n",avg);
}
Output:-
---------FCFS SCHEDULINGALGORITHM---------
Enterthenumber ofjobs:4
Enter the CPU burst time of process 1:4
Enter the CPU burst time of process 2:6
Enter the CPU burst time of process 3:2
Enterthe CPU bursttimeofprocess4:7
1 4 0 4 0
2 6 4 10 4
3 2 10 12 10
4 7 12 19 12
Theaveragewaitingtimeis6.500000
Theaverageturnaroundtime
is11.250000Theaverageresponsetimeis6.5
00000
PROCESSSCHEDULING
1. WriteaprogramtogetthePIDofparentandchildprocess.
Code:-
#include<stdio.h>
intmain(void)
{
intpid,ppid;pid
=getpid();ppid=
getppid();syste
m("ps-f”);
printf("Process Id = %d. PPID = %d. \
n",pid,ppid);return0;
}
Output:-
2. Writeaprogramtocreateaprocesswhichwillrunasabackgroundprocess forfiftysecondsandatthetime
ofexecutionitwillprintthesysteminformation.
Code:-
#include<stdio.h>
#include<stdlib.h>
intmain(void)
{system(“backgroundprocessisrunning");p
rintf("thesystemtimeis:\n");system("date +
%r");
sleep(5);
system("Thesystemtimeaftersleepis");sys
tem("date +%r");
execlp("/bin/ps", "ps"," -af",
0);system("Done");
return0;
Output:-
Background process is running
thesystemtimeis:02:06:46PM
Thesystemtimeafter sleepis02:06:51PM
UID PIDPPIDC STIMETTY TIME
CMDRoot 292212725014:06pts/100:00:00ps-af
3. Implementanorphanprocessusingfork.
(orphan process:AUnixprocesswhoseoriginalparenthasterminatedandwhichhasbecomeachildof"init(1))
Code:-
#include<sys/
types.h>#include<stdl
ib.h>#include<unistd.
h>#include<stdio.h>
intmain(void)
{
pid_tpid;
printf("Running Fork Pregram. \
n");pid=fork();
if(pid==0)
{
printf("child process \
n");sleep(3);
system("ps-f');
}
if(pid>0)
{
printf("Parent process \
n");systern("ps -
f');printf("Done\n");
}
return0;
}
Output:-
RunningForkProgram.c
hildprocess
Parentprocess
UID PIDPPIDCSTIMETTY TIMECMD
root 38623860010:14pts/0 00:00:00bash
root4149 3862010:38pts/0 00:00:00./a.out
root4150 4149010:38pts/0 00:00:00./a.out
root4151 4149 0 10:38 pts/0 00:00:00 ps -fDone
[root@localhostprocess]# UTDPIDPPIDCSTIMETTY
TIMECMDroot
38623860010:14pts/000:00:00bash
root 41501010:88pts/000:00:00./a.out
root 41524150010:38pts/000:00:00ps-f
Displaytheprocessinthesystemeverythirtysecondsbutfivetimes.
Code:-
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>i
ntmain(void)
{
intn;for(n=0;n<5;
n++)
{
system("ps-af");
system
("date");sleep(3
);
}
printf("Done\n”);
}
Output:-
5. CREATINGPROCESSWITHexample
Code:-
#include<stdlib.h>
#include<stdio.h>
main()
{
printf ("Running ps with execlp. \
n");execip("/bin/ps","ps","-
ef",0);printf("Done.In");
}
6. CREATINGPROCESSWITHexample
Code:-
#include<stdlib.h>
#include<stdio.h>
main()
{
printf ("Running ps with execlp. \
n");execip("/bin/ps","ps","-
ef",0);printf("Done.In");
}
7. CREATINGPROCESSWITHexample
Code:-
#include<stdlib.h>
#include<stdio.h>
main()
{
printf ("Running ps with execlp. \
n");execip("/bin/ps","ps","-
ef",0);printf("Done.In");
}
8. CreatingChildProcessusingFork()
Code:-
#include<sys/
types.h>#include<uni
std.h>#include<stdio.
h>Voidmain()
{
pid_tpid;c
har*mes;i
ntn;
printf("RunningForkProgram.\
n");pid=fork();
switch(pid)
{
case-1:
printf("ForkFailed");b
reak;
case0:
mes="ChildProcess.";
n= 3;
break;d
efault:
mes="ParentProcess.";
n=5;
break;
}
for(;n>0;n--)
{
puts(mes);s
leep(1);
}
printf("Done.\
n");exit(0);
}
Output:-
9. GetProcessIdandParentProcessId
Code:-
#include<stdio.h>
Voidmain()
{
intpid,ppid;pid=
getpid
();ppid=getppid(
);system("ps-
f");
} printf("ProcessId=%d.PPID=%d.\n",pid,ppid);
Output:-
#include<stdio.h>
Voidmain()
{
intpid,ppid;pid=
getpid
();ppid=getppid(
);system("ps-
f");
} printf("ProcessId=%d.PPID=%d.\n",pid,ppid);
Output:-
11. orphan process : A Unix process whose original parent has terminated and which has become a
child of“init(1)”
Code:-
#include<sys/
types.h>#include<stal
ib.h>#include<unistd.
h>#include<stdio.h>
Voidmain()
{
pid_tpid;
printf("RunningForkProgram.\
n");pid-fork();
if(pid==0)
{
printf("child procesa\n");
sleep(3);
system("ps-f");
}
}
if(pid>0)
{
printf("Parentprocess\
n");system ("ps -
f");printf("Done\n");
}
}
Output:-
Running
ForkProgram.childprocessParentproc
ess
UID PID PPID C STIME TTYTIMECMD
root 3862 3860 0 10:14 pts/0 00:00:00 bash
root 4149 3862 0 10:38 pts/0 00:00:00./a.out
root 4150 41490 10:38 pts/0 00:00:00 ./a.out
root 4151 41490 10:38 pts/0 00:00:00ps-f
Done
[root@localhostprocess]#UID
PIDPPIDCSTIMETTYTIMECMD
root 3862 3860 010:14pts/0 00:00:00bash
root 4150 1 10:38pts/0 00:00:00 •/a.out
root 4152 4150 0 10:38pts/0 00:00:00ps-f
12. Displaytheprocessinthesystemeverythirtysecondsbutfivetimes.
Code:-
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>v
oidmain()
{
intn;
for(n=0;n<5;n++)
{
system("ps-ef");
system("date");
sleep(30);
}
printf("Done");
}
Output:-