Comment detail

入出力の中継 (Nested Flatten)

This comment is reply for 310 匿名: 正統的にやってみたつもりです(入出力の中継). Go to thread root.

本来はPIPEを作ってやるべきだろうと思い直しました。Javaでは無理そうなので、C を使います。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>

int main(int argc, char *argv[])
{
  int p1[2], p2[2];
  pid_t pid1, pid2, p;
  int stat;

  pipe(p1);
  pipe(p2);
  pid1 = fork();
  if (pid1 == 0) { // child process 1
    dup2(p1[0], 0);
    close(p1[0]);
    close(p1[1]);
    dup2(p2[1], 1);
    close(p2[0]);
    close(p2[1]);
    execl(argv[1], argv[1], NULL);
    exit(2);
  }
  pid2 = fork();
  if (pid2 == 0) { // child process 2
    dup2(p2[0], 0);
    close(p2[0]);
    close(p2[1]);
    dup2(p1[1], 1);
    close(p1[0]);
    close(p1[1]);
    execl(argv[2], argv[2], NULL);
    exit(2);
  }
  
  close(p2[0]);  // parent process
  close(p2[1]);
  close(p1[0]);
  close(p1[1]);
  
  p = wait(&stat);
  if (p == pid1)
    kill(pid2, SIGINT);
  else if (p == pid2)
    kill(pid1, SIGINT);
  p = wait(&stat);
  return (0);
}
SIGINTではなくSIGTERMを使うべきらしい……

Index

Feed

Other

Link

Pathtraq

loading...