i meant i<5, sry, my bad
i meant i<5, sry, my bad
When I'm trying to figure out a loop problem, I normally use DEBUG statements to help me see where the problem is occuring, for example
Then, depending on the output...it normally points me in the right direction to look for the problem. What happens when you implement something like this?Code:printf("DEBUG_1\n"); // this is outside the for loop for(a=1; a<5;a++){ printf("DEBUG_2\n"); printf("%d", array[a]); printf("DEBUG_3\n"); } printf("DEBUG_4\n");
Your problem is not the loop. When you fork a program the child created starts execution right after the fork call. You are basically duplicating the parent. The reason your print statement executes twice is because it executes in the parent and in the child. In order to solve this put it in an if statement to dectect it pid is greater than 0. I believe this will solve your problem.
I think you will also need to put a wait command before this to assure that all child processes complete before executing the print loop.
Code:if(pid > 0) { for(a=1; a<5; ++a) { printf("%d", array[a]); } }
Last edited by jtmurphree; 02-18-2010 at 12:44 PM.