Answer to Question #63696 in C# for Tomader

Question #63696
my code shows me error
echo: write error: Bad file descriptor
when i execute echo hi | wc

this is my code:

void execute_commands(struct command mycommands[], int no_commands)
{
int i;
int *arr[no_commands-1];
for (i=0; i<no_commands-1; i++)
arr[i] = (int *)malloc(2 * sizeof(int));
for (i=0; i<no_commands-1; i++)
pipe(arr[i]);
for( i = 0; i < no_commands - 1; i++)
execute_command(mycommands[i].comm, mycommands[i].no_args, mycommands[i].args, arr[i][1], arr[i][0]);
}
int execute_command(char *command, int argc, char *args[], int fd_in, int fd_out )
{
pid_t pid;
int child_status;
if ((pid = fork()) == 0){
if (fd_in != -1) {
dup2(fd_in, 0);
close(fd_in);
}
if (fd_out != -1){
dup2(fd_out, 1);
close(fd_out);
}
execvp(command, args);
exit(0);
}
else if (pid > 0){
if (fd_in != -1) close(fd_in);
if (fd_out != -1) close(fd_out);
waitpid(pid, &child_status, 0);
}
else {
fprintf(stderr, "Error in creating a child process!\n");
exit(-1);
}
return 0;
}
1
Expert's answer
2016-11-26T11:44:19-0500
Dear Tomader, your question requires a lot of work, which neither of our experts is ready to perform for free. We advise you to convert it to a fully qualified order and we will try to help you. Please click the link below to proceed: Submit order

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS