Using execl()

This is a discussion on Using execl() within the C++ Programming forums, part of the General Programming Boards category; In a previous discussion I was informed about the execl() function that can be used in place of system(). So ...

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    Using execl()

    In a previous discussion I was informed about the execl() function that can be used in place of system(). So what are the differences between the following:

    Code:
    #include <unistd.h>
    
    int main(){
    
        execl("C:\\WINDOWS\\NOTEPAD.EXE", 0);
        return 0;
    }
    and

    Code:
    #include <iostream>
    
    int main(){
    
        system("C:\\WINDOWS\\NOTEPAD.EXE");
        return 0;
    }
    What are the advantages of using execl() if any? Is it any safer to use than the dreaded system() call function?

    Thoughts?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    system() executes a new process and only gives control back to your application process when it is done.

    execl() replaces the current process image. It can be used in one of two ways, I seem to believe; With and without a fork. With a fork, the execl replaces the child process allowing your main process (the parent) to continue untouched. Without fork execl() effectively replaces your parent process and you can't go back.

    It all depends on what you want. But something immediately strikes your eye; execl() allows you to start a new process while keeping your parent process active and running and can be used to feed information to the parent process.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No fork() in Windows. And since you can't replace a process image, the Windows implementation of execl() actually starts the new process and then kills the current one.

    By the way, <iostream> is wrong. For system(), you need <cstdlib>. And it's in std::.

    execl() is no safer than system(). If you launch an external program, you run into the possibility that it was replaced.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    not correct usage

    Not correct usage because the compiler has no way of knowing that the last argument is actually a null pointer. So need to cast.
    execl("C:\\WINDOWS\\NOTEPAD.EXE",(const char *) 0);
    Last edited by aambilw1; 10-20-2010 at 04:18 PM.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    As mentioned, system() and execl() are fundamentally different. As noted, system() runs another process and returns either the exit code of the process or -1 on failure of the system call, while execl() runs the process replacing our process image, meaning we never actually return from execl()!

    Quoting the Linux programmer's manual: "If any of the exec() functions returns, an error will have occurred. The return value is -1, and errno will be set to indicate the error."

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Please do not resurrect old threads.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execl and command line arguments
    By imtiaz3 in forum C Programming
    Replies: 13
    Last Post: 09-30-2008, 12:18 PM
  2. How to read the execl() output?
    By estratos in forum C Programming
    Replies: 3
    Last Post: 06-07-2006, 12:39 AM
  3. Can we use select() to capture output from execl()?
    By Nessarose in forum Networking/Device Communication
    Replies: 5
    Last Post: 07-05-2005, 12:53 AM
  4. execl not executing?
    By talz13 in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2004, 08:08 PM
  5. execl failing
    By carrythe0 in forum C Programming
    Replies: 1
    Last Post: 10-01-2001, 12:25 PM

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