Tja deine geschilderte Art von Kommunikation wird nur über stdout und stdin funktionieren. Ich habe dir ein Beispiel gebaut basierend auf Example - exec and pipes um zu demonstrieren wie das funktioniert.
main.c: ```#include
#include
#include
#include
void main()
{
int pid;
int pc[2]; /* Parent to child pipe /
int cp[2]; / Child to parent pipe */
char ch[100];
int outcount = 0;
const char *hello_from_c = "Hallo Welt von C
";
/* Make pipes */
if( pipe(pc) < 0)
{
perror("Can't make pipe");
exit(1);
}
if( pipe(cp) < 0)
{
perror("Can't make pipe");
exit(1);
}
pid = fork();
/* Create a child to run command. */
if (pid == 0 )
{
/* Child. */
close(STDOUT_FILENO); /* Close current stdout. */
dup( cp[1]); /* Make stdout go to write
end of pipe. */
close(STDIN_FILENO); /* Close current stdin. */
dup( pc[0]); /* Make stdin come from read
end of pipe. */
close( pc[1]);
close( cp[0]);
execlp("python3", "python3", "script.py");
exit(1);
}
else if (pid > 0)
{
/* Parent */
write(pc[1], hello_from_c, strlen(hello_from_c));
close(pc[1]);
int n = read(cp[0], ch, 100);
write(STDOUT_FILENO, ch, n);
close(cp[0]);
exit(0);
}
else
{
exit(1);
}
}```
script.py: [PYTHON]import sys
line = sys.stdin.readline()
print (‘Hallo Welt von Python’)
print (line)[/PYTHON]
Grüße,
MDickie
*** Edit ***
Da du ja eigentlich wolltest, dass man die Pipes der Python Anwendung übergibt, habe ich jetzt auch noch dafür eine Lösung gefunden.
#include
#include
#include
void main(int argc, char ** argv, char ** envp)
{
int pid;
int pc[2]; /* Parent to child pipe */
int cp[2]; /* Child to parent pipe */
char ch[100];
int outcount = 0;
const char *hello_from_c = "Hello from C
";
/* Make pipes */
if( pipe(pc) < 0)
{
perror("Can't make pipe");
exit(1);
}
if( pipe(cp) < 0)
{
perror("Can't make pipe");
exit(1);
}
pid = fork();
/* Create a child to run command. */
if (pid == 0 )
{
/* Child. */
char pc_str[15];
sprintf(pc_str, "%d", pc[0]);
char cp_str[15];
sprintf(cp_str, "%d", cp[1]);
char *args[5] = {"python3", "script.py", cp_str, pc_str, NULL};
execvp("python3", args);
exit(1);
}
else if (pid > 0)
{
write(pc[1], hello_from_c, strlen(hello_from_c));
close(pc[1]);
int n = read(cp[0], ch, 100);
write(1, ch, n);
close(cp[0]);
exit(0);
}
else
{
exit(1);
}
}```
[PYTHON]import sys
import os
f = os.fdopen(int(sys.argv[2]), 'r')
line = f.readline()
f.close()
f = os.fdopen(int(sys.argv[1]), 'w')
f.write("Hello from Python
")
f.close()[/PYTHON]