In: Computer Science
What are the benefits and disadvantages of each of the following? Consider both the system level and programmer level.
What are the benefits and disadvantages of each of the following? Consider both the system level and programmer level.
Blocking send and non-blocking send in IPC
Blocking receive and non-blocking receive in IPC
zero-capacity and bounded capacity in IPC buffers
Answer to Question 1 & 2 -
Blocking send– The sender process is blocked until message is received by receiver process.
Non Blocking send – the sending process would resume operation right after sending.
Blocking receive – Receiver Process is blocked until message is available.
Non Blocking receive – The receiver process would retrieve a valid messae or returns an error message.
Benefits of Blocking Send and Blocking Receive( or in other words -Synchronous Message Passing)
Blocking Send and Blocking Receive are easy to use
Both Send and Receive implementation has low overhead.
Disadvantage of Blocking Send and Blocking Receive Message Passing
Low Concurrency
The problem with this method of message passing is that if the name of one process changes, this method will not work.
Benefits of Non Blocking Send and Non Blocking Receive( or in other words – Asynchronous Message Passing)
Flexibility, parallel operations.
Disadvantages of Non Blocking Send and Non Blocking Receive Message Passing
Programming is tricky. Program is timing-dependent (interrupt can occur at arbitrary time, and execution irreproducible)
Though any combination of the above send/receive is possible, there are
basically three most preferred combinations:
Blocking send and blocking receive.
Non-blocking send and Non-blocking receive.
Non-blocking send and Blocking receive (Mostly used).
System Level Explanation of Synchronous Message Passing :-
In this method of message passing, the communication link get established automatically, which can be either unidirectional or bidirectional, but one link can be used between one pair of the sender process and receiver process and one pair of sender and receiver processes should not possess more than one pair of link. Symmetry and asymmetry between the sending and receiving can also be implemented i.e. either both process will name each other for sending and receiving the messages or only sender will name receiver for sending the message and there is no need for receiver for naming the sender for receiving the message. The problem with this method of communication is that if the name of one process changes, this method will not work.
System Level Explanation of Asynchronous Message Passing
:-
In Asynchronous message passing, processes uses
mailboxes (also referred to as ports) for sending and receiving
messages. Each mailbox will have a unique id and processes can
communicate only if they share a mailbox/ports. Link established
only if processes share a common mailbox and a single link can be
associated with many processes. Each pair of processes can share
several communication links and these link may be unidirectional or
bi-directional. Suppose two process want to communicate though
Indirect message passing, the required operations are: create a
mail box, use this mail box for sending and receiving messages,
destroy the mail box. The standard primitives used are :
send(A, message) which means send the message to
mailbox A. The primitive for the receiving the message also works
in the same way e.g. received (A, message). There
is a problem in this mailbox implementation. Suppose there are more
than two processes sharing the same mailbox and suppose the process
p1 sends a message to the mailbox, which process will be the
receiver? This can be solved by either forcing that only two
processes can share a single mailbox or enforcing that only one
process is allowed to execute the receive at a given time or select
any process randomly and notify the sender about the receiver. A
mailbox can be made private to a single sender/receiver pair and
can also be shared between multiple sender/receiver pairs. Port is
an implementation of such mailbox which can have multiple sender
and single receiver. It is used in client/server application (Here
server is the receiver). The port is owned by the receiving process
and created by OS on the request of the receiver process and can be
destroyed either on request of the same receiver process or when
the receiver terminates itself.
NOTE :- Enforcing that only one process is allowed to execute the receive can be done using the concept of mutual exclusion. Mutex mailbox is create which is shared by n process. Sender is non-blocking and sends the message. The first process which executes the receive will enter in the critical section and all other processes will be blocking and will wait.
A Simple Server in C/C++
// Server side C/C++ program to demonstrate Socket programming
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
valread = read( new_socket , buffer, 1024);
printf("%s\n",buffer );
send(new_socket , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
return 0;
}
A Simple Client in C/C++
// Client side C/C++ program to demonstrate Socket programming
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
struct sockaddr_in address;
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
send(sock , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
valread = read( sock , buffer, 1024);
printf("%s\n",buffer );
return 0;
}
Answer to Question 3:-
This is the concept of IPC buffering where two processes shares a common space or memory location known as buffer(Producer Process and Consumer Process), where the item produced by Producer is stored and from where the Consumer consumes the item if needed. There are two version of this problem: first one is known as unbounded buffer problem in which Producer can keep on producing items and there is no limit on size of buffer, the second one is known as bounded buffer problem in which producer can produce up to a certain amount of item and after that it starts waiting for consumer to consume it.
Queue of messages attached to the port/mailboxes; implemented in one of three ways
Zero capacity – 0 messages Sender process must wait for receiver (rendezvous).
Bounded capacity – finite length of n messages Sender process must wait if port/mailbox full.