New Process

This is a discussion on New Process within the C++ Programming forums, part of the General Programming Boards category; Code: for (int i = 0 ; i <5;i++) execl ("/bin/ls", "ls") For some reason execl only runs once, why ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    New Process

    Code:
    for (int i = 0 ; i <5;i++)
     execl ("/bin/ls", "ls")
    For some reason execl only runs once, why is that ?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Because you're not creating a new process. You're trashing the current one to run the executable you're asking it to run.

    Hint: fork().

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Also, it should be
    execl( "/bin/ls", "ls", (char*)NULL );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    Got it, thanks.
    Another question:
    How can I calculate the time each process took to complete? I am o linux and the clock() function does not work.

    I basicly have something like this:
    Code:
    ...
    for (int i = 0 ; i <5;i++) {
    pid = fork ();
    if (pid == 0)
         execl( "/bin/ls -R", "ls", (char*)NULL );
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you mean "clock doesn't work". What do you get back from clock()?

    It may not work if "ls" completes very quickly, because the time taken is shorter than one clock-tick, but otherwise I'm 100% sure that clock works.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > execl( "/bin/ls -R", "ls", (char*)NULL );
    No no no - execl doesn't parse arguments.
    Try
    execl( "/bin/ls", "ls", "-R", (char*)NULL );

    It's
    execl( path, argv0, argv1, argvn, (char*)NULL );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    37
    Quote Originally Posted by Salem View Post
    > execl( "/bin/ls -R", "ls", (char*)NULL );
    No no no - execl doesn't parse arguments.
    Try
    execl( "/bin/ls", "ls", "-R", (char*)NULL );

    It's
    execl( path, argv0, argv1, argvn, (char*)NULL );
    Uh, whats execl()? What does it do? Just asking for experience... o.O

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    For experience learn to google.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    man execl.

    Essentially it loads another binary into the current process and executes it.

    A similar function exists in MS environment, but it automatically starts a new process, rather than loading the binary into the same process.

    MSDN _execl

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    That's really kind of stupid to name it after the Unix function and then make it behave differently. Why didn't they name it forkexecl() or something instead?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MacGyver View Post
    That's really kind of stupid to name it after the Unix function and then make it behave differently. Why didn't they name it forkexecl() or something instead?
    I agree - but it does have an underscore in the beginning, rather than the deprecated VC6 version that doesn't!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    spawnl is the POSIX equivalent.
    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. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 01:14 PM
  3. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  4. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  5. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM

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