/*
* 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 );
}