使用C語言的fork()函數(shù)在Linux中創(chuàng)建進程的實例講解
在Linux中創(chuàng)建一個新進程的唯一方法是使用fork()函數(shù)。fork()函數(shù)是Linux中一個非常重要的函數(shù),和以往遇到的函數(shù)有一些區(qū)別,因為fork()函數(shù)看起來執(zhí)行一次卻返回兩個值。
fork()函數(shù)用于從已存在的進程中創(chuàng)建一個新進程。新進程稱為子進程,而園進程稱為父進程。使用fork()函數(shù)得到的子進程是父進程的一個復(fù)制品,它從父進程處繼承了整個進程的地址空間,包括進程的上下文、代碼段、進程堆棧、內(nèi)存信息、打開的文件描述符、符號控制設(shè)定、進程優(yōu)先級、進程組號、當(dāng)前工作目錄、根目錄、資源限制和控制終端等,而子進程所獨有的只有它的進程號、資源使用和計時器等。
因為子進程幾乎是父進程的完全復(fù)制,所以父子兩進程會運行同一個程序。這就需要用一種方式來區(qū)分它們,并使它們照此運行,否則,這兩個進程不可能做不同的事。實際上是在父進程中執(zhí)行fork()函數(shù)時,父進程會復(fù)制一個子進程,而且父子進程的代碼從fork()函數(shù)的返回開始分別在兩個地址空間中同時運行,從而使兩個進程分別獲得所屬fork()函數(shù)的返回值,其中在父進程中的返回值是子進程的進程號,而在子進程中返回0。因此,可以通過返回值來判斷該進程的父進程還是子進程。
同時可以看出,使用fork()函數(shù)的代價是很大的,它復(fù)制了父進程中的代碼段、數(shù)據(jù)段和堆棧段里的大部分內(nèi)容,使得fork()函數(shù)的系統(tǒng)開銷比較大,而且執(zhí)行速度也不是很快。
fork()函數(shù)語法
fork()函數(shù)出錯可能有兩種原因:
1、當(dāng)前的進程數(shù)已經(jīng)達到了系統(tǒng)規(guī)定的上限,這時errno的值被設(shè)置為EAGAIN
2、系統(tǒng)內(nèi)存不足,這時errno的值被設(shè)置為ENOMEM
示例
下面的是csapp.h頭文件,后面的討論中均只用該頭文件來完成程序的編寫。
/* $begin csapp.h */ #ifndef __CSAPP_H__ #define __CSAPP_H__ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <ctype.h> #include <setjmp.h> #include <signal.h> #include <sys/time.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/mman.h> #include <errno.h> #include <math.h> #include <pthread.h> #include <semaphore.h> #include <sys/socket.h> #include <netdb.h> #include <netinet/in.h> #include <arpa/inet.h> /* Default file permissions are DEF_MODE & ~DEF_UMASK */ /* $begin createmasks */ #define DEF_MODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH #define DEF_UMASK S_IWGRP|S_IWOTH /* $end createmasks */ /* Simplifies calls to bind(), connect(), and accept() */ /* $begin sockaddrdef */ typedef struct sockaddr SA; /* $end sockaddrdef */ /* Persistent state for the robust I/O (Rio) package */ /* $begin rio_t */ #define RIO_BUFSIZE 8192 typedef struct { int rio_fd; /* Descriptor for this internal buf */ int rio_cnt; /* Unread bytes in internal buf */ char *rio_bufptr; /* Next unread byte in internal buf */ char rio_buf[RIO_BUFSIZE]; /* Internal buffer */ } rio_t; /* $end rio_t */ /* External variables */ extern int h_errno; /* Defined by BIND for DNS errors */ extern char **environ; /* Defined by libc */ /* Misc constants */ #define MAXLINE 8192 /* Max text line length */ #define MAXBUF 8192 /* Max I/O buffer size */ #define LISTENQ 1024 /* Second argument to listen() */ /* Our own error-handling functions */ void unix_error(char *msg); void posix_error(int code, char *msg); void dns_error(char *msg); void app_error(char *msg); /* Process control wrappers */ pid_t Fork(void); void Execve(const char *filename, char *const argv[], char *const envp[]); pid_t Wait(int *status); pid_t Waitpid(pid_t pid, int *iptr, int options); void Kill(pid_t pid, int signum); unsigned int Sleep(unsigned int secs); void Pause(void); unsigned int Alarm(unsigned int seconds); void Setpgid(pid_t pid, pid_t pgid); pid_t Getpgrp(); /* Signal wrappers */ typedef void handler_t(int); handler_t *Signal(int signum, handler_t *handler); void Sigprocmask(int how, const sigset_t *set, sigset_t *oldset); void Sigemptyset(sigset_t *set); void Sigfillset(sigset_t *set); void Sigaddset(sigset_t *set, int signum); void Sigdelset(sigset_t *set, int signum); int Sigismember(const sigset_t *set, int signum); /* Unix I/O wrappers */ int Open(const char *pathname, int flags, mode_t mode); ssize_t Read(int fd, void *buf, size_t count); ssize_t Write(int fd, const void *buf, size_t count); off_t Lseek(int fildes, off_t offset, int whence); void Close(int fd); int Select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); int Dup2(int fd1, int fd2); void Stat(const char *filename, struct stat *buf); void Fstat(int fd, struct stat *buf) ; /* Memory mapping wrappers */ void *Mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset); void Munmap(void *start, size_t length); /* Standard I/O wrappers */ void Fclose(FILE *fp); FILE *Fdopen(int fd, const char *type); char *Fgets(char *ptr, int n, FILE *stream); FILE *Fopen(const char *filename, const char *mode); void Fputs(const char *ptr, FILE *stream); size_t Fread(void *ptr, size_t size, size_t nmemb, FILE *stream); void Fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); /* Dynamic storage allocation wrappers */ void *Malloc(size_t size); void *Realloc(void *ptr, size_t size); void *Calloc(size_t nmemb, size_t size); void Free(void *ptr); /* Sockets interface wrappers */ int Socket(int domain, int type, int protocol); void Setsockopt(int s, int level, int optname, const void *optval, int optlen); void Bind(int sockfd, struct sockaddr *my_addr, int addrlen); void Listen(int s, int backlog); int Accept(int s, struct sockaddr *addr, socklen_t *addrlen); void Connect(int sockfd, struct sockaddr *serv_addr, int addrlen); /* DNS wrappers */ struct hostent *Gethostbyname(const char *name); struct hostent *Gethostbyaddr(const char *addr, int len, int type); /* Pthreads thread control wrappers */ void Pthread_create(pthread_t *tidp, pthread_attr_t *attrp, void * (*routine)(void *), void *argp); void Pthread_join(pthread_t tid, void **thread_return); void Pthread_cancel(pthread_t tid); void Pthread_detach(pthread_t tid); void Pthread_exit(void *retval); pthread_t Pthread_self(void); void Pthread_once(pthread_once_t *once_control, void (*init_function)()); /* POSIX semaphore wrappers */ void Sem_init(sem_t *sem, int pshared, unsigned int value); void P(sem_t *sem); void V(sem_t *sem); /* Rio (Robust I/O) package */ ssize_t rio_readn(int fd, void *usrbuf, size_t n); ssize_t rio_writen(int fd, void *usrbuf, size_t n); void rio_readinitb(rio_t *rp, int fd); ssize_t rio_readnb(rio_t *rp, void *usrbuf, size_t n); ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen); /* Wrappers for Rio package */ ssize_t Rio_readn(int fd, void *usrbuf, size_t n); void Rio_writen(int fd, void *usrbuf, size_t n); void Rio_readinitb(rio_t *rp, int fd); ssize_t Rio_readnb(rio_t *rp, void *usrbuf, size_t n); ssize_t Rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen); /* Client/server helper functions */ int open_clientfd(char *hostname, int portno); int open_listenfd(int portno); /* Wrappers for client/server helper functions */ int Open_clientfd(char *hostname, int port); int Open_listenfd(int port); #endif /* __CSAPP_H__ */ /* $end csapp.h */
fork()函數(shù)示例一
#include "csapp.h" int main() { pid_t pid; int x=1; pid=fork(); if(pid==0) { printf("child :x=%d\n",++x); exit(0); } printf("parent:x=%d\n",--x); exit(0); }
例如上面的程序,由于fork()函數(shù)比較特殊,執(zhí)行一次,返回兩次。返回兩次分別是在父進程和子進程中各返回一個值,在子進程中返回為0,在父進程中返回進程ID,一般為正整數(shù)即非零。這樣就能根據(jù)返回值來確定其在哪個進程中了。如上面的程序,子進程中pid=0,所以執(zhí)行if語句,子進程會共享父進程的文本/數(shù)據(jù)/bss段/堆以及用戶棧,子進程隨后正常終止并且返回碼為0,因此子進程不執(zhí)行后續(xù)的共享代碼塊,因此本程序的輸出結(jié)果是
parent:x=0
child :x=2
fork()函數(shù)示例二
#include "csapp.h" int main() { if(fork()==0) { printf("a"); } else { printf("b"); waitpid(-1,NULL,0); } printf("c"); exit(0); }
此程序是用來檢驗子進程與父進程的關(guān)系。同樣再次強調(diào)一遍,fork()函數(shù)用于新建子進程,子進程具有與父進程相同的用戶級虛擬地址空間,包括文本/數(shù)據(jù)/bss段/堆/用戶棧,子進程可以讀寫任意父進程打開的文件,它們的最大區(qū)別是它們有不同的PID。fork函數(shù)調(diào)用一次,返回兩次,一次在父進程中,其返回子進程的PID;在子進程中,fork返回0,因為子進程的PID總是非零的,返回值就提供了一個明確的方法來辨別是在父進程中執(zhí)行還是在子進程中執(zhí)行。waitpid()函數(shù)是等待子進程終止,若無錯誤,則返回值為正數(shù)。因為在在子進程中,fork()返回0,因此先輸出a,并且其共享父進程的代碼段,故又輸出c;而在父進程中,fork()返回值非零,所以執(zhí)行else語句,故輸出bc。因此本程序的輸出結(jié)果為
acbc
fork()函數(shù)示例三
#include "csapp.h" int main() { int x=1; if(fork()==0) printf("printf1:x=%d\n",++x); printf("printf2:x=%d\n",--x); exit(0); }
本程序再次演示子進程與父進程的區(qū)別。程序中,在子進程中,子進程共享數(shù)據(jù)x=1,并且fork()返回0,因此if語句被執(zhí)行,輸出printf1:x=2,接著共享后面一部分代碼段,因此再輸出printf2:x=1;而對于父進程,fork()返回非零,因此不會執(zhí)行if語句段,而執(zhí)行后面的代碼,即輸出printf2:x=0.因此本程序輸出結(jié)果為(子進程與父進程順序不唯一)
printf2:x=0 printf1:x=2 printf2:x=1
fork()函數(shù)示例四
#include "csapp.h" #define N 3 int main() { int status,i; pid_t pid; for(i=0;i<N;i++) if((pid=fork())==0) //新建子進程 exit(100+i); while((pid=waitpid(-1,&status,0))>0) { //如果子進程是正常終止的,就返回進程的進程號PID if(WIFEXITED(status)) //返回退出狀態(tài) printf("child %d terminated normally with exit status =%d\n",pid,WEXITSTATUS(status)); else printf("child %d erminated abnormally\n",pid); } if(errno!=ECHILD) printf("waitpid error\n"); exit(0); }
本代碼主要是測試進程的終止,即waitpid案例程序。定義生成兩個進程,本例子是不按照特定順序來回收僵死子進程,本程序返回結(jié)果為
child 28693 terminated normally with exit status =100 child 28694 terminated normally with exit status =101 child 28695 terminated normally with exit status =102
fork()函數(shù)示例五
#include "csapp.h" #define N 2 int main() { int status,i; pid_t pid[N],retpid; for(i=0;i<N;i++) if((pid[i]=fork())==0) exit(100+i);//退出并返回狀態(tài)碼 i=0; while((retpid=waitpid(pid[i++],&status,0))>0) { if(WIFEXITED(status)) printf("child %d terminated normally with exit status=%d\n",retpid,WEXITSTATUS(status)); else printf("child %d terminated abnormally\n",retpid); } if(errno !=ECHILD) printf("waitpid error!"); exit(0); }
按照創(chuàng)建進程的順序來回收這些僵死進程,注意程序中的pid[i++]是按序的標志,本程序運行結(jié)果為
child 29846 terminated normally with exit status=100 child 29847 terminated normally with exit status=101
fork()函數(shù)示例六
#include "csapp.h" /*推測此程序會輸出什么樣的結(jié)果*/ int main() { int status; pid_t pid; printf("Hello\n"); pid=fork(); printf("%d\n",!pid); if(pid!=0) { if(waitpid(-1,&status,0)>0) { if(WIFEXITED(status)!=0) printf("%d\n",WEXITSTATUS(status)); } } printf("Bye\n"); exit(2); }
首先父進程會輸出Hello,子進程新建成功,在子進程中,pid為0,輸出1,并且子進程無法執(zhí)行if語句,但是子進程仍然可以輸出Bye,并且正常退出并返回狀態(tài)碼為2。而在父進程中,pid為非零的正數(shù),因此先輸出0,然后執(zhí)行if語句,由于子進程已經(jīng)正常退出,故輸出狀態(tài)碼2,并且最后執(zhí)行公共代碼塊,輸出Bye并正常退出。因此,總共輸出的結(jié)果如下所示:
Hello 0 1 Bye 2 Bye
當(dāng)然,順序不唯一,還有一種可能的結(jié)果是
Hello 1 Bye 0 2 Bye
相關(guān)文章
C語言二叉樹常見操作詳解【前序,中序,后序,層次遍歷及非遞歸查找,統(tǒng)計個數(shù),比較,求深度】
這篇文章主要介紹了C語言二叉樹常見操作,結(jié)合實例形式詳細分析了基于C語言的二叉樹前序,中序,后序,層次遍歷及非遞歸查找,統(tǒng)計個數(shù),比較,求深度等相關(guān)操作技巧與注意事項,需要的朋友可以參考下2018-04-04C++非遞歸隊列實現(xiàn)二叉樹的廣度優(yōu)先遍歷
這篇文章主要介紹了C++非遞歸隊列實現(xiàn)二叉樹的廣度優(yōu)先遍歷,實例分析了遍歷二叉樹相關(guān)算法技巧,并附帶了兩個相關(guān)算法實例,需要的朋友可以參考下2015-07-07一篇文章教你如何用C語言實現(xiàn)strcpy和strlen
這篇文章主要為大家介紹了C語言實現(xiàn)strcpy和strlen的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01