Hi folks, I am writing a simple program to understand how fork() and waitpid works, but it doesn't seem that is working like I wanted.
One of the problems I have encountered is that the printed PID by the father does not correspond to any of the childs (sometimes 0 is printed).
The other one is that the fater doesn't wait for the childs, the message "acaba de finalizar..." will pop out first than " soy el hijo...", and what I want is for the father to wait for the child to finish, and once is finished, print that the child with PID X has finished.
You're not quitting in your child code, so they finish executing the child code then continue blithely on into the parent code. Since you haven't posted your complete code I can't tell what they'd be doing, but it's possible that some of your children may be fork()-ing and ending up printing like the parent should. That would explain some of the strange PID values.
Instead of using a 9-long if/else chain for 9 pid's, use a loop that can handle any number of PID's with the same code:
You should put \n at the end of the line, not the beginning, because printf only actually prints AFTER it receives a \n. printf("\nthis is a string"); will leave stuff in the buffer that might be printed twice (since fork() copies the buffer along with the rest of the process).
I'm not sure what your status1, status2, ...variables are since you didn't post that code, but since you're not using & anywhere, you're probably doing it wrong. You should give waitpid() a pointer to an integer -- not an integer, and not a bare pointer.
Last edited by Corona688; 12-09-2011 at 04:44 PM..
I understand that fork create a child but I need very simple example that make child useful....
I mean how will make the program faster anyone explain with code plz
using C plz (2 Replies)
I'm attempting to write a daemon that will start, stop, and monitor processes across a network of servers, meaning that a daemon would start on each server, attempt to connect to siblings at regular intervals (if there are unconnected siblings), and start services as remote dependencies are... (3 Replies)
Hi gurus, I would like to fork more children and then write their return values: so far I tried:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
int rv=0, i;
... (5 Replies)
I'm trying to run a simple test on how to use fork(), i'm able to execute the child process first then the parent, but how can I execute parent then child..?
Thanks! (1 Reply)
Hi everybody,
I wanna write a code to understand how fork works.
my target
--------------
-Parent creates a file(called temp) and writes into this file "1".Then it closes the file.
-Then parent creates a child and wait until execution of this child ends.
-Then child opens the same... (3 Replies)
hi all
About this code
for (i = 1; i < n; i++)
if ((childpid = fork()) <= 0)
break;
I really can't understand the output .
and the way fork () return the value .
how about the process Id ,the child process Id and the parent ID
in this case
so please answer me soon (5 Replies)
Dear All,
I'm trying to write multithreading TCP Daemon which executes external program when new network connection arrives on a socket.
After accept() I'm doing fork() for initiating of new child process, in which will be executed external program. After child creation I'm doing fork() again,... (3 Replies)
Hello all.
I'm developing a filetransfer application, which is supposed to work sort of like dcc, with multiple transfers etc. Now i wonder what the best way to manage the transfers is. Should i fork() for each new transfer, hogging loads of memory or use pthreads? Maybe I can use select to see... (0 Replies)