Today, we are trying 'exec'. There are few variants of exec. To start with we will concentrate on one variant 'execl'.
Execute the program, and immediately in another terminal issue 'ps -a'
Observations are:
- After this function call, current process will be replaced with exec and current process will no longer exist
- First arg: full path of command to execute. Second arg: Name of the program. Third Arg and so on: Optional. Arguments passed to command. End of arguments specified by 'NULL'.
- Rest of the program after exec will be ignored as the current program no longer exists.
1 2 3 4 5 6 7 8 9 10 | #include <stdio.h> #include <unistd.h> int main() { printf("PID of the process %d\n",getpid()); printf("Sleep using exec\n"); execl("/bin/sleep","sleep","5",NULL); printf("lines after exec\n"); } |
Execute the program, and immediately in another terminal issue 'ps -a'
# ./a.out PID of the process 4628 Sleep using exec
# ps -a
PID TTY TIME CMD
1668 tty1 00:01:34 Xorg
1674 tty1 00:00:00 gnome-session-b
1767 tty1 00:00:00 gnome-keyring-d
1775 tty1 00:01:50 gnome-shell
1893 tty1 00:00:03 gnome-settings-
1919 tty1 00:00:00 zeitgeist-datah
1924 tty1 00:00:04 nautilus
1926 tty1 00:00:00 tracker-extract
1935 tty1 00:00:00 tracker-miner-a
1936 tty1 00:00:00 tracker-miner-f
1937 tty1 00:00:00 tracker-miner-u
1960 tty1 00:00:00 gsd-printer
3639 pts/0 00:11:38 firefox-esr
4584 tty1 00:00:00 gvim
4628 pts/1 00:00:00 sleep
4630 pts/2 00:00:00 ps
Observations are:
- process ID of current program 4628 and the process ID of sleep command in 'ps -a' output are the same. sleep replaced current program
- Line 9 of the current program is not executed. It is ignored.
No comments:
Post a Comment