Tried this on my UNIX box.. Why aren't the array contents being stored/displayed ??

#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sys/signal.h>

using namespace std;

int main(int argc, char *argv[])
{
    int pid;
	pid_t  pid_chk;
	int pid_array[2];
	int count[25], length, i, j;


	for (i=0; i<3; i++) {
		pid_array[i] = 0;
	}	
	
	for (i=0; i<3; i++) {

		for (j=0; j<=25; j++)
			count[j]=0;
		//fork();
		if ((pid=fork())==0) {
			/* child code */
			// Store the PID of newly created child in an array.
			pid_chk = getpid();
			cout << "My process id is : " << pid_chk << endl; 
			cout << "Value of i is : " << i << endl; 
			pid_array[i] = pid_chk;
			cout << "Value of pid_array is : " << pid_array[i] << endl; 
			//break;
			kill(pid_chk, SIGKILL);
		}
		else {
			// Parent's code
			// Do nothing
		}
	}
		
	for (i=0; i<3; i++) {
		cout << pid_array[i] << endl;
	}	
    return 0;
}

Output :

{linux0:~/proj} trial_suggestion
My process id is : 10106
Value of i is : 0
Value of pid_array is : 10106
My process id is : 10107
Value of i is : 1
Value of pid_array is : 10107
My process id is : 10108
Value of i is : 2
Value of pid_array is : 10108
0
0
0

Recommended Answers

All 7 Replies

I can't explain it all, and don't have a *nix box handy, but consider these two problems:

int pid_array[2];
	int count[25], length, i, j;


	for (i=0; i<3; i++) {
		pid_array[i] = 0;
	}

pid_array is size 2, but you access it out to three elements. Oops.

for (j=0; j<=25; j++)
			count[j]=0;

count is sized to 25 elements, you're going out to the 26th element, which could be overwriting the first element of pid_array.

commented: Helpful corrections... +1

pid_array is size 2, but you access it out to three elements. Oops.

Point noted. I was of the opinion that even while initializing it, we count from 0. I've corrected it out anyways, still stuck with the same result.. Not really sure what's wrong now..

EDIT:
Corrected both the problems that you pointed out. I'm pretty much confident that they are least of my worries as of now.

I'm guessing the Array is not in scope outside the loop, but it really should be. Why's this happening?

I am not 100% certain where do you see the problem. If you are concerned with the fact that the lines 42-44 print out zeroes, that is what is supposed to happen. Each process has its own copy of pid_chk. The parent never modifies it, and the children commit suicide before reaching this loop.

I am not 100% certain where do you see the problem. If you are concerned with the fact that the lines 42-44 print out zeroes, that is what is supposed to happen. Each process has its own copy of pid_chk. The parent never modifies it, and the children commit suicide before reaching this loop.

'Commit suicide' - Now that's putting it well! :)

I agree on all your points, but isn't my array a global? Shouldn't it have stored the pertinent values into itself (inside the loop)? What does it matter if the child is alive or dead at that point?

Shouldn't it have stored the pertinent values into itself (inside the loop)?

It is global all right, but it isnot global enough. It is global only within the process.
The parent initialize it to all zeroes. Each child gets its own copy initialized to zeroes. Each child puts some values to some slots - and it does not affect the copy owned by a parent.

What does it matter if the child is alive or dead at that point?

Try an experiment:
1. Comment out line 34 (a kill one) and
2. Modify line 43 to

cout << getpid() << ": " << pid_array[i] << endl;

and see what each process thinks about a state of pid_array.

commented: Great insight into the answer +1

It is global all right, but it isnot global enough. It is global only within the process.
The parent initialize it to all zeroes. Each child gets its own copy initialized to zeroes. Each child puts some values to some slots - and it does not affect the copy owned by a parent.

Try an experiment:
1. Comment out line 34 (a kill one) and
2. Modify line 43 to

cout << getpid() << ": " << pid_array[i] << endl;

and see what each process thinks about a state of pid_array.

That was an awesome explanation. I now see my folly ! :)

I'll post back my findings with improvisations later..

Point noted. I was of the opinion that even while initializing it, we count from 0. I've corrected it out anyways, still stuck with the same result.. Not really sure what's wrong now..
<snip>

Array initialization (and access) goes from index 0 to index size-1.
So, for your array of size 2, the correct loop is for ( i = 0; i < 2; i++ ) nezachem certainly hit the nail on the head regarding the actions of the child processes. Been a while since I did anything with forked processes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.