1

for my C project I need to know in which state (running, waiting, terminated, ...) the various processes are. The processes are created by myself using many fork(). Does anyone have any idea how to do that?

Example: I have a process with PPID = x I do 3 fork() -> I get three new processes with PID = x+1, PID = x+2, and PID = x+3 (more or less). I need to know if the processes with PID = x+1, PID = x+2, and PID = x+3 are running or waiting or terminated.

nikMik
  • 23
  • 3
  • What do you mean by "waiting"? Do you mean waiting as a result of SIGSTOP, or do you mean blocked on IO, or runnable but waiting for a time slice, or something else? – William Pursell Aug 17 '20 at 16:19
  • So you want [`waitpid`](https://man7.org/linux/man-pages//man2/waitpid.2.html)? – KamilCuk Aug 17 '20 at 16:20

1 Answers1

0

if you do 3 fork()'s you have more than 3 new processes. You have 2^n processes. n being the number of times you call fork()

for example

#include <stdio.h> 
#include <sys/types.h> 
int main() 
{ 
    fork(); 
    fork(); 
    fork(); 
    printf("hello\n"); 
    return 0; 
}

prints this

hello
hello
hello
hello
hello
hello
hello
hello

also I do believe your question has been answered here

  • If he only calls `fork()` again in the parent process he won't get 2^n. Since he hasn't posted actual code, I think it's premature to make assumptions. Also, it's not really relevant to the question. – Barmar Aug 17 '20 at 16:27