-
Notifications
You must be signed in to change notification settings - Fork 0
/
fork_exec.c
34 lines (34 loc) · 1.1 KB
/
fork_exec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
/* spawn a child process running a new process a new program. PROGRAM is the name of the program to run ; the path will be searched for this program. ARG_LIST is NULL-terminated list of character string to be passed as the program's argument list. Returns the process ID of the spawn process */
int spawn (char* program, char** arg_list)
{
pid_t child_pid;
/*Duplicate this process*/
child_pid=fork();
if (child_pid != 0)
/* This is the parent process. */
return child_pid;
else{
/* Now execute PROGRAM, searching for it in the path. */
execvp (program, arg_list);
/* The execvp function returns only if an error occurs. */
fprintf(stderr,"an error occured in execvp\n");
abort ();
}
}
int main()
{
/* The argument list to pass to the "ls" command. */
char* arg_list[]={
"ls", /* argv[0],the name of the program. */
"-l",
"/",
NULL /* The argument list must end with a NULL. */
};
/* spawn a child process running the "ls" command . Ignore the returned child process ID. */
spawn ("ls",arg_list);
printf("done with main program\n");
return 0;
}