C pipe


Joined
Dec 9, 2021
Messages
1
Reaction score
0
Hi everyone.
I want to execute a child process and the parent handle the input and output of the child process.
Here is my code:

int main(int argc, char const *argv[])
{
int fork_pid = fork();
char* argve[] = { NULL };
char* envp[] = { NULL };
args *arg = (args *)malloc(sizeof(args));
arg->io = (int *)malloc(2*sizeof(int));
char *command = "/bin/python3";
arg->command = (char *)malloc(strlen(command)*sizeof(char));
for (int i = 0; i < strlen(command); ++i)
{
arg->command = command;
}
int nbytes = 0;
char buffer[1025];

if (fork_pid == 0){
execve(arg->command, argve, envp);
pipe(arg->io);
}else{
sleep(5);
write(arg->io[1], "2**4\x0A", 6);
do
{
nbytes = read(arg->io[0], buffer, sizeof(buffer));
}while( nbytes == -1 );
buffer[nbytes] = '\0';
printf("%s\n", buffer);
}

return 0;
}
 
Ad

Advertisements

Joined
Mar 3, 2021
Messages
243
Reaction score
29
I can't even come close to compiling that. What is the struct args? Can you post the full code? What's not working?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top