Unit_5 Queston and Answers
Unit_5 Queston 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.
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
};
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);
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);