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.
29 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.
One solution is to have pong send a message to ping to print something to the terminal
Alternatively, dup stdout to another file descriptor BEFORE overwriting with the pipe
Or just have the pipe not overwrite stdout and write to the pipe descriptor directly
yes, but they don't let us do it.
It is a requirement to connect the STDOUT and the STDIN to pipe1 and pipe2
Then dup STDOUT to a new file descriptor before overwriting it with the pipe
But when I close it to write through the pipe again, then I won't be able to write through the terminal again, right?
One moment
;compile
#include <unistd.h>
int main() {
int newstdout = dup(STDOUT_FILENO);
close(STDOUT_FILENO);
write(newstdout, "Hello World\n", 12);
}
Hello World
Forwarding the new descriptor to PONG might be difficult, though you might be able to use a constant for that
;compile
#include <unistd.h>
int main() {
int newstdout = 10;
dup2(STDOUT_FILENO, newstdout);
close(STDOUT_FILENO);
write(newstdout, "Hello World\n", 12);
}
Hello World
So you propose that the child, when it has to be shown through the terminal, makes a dup2 and when it wants to send it through the terminal it makes another dup2 with the other decryptor, is that correct?
One dup2 to preserve stdout in another file descriptor that can be used whenever the child wants to print to the terminal.
THen the regulard dup2's to move the pipes
bruh,
call write and use the stderr file descriptor to print the text
why are you overengineering this, the answer is obvious, use stderr to print the text on the screen
If it were just one's own personal project, then sure, why not
what you wrote is needed in cases where the goal is to redirect stdout to something, let it run, and then restore stdout back to file descriptor 1, I would chose to leave fd 1 untouched and instead open the pipe with fd 3,4, etc, and use those
oh wait, you run execv
All I did was show that you can maintain a handle to stdout, while still overwriting fd 1.
From what I gathered, the assignment wanted him to have the pipe point the fd 1, while also wanting to print to the terminal.
I figured it is a resonable to expect the teacher wants to print via stdout as opposed to stderr
so, the child process just, dies in the end
file descriptors are perserved through execve
the child process uses fd 1, so you gotta prepare fd 1 before you fork and exec
yes
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.
!solved
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