Namedpipe server client program with Epoll system call
In this program, you are going to learn
How to open namedpipes for reading and writing ?
How to read data ?
How to write data ?
How to use Namedpipe APIs ?
Let us answer few basic questions in this namedpipe
What is the purpose of mkfifo
in the code?
See Answer
mkfifo
is used to create a named pipe with a specific path and permission mode.
Why is O_RDONLY
used when opening server_fifo for the first time?
See Answer
O_RDONLY
indicates that the server is opening the named pipe for reading, expecting input from a client.
What happens if the server tries to open a non-existent named pipe using O_RDONLY
?
See Answer
If the named pipe doesn’t exist, the open
call will block until another process creates it.
This is common in inter-process communication.
Why is O_WRONLY
used when reopening client_fifo for writing?
See Answer
After receiving data from the client, the server reopens the named pipe for writing (O_WRONLY) to send a response back.
What is the significance of using O_WRONLY
for the second open
call?
See Answer
It designates the server’s intention to write to the named pipe, indicating a shift from reading to writing mode.
How does the server handle errors when opening the named pipe for reading or writing?
See Answer
The code checks the return value of the open
call and prints an error message using perror
if the operation fails.
How does the server handle cases where the named pipe is never opened for writing by the client?
See Answer
The server would block indefinitely at the write
operation until the client opens the named pipe for reading.
What is the significance of using unlink(FIFO_PATH)
in the signal handler?
See Answer
It removes the named pipe from the file system, ensuring that resources are properly cleaned up when the server exits.
Why are there multiple calls to close(server_fifo)
and close(client_fifo)
in the code?
See Answer
File descriptors need to be closed after usage to release system resources. Multiple calls ensure all relevant file descriptors are closed.
What is the primary purpose of the epoll system call?
See Answer
To efficiently monitor multiple file descriptors for I/O events
What types of file descriptors can be monitored using epoll?
See Answer
sockets, files, timerfd, socketpair, message_queue, Namedpipes and shared_memory.
What data structure is used by epoll to store events?
See Answer
Hash table
How do you handle errors when using the epoll system call?
See Answer
Check the return value for -1 to detect errors, Use perror to print error messages.
How does epoll handle a set of file descriptors with different states (e.g., reading, writing, exception)?
See Answer
- Create the epoll Instance:
Before monitoring file descriptors, the application creates an epoll instance using the epoll_create system call.
int epoll_fd = epoll_create1(0);
- Register File Discriptors:
The application registers file descriptors with the epoll instance using the epoll_ctl system call. It specifies the file descriptor, the events it is interested in (EPOLLIN for readability, EPOLLOUT for writability, etc.), and a user-defined data associated with the file descriptor.
struct epoll_event event;
event.events = EPOLLIN | EPOLLOUT; // Interested in readability and writability
event.data.fd = my_file_descriptor; // File descriptor to monitor
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, my_file_descriptor, &event);
- Wait for Events:
The application enters a loop where it calls epoll_wait to wait for events. This call blocks until one or more registered file descriptors become ready or until a timeout occurs.
#define MAX_EVENTS 10
struct epoll_event events[MAX_EVENTS];
int num_events = epoll_wait(epoll_fd, events, MAX_EVENTS, timeout_ms);
- Modify or Remove File Descriptors:
The application can dynamically modify or remove file descriptors from the epoll set using the epoll_ctl system call. For example, to modify events for an existing file descriptor:
struct epoll_event new_event;
new_event.events = EPOLLOUT; // Modify to be interested in writability
epoll_ctl(epoll_fd, EPOLL_CTL_MOD, my_file_descriptor, &new_event);
To remove a file descriptor from the epoll set:
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, my_file_descriptor, NULL);
How does epoll Checking Ready File Descriptors?
See Answer
After epoll_wait returns, the application iterates through the returned events to identify which file descriptors are ready and for what types of events.
for (int i = 0; i < num_events; ++i) {
if (events[i].events & EPOLLIN) {
// File descriptor i is ready for reading
}
if (events[i].events & EPOLLOUT) {
// File descriptor i is ready for writing
}
// Check other events if needed (e.g., EPOLLERR, EPOLLHUP)
}
What does it mean if epoll returns 0?
See Answer
No file descriptors are ready within the specified timeout.
There are many functions used in namedpipe. We can classify those functions based on functionalities.
mkfifo
open
Epoll create1
Epoll_ctl
Epoll_wait
read
write
close
mkfifo
is used to create a named pipe at a specified path (FIFO_PATH) with a given permission mode. For example,
mkfifo(FIFO_PATH, 0666);
open
is used to opening the named pipe with a given permission mode. For example,
server_fifo = open(FIFO_PATH, O_RDONLY);
client_fifo = open(FIFO_PATH, O_WRONLY);
epoll_create1()
creating an epoll instance using epoll_create1, The size parameter is an advisory hint for the kernel regarding the number of file descriptors expected to be monitored, For example,
epoll_fd = epoll_create1(0));
epoll_ctl()
After creating an epoll instance, file descriptors are added to it using epoll_ctl, For example,
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, server_fifo, &event);
epoll_wait()
The application then enters a loop where it waits for events using epoll_wait, For example,
ret = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
read
is used to read data from the named pipe, expecting input from a client. For example,
ret = read(server_fifo, buffer, sizeof(buffer) - 1);
write
is used to write a data to the named pipe, writing input to the client. For example,
ret = write(client_fifo, buffer, strlen(buffer));
close
is used to close the namepipe To free up system resources associated with the namedpipe. For example,
(void)close(server_fifo);
(void)close(client_fifo);
See the full program below,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <sys/epoll.h>
#define FIFO_PATH "/tmp/my_named_pipe"
#define BUFFER_SIZE 1024
#define MAX_EVENTS 2
int client_fifo;
int server_fifo;
int epoll_fd;
static void sigint_handler(
int signo)
{
(void)close(client_fifo);
(void)close(server_fifo);
(void)close(epoll_fd);
unlink(FIFO_PATH);
sleep(2);
(void)printf("Caught sigINT!\n");
exit(EXIT_SUCCESS);
}
void register_signal_handler(
int signum,
void (*handler)(int))
{
if (signal(signum, handler) ==
SIG_ERR) {
printf("Cannot handle signal\n");
exit(EXIT_FAILURE);
}
}
int main(void)
{
int ret;
int ready_fds;
char buffer[BUFFER_SIZE];
struct epoll_event
events[MAX_EVENTS];
struct epoll_event event;
register_signal_handler(SIGINT,
sigint_handler);
mkfifo(FIFO_PATH, 0666);
while (1) {
server_fifo = open(FIFO_PATH,
O_RDONLY);
if (server_fifo == -1) {
perror("open server FIFO");
return -1;
}
printf("Server is ready\n");
printf("----------------\n");
epoll_fd = epoll_create1(0);
if (epoll_fd == -1) {
perror("Epoll creation failed");
exit(EXIT_FAILURE);
}
event.events = EPOLLIN;
event.data.fd = server_fifo;
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD,
server_fifo, &event);
if (ret < 0) {
perror("Epoll_ctl failed");
exit(EXIT_FAILURE);
}
ready_fds = epoll_wait(epoll_fd,
events, MAX_EVENTS, -1);
if (ready_fds == -1) {
perror("Epoll wait failed");
break;
}
if (events[0].data.fd == server_fifo) {
ret = read(server_fifo,
buffer, sizeof(buffer) - 1);
if (ret < 0) {
unlink(FIFO_PATH);
break;
} else {
buffer[ret] = '\0';
printf("Received : %s\n",
buffer);
(void)close(server_fifo);
(void)close(epoll_fd);
}
snprintf(buffer,
sizeof(buffer), "Hello from server!");
client_fifo = open(FIFO_PATH,
O_WRONLY);
if (client_fifo != -1) {
ret = write(client_fifo,
buffer, strlen(buffer));
if (ret == -1) {
perror("write error");
break;
}
printf("sent = %s\n\n",
buffer);
}
(void)close(client_fifo);
}
}
(void)close(server_fifo);
(void)close(client_fifo);
return 0;
}
1$ gcc -o server server.c
2
3$ sudo ./server
4
5Server is ready
6----------------
7Received : Hello from client!
8sent = Hello from server!
9
10Server is ready
11----------------
12Received : Hello from client!
13sent = Hello from server!
14
15Server is ready
16----------------
17Received : Hello from client!
18sent = Hello from server!
19
20Server is ready
21----------------
22Received : Hello from client!
23sent = Hello from server!
24
25Server is ready
26----------------
27Received : Hello from client!
28sent = Hello from server!
29
30^CCaught sigINT!
There are many functions used in namedpipe. We can classify those functions based on functionalities.
open
write
Epoll create1
Epoll_ctl
Epoll_wait
read
close
open
is used to opening the named pipe with a given permission mode. For example,
client_fifo = open(FIFO_PATH, O_WRONLY);
server_fifo = open(FIFO_PATH, O_RDONLY);
write
is used to write a data to the named pipe, writing input to the client. For example,
ret = write(client_fifo, buffer, sizeof(buffer) - 1);
epoll_create1()
creating an epoll instance using epoll_create1, The size parameter is an advisory hint for the kernel regarding the number of file descriptors expected to be monitored, For example,
epoll_fd = epoll_create1(0));
epoll_ctl()
After creating an epoll instance, file descriptors are added to it using epoll_ctl, For example,
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, server_fifo, &event);
epoll_wait()
The application then enters a loop where it waits for events using epoll_wait, For example,
ret = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
read
is used to read data from the named pipe, expecting input from a client. For example,
ret = read(server_fifo, buffer, sizeof(buffer) - 1);
close
is used to close the namepipe To free up system resources associated with the namedpipe. For example,
(void)close(server_fifo);
(void)close(client_fifo);
See the full program below,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <sys/epoll.h>
#define FIFO_PATH "/tmp/my_named_pipe"
#define BUFFER_SIZE 1024
#define MAX_EVENTS 2
int client_fifo;
int server_fifo;
int epoll_fd;
static void sigint_handler(
int signo)
{
(void)close(client_fifo);
(void)close(server_fifo);
(void)close(epoll_fd);
unlink(FIFO_PATH);
sleep(2);
(void)printf("Caught sigINT!\n");
exit(EXIT_SUCCESS);
}
void register_signal_handler(
int signum,
void (*handler)(int))
{
if (signal(signum, handler) ==
SIG_ERR) {
printf("Cannot handle signal\n");
exit(EXIT_FAILURE);
}
}
int main(void)
{
int ret;
int ready_fds;
char buffer[BUFFER_SIZE];
struct epoll_event
events[MAX_EVENTS];
struct epoll_event event;
register_signal_handler(SIGINT,
sigint_handler);
while (1) {
client_fifo = open(FIFO_PATH,
O_WRONLY);
if (client_fifo == -1) {
perror("open client FIFO");
break;
}
printf("Client is ready\n");
printf("----------------\n");
snprintf(buffer,
sizeof(buffer), "Hello from client!");
ret = write(client_fifo,
buffer, sizeof(buffer) - 1);
if (ret < 0) {
perror("write error");
break;
} else {
buffer[ret] = '\0';
printf("sent = %s\n", buffer);
(void)close(client_fifo);
}
server_fifo = open(FIFO_PATH,
O_RDONLY);
if (server_fifo == -1) {
perror("open FIFO error");
break;
}
epoll_fd = epoll_create1(0);
if (epoll_fd == -1) {
perror("Epoll creation failed");
exit(EXIT_FAILURE);
}
event.events = EPOLLIN;
event.data.fd = server_fifo;
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD,
server_fifo, &event);
if (ret < 0) {
perror("Epoll_ctl failed");
exit(EXIT_FAILURE);
}
ready_fds = epoll_wait(epoll_fd,
events, MAX_EVENTS, -1);
if (ready_fds == -1) {
perror("Epoll wait failed");
break;
}
memset(buffer, 0,
sizeof(buffer));
if (events[0].data.fd == server_fifo) {
ret = read(server_fifo,
buffer, sizeof(buffer) - 1);
if (ret < 0) {
perror("read error");
break;
} else {
buffer[ret] = '\0';
printf("Received: %s\n\n",
buffer);
}
(void)close(server_fifo);
(void)close(epoll_fd);
}
}
(void)close(server_fifo);
(void)close(client_fifo);
return 0;
}
1$ gcc -o client client.c
2
3$ sudo ./client
4
5Client is ready
6----------------
7sent = Hello from client!
8Received: Hello from server!
9
10Client is ready
11----------------
12sent = Hello from client!
13Received: Hello from server!
14
15Client is ready
16----------------
17sent = Hello from client!
18Received: Hello from server!
19
20Client is ready
21----------------
22sent = Hello from client!
23Received: Hello from server!
24
25Client is ready
26----------------
27sent = Hello from client!
28Received: Hello from server!
29
30^CCaught sigINT!
Socket API |
Learning |
---|---|
mkfifo |
create a named pipe at a specified path (FIFO_PATH) with a given permission mode. |
open |
opening the named pipe with a given permission mode. |
read |
read data from the named pipe, expecting input from a client. |
epoll |
handles a set of file descriptors with different states, such as reading, writing, and exceptions, by using the struct epoll_event structure and the associated event flags.. |
write |
write a data to the named pipe, writing input to the client. |
Previous topic
Current topic
Other IPCs