Directory: http://blog.csdn.net/alex_my/article/details/39346381
Process Control
1 exec Functions
# Include <unistd. h>
Extern char ** environ;
Int execl (const char * path, const char * Arg ,...); int execlp (const char * file, const char * Arg ,...); int execle (const char * path, const char * Arg ,..., char * const envp []);
Int execv (const char * path, char * const argv []); int execvp (const char * file, char * const argv []); int execvpe (const char * file, char * const argv [], char * const envp []);
Int fexecve (int fd, char * const argv [], char * const envp []);
System invocation exec replaces the original code segment, data segment, heap, and stack with a new program. The new program starts to run from the main function. No new process is created. Therefore, the Process IDs remain unchanged before and after replacement.
This is a function cluster composed of the functions listed above. Some functions end with the command line parameter: the end can use null, (char *) 0. It is best not to end with 0. If the system regards 0 as a parameter, function execution will fail. The book says that if the length of an integer is different from that of char *, it will fail. While the local int-4, char *-8, but can run with 0 end.
Some use string Arrays:
Program use case:
// Compile the called program into test8.4 // G ++-O test8.4 test8.4.cc
# Include <stdio. h> # include <stdlib. h>
Int main (INT argc, char * argv []) {extern char ** environ;
For (INT I = 0; I <argc; ++ I) printf ("Arg [% d]: % s \ n", I, argv [I]); printf ("\ nenviron: \ n ");
For (char ** PTR = environ; * PTR! = 0; ++ PTR) printf ("% s \ n", * PTR );
Exit (exit_success );}
// Manually execute the program using execl, absolute path
# Include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <sys/Wait. h> # include <errno. h> # include <string. h>
Int main (INT argc, char ** argv) {pid_t pid = fork (); If (PID <0) {printf ("fork failed \ n "); exit (exit_failure );}
If (pid = 0) {If (execl ("/home/alex_my/code/apue/test8.4", "test8.4", "arg1", "arg2", null) <0) {printf ("Child execlp failed, error [% d]: % s \ n", errno, strerror (errno); exit (exit_failure );}}
Waitpid (PID, null, 0 );
Exit (exit_success );}
Output:
Arg [0]: test8.4 Arg [1]: arg1 Arg [2]: arg2
Environ: xdg_vtnr = 1 ssh_agent_pid = 2946 xdg_session_id = 2 ......
2 changing user IDs and group IDs # include <unistd. h>
Int setuid (uid_t UID); int setgid (gid_t GID );
(From apue) indicates the change of the three IDs in various situations when the exec or setuid function is used.
Setuid:
-1: If it is a Super User, the three IDs will become uid-2: if it is not a super user, and the UID of this user is the same as real User ID/saved set-User ID, the valid user ID is set to uid.
Exec:
It depends on whether the set-user-ID bit is disabled.
3 process time
# Include <sys/times. h>
Clock_t times (struct TMS * buffer );
TMS definition:
Struct TMS {clock_t tms_utime;/* User CPU time */clock_t tms_stime;/* system CPU time */clock_t tms_cutime;/* User CPU time, terminated children */clock_t tms_cstime; /* system CPU time, terminated children */};
Reading Notes for advanced programming in UNIX environment (8)