We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,186 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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?

5
Contributors
6
Replies
2 Years
Discussion Span
5 Months Ago
Last Updated
10
Views
boiishuvo
Junior Poster in Training
76 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
913 posts since Dec 2009
Reputation Points: 719
Solved Threads: 197
Skill Endorsements: 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()?

boiishuvo
Junior Poster in Training
76 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
913 posts since Dec 2009
Reputation Points: 719
Solved Threads: 197
Skill Endorsements: 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.

Aia
Nearly a Posting Maven
2,394 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 220
Skill Endorsements: 14

> Dev-C++

Missed that.

nezachem
Posting Shark
913 posts since Dec 2009
Reputation Points: 719
Solved Threads: 197
Skill Endorsements: 0

please explain briefly how it happens?

rakeshbiswal
Newbie Poster
1 post since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0766 seconds using 2.77MB