can't send values to variable in child proces

November 2, 2011 at 18:43:15
Specs: Windows 7
Hi, I am trying to send values to my variables in my main function in a child process....but it doesnt record any values....I need help....

and here's my code....

int planes[31];//stores at most 30 planes
int countp;//counter for planes

int produce;//produce a process
int count3=0; //3-sec timer;
int i,j; //for loop counter

produce=fork();//get both parent and child processes
if (produce == 0)//child process if produce = 0
{//code only executed for child process
//get pid in this process
countp = countp + 1;
printf("Launching plane %d!\n", planes[countp-1]);
fuel = 100;
while (fuel>0)
if ((count3 % 3) == 0)
// report status
fprintf(fpt1,"Bombadeer %d to base, %d fuel left\n", planes[countp-1], fuel);
kill(planes[countp-1], SIGUSR2);
}//end of child process

//trying to display values from planes[] again
printf("Plane %d\n",planes[j]);
//end of code

not only does countp doesn't perform increment, but also the pid which I had inserted into planes[] do not receive the pid from getpid(); I urgently need help as I have so little understanding, even though I made all kinds of research and read my textbook :(

See More: cant send values to variable in child proces

Report •

November 3, 2011 at 00:19:10
When you fork() you create two separate processes. They cannot see each others' variables. You need to use some form of interprocess communication to tranfer data from one process to the other, such as shared memory or pipes. Google these terms for more information; it's too big a subject to cover in a post.

Report •

November 3, 2011 at 08:45:57
oh that helps, thanks for helping me!

I have one more question, if I am managing many child processes at the same time, assuming that I already have the pid of the child processes stored in my main process, how do I send a signal to that specific child process? can you show me an example?

Report •
Related Solutions

Ask Question