Original article address:
TwoProgramTo test the communication between unrelated processes in a famous Pipeline:
Read pipeline Program: Main. c
# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <sys/types. h>
# DefineFifo_name "myfifo"
# DefineBuf_size 1024
IntMain (Void){
IntFD;
CharBuf [buf_size];
Umask (0);
FD = open (export o_name, o_rdonly );
Read (FD, Buf, buf_size );
Printf ("Read content: % s \ n", Buf );
Close (FD );
Exit (0);
}
Write pipeline Program: Client. c
# Include <stdio. h>
# Include <stdlib. h>
# Include < String . H>
# Include <fcntl. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include < String . H>
# Define Fifo_name "myfifo"
# Define Buf_size 1024
Int Main ( Void ){
Int FD;
Char Buf [buf_size] = " Hello procwrite, I come from process named procread! " ;
Umask ( 0 );
If (Mkfifo (writable o_name, s_ififo | 0666 ) =- 1 ){
Perror ( " Mkfifo error! " );
Exit ( 1 );
}
If (FD = open (export o_name, o_wronly) =- 1 ) {Determine whether FIFO is enabled
Perror ( " Open error! " );
Exit ( 1 );
}
Write (FD, Buf, strlen (BUF) + 1 ); /* Strlen (BUF) + 1 also writes '\ 0' */
Close (FD );
// Unlink (writable o_name); deletes a FIFO file
Exit ( 0 );
}
Makefile
ALL: Main
Main:
Gcc-g-wall-O0 main. C-o main
Client:
Gcc-g-wall-O0 client. C-O client
Clean:
RM *. O main client
Operation and Output:
After compilation, run the client first (the client is blocked after running)
Open another terminal and run the main program. Output: Reference
Read content: Hello procwrite, I come from process named procread!
End