»ù±¾¸ÅÄî
fork ºÍ vfork
exit
wait º¯Êý
¾ºÌ¬
exec º¯ÊýÒÔ¼°½Å±¾½âÊÍÆ÷
system º¯Êý
½ø³Ì¼ÇÕÊ
Óû§ºÍ×é±êʶ·û
½ø³Ìʱ¼ä
=============================================================================== #include <unistd.h> pid_t getpid (void); pid_t getppid (void); ===============================================================================
=============================================================================== #include <unistd.h> #include <sys/types.h> uid_t getuid (void); uid_t geteuid (void); uid_t getgid (void); uid_t getegid (void); ===============================================================================
=============================================================================== #include <unistd.h> #include <sys/types.h> pid_t fork (void); ------------------------------------------------------------------------------- EXAMPLE: /* ÅÉÉú×Ó½ø³Ì */ #include <sys/types.h> #include <unistd.h> void err_sys (const char* info) { perror (info); exit (1); } int main (void) { pid_t pid; if ( (pid = fork ()) < 0) err_sys ("fork error"); else if (pid > 0) { // this is parent. } else { // this is child. } exit (0); } ------------------------------------------------------------------------------- * fork Ö®ºó, ×Ó½ø³ÌºÍ¸¸½ø³Ì¹²Ïí´ò¿ªµÄÎļþ. ÕâÊÇʵÏֹܵÀµÈ IPC »úÖÆµÄ»ù´¡. * Linux ÀûÓð´Ðè·ÖÒ³ (paging on demand) ºÍдʱ¸´ÖÆ (copy-on-write) ·½Ê½ÅÉÉú×Ó½ø³Ì. ===============================================================================
=============================================================================== #include <unistd.h> #include <sys/types.h> pid_t vfork (void); ------------------------------------------------------------------------------- * ²»¸´ÖƸ¸½ø³ÌµÄÈ«²¿µØÖ·¿Õ¼ä. * vfork ±£Ö¤×Ó½ø³ÌÊ×ÏÈÔËÐÐ, Ö±µ½µ÷Óà exec »ò exit Ö®ºó, ¸¸½ø³Ì²ÅÄܵõ½ÔËÐлú»á. ===============================================================================
=============================================================================== #include <stdlib.h> void exit (int status); #include <unistd.h> void _exit (int status); ===============================================================================
=============================================================================== #include <sys/types.h> #include <sys/wait.h> pid_t wait (int *status) pid_t waitpid (pid_t pid, int *status, int options); ------------------------------------------------------------------------------- * wait µÈ´ýij¸ö×Ó½ø³ÌÍ˳ö. * waitpid ¿ÉµÈ´ýÖ¸¶¨µÄ×Ó½ø³ÌÍ˳ö, ¿ÉÖ¸¶¨ WNOHANG Ñ¡Ïî¶ø²»×èÈûµ÷Óýø³ÌµÄÖ´ÐÐ. ------------------------------------------------------------------------------- ÓÃÀ´°ïÖúÅжÏÍ˳ö״̬µÄºê: WIFEXITED (status) is non-zero if the child exited normally. WEXITSTATUS (status) evaluates to the least significant eight bits of the return code of the child which terminated, which may have been set as the argument to a call to exit() or as the argument for a return state- ment in the main program. This macro can only be evaluated if WIFEXITED returned non-zero. WIFSIGNALED (status) returns true if the child process exited because of a signal which was not caught. WTERMSIG (status) returns the number of the signal that caused the child process to terminate. This macro can only be evaluated if WIFSIGNALED returned non-zero. WIFSTOPPED (status) returns true if the child process which caused the return is currently stopped; this is only possible if the call was done using WUNTRACED. WSTOPSIG (status) returns the number of the signal which caused the child to stop. This macro can only be evaluated if WIFSTOPPED returned non-zero. ===============================================================================
=============================================================================== #define _USE_BSD #include <sys/types.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) ------------------------------------------------------------------------------ * wait3 ºÍ wait4 ·Ö±ðÊÇ wait ºÍ waitpid µÄÀ©Õ¹µ÷ÓÃ, ËüÃÇ¿ÉÒÔ·µ»Ø×Ó½ø³ÌµÄ ×ÊԴʹÓÃÇé¿ö. ===============================================================================
=============================================================================== #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 execve (const char *filename, char *const argv [], char *const envp[]); ------------------------------------------------------------------------------ * ÒԿɱä²ÎÊýÐÎʽ´«µÝÃüÁîÐÐʱ, ×îºóÒ»¸ö²ÎÊýÓ¦¸ÃΪ NULL Ö¸Õë. * execve ÊÇʵ¼ÊµÄϵͳµ÷ÓÃ, ¶øÆäËûµÄº¯ÊýÒԿ⺯ÊýʵÏÖ, ×îÖÕÒªµ÷Óà execve. ===============================================================================