When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
25 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
You need pipes the amount of processes you launch - 1. Having just one pipe won't cut it. A pipe acts like a FIFO. If all processes tries to write to the same pipe - you'll get some mismatch output. Same applies to reading from a pipe
As a side note - since you are aware of loops, you might want to separate that huge function into sub functions
That loop seems a bit off. Why pipe_count + 1? What the porpose of that if right after it?
https://github.com/nofunadler/simple-shell/blob/main/lib/util/pipes.c is a bad implementation i wrote some time ago. Might be of some help
As for
technically you can if either you don't launch more than 2 processes at a time / each child forks itself (in which case it's child gets a copy of the pipe)
As mentioned before - you need commands - 1 pipes
As for that if, my main concern here is the odd/even check. Why do you need that exactly?
Thats a bit complicated for no reason, don't you think? (Assuming it is correct, which appears to not be the case here sadly)
Did you manage to take a look at the link i posted above? I think you'd the way there a bit more intuitive or at the very least - simpler
As for your first line here - it still doesn't explain the +1 part
COUNT_END is the constant expression with the value of 2 due to the way enums work (it's for readability, and to get rid of 'magic numbers' within the code).
pos (a shorthand for 'position') is used to determine how to configue the pipes.
for example - if we had something along the lines of: ls . | grep ... | xargs ...
then:
ls will need to bind its STDOUT_FILENO to the write end of the pipe
grep will need to bind its STDIN_FILENO to the read end of the pipe and its STDOUT_FILENO to the write end of the pipe
and so on.
pos here is the position of a command within the list of commands if you will. in the above example ls is at position 0, grep at position 1 and so on
a. a child doesn't need an entire array of pipes
b. leaving them open will not terminate certain programs. they won't receive EOF otherwise
c. after that dup call - there's no need for the pipe. STDIN_FILENO / STDOUT_FILENO will 'point' to the respective ends of the pipe (closing the pipe then become necessary due to point b.)
bare in mind that all the above comes from the fact that a child will inherit all open fds from its parent by default
@slender comet Has your question been resolved? If so, type !solved :)
gl!
This question is being automatically marked as stale.
If your question has been answered, type !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.
Sorry for the delay, Your malloc call in create_pipefd is wrong. You either want malloc(sizeof(int) * ...) or, better yet malloc(sizeof *pipes * ...)
Note sizeof(int *) may not (and is not on most architectures) be equal to sizeof(int)
@slender comet Has your question been resolved? If so, type !solved :)
Would you mind sending a minimal working (or rather - not working) example?
Assuming you fixed that malloc call - I can't see anything wrong with the snippet above
commands[0].argv = malloc(4096 * sizeof(char));
commands[1].argv = malloc(4096 * sizeof(char));
commands[2].argv = malloc(4096 * sizeof(char));```
seem a bit off considering `command::argv` is a `char **` (and not a `char *`)
this then
```c
for (size_t x = 0; x < BUFFER_SIZE; x++) {
commands[0].argv[x] = malloc(COMMAND_SIZE * sizeof(char)); // malloc each element of the array seperately
memset(commands[0].argv[x], 0, COMMAND_SIZE); // zero out the memory
commands[1].argv[x] = malloc(COMMAND_SIZE * sizeof(char)); // malloc each element of the array seperately
memset(commands[1].argv[x], 0, COMMAND_SIZE); // zero out the memory
commands[2].argv[x] = malloc(COMMAND_SIZE * sizeof(char)); // malloc each element of the array seperately
memset(commands[2].argv[x], 0, COMMAND_SIZE); // zero out the memory
}```
becomes wrong
that wasn't the point. i have no idea what you're trying to do but the correct form would be either:
commands[i].argv = malloc(4096 * sizeof(char *)); or better yet commands[i].argv = malloc(4096 * sizeof *commands[i].argv); and then assign it some char *
OR
having command::argv as a char * allocating it as you did in the snippet above
yes. in addition it make it so you don't have to repeat yourself (in code), as well as guarding you from the above mistake
take a closer look here:
for (size_t j = 0; j < pipe_count; j++) {
close(pipes[i * ...]);
close(pipes[i * ...]);
}```
whats wrong?
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
np