1,105,229 Community Members

Create three child processes by a parent process

Member Avatar
boiishuvo
Junior Poster in Training
86 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
nezachem
Practically a Posting Shark
896 posts since Dec 2009
Reputation Points: 616 [?]
Q&As Helped to Solve: 197 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
boiishuvo
Junior Poster in Training
86 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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()?

Member Avatar
nezachem
Practically a Posting Shark
896 posts since Dec 2009
Reputation Points: 616 [?]
Q&As Helped to Solve: 197 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
Aia
Nearly a Posting Maven
2,307 posts since Dec 2006
Reputation Points: 1,977 [?]
Q&As Helped to Solve: 236 [?]
Skill Endorsements: 20 [?]
 
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.

Member Avatar
nezachem
Practically a Posting Shark
896 posts since Dec 2009
Reputation Points: 616 [?]
Q&As Helped to Solve: 197 [?]
Skill Endorsements: 0 [?]
 
-1
 

> Dev-C++

Missed that.

rakeshbiswal
Newbie Poster
1 post since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

please explain briefly how it happens?

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