Linux System Programming note 5—— Process Management
2014-06-06 09:18
369 查看
1. The four program -- be the init process
/sbin/init
/etc/init
/bin/init
/bin/sh
2. /proc/sys/kernel/pid_max -----32768
3. Obtaining the Process ID and Parent Process ID
#include <sys/types.h>
#include <unistd.h>
pid_t getpid(void);
pid_t getppid(void);
4. The Exec Family of Calls
#include <unistd.h>
int execl (const char *path, const char *arg, ...); // The list of arguments must be NULL-terminated.
#include <unistd.h>
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 execve (const char *filename,
char *const argv[],
char *const envp[]);
5. The fork() System Call
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
6. fork plus exec
pid_t pid;
pid = fork();
if (pid == -1)
perror("fork");
if (!pid){
const char *args[] = {"windlass", NULL};
int ret;
ret = execv("/bin/windlass", args);
if (ret == -1){
perror("execv");
exit(EXIT_FAILURE);
}
}
7. vfork()
#include <sys/types.h>
#include <unistd.h>
pid_t vfork(void);
8. Terminate a Process
#include <stdlib.h>
void exit(int status);
Before terminating the process, the c library performs the following shutdown steps, in order:
1. Call any functions registered with atexit() or on_exit(), in the reverse order of their registration.
2. Flush all open standard I/O streams
3. Remove any temporary files created with the tmpfile() function.
exit() invokes the system call _exit() to let the kernel handle the rest of the termination process:
#include <unistd.h>
void _exit(int status);
Note, however, that vfork() users should call _exit(), and not exit().
9. atexit()
#include <stdlib.h>
int atexit(void (*function)(void));
10. on_exit()
#include <stdlib.h>
int on_exit(void (*function)(int, void *), void *arg);
void my_function(int status, void *arg);
11. Waiting for Terminated Child Processes
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
#include <sys/wait.h>
int WIFEXITED(status);
int WIFSIGNALED(status);
int WIFSTOPPED(status);
int WIFCONTINUED(status);
int WEXITSTATUS(status);
int WTERMSIG(status);
int WSTOPSIG(status);
int WCOREDUMP(status);
12. Waiting for a Specific Process
#include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid(pid_t pid, int *status, int options);
13. Even More Waiting Versatility
#include <sys/wait.h>
int waitid(idtype_t idtype,
id_t id,
siginfo_t *infop,
int options);
idtype:
P_PID
P_GID
P_ALL
14. BSD Wants to Play: wait3() and wait4()
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
pid_t wait3(int *status,
int options,
struct rusage *rusage);
pid_t wait4(pid_t pid,
int *status,
int options,
struct rusage *rusage);
#include <sys/resource.h>
struct usage {
struct timeval ru_utime;
struct timeval ru_stime;
long ru_maxrss;
long ru_ixrss;
long ru_idrss;
long ru_isrss;
long ru_minflt;
long ru_majflt;
long ru_nswap;
long ru_inblock;
long ru_oublock;
long ru_msgsnd;
long ru_msgrcv;
long ru_nsignals;
long ru_nvcsw;
long ru_nivcsw;
};
15. Launching and Waiting for a New Process
#include _XOPEN_SOURCE
#include <stdio.h>
int system(const char *command);
16. There are for user IDs associated with a process: The real, effective, saved, and filesystem user IDs.
By executing a setuid(suid) binary, the process can change its effective user ID.
17. Changing the Real or Saved User or Group ID
#include <sys/types.h>
#include <unistd.h>
int setuid(uid_t uid);
int setgid(gid_t gid);
18. Changing the Effective User or Group ID
#include <sys/types.h>
#include <unistd.h>
int seteuid(uid_t euid);
int setegid(gid_t egid);
19. Changing the User and Group IDs, BSD Style
#include <sys/types.h>
#include <unistd.h>
int setreuid(uid_t ruid, uid_t euid);
int setregid(gid_t rgid, gid_t egid);
20. Changing the User and Group IDs, HP-UX Style
#include _GNU_SOURCE
#include <unistd.h>
int setresuid(uid_t ruid, uid_t euid, uid_t suid);
int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
21. Obtaining the User and Group IDs
#include <unistd.h>
#include <sys/types.h>
uid_t getuid(void);
gid_t getgid(void);
uid_t geteuid(void);
gid_t getegid(void);
22. Session System Calls
#include <unistd.h>
pid_t setsid(void);
Obtaining the current session ID:
#define _XOPEN_SOURCE 500
#define <unistd.h>
pid_t getsid(pid_t pid);
23. Process Group System Calls
#define _XOPEN_SOURCE 500
#include <unistd.h>
int setpgid(pid_t pid, pid_t pgid);
pid_t getpgid(pid_t pid);
#include <unistd.h>
int daemon (int nochdir, int noclose);
/sbin/init
/etc/init
/bin/init
/bin/sh
2. /proc/sys/kernel/pid_max -----32768
3. Obtaining the Process ID and Parent Process ID
#include <sys/types.h>
#include <unistd.h>
pid_t getpid(void);
pid_t getppid(void);
4. The Exec Family of Calls
#include <unistd.h>
int execl (const char *path, const char *arg, ...); // The list of arguments must be NULL-terminated.
#include <unistd.h>
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 execve (const char *filename,
char *const argv[],
char *const envp[]);
5. The fork() System Call
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
6. fork plus exec
pid_t pid;
pid = fork();
if (pid == -1)
perror("fork");
if (!pid){
const char *args[] = {"windlass", NULL};
int ret;
ret = execv("/bin/windlass", args);
if (ret == -1){
perror("execv");
exit(EXIT_FAILURE);
}
}
7. vfork()
#include <sys/types.h>
#include <unistd.h>
pid_t vfork(void);
8. Terminate a Process
#include <stdlib.h>
void exit(int status);
Before terminating the process, the c library performs the following shutdown steps, in order:
1. Call any functions registered with atexit() or on_exit(), in the reverse order of their registration.
2. Flush all open standard I/O streams
3. Remove any temporary files created with the tmpfile() function.
exit() invokes the system call _exit() to let the kernel handle the rest of the termination process:
#include <unistd.h>
void _exit(int status);
Note, however, that vfork() users should call _exit(), and not exit().
9. atexit()
#include <stdlib.h>
int atexit(void (*function)(void));
10. on_exit()
#include <stdlib.h>
int on_exit(void (*function)(int, void *), void *arg);
void my_function(int status, void *arg);
11. Waiting for Terminated Child Processes
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
#include <sys/wait.h>
int WIFEXITED(status);
int WIFSIGNALED(status);
int WIFSTOPPED(status);
int WIFCONTINUED(status);
int WEXITSTATUS(status);
int WTERMSIG(status);
int WSTOPSIG(status);
int WCOREDUMP(status);
12. Waiting for a Specific Process
#include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid(pid_t pid, int *status, int options);
13. Even More Waiting Versatility
#include <sys/wait.h>
int waitid(idtype_t idtype,
id_t id,
siginfo_t *infop,
int options);
idtype:
P_PID
P_GID
P_ALL
14. BSD Wants to Play: wait3() and wait4()
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
pid_t wait3(int *status,
int options,
struct rusage *rusage);
pid_t wait4(pid_t pid,
int *status,
int options,
struct rusage *rusage);
#include <sys/resource.h>
struct usage {
struct timeval ru_utime;
struct timeval ru_stime;
long ru_maxrss;
long ru_ixrss;
long ru_idrss;
long ru_isrss;
long ru_minflt;
long ru_majflt;
long ru_nswap;
long ru_inblock;
long ru_oublock;
long ru_msgsnd;
long ru_msgrcv;
long ru_nsignals;
long ru_nvcsw;
long ru_nivcsw;
};
15. Launching and Waiting for a New Process
#include _XOPEN_SOURCE
#include <stdio.h>
int system(const char *command);
16. There are for user IDs associated with a process: The real, effective, saved, and filesystem user IDs.
By executing a setuid(suid) binary, the process can change its effective user ID.
17. Changing the Real or Saved User or Group ID
#include <sys/types.h>
#include <unistd.h>
int setuid(uid_t uid);
int setgid(gid_t gid);
18. Changing the Effective User or Group ID
#include <sys/types.h>
#include <unistd.h>
int seteuid(uid_t euid);
int setegid(gid_t egid);
19. Changing the User and Group IDs, BSD Style
#include <sys/types.h>
#include <unistd.h>
int setreuid(uid_t ruid, uid_t euid);
int setregid(gid_t rgid, gid_t egid);
20. Changing the User and Group IDs, HP-UX Style
#include _GNU_SOURCE
#include <unistd.h>
int setresuid(uid_t ruid, uid_t euid, uid_t suid);
int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
21. Obtaining the User and Group IDs
#include <unistd.h>
#include <sys/types.h>
uid_t getuid(void);
gid_t getgid(void);
uid_t geteuid(void);
gid_t getegid(void);
22. Session System Calls
#include <unistd.h>
pid_t setsid(void);
Obtaining the current session ID:
#define _XOPEN_SOURCE 500
#define <unistd.h>
pid_t getsid(pid_t pid);
23. Process Group System Calls
#define _XOPEN_SOURCE 500
#include <unistd.h>
int setpgid(pid_t pid, pid_t pgid);
pid_t getpgid(pid_t pid);
#include <unistd.h>
int daemon (int nochdir, int noclose);
相关文章推荐
- Linux System Programming note 6—— Adcanced Process Management
- Linux System Programming note 11 ——Time
- Linux System Programming note 7 —— Threading
- Linux System Programming note 9 ——Memory Management
- Linux System Programming note 2 ——File I/O
- Linux System Programming note 10 —— Signal
- Linux System Programming note 8 ——File and Directory Management
- Linux System Programming note 4 —— Advanced File I/O
- Linux System Programming note 8 ——File and Directory Management
- Linux System Programming note 1
- Linux System Programming --Chapter Eight
- Linux System Programming --Chapter Six
- Linux System Programming --Chapter Nine
- Linux System Programming -- Appendix
- Linux System Programming --Chapter Five
- Understanding Unix/Linux Programming note:chapter 1:more函数的流程图
- Linux System Programming --Chapter Five
- Linux System Programming --Chapter Four
- Linux System Programming -- Appendix
- Linux System Programming --Chapter Two