Linux having problems with execlp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manontheedge
    New Member
    • Oct 2006
    • 175

    Linux having problems with execlp

    I'm trying to write a program in C on Linux where the child processes get their process images replaced. I have in my code, a few child processes that I forked from a single parent process. I'm then attempting to replace their ( the child ) process images with separate executables. I have these executables written and placed in the directory.

    My problem is when I'm using execlp. When it reaches execlp in my code, it does replace the process image and executes the new image ( for the first execlp it reaches ), BUT once it reaches the first execlp and is executing that code, it never gets past that point in the code. These processes should be running concurrently. Is there some trick to this. I've never used Linux before, and I'm pretty confused. Any help would be appreciated.

    Code:
    if ( j == 1 )
    {
    	execlp("./c2", "c2", (char *)0);
    }
    else if ( j == 2 )
    {
    	execlp("./c3", "c3", (char *)0);
    }
    here's the execlp part of my code. The j is a for loop, ( it goes well past 2 ). The "c2" and "c3" are names of the .exe's that replace these child process images. Once the j==1 is reached, it never gets passed it ... I want it to continue past it ... continue having that process running with it's new image, then the next process to start running alongside the previous one.
  • sydneytroz
    New Member
    • May 2007
    • 7

    #2
    Where do you call fork()? It looks as though the process is replaced during iteration i=1 of your for loop, and that would be why nothing else happens. You might want to fork inside the if blocks.

    Code:
    if (j == 1) {
    	if (fork()) exec();
    } else if (j == 2) {
    	if (fork()) exec();
    } ...

    Comment

    • manontheedge
      New Member
      • Oct 2006
      • 175

      #3
      sidneytroz,

      thank you very much for that, it fixed my problem perfectly. But, now with that working, I have a new question:

      in my code, I'm creating these child processes, so I want to save each child's process ID in a variable, so that the parent process can later kill them, on an individual basis. If I create a tempory integer variable for each process and put the individual process ID in each of their own allotted variables, the value is erased by the time the parent process gets to look at the variable. Why is this happening? I would think the variable would hold it's value.

      Comment

      Working...