![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2004
Posts: 3
Rep Power: 0
![]() |
Hi,
I need some help with using the fork() command. What I need to do is have the program create 3 child processes using fork() then each of the 3 child processes create 1 process each using fork(). Then I need the proces ID's of each process to be printed. I have gotten the first part where I create 3 child processes, now I don't know where to insert another fork() such that each of the 3 child processes creates 1 child process of its own. I am using the bash shell and using the "Pico" text editor to write this C Program. I am using the 'gcc' compile command to compile the program. Here is the code I have so far: #include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void main()
{
*int i;
*int n;
*pid_t childpid;
*for(i = 1; i < 4; ++i)
*{
* * if((childpid = fork()) <= 0)
* * {
* * * *break;
* * }
*}
*fprintf(stderr, "This process %ld with parent %ld\n", (long)getpid(), (long)getppid());
} |
|
|
|
|
|
#2 | |
|
Programmer
|
Quote:
Try it like this #include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void main()
{
int i;
int n;
pid_t childpid;
for(i = 1; i < 4; ++i)
{
if((childpid = fork()) <= 0)) /*note that i put a ) at the end of the statement*/
{
break;
}
}
fprintf(stderr, "This process %ld with parent %ld\n", (long)getpid(), (long)getppid());
}Just remember if you have more than on "(" in a statement then you need to add 2 )) at the end of it.
__________________
"I messured the landing in feet, and programmed it in meters, $1billion wHoOoOoPsSsSiE!!" |
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2004
Posts: 3
Rep Power: 0
![]() |
Thankz but that'z not the problem...the code works correctly...I jsut want each of hte 3 child processes to spawn 1 child of their own.
Thankz |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Sebeq, take a look at that if statement - in your version, there are 3 opening tags and 4 closing tags. He was right.
|
|
|
|
|
|
#5 |
|
Programmer
|
Ahhh yes now i feel like a dummy. I can't see why i didn't see that
![]()
__________________
"I messured the landing in feet, and programmed it in meters, $1billion wHoOoOoPsSsSiE!!" |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|