Вы находитесь на странице: 1из 53

Computer Networks and Operating Systems Lab Manual

COMPUTER NETWORKS

SVECW(Autonomous)/Dept. of CSE Page 1


Computer Networks and Operating Systems Lab Manual

DATA LINK LAYER FRAMING METHODS

Exercise 1:

Aim:

To write a program for implementing the Data Link Layer framing methods such as character,
character stuffing and bit stuffing

DESCRIPTION

Data Link Layer Functions


Concerned with reliable, error-free and efficient communication between adjacent machines in the
network through the following functions:

Data Framing:

The term “frame” refers to a small block of data used in a specific network. The data link layer
groups raw data bits to/from the physical layer into discrete frames with error detection/correction
code bits added. Framing methods:
 Character count.
 Starting and ending characters, with character stuffing.
Starting and ending flags with bit stuffing.
 Physical layer coding violations.

Error Detection/Correction:

 Error Detection:
 Include enough redundant information in each frame to allow the receiver to
deduce that an error has occurred, but not which error and to request a
retransmission.
 Uses error-detecting codes.
 Error Correction:
 Include redundant information in the transmitted frame to enable the receiver
not only to deduce that an error has occurred but also correct the error.
 Uses error-correcting codes.

Services to the network layer:

 Unacknowledged connectionless service:


 Independent frames sent without having the destination acknowledge them.
 Suitable for real-time data such as speech and video where transmission speed
is more important than absolute reliability.
 Utilized in most LANS.

SVECW(Autonomous)/Dept. of CSE Page 2


Computer Networks and Operating Systems Lab Manual

 Acknowledged connectionless service:


 Each frame sent is acknowledged by the receiver.
 Acknowledgment at the layer level is not essential but provides more
efficiency than acknowledgment at higher layers (transport) which is done only
for the whole message.
 A lost acknowledgment may cause a frame to be sent and received several
times.

 Acknowledged connection-oriented service:


 The sender and receiver establish a connection before any data transmission.
 The message is broken into numbered frames.
 The data link guarantees that each frame sent is received exactly once and in
the right order.
Flow control

Protocols to control the rate the sender transmits frames at a rate acceptable to the receiver, and the
ability to retransmit lost or damaged frames. This insures that slow receivers are not swamped by
fast senders and further aids error detection/correction.
 Several flow control protocols exist, but all essentially require a form of feedback to
make the sender aware of whether the receiver can keep up.
 Stop-and-wait Protocols:
 A positive acknowledgment frame is sent by the receiver to indicate
that the frame has been received and to indicate being ready for the
next frame.
 Positive Acknowledgment with Retransmission (PAR); uses timeouts
 Sliding Window Protocols:
 Data frames and acknowledgement frames are mixed in both directions.
 Frames sent contain sequence numbers
 Timeouts used to initiate retransmission of lost frames.

SVECW(Autonomous)/Dept. of CSE Page 3


Computer Networks and Operating Systems Lab Manual

Data Link Layer: Framing


The character count method:

 The frame header includes the count of characters in the frame


 A transmission error can cause an incorrect count causing the source and destination to
get out of synchronization
 Rarely used in actual data link protocols.

Using Starting and ending characters, with character stuffing:

 Each frame starts with the ASCII character sequence DLE (Data Link Escape) and STX (Start
of TeXt) and ends with DLE ETX (End of TeXt)
 When binary data is transmitted where (DLE STX or DLE ETX) can occur in data, character
stuffing is used (additional DLE is inserted in the data).
 Limited to 8-bit characters and ASCII.

Bit-Oriented Using Start/End Flags:

a. Each frame begins and ends with 01111110


b. Bit stuffing: After each five consecutive ones in a data a zero is stuffed.
c. Stuffed zero bits are removed by the data link layer at the receiving end.

PROCEDURES:

Step-1: Print the menu as follows


Character count -1
Character stuffing – 2
Bit stuffing – 3
Exit -4
Step-2: Read the Choice

SVECW(Autonomous)/Dept. of CSE Page 4


Computer Networks and Operating Systems Lab Manual

Step-3: If the choice is 1, do the following


 Read the Character sequence
 Use random( ) function for frame length
If random() function returns 5 then
Take the first 4 characters from the character stream and print the frame (for example: if first
4 characters are a,b,c,d then the frame is 5 a b c d)
Repeat the above 2 steps until the character sequence ends
Step-4: If the choice is 2, do the following
 Read the data units with delimiters DLE STX and DLE ETX
 Scan the data for any occurrences of delimiters like DLE
 If DLE found in the sequence, then stuff the another DLE
 Repeat the above 2 step until data stream ends.
Step-5: If the choice is 3, do the following
 Read the bit stream
 Scan the bit stream for continuous 6 1’s
 If it is found then insert 0 after 5th 1
 Finally print the bit stream with delimiter 0 1 1 1 1 1 1 0
(i.e. 0 1 1 1 1 1 1 1 0 bit stream 0 1 1 1 1 1 1 0 )
Step-6: If the choice is 4 , then call exit() function.

PROGRAM FOR CHARACTER STUFFING

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
int i=0,j=0,n,pos;
char a[20],b[50],ch;
clrscr();
printf("enter string\n");
scanf("%s",&a);
n=strlen(a);
printf("enter position\n");
scanf("%d",&pos);
if(pos>n)
{
printf("invalid position, Enter again :");
scanf("%d",&pos);
}
printf("enter the character\n");
ch=getche();

b[0]='d';
b[1]='l';
b[2]='e';

SVECW(Autonomous)/Dept. of CSE Page 5


Computer Networks and Operating Systems Lab Manual

b[3]='s';
b[4]='t';
b[5]='x';
j=6;
while(i<n)
{
if(i==pos-1)
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]=ch;
b[j+4]='d';
b[j+5]='l';
b[j+6]='e';
j=j+7;
}
if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e')
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
j=j+3;
}
b[j]=a[i];
i++;
j++;
}
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]='e';
b[j+4]='t';
b[j+5]='x';
b[j+6]='\0';
printf("\nframe after stuffing:\n");
printf("%s",b);
getch();
}
INPUT:
enter string:
asdlefgh
enter position: 8
invalid position,enter again: 3
enter the character: k
OUTPUT:
frame after stuffing:
dlestx as dle k dledledlefghdleetx

SVECW(Autonomous)/Dept. of CSE Page 6


Computer Networks and Operating Systems Lab Manual

PROGRAM FOR BIT STUFFING

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int a[20],b[30],i,j,k,count,n;
clrscr();
printf("Enter frame length:");
scanf("%d",&n);
printf("Enter input frame (0's & 1's only):");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
i =0; count=1; j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1;a[k]==1 && k<n && count<5;k++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
j++;
b[j]=0;
}
i=k;
}}
else
{
b[j]=a[i];
}
i++;
j++;
}
printf("After stuffing the frame is:");
for(i=0;i<j;i++)
printf("%d",b[i]);
getch();
}

SVECW(Autonomous)/Dept. of CSE Page 7


Computer Networks and Operating Systems Lab Manual

INPUT:
Enter frame length: 10
Enter input frame (0's & 1's only):
1010111111

OUTPUT:
After stuffing the frame is:
10101111101

SVECW(Autonomous)/Dept. of CSE Page 8


Computer Networks and Operating Systems Lab Manual

CRC POLYNOMIALS
Exercise-2

Implement CRC and checksum methods.

DESCRIPTION
Data Link Layer: Error Detection/Correction

. Simplest error detection : Parity bits and checksum (sum of 1’s in Data).
. Error-detecting and -correcting codes:
 m data bits + r redundant bits added.
 n =m + r transmitted in frame.
 Only 2m code words out of possible 2m+r words are legal.
 The Hamming distance --minimum number of positions any two legal code words
differ-- of a code defines its error detection/correction ability.
 To detect d errors code Hamming distance = d + 1
 To correct d errors code Hamming distance = 2d + 1
 Some codes are more suitable to correct burst errors rather than isolated errors.
 Polynomial codes: Cyclic Redundancy Check (CRC) Codes, are characterized by a
generating polynomial G(X)

Cyclic Redundancy Check (CRC)

 Based on polynomial arithmetic over finite field.


 View m-bit string a m-1a m-2 . . . a0 as a polynomial of degree m-1:
M(x) = a m-1 x m-1 + a m-2 x m-2 + …. + a0
 Select a generating polynomial G(x) of degree r.
 Let R(x) be the remainder of xr M(x) / G(x)
 The code word T(x) of length m + r bit generated is then given by:
T(x) = xr M(x) - R(x)
 Assume code word T(x) is transmitted, but T(x) + E(x) arrives at the receiver:
 If E(x) = 0 then no transmission errors and T(x)/G(x) = 0
 If E(x) ¹ 0 then transmission error(s) occurred and:
[T(x) + E(x)] / G(x) ¹ 0
Calculation of Polynomial Code (CRC) Checksum

1. For degree of generating polynomial G(x) = r , append r zero bits to low-order of frame. The
frame now has m+r bits.
2. Divide the bit string corresponding to G(X) into the bit string xrM(x) mod(2)
3. Subtract the remainder R(x) from the bit string xrM(x) mod(2)

PROCEDURES:

Step-1: Read the frame


Step-2: Read the generator polynomial

SVECW(Autonomous)/Dept. of CSE Page 9


Computer Networks and Operating Systems Lab Manual

Step-3: find out the degree of the generator polynomial


Step-4: Append the number of the zero’s to the frame that number is equal to the degree of the
polynomial
Step-5: Find out number of digits in the generator polynomial
Step-6: Repeat the following until the number of digits are exhausted
Step-7: If the frame is starting with 1 , then exclusive-or the frame with generator
Step-8: Check whether the result obtained in step 7 is starting with 1, If so exclusive-or the remainder
with the generator
Step-9: If the result obtained in step7 is starting with 0, then exclusive –or the
remainder(result) with zeros. The number of zeroes must be equal to the length of the
generator.

PROGRAM FOR CYCLIC REDUNDANCY CHECK

#include<stdio.h>
#include<conio.h>
int gen[4],genl,frl,rem[4];
void main()
{
int i,j,fr[8],dupfr[11],recfr[11],tlen,flag;
clrscr();
frl=8; genl=4;
printf("enter frame:");
for(i=0;i<frl;i++)
{
scanf("%d",&fr[i]);
dupfr[i]=fr[i];
}
printf("enter generator:");
for(i=0;i<genl;i++)
scanf("%d",&gen[i]);
tlen=frl+genl-1;
for(i=frl;i<tlen;i++)

{
dupfr[i]=0;
}
remainder(dupfr);
for(i=0;i<frl;i++)
{
recfr[i]=fr[i];
}
for(i=frl,j=1;j<genl;i++,j++)
{
recfr[i]=rem[j];
}
remainder(recfr);

SVECW(Autonomous)/Dept. of CSE Page 10


Computer Networks and Operating Systems Lab Manual

flag=0;
for(i=0;i<4;i++)
{
if(rem[i]!=0)
flag++;
}
if(flag==0)
{
printf("frame received correctly");
}
else
{
printf("the received frame is wrong");
}
getch();
}
remainder(int fr[])
{
int k,k1,i,j;
for(k=0;k<frl;k++)
{
if(fr[k]==1)
{
k1=k;
for(i=0,j=k;i<genl;i++,j++)
{
rem[i]=fr[j]^gen[i];
}
for(i=0;i<genl;i++)
{
fr[k1]=rem[i];
k1++;
}
}
}
}

INPUT:
Enter frame :
11111111
enter generator :
1101

OUTPUT: frame received correctly

SVECW(Autonomous)/Dept. of CSE Page 11


Computer Networks and Operating Systems Lab Manual

Hamming code

Exercise 3

Implement one bit error correction in the received frame using Hamming code.

Description:

Hamming code is a popular error detection and error correction method in data communication.
Hamming code can only detect 2 bit error and correct a single bit error which means it is unable to
correct burst errors if may occur while transmission of data.

Hamming code uses redundant bits (extra bits) which are calculated according to the below formula:-

2 ≥ m+r+1
r

Where r is the number of redundant bits required and m is the number of data bits.

R is calculated by putting r = 1, 2, 3 … until the above equation becomes true.

R1 bit is appended at position 2 0

R2 bit is appended at position 2 1

R3 bit is appended at position 2 and so on.


2

These redundant bits are then added to the original data for the calculation of error at the receiver's end.
At the receiver's end with the help of even parity (generally) the erroneous bit position is identified and
since data is in binary we take complement of the erroneous bit position to correct received data.

Implement one bit error correction in the received frame using Hamming code.

Program for Hamming Code in C


#include<stdio.h>
void main() {
int data[10];
int dataatrec[10],c,c1,c2,c3,i;

printf("Enter 4 bits of data one by one\n");


scanf("%d",&data[0]);
scanf("%d",&data[1]);

SVECW(Autonomous)/Dept. of CSE Page 12


Computer Networks and Operating Systems Lab Manual

scanf("%d",&data[2]);
scanf("%d",&data[4]);

//Calculation of even parity


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("\nEncoded data is\n");


for(i=0;i<7;i++)
printf("%d",data[i]);

printf("\n\nEnter received data bits one by one\n");


for(i=0;i<7;i++)
scanf("%d",&dataatrec[i]);

c1=dataatrec[6]^dataatrec[4]^dataatrec[2]^dataatrec[0];
c2=dataatrec[5]^dataatrec[4]^dataatrec[1]^dataatrec[0];
c3=dataatrec[3]^dataatrec[2]^dataatrec[1]^dataatrec[0];
c=c3*4+c2*2+c1 ;

if(c==0) {
printf("\nNo error while transmission of data\n");
}
else {
printf("\nError on position %d",c);

printf("\nData sent : ");


for(i=0;i<7;i++)
printf("%d",data[i]);

printf("\nData received : ");


for(i=0;i<7;i++)
printf("%d",dataatrec[i]);

printf("\nCorrect message is\n");

//if errorneous bit is 0 we complement it else vice versa


if(dataatrec[7-c]==0)
dataatrec[7-c]=1;
else
dataatrec[7-c]=0;

for (i=0;i<7;i++) {
printf("%d",dataatrec[i]);
}
}

SVECW(Autonomous)/Dept. of CSE Page 13


Computer Networks and Operating Systems Lab Manual

OUTPUT:
Enter 4 bits of data one by one
1
0
1
0
Encoded data is
1010010
Enter received data bits one by one
1
0
1
0
0
1
0
No error while transmission of data

SVECW(Autonomous)/Dept. of CSE Page 14


Computer Networks and Operating Systems Lab Manual

DISTANCE VECTOR ROUTING ALGORITHM

Exercise 4

To write a program for implementation of distance vector routing algorithm.

DESCRIPTION

Routing algorithms are a part of the network layer software whose responsibility is to decide what
destination output line should be selected for successful journey completion of the packet from the
source machine to destination machine. The network layer of ISO-OSI reference model is responsible
for getting packets from the source to the destination. It uses routing algorithms to effectively use all
communication lines and routers present in the communication subnet and decide which lines to use
for forwarding incoming packets.

Distance Vector Routing

Most computer networks in operation today use dynamic routing algorithms rather than static
routing algorithms. One of the widely used dynamic algorithms is Distance Vector routing, which is
also known as Bellman Ford algorithm. A table maintained by a router is called a vector. Vectors
contain information on how to get to the destination using the best possible path to get to it. It also
contains an entry for each router in the subnet. Each of the routing tables is updated continuously.
This can be done by exchanging information with the neighboring routers. For measuring the optimal
distance between each node, metrics are used, similar to ones described above in the classification
section of the article. The optimal path is followed till the packet reaches the destination machine.

Distance vector algorithms use the Bellman-Ford algorithm. This approach assigns a number, the
cost, to each of the links between each node in the network. Nodes will send information from point
A to point B via the path that results in the lowest total cost (i.e. the sum of the costs of the links
between the nodes used). The algorithm operates in a very simple manner. When a node first starts, it
only knows of its immediate neighbours, and the direct cost involved in reaching them. (This
information, the list of destinations, the total cost to each, and the next hop to send data to get there,
makes up the routing table, or distance table.) Each node, on a regular basis, sends to each neighbour
its own current idea of the total cost to get to all the destinations it knows of. The neighbouring
node(s) examine this information, and compare it to what they already 'know'; anything which
represents an improvement on what they already have, they insert in their own routing table(s). Over
time, all the nodes in the network will discover the best next hop for all destinations, and the best
total cost.

When one of the nodes involved goes down, those nodes which used it as their next hop for
certain destinations discard those entries, and create new routing-table information. They then pass
this information to all adjacent nodes, which then repeat the process. Eventually all the nodes in the

SVECW(Autonomous)/Dept. of CSE Page 15


Computer Networks and Operating Systems Lab Manual

network receive the updated information, and will then discover new paths to all the destinations
which they can still "reach".

Implementation Using C

PROGRAM FOR DISTANCE VECTOR ROUTING ALGORITHM

#include<ctype.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
int dest,ja,ji,jk,j,jh,min,delay;
char dnode,line;
struct newj
{
int delay;
char line;
}s[12];
int a[12]={0,12,25,40,14,23,18,17,21,9,24,29};
int i[12]={24,36,18,27,7,20,31,20,0,11,22,33};
int h[12]={20,31,19,8,30,19,6,0,14,7,22,9};
int k[12]={21,28,36,24,22,40,31,19,22,10,0,9};
main()
{
ja=8;
ji=10;
jh=12;
jk=6;
clrscr();
printf("\n The source node is j");
printf("\n The adjacent nodes from j are a,i,h,k");
printf("\n Enter the destination node");
scanf("%c",&dnode);
dest=toupper(dnode)-'A';
for(j=0;j<12;j++)
{
min=a[j]+ja;
line='a';
if(min>i[j]+ji)
{
line='i';
min=i[j]+ji;
}
if(min>h[j]+jh)
{

SVECW(Autonomous)/Dept. of CSE Page 16


Computer Networks and Operating Systems Lab Manual

line='h';
min=h[j]+jh;
}
if(min>k[j]+jk)
{
line='k';
min=k[j]+jk;
}
s[j].delay=min;
s[j].line=line;
}
printf("The routing table is ");
printf("\n Dest \t Distance \t via node");
for(j=0;j<12;j++)
if(j==9)
printf("\n j \t 0 \t \t j");
else
printf("\n %c \t %d \t \t %c", j+'a', s[j].delay”);
s[j].line);
if(dest==9)
printf("\n Delay from source j to destination j is 0 via j");
else
printf("\n Delay from source j to %c is %d via %c",
dnode,s[dest]. delay, s[dest]. line);
getch(); }

OUTPUT

The source node is j


The adjacent nodes from j are a,i,h,k
Enter the destination node a
The routing table is
Dest Distance via node
a 8 a
b 20 a
c 28 i
d 20 h
e 17 i
f 30 i
g 18 h
h 12 h
i 10 i
j 0 j
k 6 k
l 15 k

SVECW(Autonomous)/Dept. of CSE Page 17


Computer Networks and Operating Systems Lab Manual

Delay from source j to a is 8 via a

BROADCAST TREE FROM SUBNET OF HOST

Exercise 5:

Write a program to implement a Broadcast routing algorithm.

DESCRIPTION

The Minimum Spanning Tree Problem

Suppose we have a group of islands that we wish to link with bridges so that it is possible to travel
from one island to any other in the group. Further suppose that (as usual) our government wishes to
spend the absolute minimum amount on this project (because other factors like the cost of using,
maintaining, etc, these bridges will probably be the responsibility of some future government.

The engineers are able to produce a cost for a bridge linking each possible pair of islands. The set of
bridges which will enable one to travel from any island to any other at minimum capital cost to the
government is the minimum spanning tree.

We will need some definitions first:

Graphs

A graph is a set of vertices and edges which connect them. We write:

G = (V,E)

where V is the set of vertices and the set of edges,

E = { (v ,v ) } i j

where v and v are in V.


i j

Paths

A path, p, of length, k, through a graph is a sequence of connected vertices:


p = <v ,v ,...,v >
0 1 k

where, for all i in (0,k-1:


(v ,v ) is in E.
i i+1

SVECW(Autonomous)/Dept. of CSE Page 18


Computer Networks and Operating Systems Lab Manual

Cycles

A graph contains no cycles if there is no path of non-zero length through the graph, p = <v ,v ,...,v > 0 1 k

such that v = v .
0 k

Spanning Trees

A spanning tree of a graph, G, is a set of |V|-1 edges that connect all vertices of the graph.

Minimum Spanning Tree

In general, it is possible to construct multiple spanning trees for a graph, G. If a cost, c , is associated
ij

with each edge, e = (v ,v ), then the minimum spanning tree is the set of edges, E , forming a
ij i j span

spanning tree, such that:


C = sum( c | all e in E ) is a minimum.
ij ij span

Forexample:
We want a subset, T, of edges, E, such that the graph remains connected if only the edges in T are
used, and the sum of the lengths of edges in T is as small as possible.

Such a subgraph must be a tree, and is called a Minimum Spanning Tree.


For the above graph, the following are both minimum spanning trees (cost 7).

SVECW(Autonomous)/Dept. of CSE Page 19


Computer Networks and Operating Systems Lab Manual

Krushkal’s Algorithm

This algorithm creates a forest of trees. Initially the forest consists of n single node trees (and no
edges). At each step, we add one (the cheapest one) edge so that it joins two trees together. If it were
to form a cycle, it would simply link two nodes that were already part of a single connected tree, so
that this edge would not be needed.

The basic algorithm looks like this:

Forest MinimumSpanningTree( Graph g, int n, double **costs ) {


Forest T;
Queue q;
Edge e;
T = ConsForest( g );
q = ConsEdgeQueue( g, costs );
for(i=0;i<(n-1);i++) {
do {
e = ExtractCheapestEdge( q );
} while ( !Cycle( e, T ) );
AddEdge( T, e );
}
return T;
}
The steps are:

1. The forest is constructed - with each node in a separate tree.


2. The edges are placed in a priority queue.
3. Until we've added n-1 edges,
 Extract the cheapest edge from the queue,
 If it forms a cycle, reject it,
 Else add it to the forest. Adding it to the forest will join two trees together.

SVECW(Autonomous)/Dept. of CSE Page 20


Computer Networks and Operating Systems Lab Manual

Every step will have joined two trees in the forest together, so that at the end, there will only be one
tree in T.

Prim's Algorithm

Step 1

Pick any vertex as a starting vertex. (Call it S). Mark it with any given colour, say red.

Step 2

Find the nearest neighbour of S (call it P1). Mark both P1 and the edge SP1 red. cheapest unmarked
(uncoloured) edge in the graph that doesn't close a coloured circuit. Mark this edge with same colour
of Step 1.

Step 3

Find the nearest uncoloured neighbour to the red subgraph (i.e., the closest vertex to any red vertex). Mark it
and the edge connecting the vertex to the red subgraph in red.

Step 4

Repeat Step 2 until all vertices are marked red. The red subgraph is a minimum spanning tree.

Implementation Using C

PROGRAM TO FIND MINIMUM SPANNING TREE

#include<stdio.h>
int p,q,u,v,n;
int min=99,mincost=0;
int t[50][2],i,j;
int parent[50],edge[50][50];
main()
{
clrscr();
printf("\n Enter the number of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)

SVECW(Autonomous)/Dept. of CSE Page 21


Computer Networks and Operating Systems Lab Manual

{
printf("%c\t",65+i);
parent[i]=-1;
}
printf("\n");
for(i=0;i<n;i++)
{
printf("%c",65+i);
for(j=0;j<n;j++)
scanf("%d",&edge[i][j]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(edge[i][j]!=99)
if(min>edge[i][j])
{
min=edge[i][j];
u=i;
v=j;
}
p=find(u);
q=find(v);
if(p!=q)
{
t[i][0]=u;
t[i][1]=v;
mincost=mincost+edge[u][v];
sunion(p,q);
}
else
{
t[i][0]=-1;
t[i][1]=-1;
}
min=99;
}
printf("Minimum cost is %d\n Minimum spanning tree is\n" ,mincost);
,mincost);
for(i=0;i<n;i++)
if(t[i][0]!=-1 && t[i][1]!=-1)
{
printf("%c %c %d", 65+t[i][0], 65+t[i][1],
edge[t[i][0]][t[i][1]]);
printf("\n");
}
getch();

SVECW(Autonomous)/Dept. of CSE Page 22


Computer Networks and Operating Systems Lab Manual

}
void sunion(int l,int m)
{
parent[l]=m;
}
Int find(int l)
{
if(parent[l]>0)
l=parent[l];
return l;
}

INPUT:

Enter the number of nodes6


A B C D E F
A 99 1 1 2 2 99
B 1 99 99 1 99 99
C 1 99 99 99 2 99
D 2 1 99 99 2 2
E 2 99 2 2 99 3
F 99 99 99 2 3 99

OUTPUT:

Minimum cost is 7
Minimum spanning tree is
AB1
CA1
DB1
EA2
FD2

SVECW(Autonomous)/Dept. of CSE Page 23


Computer Networks and Operating Systems Lab Manual

TCP and UDP Chat Applications


Exercise 6:

a. Write a program to develop a simple Chat TCP application.


b. Write a program to develop a simple Chat UDP application.

DESCRIPTION

If we are creating a connection between client and server using TCP then it has few functionalities like,
TCP is suited for applications that require high reliability, and transmission time is relatively less
critical. It is used by other protocols like HTTP, HTTPs, FTP, SMTP, Telnet. TCP rearranges data
packets in the order specified. There is absolute guarantee that the data transferred remains intact and
arrives in the same order in which it was sent. TCP does Flow Control and requires three packets to set
up a socket connection, before any user data can be sent. TCP handles reliability and congestion
control. It also does error checking and error recovery. Erroneous packets are retransmitted from the
source to the destination.
The entire process can be broken down into following steps:
TCP Server:
1. using create(), Create TCP socket.
2. using bind(), Bind the socket to server address.
3. using listen(), put the server socket in a passive mode, where it waits for the client to approach
the server to make a connection
4. using accept(), At this point, connection is established between client and server, and they are
ready to transfer data.
5. Go back to Step 3.
TCP Client:
1. Create TCP socket.
2. connect newly created client socket to server.
PROGRAM:
TCP SERVER:
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr

SVECW(Autonomous)/Dept. of CSE Page 24


Computer Networks and Operating Systems Lab Manual

// Function designed for chat between client and server.


void func(int sockfd)
{
char buff[MAX];
int n;
// infinite loop for chat
for (;;) {
bzero(buff, MAX);

// read the message from client and copy it in buffer


read(sockfd, buff, sizeof(buff));
// print buffer which contains the client contents
printf("From client: %s\t To client : ", buff);
bzero(buff, MAX);
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != '\n')
;

// and send that buffer to client


write(sockfd, buff, sizeof(buff));

// if msg contains "Exit" then server exit and chat ended.


if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...\n");
break;
}
}
}

// Driver function
int main()
{
int sockfd, connfd, len;
struct sockaddr_inservaddr, cli;

// socket create and verification


sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));

// assign IP, PORT

SVECW(Autonomous)/Dept. of CSE Page 25


Computer Networks and Operating Systems Lab Manual

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);

// Binding newly created socket to given IP and verification


if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
exit(0);
}
else
printf("Socket successfully binded..\n");

// Now server is ready to listen and verification


if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0);
}
else
printf("Server listening..\n");
len = sizeof(cli);

// Accept the data packet from client and verification


connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd< 0) {
printf("server acccept failed...\n");
exit(0);
}
else
printf("server acccept the client...\n");

// Function for chatting between client and server


func(connfd);

// After chatting close the socket


close(sockfd);
}

TCP CLIENT:
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
void func(int sockfd)

SVECW(Autonomous)/Dept. of CSE Page 26


Computer Networks and Operating Systems Lab Manual

{
char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}

int main()
{
int sockfd, connfd;
struct sockaddr_inservaddr, cli;

// socket create and varification


sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));

// assign IP, PORT


servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);

// connect the client socket to server socket


if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");

SVECW(Autonomous)/Dept. of CSE Page 27


Computer Networks and Operating Systems Lab Manual

// function for chat


func(sockfd);

// close the socket


close(sockfd);
}

Compilation –

Server side:
gccserver.c -o server
./server
Client side:
gccclient.c -o client
./client
OUTPUT –

Server side:
Socket successfully created..
Socket successfully binded..
Server listening..
server acccept the client...
From client: hi
To client : hello
From client: exit
To client : exit
Server Exit...

Client side:
Socket successfully created..
connected to the server..
Enter the string : hi
From Server : hello
Enter the string : exit
From Server : exit
Client Exit...

SVECW(Autonomous)/Dept. of CSE Page 28


Computer Networks and Operating Systems Lab Manual

OPERATING SYSTEMS

SVECW(Autonomous)/Dept. of CSE Page 29


Computer Networks and Operating Systems Lab Manual

CPU Scheduling Algorithms


Exercise 7:

Simulate the following CPU Scheduling Algorithms


a. FCFS b. SJF c. Priority
CPU scheduling deals with the problem of deciding which of the processes in the ready queue is to be
allocated the CPU.
There are many different CPU scheduling algorithms
 First Come First Serve(FCFS)
 Shortest Job First(SJF)
 Priority Scheduling
 Round Robin
First Come First Served (FCFS)
 First-come, First served is simplest scheduling algorithm
 Ready queue is a FIFO queue
 Longest waiting process at the front of queue
 New ready processes join the rear
 Non-preemptive: executes until voluntarily gives up CPU finished or waits for some event
 Problem: CPU bound process may require a long CPU burst
 Other processes, with very short CPU bursts, wait in queue Reduces CPU and I/O device
utilization
Program:
// C program for implementation of FCFS
// scheduling
#include<stdio.h>
// Function to find the waiting time for all
// processes
void findWaitingTime(int processes[], int n,
int bt[], int wt[])
{
// waiting time for first process is 0
wt[0] = 0;

// calculating waiting time


for (int i = 1; i<n ;i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}

// Function to calculate turn around time


void findTurnAroundTime( int processes[], int n,
int bt[], int wt[], int tat[])
{

SVECW(Autonomous)/Dept. of CSE Page 30


Computer Networks and Operating Systems Lab Manual

// calculating turnaround time by adding


// bt[i] + wt[i]
for (int i = 0; i<n ;i++)
tat[i] = bt[i] + wt[i];
}

//Function to calculate average time


void findavgTime( int processes[], int n, int bt[])
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

//Function to find waiting time of all processes


findWaitingTime(processes, n, bt, wt);

//Function to find turn around time for all processes


findTurnAroundTime(processes, n, bt, wt, tat);

//Display processes along with all details


printf("Processes Burst time Waiting time Turn around time\n");

// Calculate total waiting time and total turn


// around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d ",(i+1));
printf(" %d ", bt[i] );
printf(" %d",wt[i] );
printf(" %d\n",tat[i] );
}
int s=(float)total_wt / (float)n;
int t=(float)total_tat / (float)n;
printf("Average waiting time = %d",s);
printf("\n");
printf("Average turn around time = %d ",t);
}

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

//Burst time of all processes


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

SVECW(Autonomous)/Dept. of CSE Page 31


Computer Networks and Operating Systems Lab Manual

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

Output:

Processes Bursttime Waitingtime Turnaround time


1 10 0 10
2 5 10 15
3 8 15 23
Average waiting time = 8.33333
Average turnaround time = 16

Shortest Job First(SJF)


Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with
the smallest execution time to execute next. SJN is a non-pre-emptive algorithm.
 Shortest Job first has the advantage of having a minimum average waiting time among all
scheduling algorithms.
 It is a Greedy Algorithm.
 It may cause starvation if shorter processes keep coming. This problem can be solved using the
concept of aging.
 It is practically infeasible as Operating System may not know burst time and therefore may not
sort them. While it is not possible to predict execution time, several methods can be used to
estimate the execution time for a job, such as a weighted average of previous execution times.
SJF can be used in specialized environments where accurate estimates of running time are
available.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
//clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter process name, arrival time& execution time:");
SVECW(Autonomous)/Dept. of CSE Page 32
Computer Networks and Operating Systems Lab Manual

//flushall();
scanf("%s%d%d",pn[i],&at[i],&et[i]);
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(et[i]<et[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0; i<n; i++)
{
if(i==0)
st[i]=at[i];
else
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\twaitingtime\ttatime");
for(i=0; i<n; i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverageturnaroundtime is:%f",ata);
getch();
}

SVECW(Autonomous)/Dept. of CSE Page 33


Computer Networks and Operating Systems Lab Manual

Output:
Enter the number of process:3
Enter process name, arrival time&amp; execution time:2 5 7
Enter process name, arrival time&amp; execution time:3 6 14
Enter process name, arrival time&amp; execution time:4 7 12

Pnamearrivaltimeexecutiontimewaitingtimetatime
2 5 7 0 7
4 7 12 5 17
3 6 14 18 32
Average waiting time is:7.666667
Average turnaround time is:18.666666

SVECW(Autonomous)/Dept. of CSE Page 34


Computer Networks and Operating Systems Lab Manual

Priority Scheduling:

In priority scheduling algorithm each process has a priority associated with it and as each process hits
the queue, it is stored in based on its priority so that process with higher priority are dealt with first.
It should be noted that equal priority processes are scheduled in FCFS order.

To prevent high priority processes from running indefinitely the scheduler may decrease the priority of
the currently running process at each clock tick (i.e., at each clock interrupt). If this action causes its
priority to drop below that of the next highest process, a process switch occurs. Alternatively, each
process may be assigned a maximum time quantum that it is allowed to run. When this quantum is used
up, the next highest priority process is given a chance to run.

Program:

#include<stdio.h>

int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);

printf("\nEnter Burst Time and Priority\n");


for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}

//sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}

temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;

SVECW(Autonomous)/Dept. of CSE Page 35


Computer Networks and Operating Systems Lab Manual

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0; //waiting time for first process is zero

//calculate waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=total/n; //average waiting time


total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);

return 0;
}

Output:

SVECW(Autonomous)/Dept. of CSE Page 36


Computer Networks and Operating Systems Lab Manual

Enter Total Number of Process: 4


Enter Burst Time and Priority

P[1]
Burst Time:6
Priority:3

P[2]
Burst Time:2
Priority:2

P[3]
Burst Time:14
Priority:1

P[4]
Burst Time:6
Priority:4

Process Burst Time Waiting Time Turnaround Time


P[3] 14 0
14
P[2] 2 14
16
P[1] 6 16
22
P[4] 6 22
28

Average Waiting Time =13


Average Turnaround Time=20

SVECW(Autonomous)/Dept. of CSE Page 37


Computer Networks and Operating Systems Lab Manual

Bankers Algorithm for Deadlock avoidance


Exercise 8:

Simulate Bankers Algorithm for Deadlock Avoidance.

The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety
by simulating the allocation for predetermined maximum possible amounts of all resources, then makes
an “s-state” check to test for possible activities, before deciding whether allocation should be allowed
to continue.

Program:

// Banker's Algorithm
#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the Process names here

int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4

int max[5][3] = { { 7, 5, 3 }, // P0 // MAX Matrix


{ 3, 2, 2 }, // P1
{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // P4

int avail[3] = { 3, 3, 2 }; // Available Resources

int f[n], ans[n], ind = 0;


for (k = 0; k < n; k++) {
f[k] = 0;
}
int need[n][m];
for (i = 0; i< n; i++) {
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
int y = 0;
for (k = 0; k < 5; k++) {

SVECW(Autonomous)/Dept. of CSE Page 38


Computer Networks and Operating Systems Lab Manual

for (i = 0; i< n; i++) {


if (f[i] == 0) {

int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > avail[j]){
flag = 1;
break;
}
}

if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}

printf("Following is the SAFE Sequence\n");


for (i = 0; i< n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);

return (0);

Output:

Following is the SAFE Sequence


P1 -> P3 -> P4 -> P0 -> P2

SVECW(Autonomous)/Dept. of CSE Page 39


Computer Networks and Operating Systems Lab Manual

Page Replacement Algorithms


Exercise 9:
FIFO Page Replacement Algorithm:
In operating systems that use paging for memory management, page replacement algorithm are needed
to decide which page needed to be replaced when new page comes in. Whenever a new page is referred
and not present in memory, page fault occurs and Operating System replaces one of the existing pages
with newly needed page. Different page replacement algorithms suggest different ways to decide which
page to replace. The target for all algorithms is to reduce number of page faults.
This is the simplest page replacement algorithm. In this algorithm, operating system keeps track of all
pages in the memory in a queue, oldest page is in the front of the queue. When a page needs to be
replaced page in the front of the queue is selected for removal.
Program:
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}

SVECW(Autonomous)/Dept. of CSE Page 40


Computer Networks and Operating Systems Lab Manual

printf("Page Fault Is %d",count);


return 0;}
Output:

ENTER THE NUMBER OF PAGES: 20


ENTER THE PAGE NUMBER: 70120304230321201701
ENTER THE NUMBER OF FRAMES :3
ref string page frames
7 7 -1 -1
0 7 0 -1
1 7 0 1
2 2 0 1
0
3 2 3 1
0 2 3 0
4 4 3 0
2 4 2 0
3 4 2 3
0 0 2 3
3
2
1 0 1 3
2 0 1 2
0
1
7 7 1 2
0 7 0 2
1 7 0 1
Page Fault Is 15

SVECW(Autonomous)/Dept. of CSE Page 41


Computer Networks and Operating Systems Lab Manual

LRU Page Replacement Algorithm:

Least Recently Used (LRU) page replacement algorithm works on the concept that the pages that are
heavily used in previous instructions are likely to be used heavily in next instructions. And the page
that are used very less are likely to be used less in future. Whenever a page fault occurs, the page that is
least recently used is removed from the memory frames. Page fault occurs when a referenced page in
not found in the memory frames.

Program:
#include<stdio.h>

int findLRU(int time[], int n){


int i, minimum = time[0], pos = 0;

for(i = 1; i< n; ++i){


if(time[i] <minimum){
minimum = time[i];
pos = i;
}
}

return pos;
}

int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i,
j, pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);

printf("Enter number of pages: ");


scanf("%d", &no_of_pages);

printf("Enter reference string: ");

for(i = 0; i<no_of_pages; ++i){


scanf("%d", &pages[i]);
}

for(i = 0; i<no_of_frames; ++i){


frames[i] = -1;
}

for(i = 0; i<no_of_pages; ++i){


flag1 = flag2 = 0;
SVECW(Autonomous)/Dept. of CSE Page 42
Computer Networks and Operating Systems Lab Manual

for(j = 0; j <no_of_frames; ++j){


if(frames[j] == pages[i]){
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}

if(flag1 == 0){
for(j = 0; j <no_of_frames; ++j){
if(frames[j] == -1){
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}

if(flag2 == 0){
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}

printf("\n");

for(j = 0; j <no_of_frames; ++j){


printf("%d\t", frames[j]);
}
}

printf("\n\nTotal Page Faults = %d", faults);

return 0;
}

SVECW(Autonomous)/Dept. of CSE Page 43


Computer Networks and Operating Systems Lab Manual

Output:
Enter number of frames: 3
Enter number of pages: 6
Enter reference string: 5 7 5 6 7 3
5 -1 -1
5 7 -1
5 7 -1
576
576
376
Total Page Faults = 4

SVECW(Autonomous)/Dept. of CSE Page 44


Computer Networks and Operating Systems Lab Manual

File Allocation Strategies

Exercise 10:

Simulate the following File Allocation Strategies


a. Sequential b. Indexed

Sequential File allocation:

Files are normally stored on the disks. So, the main problem is how to allocate space to those files. So
that disk space is utilized effectively and files can be accessed quickly. Three major strategies of
allocating disc space are in wide use. Sequential, indexed and linked.

In this allocation strategy, each file occupies a set of contiguously blocks on the disk. This strategy is
best suited. For sequential files, the file allocation table consists of a single entry for each file. It shows
the filenames, starting block of the file and size of the file. The main problem with this strategy is, it is
difficult to find the contiguous free blocks in the disk and some free blocks could happen between two
files.

Program:

#include <stdio.h>
#include<conio.h>
void main()
{
int f[50], i, st, len, j, c, k, count = 0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Files Allocated are : \n");
x: count=0;
printf(“Enter starting block and length of files: ”);
scanf("%d%d", &st,&len);
for(k=st;k<(st+len);k++)
if(f[k]==0)
count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf(” The file is allocated to disk\n");

SVECW(Autonomous)/Dept. of CSE Page 45


Computer Networks and Operating Systems Lab Manual

}
else
printf(” The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit();
getch();
}

Output:

Files Allocated are :


Enter starting block and length of files: 14 3
14 1
15 1
16 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 1
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 4
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)0

SVECW(Autonomous)/Dept. of CSE Page 46


Computer Networks and Operating Systems Lab Manual

Indexed File Allocation:

In this scheme, a special block known as the Index block contains the pointers to all the blocks
occupied by a file. Each file has its own index block. The ith entry in the index block contains the disk
address of the ith file block.

Advantages:

 This supports direct access to the blocks occupied by the file and therefore provides fast access
to the file blocks.

 It overcomes the problem of external fragmentation.

Disadvantages:

 The pointer overhead for indexed allocation is greater than linked allocation.

 For very small files, say files that expand only 2-3 blocks, the indexed allocation would keep
one entire block (index block) for the pointers which is inefficient in terms of memory
utilization. However, in linked allocation we lose the space of only 1 pointer per block.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);

SVECW(Autonomous)/Dept. of CSE Page 47


Computer Networks and Operating Systems Lab Manual

if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
Output:

Enter the index block: 5


Enter no of blocks needed and no of files for the index 5 on the disk:
4
1234
Allocated
File Indexed
5-------->1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0)1
Enter the index block: 4
4 index is already allocated
Enter the index block: 6
Enter no of blocks needed and no of files for the index 6 on the disk :
2
78
A5llocated
File Indexed

SVECW(Autonomous)/Dept. of CSE Page 48


Computer Networks and Operating Systems Lab Manual

6-------->7 : 1
6-------->8 : 1
Do you want to enter more file(Yes - 1/No - 0)0

SVECW(Autonomous)/Dept. of CSE Page 49


Computer Networks and Operating Systems Lab Manual

Disk scheduling Algorithms

Exercise 11:

Simulate the following disk scheduling algorithms


a. SCAN b. SSTF

SCAN Disk Scheduling algorithm:

In SCAN disk scheduling algorithm, head starts from one end of the disk and moves towards the other
end, servicing requests in between one by one and reach the other end. Then the direction of the head is
reversed and the process continues as head continuously scan back and forth to access the disk. So, this
algorithm works as an elevator and hence also known as the elevator algorithm. As a result, the
requests at the midrange are serviced more and those arriving behind the disk arm will have to wait.

Program:

#include<conio.h>
#include<stdio.h>
int main()
{
int i,j,sum=0,n;
int d[20];
int disk; //loc of head
int temp,max;
int dloc; //loc of disk in array
clrscr();
printf("enter number of location\t");
scanf("%d",&n);
printf("enter position of head\t");
scanf("%d",&disk);
printf("enter elements of disk queue\n");
for(i=0;i<n;i++)
{
scanf("%d",&d[i]);
}
d[n]=disk;
n=n+1;
for(i=0;i<n;i++) // sorting disk locations
{
for(j=i;j<n;j++)
{
if(d[i]>d[j])
{
temp=d[i];
d[i]=d[j];
d[j]=temp;

SVECW(Autonomous)/Dept. of CSE Page 50


Computer Networks and Operating Systems Lab Manual

}
}
}
max=d[n];
for(i=0;i<n;i++) // to find loc of disc in array
{
if(disk==d[i]) { dloc=i; break; }
}
for(i=dloc;i>=0;i--)
{
printf("%d -->",d[i]);
}
printf("0 -->");
for(i=dloc+1;i<n;i++)
{
printf("%d-->",d[i]);
}
sum=disk+max;
printf("\nmovement of total cylinders %d",sum);
getch();
return 0;
}
Output:

Enter the number of location 8


Enter the position of head 53
Enter elements of disk queue
98
183
37
122
14
124
65
67
53--->37---->14--->0---->65--->67--->98--->122--->124--->183--->
Movement of total cylinders 236

SVECW(Autonomous)/Dept. of CSE Page 51


Computer Networks and Operating Systems Lab Manual

SSTF Scheduling algorithm:

The SSTF disk scheduling algorithm is a secondary storage scheduling algorithm


which enables to determine the motion of the disk’s arm and the disk head in
servicing read and write requests.

This algorithm helps to determine which job is nearest to the current head
position with minimum seek time, and then services that job next.

In the SSTF algorithm, the jobs having the shortest seek time are executed first.
Therefore, the seek time of each job is pre-calculated in the job queue and every
job is scheduled according to its seek time.

This enables the job nearest to the disk arm to get executed first. It also has a
better average response time and throughput as compared to the FCFS
scheduling algorithm.

Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int queue[100],t[100],head,seek=0,n,i,j,temp;
float avg;
//clrscr();
printf("*** SSTF Disk Scheduling Algorithm ***\n");
printf("Enter the size of Queue\t");
scanf("%d",&n);
printf("Enter the Queue\t");
for(i=0;i<n;i++)
{
scanf("%d",&queue[i]);
}
printf("Enter the initial head position\t");
scanf("%d",&head);
for(i=1;i<n;i++)
t[i]=abs(head-queue[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
SVECW(Autonomous)/Dept. of CSE Page 52
Computer Networks and Operating Systems Lab Manual

if(t[i]>t[j])
{
temp=t[i];
t[i]=t[j];
t[j]=temp;
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}
}
}
for(i=1;i<n-1;i++)
{
seek=seek+abs(head-queue[i]);
head=queue[i];
}
printf("\nTotal Seek Time is%d\t",seek);
avg=seek/(float)n;
printf("\nAverage Seek Time is %f\t",avg);
getch();
}

Output:

Enter the size of Queue 8


Enter the Queue 98 183 37 122 14 124 65 67
Enter the Initial Head Position 53

Total Seek time is 236


Average Seek Time is 29.500000

SVECW(Autonomous)/Dept. of CSE Page 53

Вам также может понравиться