0% found this document useful (0 votes)
14 views21 pages

CN Lab 04

Computer networks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views21 pages

CN Lab 04

Computer networks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

NAME:CH.

DILEEP
REG NO:20BBS0163

LAB ASSESSMENT-04
1. Code the following scheduling algorithms in C and print the performance
parameters for an arbitrary set of inputs (Process IDs, Burst Times, Arrival
Times, Priorities). Use of appropriate data structure will be appreciable.
a. FCFS
b. SJF
c. SRTF
d. PRIORITY
e. ROUND ROBIN
SOLUTION:
FCFS
#include<stdio.h>
void findWaitingTime(int processes[], int n,
int bt[], int wt[])
{

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] );
}
int s=(float)total_wt / (float)n;
int t=(float)total_tat / (float)n;
printf("Average waiting time = %d",s);
printf("\n");
printf("Average turn around time = %d ",t);
}

// Driver
codeint
main()
{
//process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];

//Burst time of all


processesint
burst_time[] = {10, 5,
8};

findavgTime(processes, n,
burst_time);return 0;
}

OUTPUT:-

SJF:
#include<stdio.h>
int main()
{
int
bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,t
emp;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])
po
s=
j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p
[i];
p[i]=p[p
os];
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 tWaitingTimet Turnaround


Time");for(i=0;i<n;i++)
{
tat[i]=bt[i]
+wt[i];
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=(float)total/n;
printf("\nnAverage Waiting
Time=%f",avg_wt); printf("\nAverage
Turnaround Time=%fn",avg_tat);
}

OUTPUT:

SRTF:
#include<stdio.h>
int main()
{
int n, ari[10], bur[10], total = 0, i, j, small, temp, procs[100], k,
waiting[10],
finish[10];
float tavg = 0.0, wavg = 0.0;
printf("ENTER THE NUMBER OF PROCESSES:");
scanf("%d", & n);
for (i = 0; i< n; i++)
{
printf("ENTER THE ARRIVAL TIME OF PROCESS %d:\t", i);
scanf("%d", &ari[i]);
printf("ENTER THE BURST TIME OF PROCESS %d:\t", i);
scanf("%d", & bur[i]);
waiting[i] = 0;
total += bur[i];
}
for (i = 0; i< n; i++)
{
for (j = i + 1; j < n; j++)
{
if (ari[i] >ari[j])
{
temp = ari[i];
ari[i] = ari[j];
ari[j] = temp;
temp = bur[i];
bur[i] = bur[j];
bur[j] = temp;
}
}
}
for (i = 0; i< total; i++)
{
small = 3200;
for (j = 0; j < n; j++)
{
if ((bur[j] != 0) && (ari[j] <= i) && (bur[j] < small))
{
small = bur[j];
k = j;
}
}
bur[k]--;
procs[i] = k;
}
k = 0;
for (i = 0; i< total; i++)
{
for (j = 0; j < n; j++)
{
if (procs[i] == j)
{
finish[j] = i;
waiting[j]++;
}
}
}
for (i = 0; i< n; i++)
{
printf("\n PROCESS %d:-FINISH TIME==> %d TURNAROUND TIME==>%d
WAITING
TIME==>%d\n", i + 1, finish[i] + 1, (finish[i] - ari[i]) + 1, (((finish[i] + 1) -
waiting[i]) -
ari[i]));
wavg = wavg + (((finish[i] + 1) - waiting[i]) - ari[i]);
tavg = tavg + ((finish[i] - ari[i]) + 1);
}
printf("\n WAvG==>\t%f\n TAVG==>\t%f\n", (wavg / n), (tavg / n));
return 0;
}

OUTPUT:
D)PRIORITY
PROGRAM:
#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\tTurnaroundTime");
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;
}
OUTPUT:-

E. ROUND ROBIN
#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d
:",count+1);
scanf("%d",&at[count]);
NAME: KARTHIK.T
REG.NO: 20BBS0096
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|TurnaroundTime|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum&& rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-
bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;}
OUTPUT:

2. Create two POSIX threads. Let the first thread copy the contents of
“file1.txt” to “file2.txt” and let the second thread collapse all spaces
more than one to one space in an input string. Write the main
function to test the working of the two threads.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>
void *t1();
void *t2();
int main()
{
pthread_t thread1,thread2;
if(pthread_create(&thread1,NULL,t1,NULL)<0)
{
perror("pthread_create");
exit(1);
}
if(pthread_create(&thread2,NULL,t2,NULL)<0)
{
perror("pthread_create");
exit(1);
}
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
return(0);
}
void *t1()
{
printf("\n\t\t\t\tThread 1 is executing");
FILE *fp,*fp1;
char s;
fp=fopen("file1.txt","r");
fp1=fopen("file2.txt","w");
if(fp==NULL)
{
printf("Error in file opening\n");
exit(-1);
}
if(fp1==NULL)
{
printf("Error in file opening\n");
exit(-1);
}
while((s=fgetc(fp))!=EOF)
{
fputc(s,fp1);
}
fclose(fp);
fclose(fp1);
printf("\nContents in file 1 is copied to file 2 successfully\n");
}
void *t2()
{
printf("\n\t\t\t\tThread 2 is executing");
char string[50],s[50];
int i,j=0,l;
printf("\nEnter a string: ");
scanf("%[^\n]",string);
l=strlen(string);
for(i=0;i<l;i++)
{
if(string[i]!=' ')
{
s[j]=string[i];
j++;
}
else
{
s[j]=string[i];
j++;
i++;
while(string[i]==' ' && i<l)
{
i++;
}
if(i<l)
{
s[j]=string[i];
j++;
}
}
}
s[i]='\0';
printf("\nThe string after collapsed space: \n%s\n",s);
}
OUTPUT:
3.Write a C to implement the pipe given below.
ls -l | grep .c$
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int pid;
int fd[2];
pipe(fd);
pid=fork();
if( pid < 0 )
{
perror( "fork" );
exit( -1 );
}
if( pid == 0 )
{
}
else
{
close(1);
dup (fd[1]);
close(fd[0]);
close(fd[1]);
execl("/bin/ls", "ls", "-l", (char *) 0);
close(0);
dup (fd[0]);
close(fd[0]);
close(fd[1]);
execl("/usr/bin/grep","grep",".c$",(char *) 0);
}
}
OUTPUT:

You might also like