1. BYTESTUFF
#include<stdio.h>
struct FRAME
{
char flag;
char Data[50];
}F;
void send(char[]);
void receive();
char escape;
int main()
{
char data[50];
printf("Enter flag : ");
F.flag=getchar();
printf("Enter Escape Character : ");
fflush(stdin);
escape=getchar();
fflush(stdin);
printf("Enter Data : ");
scanf("%s",data);
send(data);
printf("\n\nSent FRAME = %c%s%c",F.flag,F.Data,F.flag);
receive();
return 0;
}
void send(char Data[])
{
int i,j;
for(i=0,j=0;i<=strlen(Data);i++,j++)
{
if(Data[i]==F.flag || Data[i]==escape)
{
F.Data[j]=escape;
F.Data[++j]=Data[i];
}
else
{
F.Data[j]=Data[i];
}
}
}
void receive()
{
int i,j;
char DestuffedString[50];
for(i=0,j=0;i<=strlen(F.Data);i++,j++)
{
if(F.Data[i]==escape)
{
DestuffedString[j]=F.Data[++i];
}
else
{
DestuffedString[j]=F.Data[i];
}
}
printf("\n\nReceived Data = %s\n",DestuffedString);
}
2.BITSTUFF
#include <stdio.h>
void send(char[]);
void receive();
char frame[100];
char sframe[100];
int i=0,j,count=0,k=0;
int main()
{
int n;
printf("Enter the bits : ");
scanf("%s",frame);
send(frame);
printf("\n\nSent FRAME = 01111110 %s 01111110\n",sframe);
receive();
return 0;
}
void send(char tframe[])
{
for(i=0;tframe[i];i++)
{
if(tframe[i]=='1')
count++;
else
count=0;
sframe[k++]=tframe[i];
if(count==5&&(tframe[i-5]=='0'))
{
sframe[k++]='0';
count=0;
}
}
sframe[k+1]='\0';
}
void receive()
{
char Destuffedframe[100];
count=0;
for(i=0,j=0;sframe[i];i++,j++)
{
if(sframe[i]=='1')
count++;
else
count=0;
Destuffedframe[j]=sframe[i];
if(count==5&&(sframe[i-5]=='0'))
{
i++;
count=0;
}
}
Destuffedframe[j]='\0';
printf("\n\nReceived Data = 01111110 %s 01111110\n ",Destuffedframe);
}
3.SJF
#include<stdio.h>
int n,Bu[20],Twt,Ttt,A[10],Wt[10],tat[10],star[10],finish[10];
float Awt,Att;
char pname[20][20],c[20][20];
void Getdata();
void Gantt_chart();
void Calculate();
void sjf();
void Getdata()
{
int i;
printf("\n Enter the number of processes: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fflush(stdin);
printf("\n\n Enter the process name: ");
scanf("%s",&pname[i]);
printf("\n Enter The BurstTime for Process %s = ",pname[i]);
scanf("%d",&Bu[i]);
printf("\n Enter the Arrival Time for Process %s = ",pname[i]);
scanf("%d",&A[i]);
}
}
void Gantt_chart()
{
int i;
printf("\n\n\t\t\tGANTT CHART\n");
printf("\n-----------------------------------------------------------\n");
for(i=1;i<=n;i++)
printf("|\t%s\t",pname[i]);
printf("|\t\n");
printf("\n-----------------------------------------------------------\n");
printf("\n");
printf("%d\t\t",star[1]);
for(i=1;i<=n;i++)
printf("%d\t\t",finish[i]);
printf("\n--------------------------------------------------------------------\n");
printf("\n");
}
void Calculate()
{
int i;
for(i=1;i<=n;i++)
{
if(i==1)
star[i]=A[i];
else
star[i]=finish[i-1];
Wt[i]=star[i]-A[i];
finish[i]=star[i]+Bu[i];
tat[i]=finish[i]-A[i];
}
printf("\nPName Arrtime Burtime WaitTime Start TAT Finish");
for(i=1;i<=n;i++)
{
printf("\n%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d",pname[i],A[i],Bu[i],Wt[i],star[i],tat[i],finish[i]);
Twt+=Wt[i];
Ttt+=tat[i];
}
printf("\n\nAverage Waiting time:%f",(float)Twt/n);
printf("\n\nAverage Turn Around Time:%f",(float)Ttt/n);
}
void sjf()
{
int i,j,temp, temp1;
Twt=0;
Ttt=0;
printf("\n\n Shortest Job Fisrt ALGORITHM\n\n");
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(Bu[i]>Bu[j])
{
temp=Bu[i];
temp1=A[i];
Bu[i]=Bu[j];
A[i]=A[j];
Bu[j]=temp;
A[j]=temp1;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);
}
}
}
Calculate();
Gantt_chart();
}
int main()
{
int ch;
Getdata();
sjf();
}
4.FCFS
#include<stdio.h>
#include<string.h>
int n,Bu[20],Twt,Ttt,A[10],Wt[10],tat[10],star[10],finish[10];
float Awt,Att;
char pname[20][20],c[20][20];
void Getdata();
void Gantt_chart();
void Calculate();
void fcfs();
void Getdata()
{
int i;
printf("\n Enter the number of processes: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fflush(stdin);
printf("\n\n Enter the process name: ");
scanf("%s",&pname[i]);
printf("\n Enter The BurstTime for Process %s = ",pname[i]);
scanf("%d",&Bu[i]);
printf("\n Enter the Arrival Time for Process %s = ",pname[i]);
scanf("%d",&A[i]);
}
}
void Gantt_chart()
{
int i;
printf("\n\n\t\t\tGANTT CHART\n");
printf("\n-----------------------------------------------------------\n");
for(i=1;i<=n;i++)
printf("|\t%s\t",pname[i]);
printf("|\t\n");
printf("\n-----------------------------------------------------------\n");
printf("\n");
printf("%d\t\t",star[1]);
for(i=1;i<=n;i++)
printf("%d\t\t",finish[i]);
printf("\n--------------------------------------------------------------------\n");
printf("\n");
}
void Calculate()
{
int i;
for(i=1;i<=n;i++)
{
if(i==1)
star[i]=A[i];
else
star[i]=finish[i-1];
Wt[i]=star[i]-A[i];
finish[i]=star[i]+Bu[i];
tat[i]=finish[i]-A[i];
}
printf("\nPName Arrtime Burtime WaitTime Start TAT Finish");
for(i=1;i<=n;i++)
{
printf("\n%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d",pname[i],A[i],Bu[i],Wt[i],star[i],tat[i],finish[i]);
Twt+=Wt[i];
Ttt+=tat[i];
}
printf("\n\nAverage Waiting time:%f",(float)Twt/n);
printf("\n\nAverage Turn Around Time:%f",(float)Ttt/n);
}
void fcfs()
{
int i,j,temp, temp1;
Twt=0;
Ttt=0;
printf("\n\n FIRST COME FIRST SERVED ALGORITHM\n\n");
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(A[i]>A[j])
{
temp=Bu[i];
temp1=A[i];
Bu[i]=Bu[j];
A[i]=A[j];
Bu[j]=temp;
A[j]=temp1;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);
}
}
}
Calculate();
Gantt_chart();
}
int main()
{
int ch;
Getdata();
fcfs();
}
5.PRIORITY
#include<stdio.h>
#include<string.h>
int n,Bu[20],Twt,Ttt,A[10],Wt[10],tat[10],star[10],finish[10],prior[10];
float Awt,Att;
char pname[20][20],c[20][20];
void Getdata();
void Gantt_chart();
void Calculate();
void Priority();
void Getdata()
{
int i;
printf("\n Enter the number of processes: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fflush(stdin);
printf("\n\n Enter the process name: ");
scanf("%s",&pname[i]);
printf("\n Enter The priority for Process %s = ",pname[i]);
scanf("%d",&prior[i]);
printf("\n Enter The BurstTime for Process %s = ",pname[i]);
scanf("%d",&Bu[i]);
printf("\n Enter the Arrival Time for Process %s = ",pname[i]);
scanf("%d",&A[i]);
}
}
void Gantt_chart()
{
int i;
printf("\n\n\t\t\tGANTT CHART\n");
printf("\n-----------------------------------------------------------\n");
for(i=1;i<=n;i++)
printf("|\t%s\t",pname[i]);
printf("|\t\n");
printf("\n-----------------------------------------------------------\n");
printf("\n");
printf("%d\t\t",star[1]);
for(i=1;i<=n;i++)
printf("%d\t\t",finish[i]);
printf("\n--------------------------------------------------------------------\n");
printf("\n");
}
void Calculate()
{
int i;
for(i=1;i<=n;i++)
{
if(i==1)
star[i]=A[i];
else
star[i]=finish[i-1];
Wt[i]=star[i]-A[i];
finish[i]=star[i]+Bu[i];
tat[i]=finish[i]-A[i];
}
printf("\nPName Priority Arrtime Burtime WaitTime Start TAT Finish");
for(i=1;i<=n;i++)
{
printf("\n%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d",pname[i],prior[i],A[i],Bu[i],Wt[i],star[i],tat[i],finish[i]);
Twt+=Wt[i];
Ttt+=tat[i];
}
printf("\n\nAverage Waiting time:%f",(float)Twt/n);
printf("\n\nAverage Turn Around Time:%f",(float)Ttt/n);
}
void Priority()
{
int i,j,temp, temp1,temp2;
Twt=0;
Ttt=0;
printf("\n\n Priority Based Sheduling Algorithm \n\n");
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(prior[i]>prior[j])
{
temp=Bu[i];
temp1=A[i];
temp2=prior[i];
Bu[i]=Bu[j];
A[i]=A[j];
prior[i]=prior[j];
Bu[j]=temp;
A[j]=temp1;
prior[j]=temp2;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);
}
}
}
Calculate();
Gantt_chart();
}
int main()
{
int ch;
Getdata();
Priority();
}
6.ROUNDROBIN
#include<stdio.h>
#include<string.h>
int n,j=1,flag=0,Bu[20],tBu[10],Twt,Ttt,A[10],Wt[10],tat[10],finish[10],ts,ganttStatBuTime[50],time;
char pname[20][20],c[20][20],ganttP[50][50];
void Getdata();
void Gantt_chart();
void Calculate();
void RR();
void Getdata()
{
int i;
printf("\n Enter the number of processes: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fflush(stdin);
printf("\n\n Enter the process name: ");
scanf("%s",&pname[i]);
printf("\n Enter The BurstTime for Process %s = ",pname[i]);
scanf("%d",&Bu[i]);
printf("\n Enter the Arrival Time for Process %s = ",pname[i]);
scanf("%d",&A[i]);
tBu[i]=Bu[i];
}
printf("\n Enter Time slice = ");
scanf("%d",&ts);
}
void Gantt_chart()
{
int i;
printf("\n\n\t\t\tGANTT CHART\n");
printf("\n----------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
for(i=1;i<j;i++)
printf("|\t%s\t",ganttP[i]);
printf("|\t\n");
printf("\n---------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("\n");
for(i=1;i<j;i++)
printf("%d\t\t",ganttStatBuTime[i]);
printf("%d",time);
printf("\n------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("\n");
}
void Calculate()
{
int i,remain=n;
printf("\nPName Artime Burtime WaitTime Start TAT Finish");
for(time=A[1],i=1;remain!=0;)
{
if(tBu[i]<=ts && tBu[i]>0)
{
strcpy(ganttP[j],pname[i]);
ganttStatBuTime[j++]=time;
time+=tBu[i];
tBu[i]=0;
flag=1;
}
else if(tBu[i]>0)
{
strcpy(ganttP[j],pname[i]);
ganttStatBuTime[j++]=time;
tBu[i]-=ts;
time+=ts;
}
if(tBu[i]==0 && flag==1)
{
remain--;
printf("\n%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d",pname[i],A[i],Bu[i],time-A[i]-Bu[i],ganttStatBuTime[i],time-A[i],time);
Twt+=time-A[i]-Bu[i];
Ttt+=time-A[i];
flag=0;
}
if(i==n)
i=1;
else if(A[i+1]<=time)
i++;
}
printf("\n\nAverage Waiting time:%f",(float)Twt/n);
printf("\n\nAverage Turn Around Time:%f",(float)Ttt/n);
}
void RR()
{
int i,j,temp, temp1;
Twt=0;
Ttt=0;
printf("\n\n Round Robin Scheduling ALGORITHM\n\n");
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(A[i]>A[j])
{
temp=Bu[i];
temp1=A[i];
Bu[i]=Bu[j];
tBu[i]=tBu[j];
A[i]=A[j];
Bu[j]=temp;
tBu[j]=temp;
A[j]=temp1;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);
}
}
}
Calculate();
Gantt_chart();
}
int main()
{
int ch;
Getdata();
RR();
}
7.CYCLIC REDUNDANCY CHECK
#include<stdio.h>
#include<string.h>
void GetData();
void Xor();
void CRC();
char m[50],Checks[50],genP[50],Rd[50];
int lenm,i,j,lengP,ch,lenRd;
void GetData()
{
printf("\nEnter Data : ");
scanf("%s",&m);
lenm=strlen(m);
printf("\n1.CRC-10\n2.CRC-16\n3.CRC-CCITT\n");
printf("\nEnter choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
genP[0]=genP[1]=genP[9]=genP[10]=genP[11]=genP[12]='1';
genP[2]=genP[3]=genP[4]=genP[5]=genP[6]=genP[7]=genP[8]='0';
lengP=13;
break;
case 2:
genP[0]=genP[1]=genP[14]=genP[16]='1';
genP[2]=genP[3]=genP[4]=genP[5]=genP[6]=genP[7]=genP[8]=genP[9]=genP[10]=genP[11]=genP[12]=genP[13]=genP[15]='0';
lengP=17;
break;
case 3:
genP[0]=genP[4]=genP[11]=genP[16]='1';
genP[2]=genP[3]=genP[4]=genP[5]=genP[6]=genP[7]=genP[8]=genP[9]=genP[10]=genP[12]=genP[13]=genP[14]=genP[15]='0';
lengP=17;
break;
default:
printf("\n\nInvalid Choice ");
break;
}
}
void Xor()
{
int k;
for(k=0;k<lengP;k++)
Checks[k]=(( Checks[k]==genP[k])?'0':'1');
}
void CRC()
{
for(i=0;i<lengP;i++)
Checks[i]=m[i];
while(i<lenm+lengP)
{
if(Checks[0]=='1')
Xor();
for(j=0;j<lengP-1;j++)
Checks[j]=Checks[j+1];
Checks[j]=m[i++];
}
}
int main()
{
GetData();
for(i=lenm;i<lenm+lengP-1;i++)
m[i]='0';
printf("\n\nData Modified As : %s\n",m);
CRC();
printf("\nCheckSum is : %s\n",Checks);
for(i=lenm;i<lenm+lengP-1;i++)
m[i]=Checks[i-lenm];
printf("\nFinal CodeWord Is : %s\n",m);
printf("\nWant To Do Error Detection at Receiver Side : \n1.YES\n2.NO\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the Data Received : ");
scanf("%s",&Rd);
lenRd=strlen(Rd);
for(i=0;i<lenm+lengP-1;i++)
m[i]='0';
for(i=0;i<lenRd;i++)
m[i]=Rd[i];
CRC();
for(i=0;(Checks[i]!='1')&&(i<lengP-1);i++);
if(i<lengP-1)
printf("\n\nError Detected\n\n");
else
printf("\nNo Error Detected\n\n");
break;
case 2:
break;
default:
printf("\nInvalid Choice ");
break;
}
}
#include<stdio.h>
struct FRAME
{
char flag;
char Data[50];
}F;
void send(char[]);
void receive();
char escape;
int main()
{
char data[50];
printf("Enter flag : ");
F.flag=getchar();
printf("Enter Escape Character : ");
fflush(stdin);
escape=getchar();
fflush(stdin);
printf("Enter Data : ");
scanf("%s",data);
send(data);
printf("\n\nSent FRAME = %c%s%c",F.flag,F.Data,F.flag);
receive();
return 0;
}
void send(char Data[])
{
int i,j;
for(i=0,j=0;i<=strlen(Data);i++,j++)
{
if(Data[i]==F.flag || Data[i]==escape)
{
F.Data[j]=escape;
F.Data[++j]=Data[i];
}
else
{
F.Data[j]=Data[i];
}
}
}
void receive()
{
int i,j;
char DestuffedString[50];
for(i=0,j=0;i<=strlen(F.Data);i++,j++)
{
if(F.Data[i]==escape)
{
DestuffedString[j]=F.Data[++i];
}
else
{
DestuffedString[j]=F.Data[i];
}
}
printf("\n\nReceived Data = %s\n",DestuffedString);
}
2.BITSTUFF
#include <stdio.h>
void send(char[]);
void receive();
char frame[100];
char sframe[100];
int i=0,j,count=0,k=0;
int main()
{
int n;
printf("Enter the bits : ");
scanf("%s",frame);
send(frame);
printf("\n\nSent FRAME = 01111110 %s 01111110\n",sframe);
receive();
return 0;
}
void send(char tframe[])
{
for(i=0;tframe[i];i++)
{
if(tframe[i]=='1')
count++;
else
count=0;
sframe[k++]=tframe[i];
if(count==5&&(tframe[i-5]=='0'))
{
sframe[k++]='0';
count=0;
}
}
sframe[k+1]='\0';
}
void receive()
{
char Destuffedframe[100];
count=0;
for(i=0,j=0;sframe[i];i++,j++)
{
if(sframe[i]=='1')
count++;
else
count=0;
Destuffedframe[j]=sframe[i];
if(count==5&&(sframe[i-5]=='0'))
{
i++;
count=0;
}
}
Destuffedframe[j]='\0';
printf("\n\nReceived Data = 01111110 %s 01111110\n ",Destuffedframe);
}
3.SJF
#include<stdio.h>
int n,Bu[20],Twt,Ttt,A[10],Wt[10],tat[10],star[10],finish[10];
float Awt,Att;
char pname[20][20],c[20][20];
void Getdata();
void Gantt_chart();
void Calculate();
void sjf();
void Getdata()
{
int i;
printf("\n Enter the number of processes: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fflush(stdin);
printf("\n\n Enter the process name: ");
scanf("%s",&pname[i]);
printf("\n Enter The BurstTime for Process %s = ",pname[i]);
scanf("%d",&Bu[i]);
printf("\n Enter the Arrival Time for Process %s = ",pname[i]);
scanf("%d",&A[i]);
}
}
void Gantt_chart()
{
int i;
printf("\n\n\t\t\tGANTT CHART\n");
printf("\n-----------------------------------------------------------\n");
for(i=1;i<=n;i++)
printf("|\t%s\t",pname[i]);
printf("|\t\n");
printf("\n-----------------------------------------------------------\n");
printf("\n");
printf("%d\t\t",star[1]);
for(i=1;i<=n;i++)
printf("%d\t\t",finish[i]);
printf("\n--------------------------------------------------------------------\n");
printf("\n");
}
void Calculate()
{
int i;
for(i=1;i<=n;i++)
{
if(i==1)
star[i]=A[i];
else
star[i]=finish[i-1];
Wt[i]=star[i]-A[i];
finish[i]=star[i]+Bu[i];
tat[i]=finish[i]-A[i];
}
printf("\nPName Arrtime Burtime WaitTime Start TAT Finish");
for(i=1;i<=n;i++)
{
printf("\n%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d",pname[i],A[i],Bu[i],Wt[i],star[i],tat[i],finish[i]);
Twt+=Wt[i];
Ttt+=tat[i];
}
printf("\n\nAverage Waiting time:%f",(float)Twt/n);
printf("\n\nAverage Turn Around Time:%f",(float)Ttt/n);
}
void sjf()
{
int i,j,temp, temp1;
Twt=0;
Ttt=0;
printf("\n\n Shortest Job Fisrt ALGORITHM\n\n");
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(Bu[i]>Bu[j])
{
temp=Bu[i];
temp1=A[i];
Bu[i]=Bu[j];
A[i]=A[j];
Bu[j]=temp;
A[j]=temp1;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);
}
}
}
Calculate();
Gantt_chart();
}
int main()
{
int ch;
Getdata();
sjf();
}
4.FCFS
#include<stdio.h>
#include<string.h>
int n,Bu[20],Twt,Ttt,A[10],Wt[10],tat[10],star[10],finish[10];
float Awt,Att;
char pname[20][20],c[20][20];
void Getdata();
void Gantt_chart();
void Calculate();
void fcfs();
void Getdata()
{
int i;
printf("\n Enter the number of processes: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fflush(stdin);
printf("\n\n Enter the process name: ");
scanf("%s",&pname[i]);
printf("\n Enter The BurstTime for Process %s = ",pname[i]);
scanf("%d",&Bu[i]);
printf("\n Enter the Arrival Time for Process %s = ",pname[i]);
scanf("%d",&A[i]);
}
}
void Gantt_chart()
{
int i;
printf("\n\n\t\t\tGANTT CHART\n");
printf("\n-----------------------------------------------------------\n");
for(i=1;i<=n;i++)
printf("|\t%s\t",pname[i]);
printf("|\t\n");
printf("\n-----------------------------------------------------------\n");
printf("\n");
printf("%d\t\t",star[1]);
for(i=1;i<=n;i++)
printf("%d\t\t",finish[i]);
printf("\n--------------------------------------------------------------------\n");
printf("\n");
}
void Calculate()
{
int i;
for(i=1;i<=n;i++)
{
if(i==1)
star[i]=A[i];
else
star[i]=finish[i-1];
Wt[i]=star[i]-A[i];
finish[i]=star[i]+Bu[i];
tat[i]=finish[i]-A[i];
}
printf("\nPName Arrtime Burtime WaitTime Start TAT Finish");
for(i=1;i<=n;i++)
{
printf("\n%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d",pname[i],A[i],Bu[i],Wt[i],star[i],tat[i],finish[i]);
Twt+=Wt[i];
Ttt+=tat[i];
}
printf("\n\nAverage Waiting time:%f",(float)Twt/n);
printf("\n\nAverage Turn Around Time:%f",(float)Ttt/n);
}
void fcfs()
{
int i,j,temp, temp1;
Twt=0;
Ttt=0;
printf("\n\n FIRST COME FIRST SERVED ALGORITHM\n\n");
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(A[i]>A[j])
{
temp=Bu[i];
temp1=A[i];
Bu[i]=Bu[j];
A[i]=A[j];
Bu[j]=temp;
A[j]=temp1;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);
}
}
}
Calculate();
Gantt_chart();
}
int main()
{
int ch;
Getdata();
fcfs();
}
5.PRIORITY
#include<stdio.h>
#include<string.h>
int n,Bu[20],Twt,Ttt,A[10],Wt[10],tat[10],star[10],finish[10],prior[10];
float Awt,Att;
char pname[20][20],c[20][20];
void Getdata();
void Gantt_chart();
void Calculate();
void Priority();
void Getdata()
{
int i;
printf("\n Enter the number of processes: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fflush(stdin);
printf("\n\n Enter the process name: ");
scanf("%s",&pname[i]);
printf("\n Enter The priority for Process %s = ",pname[i]);
scanf("%d",&prior[i]);
printf("\n Enter The BurstTime for Process %s = ",pname[i]);
scanf("%d",&Bu[i]);
printf("\n Enter the Arrival Time for Process %s = ",pname[i]);
scanf("%d",&A[i]);
}
}
void Gantt_chart()
{
int i;
printf("\n\n\t\t\tGANTT CHART\n");
printf("\n-----------------------------------------------------------\n");
for(i=1;i<=n;i++)
printf("|\t%s\t",pname[i]);
printf("|\t\n");
printf("\n-----------------------------------------------------------\n");
printf("\n");
printf("%d\t\t",star[1]);
for(i=1;i<=n;i++)
printf("%d\t\t",finish[i]);
printf("\n--------------------------------------------------------------------\n");
printf("\n");
}
void Calculate()
{
int i;
for(i=1;i<=n;i++)
{
if(i==1)
star[i]=A[i];
else
star[i]=finish[i-1];
Wt[i]=star[i]-A[i];
finish[i]=star[i]+Bu[i];
tat[i]=finish[i]-A[i];
}
printf("\nPName Priority Arrtime Burtime WaitTime Start TAT Finish");
for(i=1;i<=n;i++)
{
printf("\n%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d",pname[i],prior[i],A[i],Bu[i],Wt[i],star[i],tat[i],finish[i]);
Twt+=Wt[i];
Ttt+=tat[i];
}
printf("\n\nAverage Waiting time:%f",(float)Twt/n);
printf("\n\nAverage Turn Around Time:%f",(float)Ttt/n);
}
void Priority()
{
int i,j,temp, temp1,temp2;
Twt=0;
Ttt=0;
printf("\n\n Priority Based Sheduling Algorithm \n\n");
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(prior[i]>prior[j])
{
temp=Bu[i];
temp1=A[i];
temp2=prior[i];
Bu[i]=Bu[j];
A[i]=A[j];
prior[i]=prior[j];
Bu[j]=temp;
A[j]=temp1;
prior[j]=temp2;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);
}
}
}
Calculate();
Gantt_chart();
}
int main()
{
int ch;
Getdata();
Priority();
}
6.ROUNDROBIN
#include<stdio.h>
#include<string.h>
int n,j=1,flag=0,Bu[20],tBu[10],Twt,Ttt,A[10],Wt[10],tat[10],finish[10],ts,ganttStatBuTime[50],time;
char pname[20][20],c[20][20],ganttP[50][50];
void Getdata();
void Gantt_chart();
void Calculate();
void RR();
void Getdata()
{
int i;
printf("\n Enter the number of processes: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fflush(stdin);
printf("\n\n Enter the process name: ");
scanf("%s",&pname[i]);
printf("\n Enter The BurstTime for Process %s = ",pname[i]);
scanf("%d",&Bu[i]);
printf("\n Enter the Arrival Time for Process %s = ",pname[i]);
scanf("%d",&A[i]);
tBu[i]=Bu[i];
}
printf("\n Enter Time slice = ");
scanf("%d",&ts);
}
void Gantt_chart()
{
int i;
printf("\n\n\t\t\tGANTT CHART\n");
printf("\n----------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
for(i=1;i<j;i++)
printf("|\t%s\t",ganttP[i]);
printf("|\t\n");
printf("\n---------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("\n");
for(i=1;i<j;i++)
printf("%d\t\t",ganttStatBuTime[i]);
printf("%d",time);
printf("\n------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("\n");
}
void Calculate()
{
int i,remain=n;
printf("\nPName Artime Burtime WaitTime Start TAT Finish");
for(time=A[1],i=1;remain!=0;)
{
if(tBu[i]<=ts && tBu[i]>0)
{
strcpy(ganttP[j],pname[i]);
ganttStatBuTime[j++]=time;
time+=tBu[i];
tBu[i]=0;
flag=1;
}
else if(tBu[i]>0)
{
strcpy(ganttP[j],pname[i]);
ganttStatBuTime[j++]=time;
tBu[i]-=ts;
time+=ts;
}
if(tBu[i]==0 && flag==1)
{
remain--;
printf("\n%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d",pname[i],A[i],Bu[i],time-A[i]-Bu[i],ganttStatBuTime[i],time-A[i],time);
Twt+=time-A[i]-Bu[i];
Ttt+=time-A[i];
flag=0;
}
if(i==n)
i=1;
else if(A[i+1]<=time)
i++;
}
printf("\n\nAverage Waiting time:%f",(float)Twt/n);
printf("\n\nAverage Turn Around Time:%f",(float)Ttt/n);
}
void RR()
{
int i,j,temp, temp1;
Twt=0;
Ttt=0;
printf("\n\n Round Robin Scheduling ALGORITHM\n\n");
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(A[i]>A[j])
{
temp=Bu[i];
temp1=A[i];
Bu[i]=Bu[j];
tBu[i]=tBu[j];
A[i]=A[j];
Bu[j]=temp;
tBu[j]=temp;
A[j]=temp1;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);
}
}
}
Calculate();
Gantt_chart();
}
int main()
{
int ch;
Getdata();
RR();
}
7.CYCLIC REDUNDANCY CHECK
#include<stdio.h>
#include<string.h>
void GetData();
void Xor();
void CRC();
char m[50],Checks[50],genP[50],Rd[50];
int lenm,i,j,lengP,ch,lenRd;
void GetData()
{
printf("\nEnter Data : ");
scanf("%s",&m);
lenm=strlen(m);
printf("\n1.CRC-10\n2.CRC-16\n3.CRC-CCITT\n");
printf("\nEnter choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
genP[0]=genP[1]=genP[9]=genP[10]=genP[11]=genP[12]='1';
genP[2]=genP[3]=genP[4]=genP[5]=genP[6]=genP[7]=genP[8]='0';
lengP=13;
break;
case 2:
genP[0]=genP[1]=genP[14]=genP[16]='1';
genP[2]=genP[3]=genP[4]=genP[5]=genP[6]=genP[7]=genP[8]=genP[9]=genP[10]=genP[11]=genP[12]=genP[13]=genP[15]='0';
lengP=17;
break;
case 3:
genP[0]=genP[4]=genP[11]=genP[16]='1';
genP[2]=genP[3]=genP[4]=genP[5]=genP[6]=genP[7]=genP[8]=genP[9]=genP[10]=genP[12]=genP[13]=genP[14]=genP[15]='0';
lengP=17;
break;
default:
printf("\n\nInvalid Choice ");
break;
}
}
void Xor()
{
int k;
for(k=0;k<lengP;k++)
Checks[k]=(( Checks[k]==genP[k])?'0':'1');
}
void CRC()
{
for(i=0;i<lengP;i++)
Checks[i]=m[i];
while(i<lenm+lengP)
{
if(Checks[0]=='1')
Xor();
for(j=0;j<lengP-1;j++)
Checks[j]=Checks[j+1];
Checks[j]=m[i++];
}
}
int main()
{
GetData();
for(i=lenm;i<lenm+lengP-1;i++)
m[i]='0';
printf("\n\nData Modified As : %s\n",m);
CRC();
printf("\nCheckSum is : %s\n",Checks);
for(i=lenm;i<lenm+lengP-1;i++)
m[i]=Checks[i-lenm];
printf("\nFinal CodeWord Is : %s\n",m);
printf("\nWant To Do Error Detection at Receiver Side : \n1.YES\n2.NO\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the Data Received : ");
scanf("%s",&Rd);
lenRd=strlen(Rd);
for(i=0;i<lenm+lengP-1;i++)
m[i]='0';
for(i=0;i<lenRd;i++)
m[i]=Rd[i];
CRC();
for(i=0;(Checks[i]!='1')&&(i<lengP-1);i++);
if(i<lengP-1)
printf("\n\nError Detected\n\n");
else
printf("\nNo Error Detected\n\n");
break;
case 2:
break;
default:
printf("\nInvalid Choice ");
break;
}
}
0 comments:
Post a Comment