0

Hello Guys,

im new to linux ..

i read something about -linux allowing users to open processes from within another running processes and also allows a given processes to specify which programs will run in the new processes.

how does it do that ? can you give me a concrete example...any code would do.

thanks.

2
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by John A
0

The way you usually open new processes is with the system() call. But note that this call must be made within the source code, so it's predefined before the program binary is ever made. Or you can make shell scripts that open new processes.

0

i googled system () they said that sometimes it causes problems. and that we can use fork ( ) and exec () instead... so i found this code

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
void
main(int argc, char *argv[])
{
int status;
char *prog = argv[0];
char *new_cmd = "/bin/echo";
char *cmd_args[] = {"echo", "hello", "world", NULL};
char *cmd_env[] = {NULL};
status = execve(new_cmd, cmd_args, cmd_env);
/*
* execve does not return unless there is an error.
*/
fprintf(stderr, "%s: ", prog);
perror("execve");
exit(1);
}
A sample run of this program gives the following output:
> ./sys2
hello world
>

can you please what it does and how it does it?

0

system() merely opens a new shell, and runs the command you send it. The function returns once the program running in the shell exits. fork() is a command that is used to create another instance of the program that's already running as a child process. You can't use it to run external programs. Finally, the exec() family of functions does something similar to system(), except that it doesn't open a new shell within the current shell; all it does is tell the operating system to execute a file (which is typically a shell script or an application program).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.