#problem with pipes and processes

29 messages · Page 1 of 1 (latest)

hexed sparrowBOT
#

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.

ocean phoenix
#

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

swift nimbus
swift nimbus
ocean phoenix
#

Then dup STDOUT to a new file descriptor before overwriting it with the pipe

swift nimbus
#

But when I close it to write through the pipe again, then I won't be able to write through the terminal again, right?

ocean phoenix
#

One moment

#

;compile

#include <unistd.h>

int main() {
    int newstdout = dup(STDOUT_FILENO);
    close(STDOUT_FILENO);
    write(newstdout, "Hello World\n", 12);
}
rocky condorBOT
#
Program Output
Hello World
ocean phoenix
#

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);
}
rocky condorBOT
#
Program Output
Hello World
swift nimbus
ocean phoenix
#

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

modern blade
modern blade
ocean phoenix
#

If it were just one's own personal project, then sure, why not

modern blade
#

oh wait, you run execv

ocean phoenix
#

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

modern blade
#

so, the child process just, dies in the end

ocean phoenix
#

file descriptors are perserved through execve

modern blade
ocean phoenix
#

yes

hexed sparrowBOT
#

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.

swift nimbus
#

!solved

hexed sparrowBOT
#

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