You are on page 1of 18

CS303-Operating Systems Lab-syllabus

1. Develop a c program to implement the Process system calls

(fork (), exec(), wait(), create process, terminate process


Fork.c
#include<stdio.h>

#include<sys/types.h>
#include<unistd.h> int
main ()

{
printf("Hello welcome to the os class:\n PID=%d\n",getpid());
return 0;
}
Output

Fork1.c
#include<stdio.h>
#include<sys/types.h>

#include<unistd.h>
int main ()
{

fork();
printf("Hello welcome to the os class:\n PID=%d\n",getpid());
return 0;
}
Output
Fork2.c
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main ()

{
fork();
printf("Hello welcome to the os class:\n PID=%d\n",getpid());
return 0;

}
Output
Exec() System Call
ex1.c
#include<stdio.h>

#include<sys/types.h>
#include<unistd.h>
int main(int agc,char *argv[])

{
printf("PID of ex1.c=%d\n",getpid());

char *args[]={"Hello","welcome","os",NULL};
execv("/ex2",args);

printf("Back to ex1.c");
return 0;
}
ex2.c
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main(int agc,char *argv[])

printf("we are in ex2.c\n");


printf("PID of ex2.c=%d\n",getpid());
return 0;
}

Output
Wait() System Call
Wait.c
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<sys/wait.h>

int main()
{
pid_t p;

printf("before fork\n");
p=fork();
if(p==0)//child

printf("I am child having id %d\n",getpid());


printf("My parent's id is %d\n",getppid());
}

else//parent
{
wait(NULL);

printf("My child's id is %d\n",p);


printf("I am parent having id %d\n",getpid());

}
printf("Common\n");}
Output

Create and terminate Process-System call using Fork() and exit()

ct.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

main(void) {
pid_t pid = 0;

pid = fork();
if (pid == 0) {
printf("I am the child.\\n");
}

if (pid > 0) {
printf("I am the parent, the child is %d.\\n", pid);

}
if (pid < 0) {
perror("In fork():");
}

exit(0);

OUTPUT
2.Simulate the following CPU scheduling algorithms to find

turnaround time and waiting time .

FCFS b) SJF c) Round Robin d) Priority.

PROGRAM
FCFS.C
// C program for implementation of FCFS // scheduling
#include<stdio.h>
// Function to find the waiting time for all // processes
void findWaitingTime(int processes[], int n, int bt[], int wt[])
{
// waiting time for first process is 0
wt[0] = 0;
// calculating waiting time
for (int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}
// Function to calculate turn around time
void findTurnAroundTime( int processes[], int n, int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
//Function to calculate average time
void findavgTime( int processes[], int n, int bt[])
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
//Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt);
//Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
//Display processes along with all details
printf("Processes Burst time Waiting time Turn around time\n");
// Calculate total waiting time and total turn // around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d ",(i+1));
printf(" %d ", bt[i] );
printf(" %d",wt[i] );
printf(" %d\n",tat[i] );
}
float s=(float)total_wt / (float)n;
float t=(float)total_tat / (float)n;
printf("Average waiting time = %f",s);
printf("\n");
printf("Average turn around time = %f ",t);
}
// Driver code
int main()
{
//process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
//Burst time of all processes
int burst_time[] = {10, 5, 8};
findavgTime(processes, n, burst_time);
return 0;
}

OUTPUT
PROGRAM
Sjf.c
#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);
}

OUTPUT
Program
rr.c

#include<stdio.h>
void main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
//clrscr();
printf("Enter the no of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t)
{
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
}
else
{
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++)
{
wa[i]=tat[i]-ct[i];
att+=tat[i];
awt+=wa[i];
}
printf("\nThe Average Turnaround time is -- %f",att/n);
printf("\nThe Average Waiting time is -- %f ",awt/n);
printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
getc;
}

INPUT

Enter the no of processes – 3


Enter Burst Time for process 1 – 24
Enter Burst Time for process 2 -- 3
Enter Burst Time for process 3 – 3

Enter the size of time slice – 3

OUTPUT
The Average Turnaround time is – 15.666667
The Average Waiting time is – 5.666667

PROCESS BURST TIME WAITING TIME TURNAROUND TIME


1 24 6 30
2 3 4 7
3 3 7 10

PROGRAM

Priority.c

#include<stdio.h>
main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
clrscr();
printf("Enter the number of processes --- ");
scanf("%d",&n);

for(i=0;i<n;i++)
{
p[i] = i;
printf("Enter the Burst Time & Priority of Process %d --- ",i);
scanf("%d %d",&bt[i], &pri[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(pri[i] > pri[k])
{
temp=p[i]; p[i]=p[k]; p[k]=temp;

temp=bt[i]; bt[i]=bt[k]; bt[k]=temp;


temp=pri[i]; pri[i]=pri[k]; pri[k]=temp;

}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];

for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg + wt[i]; tatavg =
tatavg + tat[i];
}

printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\tTURNAROUND


TIME");
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],pri[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is --- %f",wtavg/n);
printf("\nAverage Turnaround Time is --%f",tatavg/n);
getch();
}

OUTPUT

You might also like