ITL331 Operating System and Network Programming Lab by Vinish Alikkal
ITL331 Operating System and Network Programming Lab by Vinish Alikkal
Vinish Alikkal
Assistant Professor
1/1/2022
OPERATING SYSTEM
LIST OF EXPERIMENTS
(Experiments are to be implemented using C programming language)
1. Familiarization of system calls (fork, exec, getpid, exit, wait, close, stat etc) in
operating system.
2. Implement process scheduling algorithms (FCFS, SJF, Round-Robin, Priority) and
compute average waiting time and average turn-around time.
3. Inter-process communication using mail boxes, pipes, message queues and shared
memory.
4. Implementation of dining philosopher’s problem using threads, semaphores and
shared memory.
5. Implementation of banker’s algorithm.
6. Implement memory management schemes (first fit, best fit and worst fit).
NETWORK PROGRAMMING
LIST OF EXPERIMENTS
(Experiments are to be implemented using JAVA programming language)
NETWORKING BASICS
CLIENT-SERVER MODEL
Network applications can be divided into two process: a Client and a Server, with
acommunication link joining the two processes.
Normally, from Client-side it is one-one connection. From the Server Side, it is many-
oneconnection.
The standard model for network applications is he Client-Server model. A Server is a process
thatis waiting to be contacted by a Client process so that server can do something for the
clientTypical BSD Sockets applications consist of two separate application level processes;
oneprocess (the client) requests a connection and the other process (the server ) accepts it.
SOCKET FUNCTIONS FOR ELEMENTARY TCP CLIENT/SERVER IN
CONNECTION-ORIENTED SCENARIO
The server process creates a socket, binds an address to it, and sets up a mechanism (called
alisten queue) for receiving connection requests. The client process creates a socket and requests
aconnection to the server process. Once the server process accepts a client process's request
andestablishes a connection, full-duplex (two-way) communication can occur between the
twosockets.
SOCKETS OVERVIEW
The operating system includes the Berkeley Software Distribution (BSD) interprocess
communication (IPC) facility known as sockets. Sockets are communication channels that
enableunrelated processes to exchange data locally and across networks. A single socket is one
endpoint of a two-way communication channel.
Sockets Overview:
Sockets were developed in response to the need for sophisticatedinterprocess facilities to meet
the following goals:
Socket Facilities:
Socket subroutines and network library subroutines provide the buildingblocks for IPC. An
application program must perform the following basic functions to conductIPC through the
socket layer:
Socket Addresses:
Sockets can be named with an address so that processes can connect to them.Most socket
functions require a pointer to a socket address structure as an argument. Eachsupported protocol
suite defines its own socket address structure. The names of these structuresbegin with
sockaddr_ and end with a unique suffix for each protocol suite.Generic socket address structure:
Many of the Networking system calls require a pointer to asocket address structure as an
argument. Definition of this structure is in
#include<sys/socket.h>
structsockaddr {
};
Internet Socket address structure: The protocol specific structuresockaddr_in is identical in size to
generic structure which is 16 bytes
ELEMENTARY SOCKET SYSTEM CALLS
Socket() System Call: Creates an end point for communication and returns a descriptor.
Syntax
#include <sys/socket.h>
#include <sys/types.h>
Description: The socket subroutine creates a socket in the specified Address Family and of the
specified type. A protocol can be specified or assigned by the system. If the protocol is
leftunspecified (a value of 0), the system selects an appropriate protocol from those protocols in
theaddress family that can be used to support the requested socket type.
The socket subroutine returns a descriptor (an integer) that can be used in later subroutines that
operate on sockets.
Return Values Upon successful completion, the socket subroutine returns an integer (the socket
descriptor). It returns -1 on error.
Description: The bind subroutine assigns a Name parameter to an unnamed socket. It assigns a
local protocol address to a socket.
Syntax
#include <sys/socket.h>
int bind (int sockfd, struct sockaddr *myaddr, int addrlen);
sockfd is a socket descriptor returned by the socket function. The second argument is a pointer to a protocol sp
third argument is size of this address structure.
There are 3 uses of bind:
a) Server registers their well-known address with a system. Both connection-oriented and
connection-less servers need to do this before accepting client requests.
b) A Client can register a specific address for itself.
c) A Connectionless client needs to assure that the system assigns it some unique address, so that
the other end (the server) has a valid return address to send its responses to.
Return Values: Upon successful completion, the bind subroutine returns a value of 0. Otherwise, it
returns a value of -1 to the calling program.
Return Values: Upon successful completion, the connect subroutine returns a value of 0.
Otherwise, it returns a value of -1 to the calling program.
This system call is used by a connection-oriented server to indicate that it is willing to receive
connections.
#include <sys/socket.h>
is usually executed after both the socket and bind system calls, and immediately before accept
system call. The backlog argument specifies how many connections requests can be queued by
the system while it waits for the server to execute the accept system call.
The actual connection from some client process is waited for by having the server execute the
accept system call.
#include <sys/socket.h>
int accept (int sockfd, struct sockaddr *cliaddr, int *addrlen);
DESCRIPTION:
The sender window size is N in GBN. For example, if it is Go Back 10 (GB10) it indicates
that the window size of the sender is 10. In GBN, the window size of the receiver is always
equal to 1 irrespective of the size of the sender. GBN uses cumulative acknowledgments. At
the receiver’s end, it starts an acknowledgment timer. The receiver sends an
acknowledgment for the set of finite frames received once the timer ends. If the
acknowledgment is not sent, then the set of frames has to be retransmitted from the sender.
The acknowledgment timer will not start once the earlier timer expires. It starts once it
receives a packet or a frame.
Let’s say it is GB3 with a sequence number field of 2 bits. The size of the sender
window=3 (since N=3). We require a minimum of 3 sequence numbers to represent
the frames.
Now the sender sends three frames at once (window size=3). Then the window
slides to the left of three frames. The receiver responds with an increased value of
the acknowledgment number stating that it is waiting for the “acknowledgment
number” frame. Suppose if frames 0,1,2 are transmitted. If there is no loss of
packets, then the receiver responds with ACK=3 (n+1) asking for the transmission
of frame 3. With reference to Fig 1.
If there is any loss in the frame, say 1 is lost when 0,1, and 2 are transmitted. The
window is slid to the next three frames at the sender’s side. Since the frame 1 is
lost, the receiver waits for the frame 1 until the timer expires. When the timer
expires, the acknowledgment number sent is 1, as only the frame 0 is received at
the receiver side and the other frames are simply discarded. Then the “window
slides back to the frame 1” or “window go back to frame 1” and transmit all the
frames starting from frame 1 i.e. 1,2, and 3.
The frames are now transmitted completely with no loss and the acknowledgment
number would be ACK+1 i.e. 4. Now, what if the ACK packet is lost??
Acknowledgment lost
The sender waits for the acknowledgment until the timer expires. If the
acknowledgment is not received, then the window slides to the initial or the earlier
set of frames and retransmits them. As the receiver is now waiting for frame 3
(assuming 0,1, and 2 frames are transmitted earlier), it simply discards them and
sends the acknowledgment number 3 again. This process continues until the
reception of respective packets take place at both ends of the communication.
PROGRAM:
import java.io.*;
public class GoBackN {
while(loop)
{
if(ack == window)
loop = false;
else
sent = ack;
}
OUTPUT:
DESCRIPTION:
Selective repeat protocol is a sliding window protocol that uses the concept of
pipelining where multiple packets can be sent while the sender is waiting for the
acknowledgement for the first sent packet. The selective repeat protocol manages
error and flows control between the sender and receiver.
Like go-back-n, selective repeat protocol is also a sliding window protocol. Unlike
the go-back-n protocol, the selective repeat protocol resends only a selective packet
that is either lost or corrupted. Let us first discuss the windows in selective repeat.
Window
In selective repeat, both sender and receiver have a sliding window. On the sender
side, the window covers the sequence of packets that are either sent or can be sent.
At the receiver, the sliding window covers the sequence number of the packets that
are either received or are expected to be received. In selective repeat, the size of the
sender and receiver window is the same. Let us study the sender window and
receiver window in brief.
1. Sender Window
The sender window in selective repeat is much smaller as compared to the go-back-
n protocol. The size of the sender window here is 2m-1. Here m is the number of bits
used by the packet header to express the sequence number of the corresponding
packet.
The sender window covers the packets that are sent but not yet acknowledged, one
that is acknowledged out of order and the one that can be sent once the data for the
corresponding are received by the sender’s application layer.
2. Receiver Window
The maximum size of the receiver window is 2m-1 which is the same as the sender
window. The receiver window covers the sequence number of the packets that are
received out of order and are waiting for the packets that were sent earlier but are
not yet received.
The receiver transport layer does not deliver packets out of order to the application
layer. It waits until a set of consecutive packets are received so that they can be
delivered to the application layer
ALGORITHM:
SERVER
ALGORITHM:
1. Start.
2. Establish connection (recommended UDP)
3. Accept the window size from the client(should be <=40)
4. Accept the packets from the network layer.
5. Calculate the total frames/windows required.
6. Send the details to the client(totalpackets,totalframes.)
7. Initialise the transmit buffer.
8. Built the frame/window depending on the windowsize.
9. Transmit the frame.
10. Wait for the acknowledgement frame.
11. Check for the acknowledgement of each packet and repeat the
process for the packet for which the negative acknowledgement
isreceived.
Else continue as usual.
12. Increment the frame count and repeat steps 7 to 12 until all packets
are
transmitted.
13. Close the connection.
14.Stop.
CLIENT
ALGORITHM
import java.lang.System;
import java.net.*;
import java.io.*;
import java.text.*;
import java.util.Random;
import java.util.*;
}
for (int i = 0; i < p; i++)
if (v[i] == -1) {
System.out.println("Request to retransmit from
packet no "
+ (i+1) + " again!!");
n = i;
out.write(n);
out.flush();
}
System.out.println();
v[n] = in.read();
System.out.println("Received frame is: " + v[n]);
System.out.println("quiting");
} catch (Exception e) {
System.out.println(e);
}
}
}
OUTPUT
Localhost/127.0.0.1
No of frame is:8
30
40
50
60
70
80
90
100
quiting
/.....................SERVER SIDE (SELECTIVE REPEAT)..........//
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
try
int a[] = { 30, 40, 50, 60, 70, 80, 90, 100 };
int y = a.length;
dos.write(y);
dos.flush();
dos.write(a[i]);
dos.flush();
int k = dis.read();
dos.write(a[k]);
dos.flush();
catch (IOException e)
System.out.println(e);
finally
try
{
dis.close();
dos.close();
catch (IOException e)
e.printStackTrace();
OUTPUT
Sliding window protocols are data link layer protocols for reliable and sequential
delivery of data frames. The sliding window is also used in Transmission Control
Protocol.
In this protocol, multiple frames can be sent by a sender at a time before receiving
an acknowledgment from the receiver. The term sliding window refers to the
imaginary boxes to hold frames. Sliding window method is also known as
windowing.
Working
In these protocols, the sender has a buffer called the sending window and the
receiver has buffer called the receiving window.
The size of the sending window determines the sequence number of the outbound
frames. If the sequence number of the frames is an n-bit field, then the range of
sequence numbers that can be assigned is 0 to 2𝑛−1. Consequently, the size of the
sending window is 2𝑛−1. Thus in order to accommodate a sending window size of
2𝑛−1, a n-bit sequence number is chosen.
The sequence numbers are numbered as modulo-n. For example, if the sending
window size is 4, then the sequence numbers will be 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, and so
on. The number of bits in the sequence number is 2 to generate the binary sequence
00, 01, 10, 11.
The size of the receiving window is the maximum number of frames that the
receiver can accept at a time. It determines the maximum number of frames that
the sender can send before receiving acknowledgment.
Example
Suppose that we have sender window and receiver window each of size 4. So the
sequence numbering of both the windows will be 0,1,2,3,0,1,2 and so on. The
following diagram shows the positions of the windows after sending the frames and
receiving acknowledgments.
PROGRAM
CLIENT PROGRAM
import java.net.*;
import java.io.*;
import java.rmi.*;
public class slidsender
{
public static void main(String a[])throws Exception
{
ServerSocket ser=new ServerSocket(10);
Socket s=ser.accept();
DataInputStream in=new DataInputStream(System.in);
DataInputStream in1=new DataInputStream(s.getInputStream());
String sbuff[]=new String[8];
PrintStream p;
int sptr=0,sws=8,nf,ano,i;
String ch;
do
{
p=new PrintStream(s.getOutputStream());
System.out.print("Enter the no. of frames : ");
nf=Integer.parseInt(in.readLine());
p.println(nf);
if(nf<=sws-1)
{
import java.net.*;
import java.io.*;
class slidreceiver
{
public static void main(String a[])throws Exception
{
Socket s=new Socket(InetAddress.getLocalHost(),10);
DataInputStream in=new DataInputStream(s.getInputStream());
PrintStream p=new PrintStream(s.getOutputStream());
int i=0,rptr=-1,nf,rws=8;
String rbuf[]=new String[8];
String ch; System.out.println();
do
{
nf=Integer.parseInt(in.readLine());
if(nf<=rws-1)
{
for(i=1;i<=nf;i++)
{
rptr=++rptr%8;
rbuf[rptr]=in.readLine();
System.out.println("The received Frame " +rptr+" is : "+rbuf[rptr]);
}
rws-=nf;
System.out.println("\nAcknowledgment sent\n");
p.println(rptr+1); rws+=nf; }
else
break;
ch=in.readLine();
}
while(ch.equals("yes"));
}
}
OUTPUT:
hiii
how r u
i am fine
how is evryone
Acknowledgment received for 4 frames
Acknowledgment sent
ECHO SERVER.
DESCRIPTION:
Server
A server is a computer or computer program that provides a service to another
computer, or client. For example, the internet is based on web servers that
respond to requests from clients via web browsers. How do you connect to a server
to access it services? Often, you will connect to a server over a computer network,
which is a group of computers connected together in order to share resources. To
connect to a server, you need to know the port number that the server socket is
listening on, which is an endpoint for communication between the client and the
server. Each port number is associated with an IP address, a numerical label
assigned to each device connected to a computer network, and a protocol type,
preset rules and guidelines for communicating data. With the port number and IP
address, you can attempt to establish a connection to a server. It is important to
note that when building an echo server, it is not a requirement to be connected
within a network. The server can be run locally on a computer and a client can
connect to the server via the same computer.
Client
A client is responsible for sending a message to the server, and is also where the
message is echoed back to. Once the server is running, or listening for connections,
a client is able to send a request to the server. With the correct port number and
IP address, and if everything goes to plan, the server accepts the connection and
creates a new socket. Through the newly created socket, the client and server can
now communicate by writing to or reading from the socket.
Dealing with Input and Output
In order to build an echo server that receives a client message and echoes it back
to the client, getting the input and output streams from a connected socket is
necessary for creating the communication channels between the client and server.
These streams are flows of data that mean the client can now send messages to
the server, and the server can echo back the message to the client. In Java, we can
create this communication channel by
calling .getInputStream() and .getOutputStream() on the socket. The input stream
allows data to flow from the client to the server, whilst the output stream allows
the server to send data back to the client.
Now we have established the communication channels, how to do we actually take
some input from the client, write that to the InputStream, read from
theInputStream and then send that input back to the client via
the OutputStream?
To write to and read data from the input channel, we can use Java
classes Scanner or BufferReader. Both allows for the ability to read user input
from the command line. Whilst Scanner parses, or converts, input data (strings or
primitive types) using regular expressions, BufferReader simply reads the
characters.
import java.net.*;
import java.io.*;
public class Server {
public static void main(String args[]) throws Exception,UnknownHostException{
Socket s=ss.accept();;
String str="",str2="";
while(str!="stop")
{
System.out.println("Waiting for client's Reply...");
str=din.readUTF();
System.out.println("Client: "+str);
System.out.println("Enter Message:");
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}
}
CLIENT PROGRAM:
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws Exception {
String str="",str2="";
while(!str.equals("stop")){
System.out.println("\nEnter Response: ");
str=br.readLine();
dout.writeUTF(str);
dout.flush();
System.out.println("Waiting for Server's Reply...");
str2=din.readUTF();
System.out.println("Server says: "+str2);
}
dout.close();
s.close();
}
}
OUTPUT:
SERVER SIDE:
CLIENT SIDE
Enter Response:
Hai…
Waiting for Server’s Reply …
Server says: hellooo
Enter Response:
This is Client
Waiting for Server’s Reply …
Server says: This is Server
Enter Response:
Stop
Waiting for Server’s Reply …
Server says: Stop
CLIENT-SERVER COMMUNICATION
DESCRIPTION:
Socket and ServerSocket classes are used for connection-oriented socket programming
and DatagramSocket and DatagramPacket classes are used for connection-less socket
programming.
Here, we are going to make one-way client and server communication. In this
application, client sends a message to the server, server reads the message and prints it.
Here, two classes are being used: Socket and ServerSocket. The Socket class is used to
communicate client and server. Through this class, we can read and write message. The
ServerSocket class is used at server-side. The accept() method of ServerSocket class
blocks the console until the client is connected. After the successful connection of client,
it returns the instance of Socket at server-side.
PROGRAM:
CLIENT SIDE
import java.io.*;
import java.net.*;
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
SERVER SIDE
import java.io.*;
import java.net.*;
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){System.out.println(e);}
}
}
OUTPUT
SERVER SIDE
message=Hello Server
CLIENT SIDE