Linux_c bc/uses 2 pipes to communicate with 2 processes

Source: Internet
Author: User
Tags fread stdin

1 /*TINGBC.C2 * Use both pipe to execute the BC.3 * One pipe:todc[2], another:fromdc[2]4 * Child thread to does DC, parent do UI5  */6 7#include <stdio.h>8#include <unistd.h>9#include <string.h>Ten #defineOops (x, N) {perror (x); exit (n);} One  A voidBE_DC (intTodc[],intfromdc[]); - voidBE_BC (intTodc[],intfromdc[]); - intMainvoid) { the   inttodc[2], fromdc[2], PID; -   if(Pipe (TODC) ==-1|| Pipe (FROMDC) ==-1) -Oops"Pipe",2); -   if((Pid=fork ()) = =-1) +Oops"Fork",3); -   if(pid==0) { +BE_DC (TODC, FROMDC);//Child do DC A}Else{ atBE_BC (TODC, FROMDC);//Parent do UI - Wait (NULL); -   } -   return 0; - } -  in voidBE_DC (intTodc[],intfromdc[]) { -   //Would do DC (), change the stdin-todc[1], change the stdout to Fromdc[0] toClose (todc[1]); +Close (fromdc[0]); -   if(Dup2 (todc[0],0)==-1) theOops"could not redirect the stdin",4); *   if(Dup2 (fromdc[1],1)==-1) $Oops"could not redirct the stdout",5);Panax NotoginsengClose (todc[0]); -Close (fromdc[1]); theEXECLP ("DC","DC","-", NULL); +Oops"EXE",1); A } the  + voidBE_BC (intTodc[],intfromdc[]) { -   //would do BC (), change the stdout to todc[1], and use the fromdc[0] to read the DC ' s result $   CharCmdbuf[bufsiz]; $   intC1, C2; -   Char operator; -FILE *fout, *fread; theClose (todc[0]);//donot need to todc out -Close (fromdc[1]);//donot need to FROMDC inWuyi    theFout=fdopen (todc[1],"W");//write the todc[1] -Fread=fdopen (fromdc[0],"R");//read the DC ' s result from fromdc[0] Wu    -    while(printf ("TINYBC:") && (Fgets (Cmdbuf, Bufsiz, stdin)!=-1)) { About     if(Sscanf (Cmdbuf,"%d%c%d", &c1,&operator, &AMP;C2)! =3) { $printf"Syntax error.\n"); -       Continue; -     } -printf"2u Cin is%d%c%d\n", C1,operator, C2); A     if(fprintf (Fout,"%d\n%d\n%c\np\n", C1,C2,operator) = = EOF)//There is a problem here, C1 always read not to go in, has been 0. +Oops"fprintf",6); the      - fflush (fout); $     if(Fgets (Cmdbuf, bufsiz, fread) = =NULL) the        Break; theprintf"%d%c%d =%s", C1,operator, C2, cmdbuf); the   } theClose (todc[0]); -Close (fromdc[1]); in}

Here, in 2 pipe use, to distinguish between Chu Pipe[0],pipe[1] which is read out which is written.

At the same time, the SSCANF function is used here.

/**********************************************************************************************/

SSCANF (formatted string input)
Correlation function scanf,fscanf
Table header file #include <stdio.h>
Defines the function int sscanf (const char *str,const char * format,........);
The function Description sscanf () Converts the string of the parameter str according to the parameter format string and
The data is typed. Format conversion form please refer to scanf ()
。 The converted results are stored in the corresponding
In the parameters.
return value
Success returns the number of arguments, failure returns 1, and the reason for the error is stored in errno.
Example
#include <stdio.h>
Main ()
{
int i;
unsigned int J;
Char input[]= "Ten 0x1b aaaaaaaa bbbbbbbb";
Char s[5];
SSCANF (Input, "%d%x%5[a-z]%*s%f", &i,&j,s,s);
printf ("%d%d%s\n", i,j,s);
}

/***************************************************************************************/

Fdopen function

The Fdopen function, with the Fopen class, returns a file* type value, unlike this function, which takes a file descriptor instead of a file as a parameter.

When using fopen, pass the file name as an argument to it. Fopen can open a device file or open a regular disk file. You can use Fdopen if you know only the file descriptor but not the filename.

Using Fdopen makes processing of a remote process as if it were a regular file

Linux_c bc/uses 2 pipes to communicate with 2 processes

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.