0% found this document useful (0 votes)
30 views11 pages

03 Exec

The document explains the exec() system call used in systems programming to load and execute new programs after forking a process. It details the execve function, its parameters, and how it replaces the current process's address space without creating a new process. Additionally, it discusses the concepts of parent and child processes, waiting for child processes, and the implications of orphaned and zombie processes.

Uploaded by

viraguber3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views11 pages

03 Exec

The document explains the exec() system call used in systems programming to load and execute new programs after forking a process. It details the execve function, its parameters, and how it replaces the current process's address space without creating a new process. Additionally, it discusses the concepts of parent and child processes, waiting for child processes, and the implications of orphaned and zombie processes.

Uploaded by

viraguber3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

IT628: Systems Programming

Load and execute programs, exec

1
exec(): Loading and Running
Programs
 After fork, the child process is an identical
duplicate of the parent process
 How do we start a new program, instead of

copying the parent?


 Use the exec() system call

2
int execve( char *filename, char *argv[ ],
char *envp );
filename: name of the executable file to run

argv: command line arguments

envp: environment variable settings (e.g. $PATH,

$HOME, etc.)
returns -1 if error, otherwise doesn’t return

int main() { /* exec1.c */


char *args[2];
args[0] = “/bin/echo”;
args[1] = NULL;
execv(“/bin/echo”, args);
return 0;
}

3
int main() { /* exec2.c */
char *args[2];
args[0] = “/bin/echo”;
args[1] = NULL;
printf(“About to exec from process %d\n”, getpid());
sleep(1);
execv(“./exec2”, args);
printf(“Done exec-ing ...\n”);
return 0;
}

4
 exec() does not create a new process!
 Replaces the address space and CPU state of the current
process
 Loads the address space from the executable file and starts it
from main()
 On success, exec does not return!

 UNIX shells use fork-then-exec to run


programs

5
execv(new_program, argv[ ])

Initial process

fork
fork() returns pid=0 and runs as a
Returns a cloned parent until execv is called
new PID

Original New
process new_Program
Copy of
Continues (replacement)
Parent

execv(new_program)

6
Exercise
Write a program that creates a child process,
the child executes /bin/ls, and then the parent
prints “done”. Make sure the word “done” is
printed after the output of ls.

7
int main() { /* exec3.c */
if (fork() == 0) { /* Child process */
char *args[2];
args[0] = “/bin/ls”; /* Not required!! */
args[1] = NULL; /* Indicate end of args array */
execv(“/bin/ls”, args);
exit(0); /* in case exec fails! */
}
wait(NULL);
printf(“Done\n”);
return 0;
}

8
Waiting for a Child Process
 If a process (the parent) calls fork() to create a
process (the child), the parent doesn't automatically
wait for the child to finish. The parent must call
wait.
 So if wait is not called, which process finishes first?

 Either one could finish first.

9
Zombies
 If the parent finishes first, the child becomes an
orphan and is adopted by a system process called
init whose pid is 1.
 If the child finishes first, it becomes a zombie.

 The child is mostly dead, but the parent might call

waitpid. So it's termination information must be


retained until the parent either terminates or calls
waitpid.

10
Summary
 Basic functions
 fork spawns new processes
 exit terminates own process
 wait and waitpid wait for and reap terminated children
 execve runs new program in existing process

11

You might also like