Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 10th, 2004, 6:59 PM   #1
Tazz13
Newbie
 
Join Date: Oct 2004
Posts: 3
Rep Power: 0 Tazz13 is on a distinguished road
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());
}
Tazz13 is offline   Reply With Quote
Old Oct 10th, 2004, 8:40 PM   #2
Sebeq
Programmer
 
Sebeq's Avatar
 
Join Date: Aug 2004
Location: Houston, Tx
Posts: 53
Rep Power: 5 Sebeq is on a distinguished road
Send a message via AIM to Sebeq Send a message via Yahoo to Sebeq
Quote:

#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());
}


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!!"
Sebeq is offline   Reply With Quote
Old Oct 10th, 2004, 9:45 PM   #3
Tazz13
Newbie
 
Join Date: Oct 2004
Posts: 3
Rep Power: 0 Tazz13 is on a distinguished road
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
Tazz13 is offline   Reply With Quote
Old Oct 11th, 2004, 10:29 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Sebeq, take a look at that if statement - in your version, there are 3 opening tags and 4 closing tags. He was right.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Oct 11th, 2004, 11:53 AM   #5
Sebeq
Programmer
 
Sebeq's Avatar
 
Join Date: Aug 2004
Location: Houston, Tx
Posts: 53
Rep Power: 5 Sebeq is on a distinguished road
Send a message via AIM to Sebeq Send a message via Yahoo to Sebeq
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!!"
Sebeq is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC