954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Create three child processes by a parent process

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?

boiishuvo
Junior Poster in Training
65 posts since Jun 2009
Reputation Points: 10
Solved Threads: 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

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 
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()?

boiishuvo
Junior Poster in Training
65 posts since Jun 2009
Reputation Points: 10
Solved Threads: 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?

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

> '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 header files we include.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

> Dev-C++

Missed that.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: