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