# Starting new program with function

This is a discussion on Starting new program with function within the C Programming forums, part of the General Programming Boards category; I would like to ask which function to use to start program with another program. I would like to start ...

1. ## 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. I'm pretty certain there's something in the cboard FAQ - but it may also be about multithreading (just omit the call to fork()).

3. 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.

4. but system() is OS specific...

5. No it isn't.

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.

6. 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

k, so it depends on the command processor of the running OS, what it makes with it?

7. 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.

8. 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.

9. 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. mkee, now i get it (i should start to work also with an other OS)
thx

11. 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).
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.
Have a nice Day

Matt

12. Post what you did, rather than describing it to us.

13. Like this?
Code:
system("\\the\\path\\to\\the\\program\\the_program_name");

14. 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. 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");