Linux advanced pipe FIFO operation

Source: Internet
Author: User

/*
* Create a simple chat program using a named Pipeline
*/
# Include <stdio. h>
# Include <fcntl. h>
# Include <string. h>
# Include <stdlib. h>
# Include <sys/select. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <errno. h>
Int main ()
{
Int I, RFD, WFD, Len = 0, fd_in;
Char STR [32];
Int flag, stdinflag;
Fd_set write_fd, read_fd;
Struct timeval net_timer;
Mkfifo ("ikeo1", s_iwusr | s_irusr | s_irgrp | s_iroth);/* Create a named pipe using the mkfifo function */
Mkfifo ("ikeo2", s_iwusr | s_irusr | s_irgrp | s_iroth);/* Create a named pipe for mkfifo data */
WFD = open ("0000o1", o_wronly);/* Open the pipeline file in write mode */
RFD = open ("fifo2", o_rdonly);/* Open the pipeline file in Read mode */
If (RfD <= 0 | WFD <= 0)
{
Printf ("An error occurred while creating the MPs queue file! \ N ");
Return 0;
}
Printf ("this is three ends! ");
While (1)
{
Fd_zero (& read_fd);/* clear a file descriptor Set */
Fd_set (RfD, & read_fd );
Fd_set (fileno (stdin), & read_fd);/* Add the file descriptor RfD to the file descriptor set read_rd */
Net_timer. TV _sec = 5;
Net_timer. TV _usec = 0;
Memset (STR, 0, sizeof (STR);/* The memset function is cleared during initialization */
If (I = select (RfD + 1, & read_fd, null, null, & net_timer) <= 0)
Continue;
If (fd_isset (RfD, & read_fd ))
{
Read (RfD, STR, sizeof (STR);/* read the MPs queue and store the content in the STR variable */
Printf ("--------------------------- \ n ");
Printf ("Li Si: % s \ n", STR);/* print the output STR variable content */
}
If (fd_isset (fileno (stdin), & read_fd ))
{
Printf ("------------------- \ n ");
Fgets (STR, sizeof (STR), stdin );
Len = write (WFD, STR, strlen (STR);/* write Pipeline */
}
}
Close (RFD );
Close (WFD );

}

/* Advanced MPs queue operation file 2 */

# Include <stdio. h>
# Include <fcntl. h>
# Include <string. h>
# Include <stdlib. h>
# Include <sys/select. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <errno. h>
Int main ()
{
Int I, RFD, WFD, Len = 0, fd_in;
Char STR [32];
Int flag, stdinflag;
Fd_set write_fd, read_fd;
Struct timeval net_timer;
Mkfifo ("ikeo1", s_iwusr | s_irusr | s_irgrp | s_iroth);/* Create a named pipe using the mkfifo function */
Mkfifo ("ikeo2", s_iwusr | s_irusr | s_irgrp | s_iroth);/* Create a named pipe using the mkfifo function */
RFD = open ("0000o1", o_rdonly);/* Open the pipeline file in read-only mode */
WFD = open ("fifo2", o_wronly);/* Open the pipeline file in write mode */
Printf ("This is Li siduan! \ N ");
While (1)
{
Fd_zero (& read_fd);/* clear a file descriptor Set */
Fd_set (RfD, & read_fd);/* Add the file descriptor RfD to the descriptor set read_fd */
Fd_set (fileno (stdin), & read_fd );
Net_timer. TV _sec = 5;
Net_timer. TV _usec = 0;
Memset (STR, 0, sizeof (STR);/* The memset function is cleared during initialization */
If (I = select (RfD + 1, & read_fd, null, null, & net_timer) <= 0)
Continue;
If (fd_isset (RfD, & read_fd ))
{
Read (RfD, STR, sizeof (STR);/* read the MPs queue and store the content in the STR variable */
Printf ("---------------------- \ n ");
Printf ("Zhang San: % s \ n", STR);/* print the input STR variable content */
}
If (fd_isset (fileno (stdin), & read_fd ))
{
Printf ("-------------------- \ n ");
Fgets (STR, sizeof (STR), stdin );
Len = write (WFD, STR, strlen (STR);/* write Pipeline */
}
}
Close (RFD );
Close (WFD );
}

Related Article

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.