2016-11-13 5 views
0

したがって、親によって作成されたパイプを介して、Cの親プロセスからPythonの子プロセスに文字列を送信しようとしています。その後、私は私のpythonの子供から親に戻ってそれを印刷する文字列を送り返したいと思います。C親からPythonの子にデータを送信し、パイプを使用して返す

これは私のC-コード:

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/wait.h> 
#include <sys/types.h> 

int main(int argc, char **argv, char **envp) { 
    pid_t childpid; 
    int status; 
    int test; 
    char *argument; 
    int fd[2]; 
    char *numbers = "3,5,7\n"; 

    argument = "./fork.py"; 
    char *python[] = {argument, NULL}; 

    test = 0; 

    pipe(fd); //Create pipe 

    childpid = fork(); 

    if (childpid==(-1)) { 
     perror("fork"); 
     return -1; 
    } else if (childpid==0) { 
     //in child 
     execvp(argument, python); 
    } 

    //in parent 
    write(fd[1], numbers, sizeof(numbers)); 
    close(fd[1]); 

    waitpid(-1, &status, 0); //Wait for childprocess to finish 
    char buffer[1024]; 

    while (read(fd[0], buffer, sizeof(buffer)) != 0) {} //Read pipe to buffer 

    if (test < 0) { //If failed print error message or success message 
     printf("Error: %s\n", buffer); 
    } else { 
     printf("%s\n", buffer); 
    } 

    return 0; 
} 

これは私のPythonコード(fork.py)です:

#!/usr/bin/env python3 
import os 
import sys 

def main(): 
    r, w = os.pipe() 
    os.read(r, 12) 
    os.close(r) 
    os.write(w, "This is a test") 
    os.close(w) 
    sys.exit(0) 


if __name__ == '__main__': 
    main() 

パイプをフォークしなければならないので、私は間違って何をやっていると、したがって、子供がアクセスできる?パイプにデータが渡されないか、Pythonのプロセスが何らかの方法でパイプにアクセスしてデータを取得できないようです。そしてはい、それはパイプでなければなりません。

答えて

0

新しいパイプペアのペアをPythonで生成しています。 Pythonと使用中の引数を取る

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/wait.h> 
#include <sys/types.h> 

int main(int argc, char **argv, char **envp) { 
    pid_t childpid; 
    int status; 
    int test; 
    int fd_a[2], fd_b[2]; 
    char *numbers = "3,5,7\n"; 

    test = 0; 

    pipe(fd_a); //Create pipe 
    pipe(fd_b); 
    childpid = fork(); 

    if (childpid==(-1)) { 
     perror("fork"); 
     return -1; 
    } else if (childpid==0) { 
     //in child 
     char *argument = "./fork.py"; 
     char fd0[20], fd1[20]; 
     snprintf(fd0, 20, "%d", fd_a[0]); 
     snprintf(fd1, 20, "%d", fd_b[1]); 
     close(fd_a[1]); 
     close(fd_b[0]); 
     char *python[] = {argument, fd0, fd1, NULL}; 
     execvp(argument, python); 
    } 
    close(fd_a[0]); 
    close(fd_b[1]); 

    //in parent 
    write(fd_a[1], numbers, sizeof(numbers)); 
    close(fd_a[1]); 

    waitpid(-1, &status, 0); //Wait for childprocess to finish 
    char buffer[1024]; 

    while (read(fd_b[0], buffer, sizeof(buffer)) != 0) {} //Read pipe to buffer 

    if (test < 0) { //If failed print error message or success message 
     printf("Error: %s\n", buffer); 
    } else { 
     printf("%s\n", buffer); 
    } 

    return 0; 
} 

:あなたは同じを使用する必要があなたはC.

で作成されたハンドルのpythonにCから通信するための1ペアを作成し、CへのPythonからの通信のための別のこれらはハンドルとして:

#!/usr/bin/env python3 
import os 
import sys 

def main(): 
    r, w = map(int, sys.argv[1:]) 
    os.read(r, 12) 
    os.close(r) 
    os.write(w, b"This is a test") 
    os.close(w) 
    sys.exit(0) 


if __name__ == '__main__': 
    main() 
+0

これは私に、 'os.read'のpythonでの不良ファイル記述子エラーをもたらします。 – Alesfatalis

+0

フォークする前にハンドルを閉じましたか? – Daniel

+0

すべてのものが正常に動作し、ソリューションを実装しているサイトでエラーが発生しました。 – Alesfatalis

関連する問題