0% found this document useful (0 votes)
27 views4 pages

Unit_5 Queston and Answers

The document provides an overview of shared memory and IPC mechanisms in Linux, detailing kernel support for shared memory, APIs for creating and using shared memory segments, and the role of Berkeley Sockets in network communication. It explains the client/server model, socket address structures, system calls for TCP and UDP protocols, and includes example programs for client/server communication. Additionally, it compares various IPC mechanisms, highlighting their performance and use cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

Unit_5 Queston and Answers

The document provides an overview of shared memory and IPC mechanisms in Linux, detailing kernel support for shared memory, APIs for creating and using shared memory segments, and the role of Berkeley Sockets in network communication. It explains the client/server model, socket address structures, system calls for TCP and UDP protocols, and includes example programs for client/server communication. Additionally, it compares various IPC mechanisms, highlighting their performance and use cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Unit_5 Question and Answers

1. Explain Shared Memory in Linux: Kernel support for shared memory and
its importance.
Shared Memory Overview:
Shared memory is an IPC mechanism that allows multiple processes to access a
common memory segment. It is one of the fastest forms of IPC because it enables
direct access to the memory without requiring kernel intervention for every
read/write operation.
Kernel Support for Shared Memory:
 The Linux kernel provides System V shared memory (based on Unix System V
IPC standards) and POSIX shared memory.
 Shared memory segments are created in kernel space and can be mapped
into the address space of multiple processes.
 The kernel manages shared memory access, keeping track of which
processes are using the segment and preventing unauthorized access
through memory protection mechanisms.
Shared memory is typically used in scenarios where large amounts of data need to
be shared between processes, like multimedia applications or high-performance
computing systems.

2. Describe the APIs for Shared Memory and provide examples of how to
create, attach, and use shared memory segments.
APIs for System V Shared Memory:
1. shmget(): Creates or retrieves a shared memory segment.
int shmget(key_t key, size_t size, int shmflg);
o key: Unique identifier for the shared memory segment.
o size: Size of the memory segment.
o shmflg: Flags specifying permissions and options.
2. shmat(): Attaches the shared memory segment to the address space of the
calling process.
void *shmat(int shmid, const void *shmaddr, int shmflg);
3. shmdt(): Detaches the shared memory segment from the process's address
space.
int shmdt(const void *shmaddr);
4. shmctl(): Performs control operations on the shared memory segment.
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
Example of Shared Memory Usage:
1. Creating and Attaching Shared Memory:
key_t key = ftok("shmfile", 65);
int shmid = shmget(key, 1024, 0666 | IPC_CREAT);
char *str = (char *)shmat(shmid, NULL, 0);
2. Writing to Shared Memory:
printf("Write Data: ");
fgets(str, 1024, stdin);
3. Reading from Shared Memory:
printf("Data in memory: %s\n", str);
4. Detaching and Removing Shared Memory:
shmdt(str);
shmctl(shmid, IPC_RMID, NULL);
This example shows how two processes can share data using a shared memory
segment. The first process writes data to the shared memory, and the second
process reads it.

3. Introduction to Berkeley Sockets: Explain IPC over a network and the


role of sockets in client/server communication.
Berkeley Sockets Overview:
Sockets are the primary mechanism for IPC over a network in Unix-like systems.
They allow communication between processes on different computers using
protocols like TCP (Transmission Control Protocol) and UDP (User Datagram
Protocol).
IPC Over a Network:
Sockets enable communication between systems over a network, making them
essential for implementing network services, such as web servers, file transfer
protocols, and distributed computing. Communication can be connection-oriented
(reliable, using TCP) or connectionless (faster but unreliable, using UDP).

4. Explain the Client/Server Model Using Sockets, and Describe the Socket
Address Structures for UNIX Domain and Internet Domain.
Client/Server Model:
In the client/server model, the server listens for requests on a specific network
address and port, while the client initiates communication by connecting to the
server. This model is widely used for services like web servers, email servers, and
databases.
Socket Address Structures:
1. UNIX Domain Sockets (for IPC on the same system):
o struct sockaddr_un is used for local inter-process communication.
struct sockaddr_un {
sa_family_t sun_family; // AF_UNIX
char sun_path[108]; // Path to socket file
};
2. Internet Domain Sockets (for IPC over a network):
o struct sockaddr_in is used for network communication.
struct sockaddr_in {
sa_family_t sin_family; // AF_INET (IPv4) or AF_INET6 (IPv6)
in_port_t sin_port; // Port number
struct in_addr sin_addr; // IP address
};

5. Describe Socket System Calls for Connection-Oriented and


Connectionless Protocols.
System Calls for Connection-Oriented (TCP) Protocol:
1. socket(): Creates a new socket.
int socket(int domain, int type, int protocol);
o domain: Address domain (e.g., AF_INET for IPv4).
o type: Socket type (e.g., SOCK_STREAM for TCP).
o protocol: Usually set to 0 for default protocol.
2. bind(): Binds the socket to a specific address and port.
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
3. listen(): Marks the socket as a passive socket that will listen for incoming
connections.
int listen(int sockfd, int backlog);
4. accept(): Accepts a new connection on a listening socket.
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
5. connect(): Initiates a connection to a remote server (used by the client).
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
6. send() and recv(): Used to send and receive data over the connected
socket.
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
System Calls for Connectionless (UDP) Protocol:
1. sendto(): Sends a datagram to a specific address (used for UDP
communication).
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct
sockaddr *dest_addr, socklen_t addrlen);
2. recvfrom(): Receives a datagram from a specific address.
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr
*src_addr, socklen_t *addrlen);

6. Provide an Example Client/Server Program for Single Server-Client


Connection Using Sockets.
Server Program:
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(8080);

bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));


listen(sockfd, 5);

int connfd = accept(sockfd, (struct sockaddr *)NULL, NULL);


char buffer[1024];
recv(connfd, buffer, sizeof(buffer), 0);
printf("Received: %s\n", buffer);
send(connfd, "Message received", 17, 0);

close(connfd);
close(sockfd);
Client Program:
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(8080);
inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);

connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));


send(sockfd, "Hello Server", 12, 0);
char buffer[1024];
recv(sockfd, buffer, sizeof(buffer), 0);
printf("Server reply: %s\n", buffer);

close(sockfd);

7. Discuss Socket Options: setsockopt and fcntl System Calls, and How to
Configure Socket Behavior.
setsockopt():
The setsockopt() function is used to configure options for a socket. These options
can control various aspects of socket behavior like timeouts, buffer sizes, and more.
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t
optlen);
Example:
To enable address reuse on a socket:
int opt = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
fcntl():
The fcntl() system call can also be used to manipulate file descriptors, including
sockets. It can set the socket to non-blocking mode, for example.
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);

8. Compare IPC Mechanisms: Pipes, FIFOs, Message Queues, Shared


Memory, and Sockets.
IPC mechanisms differ based on performance, ease of use, and the environment in
which they are used. Here's a brief comparison:
 Pipes: Simple, fast, but can only be used between related processes.
 FIFOs: Named pipes that allow communication between unrelated processes.
 Message Queues: Useful for sending structured messages between
processes, with kernel-level support for queuing.
 Shared Memory: Fastest IPC, as processes can directly read/write to shared
memory, but requires synchronization mechanisms.
 Sockets: Allow IPC over networks, making them ideal for client-server
applications. Can handle both local (UNIX domain) and network (Internet
domain) communication.
Each IPC mechanism has its own advantages and is suited to specific use cases,
with shared memory being the fastest but more complex, and sockets offering
flexibility for network communication.

You might also like