0% found this document useful (0 votes)
137 views70 pages

Os and CN Lab

The document provides instructions for completing lab exercises on operating systems and computer networks using Linux. It includes a list of 13 lab exercises covering topics like CPU scheduling algorithms, memory management, file allocation strategies, deadlock avoidance, data link layer methods, error detection codes, shortest path algorithms, and routing tables. For each weekly lab, it provides the learning objectives and steps to write programs in C to simulate and implement the relevant algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views70 pages

Os and CN Lab

The document provides instructions for completing lab exercises on operating systems and computer networks using Linux. It includes a list of 13 lab exercises covering topics like CPU scheduling algorithms, memory management, file allocation strategies, deadlock avoidance, data link layer methods, error detection codes, shortest path algorithms, and routing tables. For each weekly lab, it provides the learning objectives and steps to write programs in C to simulate and implement the relevant algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 70

OS & CN THROUGH LINUX

VIDYA JYOTHI INSTITUTE OF TECHNOLOGY


(Autonomous)
Aziz Nagar Gate, C.B. Post, Hyderabad-75

Department of Computer science & Engineering

Year : III B.Tech Semester : I

OPERATING SYSTEMS
&
COMPUTER NETWORKS LAB
THROUGH LINUX

LAB MANUAL

Page No 1 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

List of Lab Exercises

Week Programs Page


No
Basic commands in Linux
1 i) File handling utilities a) cat b) mv c) rm d) cp 3
ii) Directory commands a) mkdir b) cd c) ls d) rmdir
Simulate the following CPU Scheduling Algorithms
2
using C. a)FCFS b)SJF
Simulate the following CPU Scheduling Algorithms
3
using C. a)Priority b)Round Robin
Simulate paging Technique of Memory Management
4
using C.
Write a program to implement page replacement
5
algotithas(FIFO,Optimal and LRU)
Write a C program to simulate the following file
6 allocation strategies.
a) Sequential b) Indexed c) Linked
Write a program to implement Banker’s algorithm for
7
deadlock avoidance.
Design and implement the data link layer farming
8
methods such as character stuffing and bit stuffing
9 Implementation of Hamming code algorithm
Implement on a data set of characters the CRC
10 Polynomial
CRC 12
Implement Dijkstra’s algorithm to compute the Shortest
11
path thru a graph.
Take an example subnet graph with weights indicating
12 delay between nodes.Now obtain Routing table art each
node using distance vector routing algorithm.
Take an example subnet of hosts. Obtain broadcast tree
13
for it.

Page No 2 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

WEEK1:
Basic commands in Linux

i)File handling utilities


a)cat b)mv c)rm d) cp
a) cat -In Unix file system, to create files we use this command.
Syntax: $cat <redirection symbol><filename>
Options: -v: non-printing characters.
-n: numbering the lines.

Example: $cat > file1


b)mv(move)- -In Unix file system, to move the content
Syntax: $mv [-option] [file/dir] [newfile/newdir]
Options:

-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: $rm -f file1


d)cp cp is a Linux shell command to copy files and directories
Syntax: $cp [-option] source dest

ii) Directory commands


a)mkdir b)cd c)ls d)rmdir
a)mkdir-make directory-This is used to create a directory.
Syntax: $mkdir <directory_name>
Example: mkdir directory1
b)cd-change directory -This is used to change the directory i.e getting into a particular directory.
Syntax: $cd <directory_name>
Example: $cd directory1
c)ls-list all file and directory
syntax: $ls
example: $ls
d)rmdir-remove directory This is used to remove directory.
Syntax: $rmdir <directory_name>

Page No 3 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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++)

Page No 4 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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;

Page No 5 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

atat=(float)ttat/n;
printf("\n AVERAGE WAITING TIME %4.2f",awt);
printf("\n AVERAGE TURN AROUND TIME %4.2f",atat);
getch();
}
}
OUTPUT:

Enter number of process


3

Enter the Burst Time of Process 04

Enter the Burst Time of Process 13

Enter the Burst Time of Process 25

SHORTEST JOB FIRST SCHEDULING ALGORITHM


PROCESS ID BURST TIME WAITING TIME TURNAROUND TIME

1 3 0 3

0 4 3 7

2 5 7 12

AVERAGE WAITING TIME 3.33


AVERAGE TURN AROUND TIME 7.33

Page No 6 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

Algorithm for FCFS scheduling:


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: Set the waiting of the first process as ‘0’ and its burst time as its turn around time
Step 5: for each process in the Ready Q calculate
(c) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)
(d) Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
Step 6: Calculate
(c) Average waiting time = Total waiting Time / Number of process
(d) Average Turnaround time = Total Turnaround Time / Number of process
Step 7: Stop the process

/* FCFS SCHEDULING ALGORITHM */

#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];

Page No 7 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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:

Enter number of processors:


3

Enter the Burst Time of the process 1: 2

Enter the Burst Time of the process 2: 5

Enter the Burst Time of the process 3: 4

FIRST COME FIRST SERVE SCHEDULING ALGORITHM

Process ID Waiting Time Turn Around Time


1 0 2

2 2 7

3 7 11

Average Waiting Time 3.00


Average Turnaround Time 6.67

Page No 8 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

Week 3: Simulate the following CPU Scheduling Algorithms using C. a)Priority


b)Round Robin

Algorithm for RR

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue and time quantum (or) time slice
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Calculate the no. of time slices for each process where
No. of time slice for process(n) = burst time process(n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Consider the ready queue is a circular Q, calculate
(a) Waiting time for process(n) = waiting time of process(n-1)+ burst time of process(n-1 )
+ the time difference in getting the CPU from process(n-1)
(b) Turn around time for process(n) = waiting time of process(n) + burst time of process(n)
+ the time difference in getting CPU from process(n).
Step 7: Calculate
(e) Average waiting time = Total waiting Time / Number of process
(f) Average Turnaround time = Total Turnaround Time / Number of process
Step 8: Stop the process

/* ROUND ROBIN SCHEDULING ALGORITHM */

#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;

Page No 9 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

printf("\n Enter the Timeslice \n");


scanf("%d",&ts);
for(i=1;i<=n;i++)
{
printf("\n Enter the process ID %d",i);
scanf("%d",&pid[i]);
printf("\n Enter the Burst Time for the process");
scanf("%d",&bt[i]);
need[i]=bt[i];
}
for(i=1;i<=n;i++)
{
flag[i]=1;
wt[i]=0;
}
while(n!=0)
{
for(i=1;i<=n;i++)
{
if(need[i]>=ts)
{
for(j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=ts;
}
need[i]-=ts;
if(need[i]==0)
{
flag[i]=0;
n--;
}
}
else
{
for(j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=need[i];
}
need[i]=0;
n--;
flag[i]=0;
}
}

Page No 10 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

}
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:

ROUND ROBIN SCHEDULING


Enter the number of Processors
4

Enter the Timeslice


5

Enter the process ID 1 5

Enter the Burst Time for the process 10

Enter the process ID 2 6

Enter the Burst Time for the process 15

Enter the process ID 3 7

Enter the Burst Time for the process 20

Enter the process ID 4 8

Enter the Burst Time for the process 25

ROUND ROBIN SCHEDULING ALGORITHM

Page No 11 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

Process Process ID BurstTime Waiting Time TurnaroundTime

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

Page No 12 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

Week 4: Simulate paging Technique of Memory Management using C.


Aim: To implement the Memory management policy- Paging.

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.

/* Memory Allocation with Paging Technique */


#include <stdio.h>
#include <conio.h>
struct pstruct
{
int fno;
int pbit;
}ptable[10];
int pmsize,lmsize,psize,frame,page,ftable[20],frameno;
void info()
{
printf("\n\nMEMORY MANAGEMENT USING PAGING\n\n");
printf("\n\nEnter the Size of Physical memory: ");
scanf("%d",&pmsize);
printf("\n\nEnter the size of Logical memory: ");
scanf("%d",&lmsize);
printf("\n\nEnter the partition size: ");
scanf("%d",&psize);
frame = (int) pmsize/psize;
page = (int) lmsize/psize;
printf("\nThe physical memory is divided into %d no.of frames\n",frame);
printf("\nThe Logical memory is divided into %d no.of pages",page);
}
void assign()
{
int i;
for (i=0;i<page;i++)
{
ptable[i].fno = -1;
ptable[i].pbit= -1;
}
for(i=0; i<frame;i++)

Page No 13 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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);

paddr = laddr / psize;


disp = laddr % psize;
if(ptable[paddr].pbit == 1 )
phyaddr = baddr + (ptable[paddr].fno*psize) + disp;
printf("\nThe Physical Address where the instruction present: %d",phyaddr);
}
void main()
{
clrscr();
info();
assign();

Page No 14 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

cphyaddr();
getch();
}

OUTPUT:

MEMORY MANAGEMENT USING PAGING


Enter the Size of Physical memory: 16
Enter the size of Logical memory: 8
Enter the partition size: 2
The physical memory is divided into 8 no.of frames
The Logical memory is divided into 4 no.of pages
Enter the Frame number where page 0 must be placed: 5
Enter the Frame number where page 1 must be placed: 6
Enter the Frame number where page 2 must be placed: 7
Enter the Frame number where page 3 must be placed: 2
PAGE TABLE
PageAddress FrameNo. PresenceBit
0 5 1
1 6 1
2 7 1
3 2 1
FRAME TABLE
FrameAddress PageNo
0 32555
1 32555
2 3
3 32555
4 32555
5 0
6 1
7 2
Process to create the Physical Address
Enter the Base Address: 1000
Enter theLogical Address: 3
The Physical Address where the instruction present: 1013

Page No 15 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

Week 5: Write a program to implement page replacement algotithas(FIFO,Optimal


and LRU)
AIM: To implement page replacement algorithms
FIFO (First In First Out)

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

/* FIFO Page Replacement Algorithm */

#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;

Page No 16 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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();
}

Page No 17 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

OUTPUT:

FIFO PAGE REPLACEMENT ALGORITHM

Enter no.of frames....4


Enter number of reference string..
6

Enter the reference string..


564123

The given reference string:


...................................... 5 6 4 1 2 3

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

No.of pages faults...6

Page No 18 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

Ex. No: LRU PAGE REPLACEMENT ALGORITHM

AIM: To implement page replacement algorithm


LRU (Least Recently Used)

LRU (Lease Recently Used)


Here we select the page that has not been used for the longest period of time.

Algorithm:

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
Step 4: Create a stack
Step 5: When the page fault occurs replace page present at the bottom of the stack

/* LRU Page Replacement 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++)

Page No 19 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

{
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];

Page No 20 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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;
}

Page No 21 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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

Page No 22 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

Ex. No: OPTIMAL(LFU) PAGE REPLACEMENT ALGORITHM

AIM: To implement page replacement algorithms


Optimal (The page which is not used for longest time)

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

/*OPTIMAL(LFU) page replacement 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],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;

Page No 23 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

}
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;
}

Page No 24 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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;
}

Page No 25 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

OUTPUT:

OPTIMAL PAGE REPLACEMENT ALGORITHM

Enter no.of Frames....3


Enter no.of reference string..6
Enter reference string..
654231
OPTIMAL 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

Page No 26 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

Week 6: Write a C program to simulate the following file allocation strategies.


a) Sequential b) Indexed c) Linked

AIM: Write a C Program to implement Sequential File Allocation method.

Algorithm:

Step 1: Start the program.


Step 2: Get the number of memory partition and their sizes.
Step 3: Get the number of processes and values of block size for each process.
Step 4: First fit algorithm searches all the entire memory block until a hole which is
big enough is encountered. It allocates that memory block for the requesting
process.
Step 5: Best-fit algorithm searches the memory blocks for the smallest hole which can
be allocated to requesting process and allocates if.
Step 6: Worst fit algorithm searches the memory blocks for the largest hole and
allocates it to the process.
Step 7: Analyses all the three memory management techniques and display the best
algorithm which utilizes the memory resources effectively and efficiently.
Step 8: Stop the program.

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++)

Page No 27 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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

INDEXED FILE ALLOCATION

AIM: Write a C Program to implement Indexed File Allocation method.

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.

Page No 28 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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]);

Page No 29 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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:

Exp no: LINKED FILE ALLOCATION

AIM: Write a C Program to implement Linked File Allocation method.

Algorithm:

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
Step 4: Create a stack
Step 5: When the page fault occurs replace page present at the bottom of the stack
Step 6: Stop the allocation.

PROGRAM:

#include<stdio.h>
#include<conio.h>
struct file
{

Page No 30 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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

Page No 31 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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

Page No 32 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

Week 7: Write a program to implement Banker’s algorithm for deadlock avoidance.


AIM: To implement deadlock avoidance & Prevention by using Banker’s Algorithm.

Deadlock avoidance & Dead Lock Prevention

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.

Resource request algorithm


Let Request i be request vector for the process Pi, If request i=[j]=k, then process Pi wants
k instances of resource type Rj.
1. if Request<=Need I go to step 2. Otherwise raise an error condition.
2. if Request<=Available go to step 3. Otherwise Pi must since the resources are available.
3. Have the system pretend to have allocated the requested resources to process Pi by
modifying the state as follows;
Available=Available-Request I;
Allocation I =Allocation+Request I;
Need i=Need i-Request I;
If the resulting resource allocation state is safe, the transaction is completed and process Pi is
allocated its resources. However if the state is unsafe, the Pi must wait for Request i and the old
resource-allocation state is restored.

Page No 33 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

Algorithm:

1. Start the program.


2. Get the values of resources and processes.
3. Get the avail value.
4. After allocation find the need value.
5. Check whether its possible to allocate.
6. If it is possible then the system is in safe state.
7. Else system is not in safety state.
8. If the new request comes then check that the system is in safety.
9. or not if we allow the request.
10. stop the program.

/* 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];
}
}

Page No 34 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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])

Page No 35 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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();
}

Page No 36 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

OUTPUT:

//TEST CASE 1:

ENTER THE NO. OF PROCESSES:4

ENTER THE NO. OF RESOURCES:3


PROCESS 1
MAXIMUM VALUE FOR RESOURCE 1:3
MAXIMUM VALUE FOR RESOURCE 2:2
MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:1
ALLOCATED FROM RESOURCE 2:0
ALLOCATED FROM RESOURCE 3:0
PROCESS 2
MAXIMUM VALUE FOR RESOURCE 1:6
MAXIMUM VALUE FOR RESOURCE 2:1
MAXIMUM VALUE FOR RESOURCE 3:3
ALLOCATED FROM RESOURCE 1:5
ALLOCATED FROM RESOURCE 2:1
ALLOCATED FROM RESOURCE 3:1
PROCESS 3
MAXIMUM VALUE FOR RESOURCE 1:3
MAXIMUM VALUE FOR RESOURCE 2:1
MAXIMUM VALUE FOR RESOURCE 3:4
ALLOCATED FROM RESOURCE 1:2
ALLOCATED FROM RESOURCE 2:1
ALLOCATED FROM RESOURCE 3:1
PROCESS 4
MAXIMUM VALUE FOR RESOURCE 1:4
MAXIMUM VALUE FOR RESOURCE 2:2
MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:0
ALLOCATED FROM RESOURCE 2:0
ALLOCATED FROM RESOURCE 3:2
ENTER TOTAL VALUE OF RESOURCE 1:9
ENTER TOTAL VALUE OF RESOURCE 2:3
ENTER TOTAL VALUE OF RESOURCE 3:6

Page No 37 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

RESOURCES ALLOCATED NEEDED TOTAL AVAIL


P1 322 100 222 936 112
P2 613 511 102
P3 314 211 103
P4 422 002 420

AVAIL BEFORE AVAIL AFTER


P2 010 623
P1 401 723
P3 620 934
P4 514 936

THE ABOVE SEQUENCE IS A SAFE SEQUENCE

//TEST CASE:2

ENTER THE NO. OF PROCESSES:4

ENTER THE NO. OF RESOURCES:3


PROCESS 1
MAXIMUM VALUE FOR RESOURCE 1:3
MAXIMUM VALUE FOR RESOURCE 2:2
MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:1
ALLOCATED FROM RESOURCE 2:0
ALLOCATED FROM RESOURCE 3:1
PROCESS 2
MAXIMUM VALUE FOR RESOURCE 1:6
MAXIMUM VALUE FOR RESOURCE 2:1
MAXIMUM VALUE FOR RESOURCE 3:3
ALLOCATED FROM RESOURCE 1:5
ALLOCATED FROM RESOURCE 2:1
ALLOCATED FROM RESOURCE 3:1
PROCESS 3
MAXIMUM VALUE FOR RESOURCE 1:3
MAXIMUM VALUE FOR RESOURCE 2:1
MAXIMUM VALUE FOR RESOURCE 3:4
ALLOCATED FROM RESOURCE 1:2
ALLOCATED FROM RESOURCE 2:1
ALLOCATED FROM RESOURCE 3:2
PROCESS 4

Page No 38 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

MAXIMUM VALUE FOR RESOURCE 1:4


MAXIMUM VALUE FOR RESOURCE 2:2
MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:0
ALLOCATED FROM RESOURCE 2:0
ALLOCATED FROM RESOURCE 3:2
ENTER TOTAL VALUE OF RESOURCE 1:9
ENTER TOTAL VALUE OF RESOURCE 2:3
ENTER TOTAL VALUE OF RESOURCE 3:6

RESOURCES ALLOCATED NEEDED TOTAL AVAIL


P1 322 101 221 936 110
P2 613 511 102
P3 314 212 102
P4 422 002 420

AVAIL BEFORE AVAIL AFTER


DEADLOCK OCCURED

Page No 39 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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];

Page No 40 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

}
t[p]='\0';
strcat(fs,t);
}
strcat(fs,"01111110");
printf("After stuffing : %s",fs);
getch();
}

OUTPUT:

Enter bit string: 10101111110

After stuffing: 0111111010101111101001111110

Enter bit string: 1011111011110111110

After stuffing: 0111111010111110011110111110001111110

Page No 41 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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();
}

Page No 42 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

OUTPUT:

Enter characters to be stuffed : goodday

Enter a character that represents starting delimiter : d


Enter a character that represents ending delimiter : g
After stuffing : dggooddddayg

Page No 43 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

Week 9: Implement of Hamming code algorithm

#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();
}

Page No 44 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

OUTPUT:

this works for message of 4bits in size

enter message bit one by one: 1


0
1
1
encoded bits are given below:

1010101

enter the received data bits one by one:


1
0
1
0
1
0
1

congratulations there is no error:

Page No 45 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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);

Page No 46 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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++)

Page No 47 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

{
q[p++]=w[h];
}
q[p]='\0';
x=13-strlen(q);
strcpy(b,"");
strcat(b,q);
k=strlen(q); p=0;
}
return(b);
}

const char * binsub(const char *x,const char *y)


{
int i,j=0;

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);
}

Page No 48 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

OUTPUT:

Enter transferred data: 11011


Enter received data: 11011

10001000000100001 ) 110110000000000000000 (
10001000000100001
-------------------------
10100000001000010
10001000000100001
--------------------------
10100000110001100
10001000000100001
--------------------------
1010001101011010
--------------------------
crc is 1010001101011010

10001000000100001 ) 110111010001101011010 (
10001000000100001
--------------------------
10101010000101001
10001000000100001
--------------------------
10001000000100001
10001000000100001
--------------------------
00000000000000000
--------------------------
Data transferred correctly

Page No 49 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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;

Page No 50 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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;

Page No 51 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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;

Page No 52 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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--;
}

Page No 53 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

Network topology:
------------------------ 1
1 b c 1

a 1 e 3 f
2 2
d

Output of execution1:
----------------------------

Shortest Path (Dijkstra's algorithm)


*******************************************************************
Enter the number of nodes:6
Enter the adjacency matrix for the graph(-1 for no edge):
0 1 -1 2 -1 -1
1 0 1 -1 -1 -1
-1 1 0 1 1 -1
2 1 1 0 2 -1
-1 -1 1 2 0 3
-1 -1 -1 -1 3 0

Enter the source node: a

Enter the destination node: f

Shortest path:
From a to f is: a b c e f length=6

Page No 54 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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

Enter the destination node:d

Shortest path:
From a to d is: a b e f h d length=10

Page No 55 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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");

printf("Starting with source,then with neighbouring nodes in alphabetical


order:\n");

Page No 56 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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))
{

Page No 57 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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++;

printf("\n%c's distance vector after receiving %c's vector:\n",src1,i+97);


for(j=0;j<nodes;j++)
printf("To %c:\tfrom: %c \t %d\n",j+97,dv[j].name,dv[j].delay);
getch();

}
}
}

Page No 58 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

Network Graph for Output1:


B

A D

Output from first execution:


------------------------------------

Distance Vector Routing


*************************************************************
Enter the number of nodes:4

Enter the node for which Distance Vector table is needed:d

Enter the no. of neighbours to d: 2

Enter the names of the neighbours(in alphabetic order): b c

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

d's distance vector after receiving b's vector:


To a: from: b 2
To b: from: d 1
To c: from: d 2
To d: from: d 0

d's distance vector after receiving c's vector:


To a: from: b 2
To b: from: d 1
To c: from: d 2
To d: from: d 0

Page No 59 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

Network Graph for Output2:

A B C D

E F G H

I J K L

Output from second execution:


----------------------------------------

Distance Vector Routing


**************************************************************
Enter the number of nodes:12

Enter the node for which Distance Vector table is needed:j

Enter the no. of neighbours to j:4

Enter the names of the neighbours(in alphabetic order):a


h
i
k

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

j's distance vector after receiving a's vector:

To a: from: j 8
To b: from: a 20
To c: from: a 33

Page No 60 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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

j's distance vector after receiving h's vector:


To a: from: j 8
To b: from: a 20
To c: from: h 31
To d: from: h 20
To e: from: a 22
To f: from: a 31
To g: from: h 18
To h: from: j 12
To i: from: j 10
To j: from: j 0
To k: from: j 6
To l: from: h 21

j's distance vector after receiving i's vector:


To a: from: j 8
To b: from: a 20
To c: from: i 28
To d: from: h 20
To e: from: i 17
To f: from: i 30
To g: from: h 18
To h: from: j 12
To i: from: j 10
To j: from: j 0
To k: from: j 6
To l: from: h 21

Page No 61 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

j's distance vector after receiving k's vector:


To a: from: j 8
To b: from: a 20
To c: from: i 28
To d: from: h 20
To e: from: i 17
To f: from: i 30
To g: from: h 18
To h: from: j 12
To i: from: j 10
To j: from: j 0
To k: from: j 6
To l: from: k 15

Page No 62 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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)

Page No 63 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

{
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];
}
}
}

printf("Given adjacency matrix is");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d",adj[i][j]);
}
printf("\n");
}
printf("Minimal spanning tree");

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)
{

Page No 64 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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++)
{

Page No 65 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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

Minimal spanning tree is


02
20
1 sends message to 0

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

Page No 66 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

2 5

Minimal spanning tree is


012
100
200

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

Minimal spanning tree is


0600

Page No 67 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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

Minimal spanning tree is


02050
20300
03000

Page No 68 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

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

Enter the no of nodes6


Enter the adjacency matrix
020760
203000

030400
704050
600507
000070
Enter the source for broadcasting3

Given adjacency matrix is


020760
203000
030400
704050
600507
000070
6 7
4 5
0

2 8 5

1 3

3 4
2

Page No 69 Dept. of CSE, VJIT


OS & CN THROUGH LINUX

Minimal spanning tree is


020000
203000
030400
004050
000507
000070

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

Page No 70 Dept. of CSE, VJIT

You might also like