Why isn't the execlp() function doing anything?

This is a discussion on Why isn't the execlp() function doing anything? within the C Programming forums, part of the General Programming Boards category; I'm having issues finding this information without the use of a forum, so here goes: I'm making a very basic ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    100

    Question Why isn't the execlp() function doing anything?

    I'm having issues finding this information without the use of a forum, so here goes:

    I'm making a very basic program on Linux that spawns four child processes. Each child process runs a separate program in the same directory, in which it just keeps printing its ID out to the screen over and over again. After the parent process spawns these, it just does the same thing until about 20 seconds have passed since the program started, at which point it kills all processes and terminates.

    The code for the two programs is this:

    Parent Process Program:
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <time.h>
    #include <signal.h>
    
    #ifndef FALSE
    #define FALSE (0)
    #endif
    #ifndef TRUE
    #define TRUE (!FALSE)
    #endif
    
    /********************************************************************************
    * Method:  void newProcesses()                                                  *
    * Purpose: to make four child processes, each of which will execute a certain   *
    *               program in the same directory                                   *
    * Returns: N/A                                                                  *
    *                                                                               *
    * Approach: This method first creates an array for four separate process ID's.  *
    *               Then it goes through a four-iteration for loop, with each iter- *
    *               ation involving the creation of a child process.  Each time a   *
    *               process is supposedly created, the loop checks to see if the    *
    *               creation was successful.  If not an error message is printed.   *
    *               If so, and if it is the child process that is currently execut- *
    *               ing, then the child process is made to execute a program in the *
    *               same path as this program, the former being called "Subprogram".*
    *               Otherwise it is the parent process that is currently executing, *
    *               so the for loop will just use the parent to print a message of  *
    *               successful creation, including the child process's ID.          *
    ********************************************************************************/
    void newProcesses()
    {
            /* method for making new processes */
            int id[4]; /* process ID array */
             
            int i; /* for loop iterator */
            for (i = 0; i < 4; i++)
            {
                    id[i] = fork();
    
                    if (id[i] == -1) /* an error occurred */
                    {
                            printf("Creation of Child Process failed.\n");
                    }
                    else if (id[i] == 0) /* this is the child process currently acting */
                    {
                            execlp("/jroberts/Subprogram", "/jroberts/Subprogram");  
                    }
                    else /* this is the parent process currently acting */
                    {
                            printf("Creation of Child Process #%d succeeded!\n",id[i]);
                    }
            }
    }
            
    /********************************************************************************
    * Method:  int main()                                                           *
    * Purpose: to serve as the driving method for the entire program                *
    * Returns: 0 to the operating system to signal successful termination           *
    *                                                                               *
    * Approach: First this method finds the current time in seconds and stores it   *
    *               in two different variables, initial and current.  Then it gets  *
    *               the current (parent) process's ID and prints a message with     *
    *               that ID out to the screen.  Then it calls void newProcesses()   *
    *               to make child processes and handle each of them accordingly.    *
    *               Finally it enters a loop, each iteration of which checks the    *
    *               clock and updates current.  After it has been found that 20     *
    *               seconds have gone by, the loop is ended, all the processes are  *
    *               killed, and the method is basically finished.                   *  
    ********************************************************************************/
     int main ()                                                                                                                                       
    {
            /* setting up the program's "timer" for the program to know when to kill processes */
            time_t initial; // starting time of the program in seconds
            time_t current; // current time in seconds
    
            time(&initial);    /* initializing both initial and current to the starting time
            current = initial; */
    
            /* the master process will give out its ID, subsequently making new processes */
            int ownID = getpid();
            printf("Parent Process created with ID# %d.\n", ownID);
    
            /* now the child processes will be made. */
            newProcesses();
    
            /* at this point there should be child processes.  Now the parent process will wait until the
            progrm has lasted for about 20 seconds, subsequently killing all processes. */
            time(&current);
            while(current - initial < 20) // the program has executed for less than 20 seconds
            {
                    printf("Parent Process with #%d currently executing.\n",ownID);
                    time(&current);
            }
            kill(0,SIGKILL);
            
            return 0;
    }
    Child Process Program:
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    #ifndef FALSE
    #define FALSE (0)
    #endif
    #ifndef TRUE
    #define TRUE (!FALSE)
    #endif
    
    int main ()
    {
            int id = getpid();
    
            while (TRUE)
            {
                    printf("Process #%d\n",id);
            }
    
            return 0;
    }
    Yet when I execute the main program, I get this kind of output:
    Code:
    Creation of Child Process #32039 succeeded!
    Creation of Child Process #32038 succeeded!
    Creation of Child Process #32041 succeeded!
    Creation of Child Process #32046 succeeded!
    Creation of Child Process #32043 succeeded!
    Creation of Child Process #32042 succeeded!
    Creation of Child Process #32040 succeeded!
    Creation of Child Process #32047 succeeded!
    Creation of Child Process #32045 succeeded!
    Creation of Child Process #32044 succeeded!
    Creation of Child Process #32051 succeeded!
    Creation of Child Process #32048 succeeded!
    Creation of Child Process #32049 succeeded!
    Creation of Child Process #32050 succeeded!
    Creation of Child Process #32052 succeeded!
    This is followed by the Parent Process print its ID innumerable times, and finally a Kill statement.

    It shouldn't be making more than four child processes, and each of them should be taking turns printing their ID's, not just the Parent Process.

    What's going wrong here?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, for one thing, execlp expects you to have a null pointer as the last argument. You don't have that.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    100
    Alright, I've added NULL at the end of the argument list, but that didn't change anything. Also I accidentally left out of the output section that before mentioning any of the child processes, the parent process does give its ID out once, as per its instructions before newProcesses() is called

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are any (or all) of your execlp calls failing? It's possible "/jroberts/Subprogram" is a valid path, but it doesn't fill me with confidence.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It works fine if you actually give it a valid path and file. I compiled them both as jsrig1 and jsrig2, and put jsrig2 in as the name for execlp, and it works fine.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    100
    I've come to realize the reason that there are an excessive number of child processes is that, even though the child processes are falling into the if statement concerning whether their "id" is 0, they fall right back out of it and continue to iterate through the for loop, despite the execlp function.

    Shortly after writing the last paragraph, I played with the code a little more and finally found out what was going wrong with all the other paths or whatever I had tried. I finally got the full path in the correct format (the format was also an issue).

    Everything's working correctly now. Thank y'all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execlp function / digital mars compiler
    By claydan7 in forum C++ Programming
    Replies: 4
    Last Post: 08-24-2009, 08:51 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21