Link to home
Start Free TrialLog in
Avatar of GPicasso
GPicasso

asked on

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

Avatar of for_yan
for_yan
Flag of United States of America image

but it will run it if childrens<5 and childrens = 5 , so 5 is not less than 5, so it does not go there
make it
if (childrens <6 ){fork();}
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
Avatar of Kent Olsen
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
Avatar of GPicasso
GPicasso

ASKER

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

> 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
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial


I think there is a good explanation about the fork() call here:
http://www.csl.mtu.edu/cs4411/www/NOTES/process/fork/create.html
@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.
Thanks guys