How to get child PID in C?

后端 未结 5 677
失恋的感觉
失恋的感觉 2020-12-31 03:10

I\'m creating child processes in a for-loop. Inside the child process, I can retrieve the child PID with getpid().

However, for some reas

5条回答
  •  天命终不由人
    2020-12-31 03:43

    fork already returns the child's pid. Just store the return value.

    look at man 2 fork:

    RETURN VALUES

     Upon successful completion, fork() returns a value of 0 to the child process and
     returns the process ID of the child process to the parent process.  Otherwise, a
     value of -1 is returned to the parent process, no child process is created, and
     the global variable errno is set to indicate the error.
    

提交回复
热议问题