1.11M Members

Create three child processes by a parent process

 
0
 

Okay I am trying to write a program where a parent process creates three child processes. Also waits for each of them to finish. Here is my code:

#include <stdio.h>
#include <unistd.h>

main() {
       int i;
       
       // create 3 child processes
       for (i = 0; i < 3; i++) {
           if (fork() == 0) {
                      printf("Child process no.%d with ID: %d created from parent process\n", i);
                      break;
                      }
           }
           // wait for each child process to finish
           
}

There is one error line "if (fork() == 0) {"
What is wrong with that? I tried to run it using Dev-C++.

How do I build the wait() for each of those three child processes to finish?

By the way, is there any software that allow me to use a virtual terminal (aka command) Unix/Linux on Windows?

 
0
 

I don't see anything wrong with the line 9. Can you post an error message?
I do see however an error at line 10 - missing parameter for the second %d

 
0
 

I don't see anything wrong with the line 9. Can you post an error message?
I do see however an error at line 10 - missing parameter for the second %d

The error message says,

"In function 'int main()':
9 line - 'fork' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)"

Ok thanks for telling me that error at line 10.

Um so I wrote to create 3 child processes, how do I make each child to work themselves, and let each of them finish using wait()?

 
0
 

> 'fork' undeclared (first use this function)

This is really surprising. Is it the only error message? Does it maybe complain about unistd.h not being found?

 
0
 

> 'fork' undeclared (first use this function)

This is really surprising. Is it the only error message? Does it maybe complain about unistd.h not being found?

No really.
Dev-C++ is for MS Windows, and Windows do not have the ability of forking, regardless of how many <unistd.h> header files we include.

 
-1
 

> Dev-C++

Missed that.

 
0
 

please explain briefly how it happens?

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: