(FIFO) communication between unrelated processes of famous Pipelines

Source: Internet
Author: User

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

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.