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?

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

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()?

> '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?

> '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.

> Dev-C++

Missed that.

commented: why do you give me bad vote? :( you are one who made mistake!! I am giving helpful advice, look at the link!! :) +0

please explain briefly how it happens?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.