Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Program Source Code: Thus The C Program To Perform Process Creation Using Fork Was Written and Executed Successfully

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

PROGRAM SOURCE CODE

#include<sys/types.h>
#include<stdio.h>
#include<process.h>
int main()
{
int pid_t,pid,pid1,p,p1;
pid =fork();
if (pid ==-1)
{
printf("enter in connection");
}
else
if(pid==0)
{
printf("\n child process1 :\n\n");
p=getppid();
printf("parent process id of child1: %d\n",p);
p1=getpid();
printf("parent process id of child1: %d\n",p1);
}
else
{
pid1=fork();
if(pid==0)
{
printf("\nchild process 2:\n\n");
p=getppid();
printf("parent process id of child2: %d\n",p);
p1=grtpid();
printf("parent process id of child2: %d\n",p1);
}

else
{
printf("this is parent process \n");
p=getppid();
printf("grant parent: %d \n",p);
p1=getpid();
printf("process id of parent: %d \n",p1);
}
}
return 0;
}
CONCLUSION:
Thus the c program to perform process creation using fork() was written and
executed successfully.
PROGRAM SOURCE CODE
include<stdio.h>
main()
{

int i,ptime[10][10],a[10][10],n,I,j,k=0,sum=0,q;
float avg;
printf("enter the no of process \t \t");
scanf("%d",&n);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
ptime[i][j]=0;
a[i][j]=0;
}
}
for(i=0;i<n;i++)
{
j=0;
printf("\n enter the processname & time for process %d \t",i+1);
scanf("%s",&a[i][j]);
scanf("%d",&ptime[i][j]);
}
printf("enter the time quantum\t\t");
scanf("%d",&q);
printf("\n \t processname \t\t process time\t\t quantum time \t\t");
for(i=0;i<n;i++)
{
printf("\t%d\t\t%d\t\t%d\n",i+1,ptime[i][0],q);
}
printf("\n Round robin");
for(j=0;j<10;i++)
{
for(i=0;i<n;i++)
{
a[2*j][i]=k;
if((ptime[i][j]<=q)&&(ptime[i][j]!=0))
{
ptime[i][j+1]=0;
printf("\t\tprocess %d is from %d to %d \n",i+1,k,(k+ptime[i][j]));
k+=ptime[i][j];
a[2*j+1][i]=k;
}
else if(ptime[i][j]!=0)
{
ptime[i][j+1]=ptime[i][j]-q;
printf("\t\tprocess %d is from %d to %d\n",i+1,k,(k+q));
k+=q;
a[2*j+1][i]=k;
}
else
{
a[2*j][i]=0;
a[2*j+1][i]=0;
}
}
}
for(i=0;i<n;i++)
{
sum+=a[0][i];
}
for(i=0;i<n;i++)
{
for(j=1;j<10;j++)
{
if((a[j][i]!=0)&&(a[j+1][i]!=0)&&((j+1)%2==0))
{
sum+=a[j+1][i]-a[j][i];
}
}
}
avg=(float)sum/n;
printf("\n\n the avg waiting time is \t %2f ms",avg);
sum=avg=0;
for(j=0;j<n;j++)
{
I=1;
while(a[i][j]!=0)
{
i+=1;
}
sum+=a[i-1][j];
}
avg=(float)sum/n;
printf("\n\nthe turn around time is %2f ms",avg);
return 0;
}
CONCLUSION:

Thus the c program to perform round robin was written and executed
successfully.

#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("nEnter Burst Time:n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}

//sorting of burst times


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0;

for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=(float)total/n;
total=0;

printf("nProcesst Burst Time tWaiting TimetTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("np%dtt %dtt %dttt%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("nnAverage Waiting Time=%f",avg_wt);
printf("nAverage Turnaround Time=%fn",avg_tat);
}

CONCLUSION:

Thus the c program to perform SJF was written and executed successfully.

#include<stdio.h>

int main()

{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);

printf("nEnter Process Burst Timen");


for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}

wt[0]=0;

for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}

printf("nProcessttBurst TimetWaiting TimetTurnaround Time");

for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("nP[%d]tt%dtt%dtt%d",i+1,bt[i],wt[i],tat[i]);
}

avwt/=i;
avtat/=i;
printf("nnAverage Waiting Time:%d",avwt);
printf("nAverage Turnaround Time:%d",avtat);
return 0;
}

CONCLUSION:

Thus the c program to perform FCFS was written and executed successfully.

#include<stdio.h>

int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);

printf("\nEnter Burst Time and Priority\n");


for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}

//sorting burst time, priority and process number in ascending order using
selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}

temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process is zero

//calculate waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=total/n; //average waiting time


total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);

return 0;
}

CONCLUSION:

Thus the c program to perform Priority Scheduling was written and executed
successfully.

You might also like