Parent-child inter-process communication model implementation (POPEN)

Source: Internet
Author: User
Tags stdin

0, FILE *popen (const char *command, const char *type);

The Popen function is equivalent to doing several things:

1. Create a nameless pipe file

2. Fork ()

3, in the sub-process, EXEC command

4, in the sub-process,

if type = = "R", it is equivalent to:

int fd_new =  fopen ("pipe_name", O_rdonly);d up2 (0, fd_new );

if type = = "W", it is equivalent to:

int fd_new =  fopen ("pipe_name", O_wronly);d up2 (0 , fd_new);

5. The return value is a file pointer to the pipe file type operation

1, read the way Popen

In P1:

1 " R ") ;

Popen return value fp_in file pointer is a read-file pointer to the pipe file

In P2:

Popen (, "R") redirects the standard output in the P2 to the pipe file, which is equivalent to the pipe file fputs

1 printf ("%s\n", SRC);

Specific code:

P1:

1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4 intMainintargcChar* argv[])//./my_reverse5 {    6printf"PID:%d\n", Getpid ());7file*fp_in;8     Charcmd[1024x768] ;9     Charstr[1024x768] ;Ten      while(Memset (str,0,1024x768), Fgets (str,1024x768, stdin)! =NULL) One     { Amemset (CMD,0,1024x768); -sprintf (CMD,"%s%s", argv[1], str); -fp_in = Popen (cmd,"R") ; the         if(fp_in = =NULL) -         { -Perror ("Popen"); -Exit1); +         } -memset (str,0,1024x768) ; +FSCANF (fp_in,"%s", str); Aprintf"Res:%s\n", str); at  -     } -     return 0 ; -}

P2:

1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4 voidHandleChar*str)5 {6     intBG, end;7     inttmp;8BG =0; 9end = strlen (str)-1;Ten      while(BG <end) One     { ATMP =STR[BG]; -STR[BG] =Str[end]; -Str[end] =tmp; theBG + + ; -End-- ; -     } - } + intMainintargcChar* argv[])//Argv[1] - {     +     Charsrc[1024x768] ; Astrcpy (SRC, argv[1]); at handle (SRC); -printf"%s\n", SRC); -     return 0 ; -}

2, to write the way Popen

In P1:

1 fp_out = Popen (argv[1"w");

The return value of Popen is the write-file pointer to the pipe file

In P2:

Popen (, "W") redirects the standard input in the P2 to the pipe file, which is equivalent to from the pipe file fgets

1

Specific code:

P1:

1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4 intMainintargcChar* argv[])//./my_reverse5 {    6printf"PID:%d\n", Getpid ());7file*fp_out;8     Charcmd[1024x768] ;9     Charstr[1024x768] ;TenFp_out = Popen (argv[1],"W") ; One     if(Fp_out = =NULL) A     { -Perror ("Popen"); -Exit1); the     } -      while(Memset (str,0,1024x768), Fgets (str,1024x768, stdin)! =NULL) -     { - fputs (str, fp_out); + fflush (fp_out); -  +     } A Pclose (fp_out); at     return 0 ; -}

P2:

1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4 voidHandleChar*str)5 {6     intBG, end;7     inttmp;8BG =0; 9end = strlen (str)-1;Ten      while(BG <end) One     { ATMP =STR[BG]; -STR[BG] =Str[end]; -Str[end] =tmp; theBG + + ; -End-- ; -     } - } + intMainintargcChar*argv[]) - {     +     Charsrc[1024x768] ; A      while(Memset (SRC,0,1024x768), gets (SRC)! =NULL) at     { - handle (SRC); -printf"pid:%d%s\n", Getpid (), SRC); -     } -     return 0 ; -}

Parent-child inter-process communication model implementation (POPEN)

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.