Ncurses with non-blocking input and fork?

OK so here's the code I had in mind. #include #include #include int main () { int pipePC2; int pipeCP2; pipe(pipePC); pipe(pipeCP); char buf11; int n; switch (fork()) { case -1: exit (1); break; case 0: /* child */ close(pipePC1); close(pipeCP0); while (1) { /* read, then write */ n = read(pipePC0, buf, 10); /* read the question */ if (n > 0) { bufn = 0; printf ("child got '%s'\n", buf); /* calculate the answer here */ write(pipeCP1, "foobar", 6); /* write the answer */ } else { printf ("child got nothing\n"); exit (1); } sleep(2); /* only to slow down the output */ } break; default: /* parent */ close(pipePC0); close(pipeCP1); while (1) { /* write, then read */ write(pipePC1, "barfoo", 6); /* ask a question */ n = read(pipeCP0, buf, 10); /* get the answer */ if (n > 0) { bufn = 0; printf ("parent got '%s'\n", buf); } else { printf ("parent got nothing\n"); exit (1); } sleep(3); /* only to slow down the output */ } break; } }.

– Llorens Marti Jun 22 at 6:49 No. In your original code both parent and child wait for the other to finish writing, and so they do in my code. If you want you can make I/O non-blocking on one or both sides, just remove the calls to exit after the "got nothing" message.

It will still work, no semaphores required. They will sometimes print "got nothing", and sometimes print parts of the message or several messages together, like "rfoobarfoo". But you shouldn't rely on being able to read the entire message with one read call anyway.

– n.m. Jun 22 at 7:26 if I only remove the exit, it will not grant me non-bloking behaviour for the parent if son is waiting for input, because if there is nothing to read on the pipe, parent will wait until it is. I need that parent still looping if there is no data from the son.

– Llorens Marti Jun 22 at 7:53 You need to add the fcntl for non-blocking input too of course. – n.m. Jun 22 at 8:24 And then, if you imagine I remove only the semaphores in my initial code, why parent didn't recieve anything from the son if son is writing correctly to the pipe?

– Llorens Marti Jun 22 at 8:29.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions