fork()ing problem

Giorgos Keramidas charon at labs.gr
Sat Dec 8 02:41:02 EET 2001


On 2001-12-08 01:12:59, Andreou Al. wrote:
> Exw to akoloy8o programma:
> ---
> ee4299 at nemesis:~/prod > cat fork2.c
> #include <sys/types.h>
> #include <unistd.h>
> #include <stdlib.h>
>
> int main(int argc, char **argv) {
>         unsigned long times;
>         char *s;
>
>         times=atol(argv[1]);
>         printf("Hi, my name is %s (%d) and I am a parent what will spawn %d
> bastards.\n", argv[0], getpid(), atoi(argv[1]));
>         while (times--) {
>                 if (!fork()) {
>                         /* I am child. I print my pid and die dramatically.
> */
>                         printf("%d => %d\n", getppid(), getpid());
>                         exit(1);
>                 }
>         }
>
>         return 0;
> }
> ee4299 at nemesis:~/prod >
> ---
>
> to opoio to egrapsa gia na dw ti ginetai otan yparxei ta pids ftasoun sto
> 65535, kai gia na dw ligo tis fork(), getpid(), getppid().
>
> Exw twra to e3hs problhma: otan to trexw me mia parametro (estw 1000), pote
> de ftanw sto 1000o pid, alla panta stamataw kapoy pio prin (p.x.: 11550 =>
> 12333), otan xrhsimopoiw megalous ari8mous (1000 kai panw peripoy) gia th
> metablhth times.
>
> Exete kapoia idea toy ti mporei na ginetai? Yparxei kapoio limit sta spawned
> childs mias process? (de nomizw...)

Uparxoun kai user limits jereis.  To parent process sou den kanei pote
wait() gia kapoio apo ta paidia tou, ki etsi oi plhrofories pou krata
to kernel gia to parent sou, deixnoun oti ta paidia einai
'uncollected' akoma.  Trexontas to, me limit se processes = 512, den
mporesa na kseperaso ta 500 processes.

H lush einai na kaneis fork(), meta wait() na teleiosei to child
process, kai META na kaneis to epomeno fork().  To programma pou
tha breis parakato to kanei auto.  Try it, kai pes mou apotelesmata.
Ego mporesa na jeperaso kata polu ta user-limits twn 512 tautoxronwn
processes, opws fainetai parakato:

	% ./a.out 6000
	ppid=17813, pid=22706
	ppid=17813, pid=22707
	ppid=17813, pid=22708
	^C
	% _

Cheers,

-giorgos

[-- source begins -----------------------------------------------------------]
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main (int argc, char **argv)
{
	int count, k;

	if (argc != 2) {
		fprintf(stderr, "usage: fork2 number\n");
		exit(1);
	}

	errno = 0;
	count = (int) strtol(argv[1], NULL, 0);
	if (errno == ERANGE) {
		fprintf(stderr, "fork2: number out of range\n");
		exit(2);
	}

	/*
	 * Repeat `count' times:
	 * - fork a child
	 * - child prints pid/ppid
	 * - parent collects specific child status with waitpid()
	 * - loop
	 */
	for (k = 0; k < count; k++) {
		pid_t cp;

		if ((cp = fork()) < 0) {
			fprintf(stderr, "fork2: cannot fork child %d\n", k);
			exit(3);
		} else if (cp == 0) {
			printf("ppid=%d, pid=%d\n", getppid(), getpid());
			exit(0);
		} else {
			pid_t wp;
			int status;

			if ((wp = waitpid(cp, &status, 0)) < 0) {
				fprintf(stderr, "fork2: failed in "
				    "waitpid(%d, %p, 0)\n",
				    cp, (void *)&status);
				exit(1);
			}
		}
	}

	return 0;
}
[-- source ends -------------------------------------------------------------]



More information about the Linux-greek-users mailing list