English 中文(简体)
两条同时连接 C. 方案
原标题:Connection of two simultaneous C programs
  • 时间:2012-01-13 19:08:19
  •  标签:
  • c

我必须知道,如何把两个简单的C方案的投入和产出同时联系起来,把第三个 Java/C方案联系起来。

我曾尝试过管道、袖珍和客户的连接,但是,如果只使用扫描仪和印刷件,便无法使用简单的C方案。

Sample programs: Program1.c

#include<stdio.h>
int main(int argc,char *argv[])
{
  int a;
  while(1)
  {
    scanf("%d",&a);
    printf("%d",a);
  }
  return 0;
}

方案2.c

#include<stdio.h>
int main(int argc,char *argv[])
{
  int a;
  while(1)
  {
    scanf("%d",&a);
    printf("%d",a);
  }
  return 0;
}

My aim is to connect stdin and stdout of program1.c with stdin and stdout of program2.c with the help of a third Java or C program

问题回答

基本上,你们想要做这样的事情:

  • “Program3” creates a pipe
  • Program 3 calls fork, and in the child, changes out the pipe s FD for stdout, then exec s Program2;
  • Program 3 then changes out the pipe for stdin, and exec s Program1

You should use fork and exec
Write a third C program, which would open a two pipes (using pipe). This gives you 4 file descriptors (2 input, 2 output).
Then it forks - the child will later run program1 and the parent program2 (or vice versa - doesn t matter).
Each of the children will close 2 file descriptors, and remain with one input and one output (but not of the same pipe). They should also close standard input and output.
Then use the dup2 function to make the pipe fds become 0 and 1.
Now you have two processes, with one s stdin connected by pipe to the other s stdout, and vice versa.
The last stage is to use exec to make the run program1 and program2.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签