Os and CN Lab
Os and CN Lab
OPERATING SYSTEMS
&
COMPUTER NETWORKS LAB
THROUGH LINUX
LAB MANUAL
WEEK1:
Basic commands in Linux
-i : (interactive flag) The interactive flag with mv command is useful to warn the user
that the destination file already exists.
-f : (force) this is used to forcibly overwrite the already existed file.
Example: $mv -i file1 file2
c)rm –In Unix file system, to remove files we use this command.
Syntax: $rm [-option] <filename>
Options:
-f : (force) forcibly removes the file though it has all r, w, x permissions.
-r : (recursive removal) removes all files and empty directories. Even if the directory has files, it
removes the files first and then removes the directory.
-i : (interactive removal) remove command with interactive flag asks the user before removing
the file or directory.
Example:rmdir directory1
Week 2: Simulate the following CPU Scheduling Algorithms using C. a)FCFS
b)SJF
Algorithm for SJF
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time
Step 4: Start the Ready Q according the shortest Burst time by sorting according to lowest to
highest burst time.
Step 5: Set the waiting time of the first process as ‘0’ and its turnaround time as its burst
time.
Step 6: For each process in the ready queue, calculate
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)
(b) Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
Step 6: Calculate
(a) Average waiting time = Total waiting Time / Number of process
(b) Average Turnaround time = Total Turnaround Time / Number of process
Step 7: Stop the process
/* SJF SCHEDULING ALGORITHM */
#include<stdio.h>
void main()
{
int i,j,k,n,sum,wt[10],tt[10],twt,ttat;
int t[10],p[10];
float awt,atat;
clrscr();
printf("Enter number of process\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Burst Time of Process %d",i);
scanf("\n %d",&t[i]);
}
for(i=0;i<n;i++)
p[i]=i;
for(i=0;i<n;i++)
{
for(k=i+1;k<n;k++)
{
if(t[i]>t[k])
{
int temp;
temp=t[i];
t[i]=t[k];
t[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
}
printf("\n\n SHORTEST JOB FIRST SCHEDULING ALGORITHM");
printf("\n PROCESS ID \t BURST TIME \t WAITING TIME \t
TURNAROUND TIME \n\n");
wt[0]=0;
for(i=0;i<n;i++)
{
sum=0;
for(k=0;k<i;k++)
{
wt[i]=sum+t[k];
sum=wt[i];
}
}
for(i=0;i<n;i++)
{
tt[i]=t[i]+wt[i];
}
for(i=0;i<n;i++)
{
printf("%5d \t\t5%d \t\t %5d \t\t %5d \n\n",p[i],t[i],wt[i],tt[i]);
}
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tt[i];
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n AVERAGE WAITING TIME %4.2f",awt);
printf("\n AVERAGE TURN AROUND TIME %4.2f",atat);
getch();
}
}
OUTPUT:
1 3 0 3
0 4 3 7
2 5 7 12
#include<stdio.h>
void main()
{
int i,n,sum,wt,tat,twt,ttat;
int t[10];
float awt,atat;
clrscr();
printf("Enter number of processors:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Burst Time of the process %d",i+1);
scanf("\n %d",&t[i]);
}
printf("\n\n FIRST COME FIRST SERVE SCHEDULING ALGORITHM \n");
printf("\n Process ID \t Waiting Time \t Turn Around Time \n");
printf("1 \t\t 0 \t\t %d \n",t[0]);
sum=0;
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
{
sum+=t[i-1];
wt=sum;
tat=sum+t[i];
twt=twt+wt;
ttat=ttat+tat;
printf("\n %d \t\t %d \t\t %d",i+1,wt,tat);
printf("\n\n");
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n Average Waiting Time %4.2f",awt);
printf("\n Average Turnaround Time %4.2f",atat);
getch();
}
OUTPUT:
2 2 7
3 7 11
Algorithm for RR
#include<stdio.h>
#include<conio.h>
void main()
{
int ts,pid[10],need[10],wt[10],tat[10],i,j,n,n1;
int bt[10],flag[10],ttat=0,twt=0;
float awt,atat;
clrscr();
printf("\t\t ROUND ROBIN SCHEDULING \n");
printf("Enter the number of Processors \n");
scanf("%d",&n);
n1=n;
}
for(i=1;i<=n1;i++)
{
tat[i]=wt[i]+bt[i];
twt=twt+wt[i];
ttat=ttat+tat[i];
}
awt=(float)twt/n1;
atat=(float)ttat/n1;
printf("\n\n ROUND ROBIN SCHEDULING ALGORITHM \n\n");
printf("\n\n Process \t Process ID \t BurstTime \t Waiting Time \t TurnaroundTime \n ");
for(i=1;i<=n1;i++)
{
printf("\n %5d \t %5d \t\t %5d \t\t %5d \t\t %5d \n", i,pid[i],bt[i],wt[i],tat[i]);
}
printf("\n The average Waiting Time=4.2f",awt);
printf("\n The average Turn around Time=4.2f",atat);
getch();
}
OUTPUT:
1 5 10 15 25
2 6 15 25 40
3 7 20 25 45
4 8 25 20 45
The average Waiting Time=4.2f
The average Turn around Time=4.2f
Algorithm:
Step 1: Read all the necessary input from the keyboard.
Step 2: Pages - Logical memory is broken into fixed - sized blocks.
Step 3: Frames – Physical memory is broken into fixed – sized blocks.
Step 4: Calculate the physical address using the following
Physical address = ( Frame number * Frame size ) + offset
Step 5: Display the physical address.
Step 6: Stop the process.
ftable[i] = 32555;
for (i=0;i<page;i++)
{
printf("\n\nEnter the Frame number where page %d must be placed: ",i);
scanf("%d",&frameno);
ftable[frameno] = i;
if(ptable[i].pbit == -1)
{
ptable[i].fno = frameno;
ptable[i].pbit = 1;
}
}
getch();
// clrscr();
printf("\n\nPAGE TABLE\n\n");
printf("PageAddress FrameNo. PresenceBit\n\n");
for (i=0;i<page;i++)
printf("%d\t\t%d\t\t%d\n",i,ptable[i].fno,ptable[i].pbit);
printf("\n\n\n\tFRAME TABLE\n\n");
printf("FrameAddress PageNo\n\n");
for(i=0;i<frame;i++)
printf("%d\t\t%d\n",i,ftable[i]);
}
void cphyaddr()
{
int laddr,paddr,disp,phyaddr,baddr;
getch();
// clrscr();
printf("\n\n\n\tProcess to create the Physical Address\n\n");
printf("\nEnter the Base Address: ");
scanf("%d",&baddr);
printf("\nEnter theLogical Address: ");
scanf("%d",&laddr);
cphyaddr();
getch();
}
OUTPUT:
Algorithm:
FIFO:
Step 1: Create a queue to hold all pages in memory
Step 2: When the page is required replace the page at the head of the queue
Step 3: Now the new page is inserted at the tail of the queue
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
void main()
{
clrscr();
printf("\n \t\t\t FIFI PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of frames....");
scanf("%d",&nof);
printf("Enter number of reference string..\n");
scanf("%d",&nor);
printf("\n Enter the reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\nThe given reference string:");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
frm[i]=-1;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference np%d->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
pf++;
victim++;
victim=victim%nof;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n\n\t\t No.of pages faults...%d",pf);
getch();
}
OUTPUT:
Reference np5-> 5 -1 -1 -1
Reference np6-> 5 6 -1 -1
Reference np4-> 5 6 4 -1
Reference np1-> 5 6 4 1
Reference np2-> 2 6 4 1
Reference np3-> 2 3 4 1
Algorithm:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();
void main()
{
clrscr();
printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of Frames....");
scanf("%d",&nof);
printf(" Enter no.of reference string..");
scanf("%d",&nor);
printf("\n Enter reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\n\n\t\t LRU PAGE REPLACEMENT ALGORITHM ");
printf("\n\t The given reference string:");
printf("\n………………………………..");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
{
frm[i]=-1;
lrucal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference NO %d->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=lruvictim();
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
recent[ref[i]]=i;
}
printf("\n\n\t No.of page faults...%d",pf);
getch();
}
int lruvictim()
{
int i,j,temp1,temp2;
for(i=0;i<nof;i++)
{
temp1=frm[i];
lrucal[i]=recent[temp1];
}
temp2=lrucal[0];
for(j=1;j<nof;j++)
{
if(temp2>lrucal[j])
temp2=lrucal[j];
}
for(i=0;i<nof;i++)
if(ref[temp2]==frm[i])
return i;
return 0;
}
OUTPUT:
LRU PAGE REPLACEMENT ALGORITHM
Enter no.of Frames....3
Enter no.of reference string..6
Enter reference string..
654231
LRU PAGE REPLACEMENT ALGORITHM
The given reference string:
…………………. 6 5 4 2 3 1
Reference NO 6-> 6 -1 -1
Reference NO 5-> 6 5 -1
Reference NO 4-> 6 5 4
Reference NO 2-> 2 5 4
Reference NO 3-> 2 3 4
Reference NO 1-> 2 3 1
No.of page faults...6
Algorithm:
Optimal algorithm
Here we select the page that will not be used for the longest period of time.
OPTIMAL:
Step 1: Create a array
Step 2: When the page fault occurs replace page that will not be used for the longest
period of time
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],optcal[50],count=0;
int optvictim();
void main()
{
clrscr();
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHN");
printf("\n.................................");
printf("\nEnter the no.of frames");
scanf("%d",&nof);
printf("Enter the no.of reference string");
scanf("%d",&nor);
printf("Enter the reference string");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
clrscr();
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHM");
printf("\n................................");
printf("\nThe given string");
printf("\n....................\n");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=0;i<nof;i++)
{
frm[i]=-1;
optcal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\tref no %d ->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=optvictim(i);
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n Number of page faults: %d",pf);
getch();
}
int optvictim(int index)
{
int i,j,temp,notfound;
for(i=0;i<nof;i++)
{
notfound=1;
for(j=index;j<nor;j++)
if(frm[i]==ref[j])
{
notfound=0;
optcal[i]=j;
break;
}
if(notfound==1)
return i;
}
temp=optcal[0];
for(i=1;i<nof;i++)
if(temp<optcal[i])
temp=optcal[i];
for(i=0;i<nof;i++)
if(frm[temp]==frm[i])
return i;
return 0;
}
OUTPUT:
Algorithm:
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
clrscr();
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("Enter file name:");
scanf("%d",&x);
printf("File name is:%d",x);
printf("length is:%d",b[x-1]);
printf("blocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
}
OUTPUT:
Enter no.of files: 2
Enter no. of blocks occupied by file1 4
Enter the starting block of file1 2
Enter no. of blocks occupied by file2 10
Enter the starting block of file2 5
Filename Start block length
1 2 4
2 5 10
Enter file name: rajesh
File name is:12803 length is:0blocks occupied
Algorithm:
Step 1: Start.
Step 2: Let n be the size of the buffer
Step 3: check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer
Step 6: If the buffer is full the producer has to wait
Step 7: Check there is any cosumer.If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer
Step 9: If the buffer is empty, the consumer has to wait.
Step 10: Repeat checking for the producer and consumer till required
Step 11: Terminate the process.
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}
OUTPUT:
Enter no. of files:2
Enter starting block and size of file1: 2 5
Enter blocks occupied by file1:10
enter blocks of file1:3
2 5 4 6 7 2 6 4 7
Enter starting block and size of file2: 3 4
Enter blocks occupied by file2:5
enter blocks of file2: 2 3 4 5 6
File index length
1 2 10
2 3 5
Enter file name: venkat
file name is:12803
Index is:0Block occupied are:
Algorithm:
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}
OUTPUT:
Enter no. of files:2
Enter file name:venkat
Enter starting block:20
Enter no.of blocks:6
Enter block numbers: 4
12
15
45
32
25
Enter file name:rajesh
Enter starting block:12
Enter no.of blocks:5
Enter block numbers:6
5
4
3
2
File start size block
venkat 20 6 4--->12--->15--->45--->32--->25
rajesh 12 5 6--->5--->4--->3--->2
Banker’s Algorithm:
When a new process enters a system, it must declare the maximum number of instances of
each resource type it needed. This number may exceed the total number of resources in the system.
When the user request a set of resources, the system must determine whether the allocation of each
resources will leave the system in safe state. If it will the resources are allocation; otherwise the
process must wait until some other process release the resources.
Data structures
n-Number of process, m-number of resource types.
Available: Available[j]=k, k – instance of resource type Rj is available.
Max: If max[i, j]=k, Pi may request at most k instances resource Rj.
Allocation: If Allocation [i, j]=k, Pi allocated to k instances of resource Rj
Need: If Need[I, j]=k, Pi may need k more instances of resource type Rj,
Need[I, j]=Max[I, j]-Allocation[I, j];
Safety Algorithm
1. Work and Finish be the vector of length m and n respectively, Work=Available and
Finish[i] =False.
2. Find an i such that both
Finish[i] =False
Need<=Work
If no such I exists go to step 4.
3. work=work+Allocation, Finish[i] =True;
4. if Finish[1]=True for all I, then the system is in safe state.
Algorithm:
/* BANKER’S ALGORITHM */
#include<stdio.h>
#include<conio.h>
struct da
{
int max[10],a1[10],need[10],before[10],after[10];
}p[10];
void main()
{
int i,j,k,l,r,n,tot[10],av[10],cn=0,cz=0,temp=0,c=0;
clrscr();
printf("\n ENTER THE NO. OF PROCESSES:");
scanf("%d",&n);
printf("\n ENTER THE NO. OF RESOURCES:");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("PROCESS %d \n",i+1);
for(j=0;j<r;j++)
{
printf("MAXIMUM VALUE FOR RESOURCE %d:",j+1);
scanf("%d",&p[i].max[j]);
}
for(j=0;j<r;j++)
{
printf("ALLOCATED FROM RESOURCE %d:",j+1);
scanf("%d",&p[i].a1[j]);
p[i].need[j]=p[i].max[j]-p[i].a1[j];
}
}
for(i=0;i<r;i++)
{
printf("ENTER TOTAL VALUE OF RESOURCE %d:",i+1);
scanf("%d",&tot[i]);
}
for(i=0;i<r;i++)
{
for(j=0;j<n;j++)
temp=temp+p[j].a1[i];
av[i]=tot[i]-temp;
temp=0;
}
printf("\n\t RESOURCES ALLOCATED NEEDED TOTAL AVAIL");
for(i=0;i<n;i++)
{
printf("\n P%d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].max[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].a1[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].need[j]);
printf("\t");
for(j=0;j<r;j++)
{
if(i==0)
printf("%d",tot[j]);
}
printf(" ");
for(j=0;j<r;j++)
{
if(i==0)
printf("%d",av[j]);
}
}
printf("\n\n\t AVAIL BEFORE\T AVAIL AFTER ");
for(l=0;l<n;l++)
{
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
if(p[i].need[j] >av[j])
cn++;
if(p[i].max[j]==0)
cz++;
}
if(cn==0 && cz!=r)
{
for(j=0;j<r;j++)
{
p[i].before[j]=av[j]-p[i].need[j];
p[i].after[j]=p[i].before[j]+p[i].max[j];
av[j]=p[i].after[j];
p[i].max[j]=0;
}
printf("\n P %d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].before[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].after[j]);
cn=0;
cz=0;
c++;
break;
}
else
{
cn=0;cz=0;
}
}
}
if(c==n)
printf("\n THE ABOVE SEQUENCE IS A SAFE SEQUENCE");
else
printf("\n DEADLOCK OCCURED");
getch();
}
OUTPUT:
//TEST CASE 1:
//TEST CASE:2
Week 8: Implement the data link layer framing method such as bit stuffing.
PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
char a[20],fs[50]="",t[6],r[5];
int i,j,p=0,q=0;
clrscr();
printf("Enter bit string : ");
scanf("%s",a);
strcat(fs,"01111110");
if(strlen(a)<5)
{
strcat(fs,a);
}
else
{
for(i=0;i<strlen(a)-4;i++)
{
for(j=i;j<i+5;j++)
{
t[p++]=a[j];
}
t[p]='\0';
if(strcmp(t,"11111")==0)
{
strcat(fs,"111110");
i=j-1;
}
else
{
r[0]=a[i];
r[1]='\0';
strcat(fs,r);
}
p=0;
}
for(q=i;q<strlen(a);q++)
{
t[p++]=a[q];
}
t[p]='\0';
strcat(fs,t);
}
strcat(fs,"01111110");
printf("After stuffing : %s",fs);
getch();
}
OUTPUT:
Implement the data link layer framing method such as Character Stuffing.
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char a[30],fs[50]="",t[3],sd,ed,x[3],s[3],d[3],y[3];
int i,j,p=0,q=0;
clrscr();
printf("Enter characters to be stuffed : ");
scanf("%s",a);
printf("\nEnter a character that represents starting delimiter : ");
scanf(" %c",&sd);
printf("\nEnter a character that represents ending delimiter : ");
scanf(" %c",&ed);
x[0]=s[0]=s[1]=sd;
x[1]=s[2]='\0';
y[0]=d[0]=d[1]=ed;
d[2]=y[1]='\0';
strcat(fs,x);
for(i=0;i<strlen(a);i++)
{
t[0]=a[i];
t[1]='\0';
if(t[0]==sd)
strcat(fs,s);
else
if(t[0]==ed)
strcat(fs,d);
else
strcat(fs,t);
}
strcat(fs,y);
printf("\nAfter stuffing : %s",fs);
getch();
}
OUTPUT:
#include<stdio.h>
void main()
{
int data[7],rec[7],i,c1,c2,c3,c;
printf("this works for message of 4bits in size \nenter message bit one by one: ");
scanf("%d%d%d%d",&data[0],&data[1],&data[2],&data[4]);
data[6]=data[0]^data[2]^data[4];
data[5]=data[0]^data[1]^data[4];
data[3]=data[0]^data[1]^data[2];
printf("\nthe encoded bits are given below: \n");
for (i=0;i<7;i++) {
printf("%d ",data[i]);
}
printf("\nenter the received data bits one by one: ");
for (i=0;i<7;i++) {
scanf("%d",&rec[i]);
}
c1=rec[6]^rec[4]^rec[2]^rec[0];
c2=rec[5]^rec[4]^rec[1]^rec[0];
c3=rec[3]^rec[2]^rec[1]^rec[0];
c=c3*4+c2*2+c1 ;
if(c==0) {
printf("\ncongratulations there is no error: ");
} else {
printf("\nerron on the postion: %d\nthe correct message is \n",c);
if(rec[7-c]==0)
rec[7-c]=1; else
rec[7-c]=0;
for (i=0;i<7;i++) {
printf("%d ",rec[i]);
}
}
//getch();
}
OUTPUT:
1010101
Week 10: Implement CRC technique for any frame using generator Polynomial
PROGRAM:
#include<stdio.h>
const char * bindiv(const char *,const char *);
const char * binsub(const char *,const char *);
int f=0,ll=0;
void main()
{
char *a,p[13]="1100000001011",g[30],g1[30],yy[30]="",td[30],*aa;
int l=0,i;
clrscr();
printf("Enter transfered data : ");
scanf("%s",g);
printf("Enter received data : ");
scanf("%s",td);
strcpy(g1,g);
strcat(g,"000000000000");
printf("\n%s ) %s (",p,g);
a=bindiv(g,p);
if(strlen(a)<12)
{
for(i=strlen(a);i<12;i++)
{
yy[l++]='0';
}
yy[l]='\0';
}
strcat(yy,a);
strcat(g1,yy);
printf("\ncrc is %s",yy);
printf("\n ------------------");
strcat(td,yy);
printf("\n\n%s ) %s (",p,td);
ll=0;
aa=bindiv(td,p);
strcpy(a,aa);
printf("\n %s",a);
printf("\n -----------------");
if(f==1)
printf("\nData transfered correctly");
else
printf("\nData transfered incorrectly");
getch();
}
const char * bindiv(const char *s,const char *d)
{
int i,j,k=0,x=13,h,p=0,l;
char q[15]="",b[30],*w;
for(i=0;i<strlen(s);i++)
{
if((i+x)>strlen(s))
x=(i+x)-strlen(s)+1;
for(j=i;j<(i+x);j++)
{
b[k++]=s[j];
}
b[k]='\0';
if(ll!=0)
printf("\n %s",b);
ll=1;
if(strlen(b)==12)
{
break;
}
printf("\n %s",d);
printf("\n -----------------");
w=binsub(b,d);
k=0;i=j-1;
for(l=0;l<strlen(w);l++)
{
if(w[l]=='1')
break;
}
if(l==strlen(w))
{
f=1;
return(w);
}
for(h=l;h<strlen(w);h++)
{
q[p++]=w[h];
}
q[p]='\0';
x=13-strlen(q);
strcpy(b,"");
strcat(b,q);
k=strlen(q); p=0;
}
return(b);
}
char w[15]="",e[3],f[3],n[3];
e[0]='1';
e[1]='\0';
f[0]='0';
f[1]='\0';
for(i=0;i<strlen(x);i++)
{
if((x[i]=='1')&&(y[i]=='1'))
strcat(w,f);
else
if((x[i]=='0')&&(y[i]=='0'))
strcat(w,f);
else
strcat(w,e);
}
n[0]='\0';
n[1]='\0';
strcat(w,n);
return(w);
}
OUTPUT:
10001000000100001 ) 110110000000000000000 (
10001000000100001
-------------------------
10100000001000010
10001000000100001
--------------------------
10100000110001100
10001000000100001
--------------------------
1010001101011010
--------------------------
crc is 1010001101011010
10001000000100001 ) 110111010001101011010 (
10001000000100001
--------------------------
10101010000101001
10001000000100001
--------------------------
10001000000100001
10001000000100001
--------------------------
00000000000000000
--------------------------
Data transferred correctly
Week 11:Implement Dijkstra’s algorithm to compute the shortest path through a graph.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void sort(void);
static int dsp[10][10],nodes;
struct
{
char src;
char dest;
int length;
}
stemp,permanent[10]={' ',' ',0},temp[10]={' ',' ',-1};
static int perm,tem;
void main()
{
int i,j,k,l,m,n=0,point;
char initial,dest,path[10]={' '};
clrscr();
printf("\t\t Shortest Path (Dijkstra's algorithm)");
printf("\n************************************************ ");
printf(“\nEnter the number of nodes:”);
scanf(“%d”,&nodes);
printf(“\nEnter the adjacency matrix for the graph:\n”);
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
scanf(“%d”,&dsp[I][j]);
}
fflush(stdin);
printf("\n enter the source node:");
scanf("%c",&initial);fflush(stdin);
printf("\n Enter the destination node:");
scanf("%c",&dest);
permanent[perm].src=initial;
permanent[perm].dest=initial;
permanent[perm++].length=0;
i=permanent[perm-1].dest-97;
for(j=0;j<nodes;j++)
{
if(i!=j)
{
if(dsp[i][j]>0)
{
temp[tem].src=permanent[perm-1].src;
temp[tem].dest=j+97;
temp[tem++].length=dsp[i][j];
}
}
}
sort();
while(tem>=0)
{
j=permanent[perm-1].dest-97;
for(i=0;i<nodes;i++)
{
if(i!=initial-97)
{
if(dsp[j][i]>0)
{
l=-1;
for(k=0;k<perm;k++)
{
if(permanent[k].dest==(i+97))
l=k;
}
for(k=0;k<=tem;k++)
{
if(temp[k].dest==(i+97))
l=k;
}
if(l<0)
{
temp[tem].src=j+97;
temp[tem].dest=i+97;
for(m=0;m<perm;m++)
{
if(permanent[m].dest==temp[tem].src)
n=permanent[m].length;
}
temp[tem++].length=dsp[j][i]+n;
else
{
for(m=0;m<perm;m++)
{
if(permanent[m].dest==j+97)
{
n=permanent[m].length+dsp[j][i];break;
}
else
n=dsp[j][i];
}
if((n<temp[l].length))
{
temp[l].length=n;
temp[l].src=j+97;
temp[l].dest=i+97;
}
}
}
}
}
sort();
}
printf("\nShortest path:\n");
printf("From %c to %c is:",initial,dest);
for(i=0;i<perm-1;i++)
{
if(permanent[i].dest==dest)
{
point=i;n=i; break;
}
} i=0;
for(j=perm;j>0;j--)
{
if(permanent[j-1].dest==permanent[point].src)
{
path[i++]=permanent[point].dest;
point=j-1;
}
}
path[i]=initial;
for(j=i;j>=0;j--)
printf("%c ",path[j]);
printf("\t length=%d",permanent[n].length);
getch();
}
void sort()
{
int i,j,k;
for(i=0;i<=tem;i++)
{
k=1;
for(j=0;j<=tem;j++)
{
if((temp[j].length <= temp[j+1].length))
{
stemp=temp[j];
temp[j]=temp[j+1];
temp[j+1]=stemp; k=0;
}
}
if(k)
break;
}
permanent[perm++]=temp[tem-1];
temp[tem-1].src=' ';temp[tem-1].dest=' ';
temp[tem-1].length=-1; tem--;
}
Network topology:
------------------------ 1
1 b c 1
a 1 e 3 f
2 2
d
Output of execution1:
----------------------------
Shortest path:
From a to f is: a b c e f length=6
Network Topology:
B 7 C
2 2 3 3
A E 2 F D
6 1 2 2
G 4 H
Output of execution2:
----------------------------
Shortest Path (Dijkstra's algorithm)
**************************************************************
Enter the number of nodes:8
Enter the adjacency matrix for the graph(-1 for no edge):
Enter the adjacency matrix for the graph
0 2 -1 -1 -1 -1 6 -1
2 0 7 -1 2 -1 1 -1
-1 7 0 3 -1 3 -1 -1
-1 -1 3 0 -1 -1 -1 2
-1 2 -1 -1 0 2 1 -1
-1 -1 3 -1 2 0 -1 2
6 -1 -1 -1 1 -1 0 4
-1 -1 -1 2 -1 2 4 0
Enter the source node: a
Shortest path:
From a to d is: a b e f h d length=10
Week 12: Take an example subnet graph with weights indicating delay between nodes.
Now obtain Routing table art each node using distance vector routing algorithm.
PROGRAM:
#include<stdio.h>
#define e 10000
#define nodes1 40
int topology[nodes1][nodes1];
static int l,n,neighb,nodes;
static char neigh[nodes1];
struct {
char name;
int delay;
}
dv[nodes1];
void main()
{
int i,j,k[nodes1*nodes1],add=0,src1;
char src='a';
clrscr();
printf("\t\t\tDistance Vector Routing\n");
printf("***********************************************************");
printf("\nEnter the number of nodes:");
scanf("%d",&nodes);fflush(stdin);
printf("\nEnter the node for which Distance Vector table is needed:");
scanf("%c",&src);fflush(stdin);
printf("\nEnter the no. of neighbours to %c:",src);
scanf("%d",&neighb); fflush(stdin);
printf("\nEnter the names of the neighbours(in alphabetic order):");
neigh[0]=src;
for(i=1;i<neighb+1;i++)
{
scanf("%c",&neigh[i]);
fflush(stdin);
}
printf("\n Enter the distance vectors of the source and the neighbouring
nodes\n");
for(i=0;i<nodes*(neighb+1);i++)
{
scanf("%d",&k[i]);
}
src1=src;
l=nodes;n=1;
for(i=0;i<nodes;i++)
{
if(i==src1-97)
{
for(j=0;j<nodes;j++)
{
if(k[j]<0)
topology[i][j]=e;
else
topology[i][j]=k[j];
}
}
else if(i==neigh[n]-97)
{
for(j=0;j<nodes;j++)
{ if(k[l]<0)
topology[i][j]=e;
else
topology[i][j]=k[l];
l++;
} n++;
}
else
{
for(j=0;j<nodes;j++)
topology[i][j]=e;
}
}
i=src1-97;
for(j=0;j<nodes;j++)
{ dv[j].name=src1;
dv[j].delay=topology[i][j];
}
k[nodes*nodes]=e; n=1;
for(i=0;i<nodes;i++)
{
if((i==neigh[n]-97))
{
add=topology[src1-97][i];
for(j=0;j<nodes;j++)
{
k[j]=add+topology[i][j];
}
for(j=0;j<nodes;j++)
{
if(k[j]<dv[j].delay)
{
dv[j].name=i+97;
dv[j].delay=k[j];
}
}
if(i!=src1-97)
n++;
}
}
}
A D
Enter the distance vectors of the source and the neighbouring nodes
Starting with source,then with neighbouring nodes in alphabetical order:
-1 1 2 0
1011
1102
A B C D
E F G H
I J K L
Enter the distance vectors of the source and the neighbouring nodes
Starting with source,then with neighbouring nodes in alphabetical order:
8 -1 -1 -1 -1 -1 -1 12 10 0 6 -1
0 12 25 40 14 23 18 17 21 9 24 29
20 31 19 8 30 19 6 0 14 7 22 9
24 36 18 27 7 20 31 20 0 11 22 33
21 28 36 24 22 40 31 19 22 10 0 9
To a: from: j 8
To b: from: a 20
To c: from: a 33
To d: from: a 48
To e: from: a 22
To f: from: a 31
To g: from: a 26
To h: from: j 12
To i: from: j 10
To j: from: j 0
To k: from: j 6
To l: from: a 7
Week 13: Take an example subnet of hosts. Obtain broadcast tree for it.
PROGRAM:
#include<stdio.h>
#include<conio.h>
int max();
int distance[20];
int n;
void main()
{
int adj[20][20],adj1[20][20],flag[30];
int i,j,root,x;
int source,count=1,y=0;
printf("Enter no of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&adj[i][j]);
}
}
printf("Enter the source for broadcasting");
scanf("%d",&source);
for(i=0;i<n;i++)
{
flag[i]=0;
}
for(root=0;root<n;root++)
{
for(i=0;i<n;i++)
{
distance[i]=adj[root][i];
}
x=min();
for(i=0;i<n;i++)
{
if(distance[i]==x)
{
adj1[root][i]=x;
adj1[i][root]=x;
}
else
{
adj1[root][i]=0;
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(adj1[i][j]!=0)
{
adj1[j][i]=adj[i][j];
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",adj1[i][j]);
}
printf("\n");
}
root=source;
flag[root]=1;
while(count!=y)
{
for(i=0;i<n;i++)
{
if(adj1[root][i]!=0 && flag[root]==1 && flag[i]!=1)
{
printf("%d sends message to %d \n",root,i);
flag[i]=1;
}
}
if(root<n-1)
{
root++;
}
else
{
root=0;
}
for(i=0;i<n;i++)
{
if(flag[i]==0)
{
break;
}
}
if(i==n)
{
count=y;
}
}
}
int min()
{
int i,j=0;
int mini;
int distance1[10];
for(i=0;i<n;i++)
{
if(distance[i]!=0)
{
distance1[j]=distance[i];
j++;
} }
mini=distance1[0];
for(i=1;i<j;i++)
{
if(distance1[i]<mini)
{
mini=distance1[i];
}
} return(mini);
}
OUTPUT:
Enter no of nodes2
Enter the adjacency matrix
02
20
Enter the source for broadcasting1
Given adjacency matrix is
02
20
2
0 1
2
0 1
Enter no of nodes3
Enter the adjacency matrix
012
105
250
Enter the source for broadcasting2
Given adjacency matrix is
012
105
250
1
0 1
2 5
0 1 1
2 2
2 sends message to 0
0 sends message to 1
Enter no of nodes4
Enter the adjacency matrix
0687
6050
8504
7040
Enter the source for broadcasting2
Given adjacency matrix is
0687
6050
8504
7040
4 2
3
7 8 5
0 1
6
6050
0504
0040
4
2
3
5
0 1
6
2 sends message to 1
2 sends message to 3
1 sends message to 0
Enter no of nodes5
Enter the adjacency matrix
02050
20360
03079
56708
00980
Enter the source for broadcasting2
Given adjacency matrix is
02050
20360
03079
56708
00980
3
2
1
2
6 7 9
0
5 3 4
8
50008
00080
3 2
1
2
0
5
3 4
8
2 sends message to 1
1 sends message to 0
0 sends message to 3
3 sends message to 4
030400
704050
600507
000070
Enter the source for broadcasting3
2 8 5
1 3
3 4
2
7
4 5
0
2 5
1 3
3 4
2
3 sends message to 2
3 sends message to 4
4 sends message to 5
2 sends message to 1
1 sends message to 0