CPUSCHEDULING
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");
printf("\t\t\t\t -\t\t \t\t \t\t \n\t\t”);
for(i=0;i<jobs;i++)
{
for(i=0;j<5;j++)
{
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
Sno Processtime Starting time Endtime Waittime
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:-
UID PIDPPIDCSTIMETTY TIMECMD
root 36073605010:29pts/[Link]sh
root 40303607011:13pts/0 [Link]./[Link]
root40314030 011:13 pts/0 [Link] ps-
fProcessId= [Link] =3607.
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 sleepis[Link]PM
UID PIDPPIDC STIMETTY TIME
CMDRoot 292212725014:06pts/[Link]ps-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 [Link]sh
root4149 3862010:38pts/0 [Link]./[Link]
root4150 4149010:38pts/0 [Link]./[Link]
root4151 4149 0 10:38 pts/0 [Link] ps -fDone
[root@localhostprocess]# UTDPIDPPIDCSTIMETTY
TIMECMDroot
38623860010:14pts/[Link]sh
root 41501010:88pts/[Link]./[Link]
root 41524150010:38pts/[Link]ps-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:-
UID PIDPPIDC STIMETTY TIMECMD
root293222725 014:11pts/1 [Link]./[Link]
root2932329322 014:11 pts/[Link] ps -af
MonSep[Link]IST2010
UID PIDPPID CSTIMETTY TIMECMD
root2932227250 14:11pts/[Link]./[Link]
root2932529322014:11pts/[Link]ps-af
MonSep 27 [Link]IST2010
UID PIDPPIDC STIMETTY TIMECMD
root29322 2725014:11 pts/1 [Link]./[Link]
root 29327 29322 014:11 pts/[Link] ps-af
MonSep27 [Link]IST2010
UID PIDPPIDC STIMETTY TIME CMD
root2932227250 14:11pts/[Link] /[Link]
root29329293220 14:11pts/[Link] ps-af
MonSep27 [Link]IST2010
UID PIDPPID CSTIMETTY TIMECD
root293222725 014:11 pts/[Link]./[Link]
root2933129322014:11 pts/[Link]ps-af
MonSep27 [Link]IST2010
Done
[Link]
Code:-
#include<stdlib.h>
#include<stdio.h>
void main()
{
printf ("Running ps with system \
n");system("ps-ef");
printf("Done\n");
}
Output:-
UID PIDPPIDCSTIMETTY TIMECMD
root 1 00Nov13? [Link]npmstart
root 17 10Nov13? [Link][Link]
coderun+ 2691 1 0Nov14? [Link][Main]<defunct>
coderun+ 2692 1 0Nov14? [Link][Main]<defunct>
coderun+ 2693 1 0Nov14? [Link][Main]<defunct>
coderun+ 2714 1 0Nov14? [Link][Main]<defunct>
coderun+ 2715 1 0Nov14? [Link][Main]<defunct>
coderun+ 2716 1 0Nov14? [Link][Main]<defunct>
coderun+ 2773 1 0Nov14? [Link][Main]<defunct>
coderun+ 2774 1 0Nov14? [Link][Main]<defunct>
coderun+ 2775 1 0Nov14? [Link][Main]<defunct>
coderun+ 3209 1 0Nov14? [Link][Main]<defunct>
coderun+ 3656 1 0Nov14? [Link][Main]<defunct>
coderun+ 3657 1 0Nov14? [Link][Main]<defunct>
coderun+ 3658 1 0Nov14? [Link][Main]<defunct>
coderun+ 3856 1 0Nov14? [Link][Main]<defunct>
coderun+ 3857 1 0Nov14? [Link][Main]<defunct>
coderun+ 3858 1 0Nov14? [Link][Main]<defunct>
…..extends
5. CREATINGPROCESSWITHexample
Code:-
#include<stdlib.h>
#include<stdio.h>
main()
{
printf ("Running ps with execlp. \
n");execip("/bin/ps","ps","-
ef",0);printf("[Link]");
}
6. CREATINGPROCESSWITHexample
Code:-
#include<stdlib.h>
#include<stdio.h>
main()
{
printf ("Running ps with execlp. \
n");execip("/bin/ps","ps","-
ef",0);printf("[Link]");
}
7. CREATINGPROCESSWITHexample
Code:-
#include<stdlib.h>
#include<stdio.h>
main()
{
printf ("Running ps with execlp. \
n");execip("/bin/ps","ps","-
ef",0);printf("[Link]");
}
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:-
Running Fork program
ChildProcess
Parent
ProcessParent
ProcessChild
ProcessParent
ProcessChild
ProcessParent
ProcessDone
Parent
ProcessDone
9. GetProcessIdandParentProcessId
Code:-
#include<stdio.h>
Voidmain()
{
intpid,ppid;pid=
getpid
();ppid=getppid(
);system("ps-
f");
} printf("ProcessId=%[Link]=%d.\n",pid,ppid);
Output:-
UID PID PPID C STIMETTY TIMECMD
root 3607 3605 0 10:29pts/0 [Link]sh
root 4030 3607 0 11:13pts/0 [Link]./[Link]
root 4031 4030 0 11:13pts/0 [Link]ps-f
ProcessId = [Link]= 3607.
10. GetProcessIdandParentProcessId
Code:-
#include<stdio.h>
Voidmain()
{
intpid,ppid;pid=
getpid
();ppid=getppid(
);system("ps-
f");
} printf("ProcessId=%[Link]=%d.\n",pid,ppid);
Output:-
UID PID PPID C STIMETTY TIMECMD
root 3607 3605 0 10:29pts/0 [Link]sh
root 4030 3607 0 11:13pts/0 [Link]./[Link]
root 4031 4030 0 11:13pts/0 [Link]ps-f
ProcessId = [Link]= 3607.
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
[Link]
ess
UID PID PPID C STIME TTYTIMECMD
root 3862 3860 0 10:14 pts/0 [Link] bash
root 4149 3862 0 10:38 pts/0 [Link]./[Link]
root 4150 41490 10:38 pts/0 [Link] ./[Link]
root 4151 41490 10:38 pts/0 [Link]ps-f
Done
[root@localhostprocess]#UID
PIDPPIDCSTIMETTYTIMECMD
root 3862 3860 010:14pts/0 [Link]sh
root 4150 1 10:38pts/0 [Link] •/[Link]
root 4152 4150 0 10:38pts/0 [Link]ps-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:-
UID PIDPPIDCSTIMETTY TIMECMD
root 1 0012:13? [Link]npmstart
root 16 1012:13? [Link][Link]
coderun+ 10248 16013:32? [Link]timeout7./Main
coderun+ 1024910248013:32? [Link]./Main
coderun+ 1025010249013:32? [Link]ps-ef
Wed15Nov20[Link]PMUTC
Done