Thread: Starting new program with function

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    23

    Starting new program with function

    I would like to ask which function to use to start program with another program. I would like to start a program and when program ends it executes the second program.execvp, execve?
    path of the second program is /home/matej/menu/a.out
    a.out file i a compiled program so that's whay I would start a.out(only files in folder are .c file of the folder and a.out)
    What to write in the mentioned functions, what arguments. I only know defining path and then it ends, could anyone help me the rest?
    There is a menu that starts a program and when it ends it should go back to menu. I don't want to use fork function.
    Thanks

    Matej

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I'm pretty certain there's something in the cboard FAQ - but it may also be about multithreading (just omit the call to fork()).
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If you don't want to use fork() you'll need to use system(). The reason is because the exec() family of functions replace the current process image with the new one. So you'd be able to call the second program, but it wouldn't ever get back to the first program. The whole thing would just end when the second program finished. The reason you see fork() before exec() calls is because exec() is called in the child process. That way only the child process image is replaced when you call exec(). Then the parent can just wait for the child process to finish an dthen continue on its way. system() uses fork() internally so you don't have to worry about it.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    but system() is OS specific...

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No it isn't.
    [edit]
    7.20.4.6 The system function
    Synopsis
    1
    #include <stdlib.h>
    int system(const char *string);
    Description
    2 If string is a null pointer, the system function determines whether the host
    environment has a command processor. If string is not a null pointer, the system
    function passes the string pointed to by string to that command processor to be
    executed in a manner which the implementation shall document; this might then cause the
    program calling system to behave in a non-conforming manner or to terminate.
    Returns
    3 If the argument is a null pointer, the system function returns nonzero only if a
    command processor is available. If the argument is not a null pointer, and the system
    function does return, it returns an implementation-defined value.
    To clarify, the function call itself is not OS specific. It's a standard function.
    [/edit]


    Quzah.
    Last edited by quzah; 12-07-2005 at 11:18 AM.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    oh, i thought it is, because in the FAQ of system("pause") is written "A poor-mans method of using system(), OS specific."
    Or why else should'nt you use system("pause")? I know it's not that good to use the system-call if the user can define the command string himself...

    mfg

    [edit]
    k, so it depends on the command processor of the running OS, what it makes with it?
    Last edited by mabuhay; 12-07-2005 at 11:23 AM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The function itself is a standard C function.
    The arguments you pass to it are system specific.

    If your operating system has a command called 'pause', then doing 'system( "pause" )' will likely invoke it. If it doesn't, then doing 'system( "pause" )' won't. See?

    The reason using system is frowned upon is something like this:
    Code:
    char buf[BUFSIZ] = {0};
    
    puts("Enter something for me to pass off to the OS: ");
    fgets( buf, BUFSIZ, stdin );
    
    system( buf );
    Not really a good idea.


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

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Because pause is OS specific, not system(). If you don't have a program in your path called "pause" then the system() call will fail. You're just looking at the wrong part of system("pause") as being OS specific.

    EDIT: Beaten again by Quzah.
    Last edited by itsme86; 12-07-2005 at 11:44 AM.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    system("pause"); is OS specific because not every operating system has a pause command, and not every operating system that has a pause command does the same thing.

    When you want to execute another program from within a C program, system() is the only portable way to do it.

    Edit: too slow.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    mkee, now i get it (i should start to work also with an other OS)
    thx

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    23
    HI
    I managed to link another program with execve function(and I tried with execv and it works also),but there is a problem. It is very inportant what kind of program I'm linking. If I want to link the program that includes menu it writes error opening terminal: unknown.
    When I link program with menu I just change the path to the menu program, but obviously that's not enough. When compiling the program with menu I add -lncurses(it has something to do with terminal).
    Please give me some advice how to start a program(linked from other program) with menu, all other programs work.
    For the system function, do I have to put into brackets the path with compiled a.out file of the program. Do I have to add something else or just system() with entered program path inside. It work If I put ls inside, but didn't start my menu program, there was error segmentation fault.
    So please advices would be helpful.
    Have a nice Day

    Matt

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post what you did, rather than describing it to us.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Like this?
    Code:
    system("\\the\\path\\to\\the\\program\\the_program_name");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Dec 2005
    Posts
    23
    HI
    The system function works only if your previous location was address of the menu, so from the second program goes back to te same location, but there is problem with scripts. When I want to start a script there starts somekind of loop and the display is just blinking. It's a bit annoying posting code, because I would have to write it again, because I'm programming on other computer and on this one I haven't got administrator rights to install something similar for programming.
    It's also annoying that if some code for exit is behind system line, it terminates the system function and exits like there was no system function.
    system("pointer of location with /, not \\");
    Have a nice Day

    Matt

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    system("pointer of location with /, not \\");
    You can use either, except for some DOS commands; this will work:
    Code:
    system("cd \\windows");
    but this won't:
    Code:
    system("cd /windows");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM