Hello!
So, I have a problem with the following code... I'd like to know, why the variables pid and ppid are not initialized? Inside the if scope, if I print them out, they are initialized to getpid() and getppid() value, but when I want to print them out in else scope of if block, they are zero. So now I am wondering, why the value is not being keept?

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

void vzporedno()
{
	int i, j;
	
	for ( i = 0; i < 3; i++ )
	{
		int ret = fork();
		if ( ret == 0 )
		{	
			printf("PID: %d, PPID: %d\n", (int)getpid(), (int)getppid());
			exit(0);
		}
	}
	
	for ( j = 0; j < 3; j++ )
		wait(0);	
}

void zaporedno()
{
	int i;
	
	for ( i = 0; i < 1; i++ )
	{
		int ret = fork();
		if ( ret == 0 )
		{
			printf("PID: %d, PPID: %d\n", (int)getpid(), (int)getppid());
			exit(0);
		}
		else 
			wait(0);
	}
}

int main(int argc, char** argv)
{
int i, j, k, check = 0;
int pid=0, ppid=0;

	for ( i = 0; i < 3; i++ )
	{
		int ret = fork();
		if ( ret == 0 )
		{
			if ( check < 2 )
			{
				zaporedno();
				pid = (int)getpid(); int ppid = (int)getppid();
				exit(0);
			}
			else
			{
				vzporedno();
				printf("PID: %d, PPID:%d\n", pid, ppid);
				printf("PID: %d, PPID:%d\n", (int)getpid(), (int)getppid());
				exit(0);
			}
		}
		else
			wait(0);
		check++;
	}
	
	for ( k = 0; k < 2; k++ ){
		wait(0);
	}

	printf("PID: %d, PPID:%d\n", (int)getpid(), (int)getppid());

	return 0;
}

The obvious thing wrong with your program is this line: pid = (int)getpid(); int ppid = (int)getppid(); You are defining another variable called ppid, shadowing the other one. You want to remove "int" before ppid.

Noticed that, fixed it, problem still persist.
The purpose of that exercise is to create a tree of process like that: [IMG]https://vaje.uni-mb.si/vaje/dodatno//UOS/2007/7-Procesi/ProcessTree.jpg[/IMG]
and the print of program should be:

PID=5397, PPID=5128
PID=5128, PPID=4972
PID=4977, PPID=4517
PID=5219, PPID=4517
PID=5165, PPID=4517
PID=4972, PPID=4316
PID=4517, PPID=4316
PID=4316, PPID=4284

I think that the problem is in the exit function, because when it is being called, everything is lost. I probally didn't design the program in proper way, so I'd like to know, what I have to change to so that it would work the right way. Thanks for any tip!

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

void vzporedno()
{
	int i, j;
	
	for ( i = 0; i < 3; i++ )
	{
		int ret = fork();
		if ( ret == 0 )
		{	
			printf("PID: %d, PPID: %d\n", (int)getpid(), (int)getppid());
			exit(0);
		}
	}
	
	for ( j = 0; j < 3; j++ )
		wait(0);	
}

void zaporedno()
{

		int ret = fork();
		if ( ret == 0 )
		{
			printf("PID: %d, PPID: %d\n", (int)getpid(), (int)getppid());
			exit(0);
		}
		else 
			wait(0);
}

int main(int argc, char** argv)
{
int i, j, k, check = 0;
int pid=0, ppid=0;

	for ( i = 0; i < 2; i++ )
	{
		int ret = fork();
		if ( ret == 0 )
		{
			if ( check < 1 )
			{
				for ( j = 0; j < 2; j++ )
                                        zaporedno();
				pid = (int)getpid();  ppid = (int)getppid();
                                check++;
				exit(0);
			}
			else
			{
				vzporedno();
				printf("PID: %d, PPID:%d\n", pid, ppid);
				printf("PID: %d, PPID:%d\n", (int)getpid(), (int)getppid());
				exit(0);
			}
	}
	
	for ( k = 0; k < 2; k++ ){
		wait(0);
	}

	printf("PID: %d, PPID:%d\n", (int)getpid(), (int)getppid());

	return 0;
}

Instead of setting pid and ppid, don't you just want to print them? You cannot return them to the parent (well, you can, but it does not seem to be necessary in this assignment).

And your code is missing an end brace.

I must print them out in that order:

grandson(second son): PID=5397, PPID=5128
grandson(second son): PID=5128, PPID=4972
grandson(first son):  PID=4977, PPID=4517
grandson(first son):  PID=5219, PPID=4517
grandson(first son):  PID=5165, PPID=4517
second son:           PID=4972, PPID=4316
first son:            PID=4517, PPID=4316
father:               PID=4316, PPID=4284
This article has been dead for over six months. Start a new discussion instead.