• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

Forking in C

Ok I need to write a program that forks and identifies itself as the parent or child process
I am still very dumb with C so this should be simple


Here is my code and it doesn't seem to run the child processes
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{

	int childrens = 5;
	int count = 0;
	pid_t my_pid;
	for (count = 0; count < 20; count++)
	{
		my_pid = getpid();
		if (getpid != 0)
		{
			printf("I am the parent\n");
			if (childrens < 5){fork();}
		}
		else
		{
			printf("I am the child\n");
		}
	}

}

Open in new window

0
GPicasso
Asked:
GPicasso
  • 5
  • 2
  • 2
  • +3
2 Solutions
 
for_yanCommented:
but it will run it if childrens<5 and childrens = 5 , so 5 is not less than 5, so it does not go there
0
 
for_yanCommented:
make it
if (childrens <6 ){fork();}
0
 
Michael FowlerSolutions ConsultantCommented:
Been a while since I done any C programming but

You have declared childrens to be 5 and then if childrens < 5 then fork

You should set childrens to 0 and then increment its count each tinme you forl eg
   
   if (childrens < 5){
      fork();
      childrens++;
   }

Michael
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kent OlsenData Warehouse Architect / DBACommented:
Hi G,

The task's pid is always non-zero.  :)

I believe that what you want to do is call fork, and test the results.

Be EXTREMELY careful.  forking child tasks from within a loop can easily put you in an infinite fork() loop.  It's only a small error from intended to whoops....


Kent
0
 
GPicassoAuthor Commented:
Ok as noted yes I made the obvious typo initializing that value to 5... I have no idea what I was thinking, and as mentioned my program forkbombed my machine.

What I want to happen is for the process to loop 20 times and produce 5 children I had a bunch of nonsense in my code that I wasn't seeing as I was pretty flustered from other things I was working on....  This still does not work

Also @ kdo
It's my understanding that every process gets a normal PID but that forked children are given PID of 0
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{

	int childrens = 0;
	int count = 0;
	pid_t my_pid;
	for (count = 0; count < 20; count++)
	{
		my_pid = getpid();
		if (getpid != 0)
		{
			printf("I am the parent\n");
			if (childrens < 5){fork();}
		}
		else
		{
			childrens++;
			printf("I am the child\n");
		}
	}

}

Open in new window

0
 
ozoCommented:
> It's my understanding that every process gets a normal PID but that forked children are given PID of 0
That is an incorrect understanding
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi G,

fork returns the child's PID to the parent process, and zero to the child process.  

Any process can always call getpid() to determine it's own process ID, but the easiest way for the parent to determine the child's PID is for fork to return the PID to the parent.

Think about what  happens when you spawn (fork) a new task.  The parent and child are EXACTLY alike, except for the value returned by fork.  Both tasks resume executing at the same place, the line after the call to fork.  All of the variables that were owned by the parent are copied to the child.  All of the loops and controls are also assumed by both the parent and child.

As I noted above, you probably need to call fork() in the loop and not worry about getpid().

As you note, the code that you've posted is a fork bomb.  Note that just changing the code to call fork() without calling getpid() may also produce a forkbomb.

When count == 0, the program spawns a child process.  Both of these processes continue through the loop.  When count == 1, there are now two tasks running.  One the original parent, one the first child.  Each will call fork() to spawn a child task.  The next time through the loop there are 4 tasks running, each of which will fork a child task, etc....

You may have intended to start 20 child tasks, but in fact the program is attempting to start 2^20 - 1 tasks.  1,048,575 tasks.  

Starting child tasks in a loop has many of the same pitfalls as does recursive programming.  Until you're used to it, things happen that you don't expect.  And they happen very quickly.



Good Luck,
Kent
0
 
for_yanCommented:
don't knwo if this helps but tghis is waht I got with this code: (see output):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{

	int childrens = 2;
	int count = 0;
	pid_t my_pid;

	for (count = 0; count < 2; count++)
	{
		my_pid = fork();
		if (my_pid != 0)
		{
			printf("I am the parent, pid: %d\n", getpid());

		}
		else
		{
			printf("I am the child, pid: %d\n",getpid());

		}
	}

}

Open in new window


Output:
host% a.out
I am the parent, pid: 25589
I am the child, pid: 25590
I am the parent, pid: 25589
I am the child, pid: 25591
I am the parent, pid: 25590
I am the child, pid: 25592

Open in new window

0
 
for_yanCommented:
slight modification

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{

	int childrens = 2;
	int count = 0;
	int countChild = 0;
	int countParent = 0;
	pid_t my_pid;

	for (count = 0; count < 2; count++)
	{
		my_pid = fork();
		if (my_pid != 0)
		{
			countParent++;
			printf("I am the parent, countParent: %d, pid: %d\n",countParent, getpid());


		}
		else
		{
			countChild++;
			printf("I am the child,countChild: %d, pid: %d\n",countChild, getpid());

		}
	}

}

Open in new window



Output:

host% a.out
I am the parent, countParent: 1, pid: 25677
I am the child,countChild: 1, pid: 25678
I am the parent, countParent: 2, pid: 25677
I am the child,countChild: 1, pid: 25679
I am the parent, countParent: 1, pid: 25678
I am the child,countChild: 2, pid: 25680

Open in new window

0
 
for_yanCommented:


I think there is a good explanation about the fork() call here:
http://www.csl.mtu.edu/cs4411/www/NOTES/process/fork/create.html
0
 
ssnkumarCommented:
@GPicasso:
You code will never enter the else block.
So, childrens will never get incremented and your code will get stuck in an infinite loop.
And also, every child process calls fork() and number of processes increases exponentially - this will quickly choke your system.
0
 
GPicassoAuthor Commented:
Thanks guys
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now