Transcript Lecture 08

Lecture 08
Program Execution
Outline
•
•
•
•
•
•
execve() System Call
exec() Library Functions
$PATH
exec() and File Descriptors
system()
Live Coding
1
July 24, 2016
execve()
• System call
• Multiple functions layered on top of execve()
• We will use the C library functions
• exec() family of functions
• Replaces current running program with a completely new one
2
July 24, 2016
execve()
• System call loads a new program into process memory
• Old program is discarded
• Process’s text, stack, data, and heap replaced by new program
• New program commences at main()
• Frequently used in combination with fork()
• fork() a child process
• exec() the child to execute a new program
3
July 24, 2016
exec() Family
• Various library functions layered on execve() system call
• All functions start with “exec”
• Each provides a different interface with same functionality
• E.g., execle(), execlp(), etc.
• “exec()” referes to the family of these functions
4
July 24, 2016
execve()
int execve( const char *pathname, const char *argv[],
const char *envp[] );
• #include <unistd.h>
• pathname- file path of new program to be loaded
• argv – NULL terminated list of pointers to char * strings
• Corresponds to argv argument to main()
• envp – NULL terminated list of char * strings
• Corresponds to environ argument to main()
• (rarely used)
5
July 24, 2016
execve()
int execve( const char *pathname, const char *argv[],
const char *envp[] );
• Successful call to execve() never returns!
• Never need to check return value of execve()
• If execve() returns, an error occurred
• Check errno as before (e.g., pathname doesn’t exist; not executable, etc.)
6
July 24, 2016
execve()
int execve( const char *pathname, const char *argv[],
const char *envp[] );
• After execve(), PID of new program remains the same
• Why? because the same process continues to exist
• Process just has new memory segments
7
July 24, 2016
Outline
•
•
•
•
•
•
execve() System Call
exec() Library Functions
$PATH
exec() and File Descriptors
system()
Live Coding
8
July 24, 2016
exec() Library Functions
For all exec() functions:
• Make sure file being executed has executable permissions
• Here’s a command to give the user permission to execute file
UNIX> chmod u+x file
9
July 24, 2016
exec() Library Functions
int execl( const char *pathname, const char *arg, ..., NULL );
int execv( const char *pathname, const char *argv[] );
int execlp( const char *filename, const char *arg, ..., NULL );
int execvp( const char *filename, const char *argv[] );
int execle( const char *pathname, const char *arg, ..., NULL,
const char *envp[] );
10
July 24, 2016
exec() Library Functions
•
•
•
•
•
Final letter(s) provide clues of differences
l -> “list”
v -> “vector”
p -> “path”
e -> “environment” (not used in this class...)
11
July 24, 2016
execl()
int execl( const char *pathname, const char *arg, ..., NULL );
• #include <unistd.h>
• l -> “list” : programmer provides arguments as a NULLterminated list of strings within the function call
• E.g.,
execl( “/bin/ls”, “ls”, “-l”, NULL );
12
July 24, 2016
execv()
int execv( const char *pathname, const char *argv[] );
• #include <unistd.h>
• v -> “vector” : argument list is a NULL-terminated array,
• E.g.,
char *argv[] = { “ls”, “-l”, NULL };
execv( “/bin/ls”, argv );
13
July 24, 2016
exevlp()
int execlp( const char *filename, const char *arg, ..., NULL );
• #include <unistd.h>
• l -> “list”
• p -> “path” : filename sought in the list of directories specified
in the PATH environmental variable (i.e., $PATH)
• E.g.,
/* /bin is within $PATH... exec can find file “ls” */
execlp( “ls”, “ls”, “-l”, NULL );
14
July 24, 2016
execvp()
int execvp( const char *filename, const char *argv[] );
•
•
•
•
#include <unistd.h>
v -> “vector”
p -> “path”
E.g.,
char *argv[] = { “ls”, “-l”, NULL };
execvp( argv[0], argv );
15
July 24, 2016
exec() Functions Summary
16
July 24, 2016
functio
n
program file
argument
s
environment
execl
pathname
list
caller’s environ
execv
pathname
array
caller’s environ
execlp
$PATH + filename list
caller’s environ
execvp
$PATH + filename array
caller’s environ
execle
pathname
list
envp argument
execve
pathname
array
envp argument
Outline
•
•
•
•
•
•
execve() System Call
exec() Library Functions
$PATH
exec() and File Descriptors
system()
Live Coding
17
July 24, 2016
$PATH
• $PATH is an environmental variable used by shell
• Contains list of directories separated by “:”
//on my Mac
UNIX> which ls
/bin/ls
UNIX> echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin
• /bin directory is in PATH, hence why ls works without specifying
“/bin/ls”
18
July 24, 2016
$PATH and exec()
• Child process inherits environment from parent
• bash shell is a child of some other process (e.g., init)
• inherits PATH environmental variable from parent
• $PATH variable contains “/bin”
• bash shell can execute “ls” command within /bin
• Program with exec() is child of shell process
• $PATH inherited as well
• Hence why execlp() and execvp() works
19
July 24, 2016
Outline
•
•
•
•
•
•
execve() System Call
exec() Library Functions
$PATH
exec() and File Descriptors
system()
Live Coding
20
July 24, 2016
exec() and File Descriptors
• By default, all FDs opened by calling program remain open
across exec() and are available for use by new program
• E.g., file redirection (e.g., UNIX> ls /tmp > dir.txt )
• Shell performs following steps:
•
•
•
•
•
fork() creates a child process
child opens “dir.txt”, obtains new FD
child duplicates stdout to new FD ( dup2(FD, 1) )
child process execs “ls” command
(parent waits for child)
21
July 24, 2016
close-on-exec Flag (FD_CLOEXEC)
• Sometimes desirable to close certain FDs before exec()
• Easier to set close-on-exec flag than trying to close FDs
• If set, FD automatically closes during call to exec()
• FD left open if exec() fails
• Must use fcntl system call
flags = fcntl( FD, F_GETFD );
flags |= FD_CLOEXEC;
fcntl( FD, F_SETFD, flags );
22
July 24, 2016
Outline
•
•
•
•
•
•
execve() System Call
exec() Library Functions
$PATH
exec() and File Descriptors
system()
Live Coding
23
July 24, 2016
system()
int system( const char *command );
• #include <stdlib.h>
• Allows calling process to execute an arbitrary shell command
• Convenient:
• Don’t need to deal with fork(), exec(), wait(), and exit()
• Error and signal handling done by system()
• system() uses shell to execute command
24
July 24, 2016
system()
int system( const char *command );
• You are NOT allowed to use system() in your homework!!
• You are NOT allowed to use system() in your homework!!
• You are NOT allowed to use system() in your homework!!
• In today’s lecture to show existence (for future use)
25
July 24, 2016
Outline
•
•
•
•
•
•
execve() System Call
exec() Library Functions
$PATH
exec() and File Descriptors
system()
Live Coding
26
July 24, 2016
Live Coding
#1: Implement the system() function
• Read a line of user input
• Parse into argv
• fork(), exec(), exit(), and wait()
#2: Implement exec with file redirection
• exec() a program
• dup2() to read stdin from a file
• dup2() to write stdout to a file
27
July 24, 2016