Considerations for Write FIFO in o_nonblock mode

Source: Internet
Author: User

The background network communication framework generally uses FIFO as the mechanism of event notification: Create a FIFO, then open the FIFO in a non-blocking read and non-blocking write, and then add the FD to the Epoll as the FD that notifies the network event.

There is a vague question that is easy to overlook. FIFO opens in non-blocking mode, you must first open read and then open write. Otherwise it will be error no such device or address. That is, the code below is correct.

#define Fifo_file "/tmp/fifo.xxx"#define fifo_mode File_mode s_irusr | S_IWUSR | S_irgrp | S_iwgrp | S_iroth | S_iwothMkfifo (Fifo_file, fifo_mode); int readfd  0); int 0);

The two open in the above code cannot be reversed, or it will be an error: No such device or address

Note that, even if later we write code is in two processes through FIFO to communicate, do not care to write to open the FIFO, whether the FIFO has been read to open, because we can set a dummy fd, as follows:

int 0 ); int 0);

Interested friends can copy the following code in the past, to see the results of the output:

1/Note L18

2/No comment L18

1#include <unistd.h>2#include <sys/types.h>3#include <sys/stat.h>4#include <fcntl.h>5                                                                                                     6#include <errno.h>7#include <stdio.h>8#include <string.h>9                                                                                                     Ten #defineFile_mode S_IRUSR | S_IWUSR | S_irgrp | S_iwgrp | S_iroth | S_iwoth One #defineFifo_file "/tmp/fifo.test_nonblock_write" A                                                                                                      - intMainvoid)                                                                                       - {                                                                                                    the Mkfifo (Fifo_file, File_mode);  -fprintf (stderr,"Mkfifo, errno=%d, error=%s\n", errno, Strerror (errno));  -                                                                                                      -     //Open (Fifo_file, o_rdonly|                                                      O_nonblock, 0);  +fprintf (stderr,"Open (o_rdonly), errno=%d, error=%s\n", errno, Strerror (errno));  -                                                                                                      +Open (Fifo_file, o_wronly| O_nonblock,0);  Afprintf (stderr,"Open (o_wronly), errno=%d, error=%s\n", errno, Strerror (errno));  at                                                                                                      -Unlink (Fifo_file);  -                                                                                                      -     return 0;  -}

Considerations for Write FIFO in o_nonblock mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.