使用C語(yǔ)言的fork()函數(shù)在Linux中創(chuàng)建進(jìn)程的實(shí)例講解
在Linux中創(chuàng)建一個(gè)新進(jìn)程的唯一方法是使用fork()函數(shù)。fork()函數(shù)是Linux中一個(gè)非常重要的函數(shù),和以往遇到的函數(shù)有一些區(qū)別,因?yàn)閒ork()函數(shù)看起來(lái)執(zhí)行一次卻返回兩個(gè)值。
fork()函數(shù)用于從已存在的進(jìn)程中創(chuàng)建一個(gè)新進(jìn)程。新進(jìn)程稱為子進(jìn)程,而園進(jìn)程稱為父進(jìn)程。使用fork()函數(shù)得到的子進(jìn)程是父進(jìn)程的一個(gè)復(fù)制品,它從父進(jìn)程處繼承了整個(gè)進(jìn)程的地址空間,包括進(jìn)程的上下文、代碼段、進(jìn)程堆棧、內(nèi)存信息、打開(kāi)的文件描述符、符號(hào)控制設(shè)定、進(jìn)程優(yōu)先級(jí)、進(jìn)程組號(hào)、當(dāng)前工作目錄、根目錄、資源限制和控制終端等,而子進(jìn)程所獨(dú)有的只有它的進(jìn)程號(hào)、資源使用和計(jì)時(shí)器等。
因?yàn)樽舆M(jìn)程幾乎是父進(jìn)程的完全復(fù)制,所以父子兩進(jìn)程會(huì)運(yùn)行同一個(gè)程序。這就需要用一種方式來(lái)區(qū)分它們,并使它們照此運(yùn)行,否則,這兩個(gè)進(jìn)程不可能做不同的事。實(shí)際上是在父進(jìn)程中執(zhí)行fork()函數(shù)時(shí),父進(jìn)程會(huì)復(fù)制一個(gè)子進(jìn)程,而且父子進(jìn)程的代碼從fork()函數(shù)的返回開(kāi)始分別在兩個(gè)地址空間中同時(shí)運(yùn)行,從而使兩個(gè)進(jìn)程分別獲得所屬fork()函數(shù)的返回值,其中在父進(jìn)程中的返回值是子進(jìn)程的進(jìn)程號(hào),而在子進(jìn)程中返回0。因此,可以通過(guò)返回值來(lái)判斷該進(jìn)程的父進(jìn)程還是子進(jìn)程。
同時(shí)可以看出,使用fork()函數(shù)的代價(jià)是很大的,它復(fù)制了父進(jìn)程中的代碼段、數(shù)據(jù)段和堆棧段里的大部分內(nèi)容,使得fork()函數(shù)的系統(tǒng)開(kāi)銷比較大,而且執(zhí)行速度也不是很快。
fork()函數(shù)語(yǔ)法
fork()函數(shù)出錯(cuò)可能有兩種原因:
1、當(dāng)前的進(jìn)程數(shù)已經(jīng)達(dá)到了系統(tǒng)規(guī)定的上限,這時(shí)errno的值被設(shè)置為EAGAIN
2、系統(tǒng)內(nèi)存不足,這時(shí)errno的值被設(shè)置為ENOMEM
示例
下面的是csapp.h頭文件,后面的討論中均只用該頭文件來(lái)完成程序的編寫(xiě)。
/* $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í)行一次,返回兩次。返回兩次分別是在父進(jìn)程和子進(jìn)程中各返回一個(gè)值,在子進(jìn)程中返回為0,在父進(jìn)程中返回進(jìn)程ID,一般為正整數(shù)即非零。這樣就能根據(jù)返回值來(lái)確定其在哪個(gè)進(jìn)程中了。如上面的程序,子進(jìn)程中pid=0,所以執(zhí)行if語(yǔ)句,子進(jìn)程會(huì)共享父進(jìn)程的文本/數(shù)據(jù)/bss段/堆以及用戶棧,子進(jìn)程隨后正常終止并且返回碼為0,因此子進(jìn)程不執(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);
}
此程序是用來(lái)檢驗(yàn)子進(jìn)程與父進(jìn)程的關(guān)系。同樣再次強(qiáng)調(diào)一遍,fork()函數(shù)用于新建子進(jìn)程,子進(jìn)程具有與父進(jìn)程相同的用戶級(jí)虛擬地址空間,包括文本/數(shù)據(jù)/bss段/堆/用戶棧,子進(jìn)程可以讀寫(xiě)任意父進(jìn)程打開(kāi)的文件,它們的最大區(qū)別是它們有不同的PID。fork函數(shù)調(diào)用一次,返回兩次,一次在父進(jìn)程中,其返回子進(jìn)程的PID;在子進(jìn)程中,fork返回0,因?yàn)樽舆M(jìn)程的PID總是非零的,返回值就提供了一個(gè)明確的方法來(lái)辨別是在父進(jìn)程中執(zhí)行還是在子進(jìn)程中執(zhí)行。waitpid()函數(shù)是等待子進(jìn)程終止,若無(wú)錯(cuò)誤,則返回值為正數(shù)。因?yàn)樵谠谧舆M(jìn)程中,fork()返回0,因此先輸出a,并且其共享父進(jìn)程的代碼段,故又輸出c;而在父進(jìn)程中,fork()返回值非零,所以執(zhí)行else語(yǔ)句,故輸出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);
}
本程序再次演示子進(jìn)程與父進(jìn)程的區(qū)別。程序中,在子進(jìn)程中,子進(jìn)程共享數(shù)據(jù)x=1,并且fork()返回0,因此if語(yǔ)句被執(zhí)行,輸出printf1:x=2,接著共享后面一部分代碼段,因此再輸出printf2:x=1;而對(duì)于父進(jìn)程,fork()返回非零,因此不會(huì)執(zhí)行if語(yǔ)句段,而執(zhí)行后面的代碼,即輸出printf2:x=0.因此本程序輸出結(jié)果為(子進(jìn)程與父進(jìn)程順序不唯一)
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) //新建子進(jìn)程
exit(100+i);
while((pid=waitpid(-1,&status,0))>0) { //如果子進(jìn)程是正常終止的,就返回進(jìn)程的進(jìn)程號(hào)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);
}
本代碼主要是測(cè)試進(jìn)程的終止,即waitpid案例程序。定義生成兩個(gè)進(jìn)程,本例子是不按照特定順序來(lái)回收僵死子進(jìn)程,本程序返回結(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)建進(jìn)程的順序來(lái)回收這些僵死進(jìn)程,注意程序中的pid[i++]是按序的標(biāo)志,本程序運(yùn)行結(jié)果為
child 29846 terminated normally with exit status=100 child 29847 terminated normally with exit status=101
fork()函數(shù)示例六
#include "csapp.h"
/*推測(cè)此程序會(huì)輸出什么樣的結(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);
}
首先父進(jìn)程會(huì)輸出Hello,子進(jìn)程新建成功,在子進(jìn)程中,pid為0,輸出1,并且子進(jìn)程無(wú)法執(zhí)行if語(yǔ)句,但是子進(jìn)程仍然可以輸出Bye,并且正常退出并返回狀態(tài)碼為2。而在父進(jìn)程中,pid為非零的正數(shù),因此先輸出0,然后執(zhí)行if語(yǔ)句,由于子進(jìn)程已經(jīng)正常退出,故輸出狀態(tài)碼2,并且最后執(zhí)行公共代碼塊,輸出Bye并正常退出。因此,總共輸出的結(jié)果如下所示:
Hello 0 1 Bye 2 Bye
當(dāng)然,順序不唯一,還有一種可能的結(jié)果是
Hello 1 Bye 0 2 Bye
上一篇:詳解C++的模板中typename關(guān)鍵字的用法
欄 目:C語(yǔ)言
下一篇:C++使用WideCharToMultiByte函數(shù)生成UTF-8編碼文件的方法
本文標(biāo)題:使用C語(yǔ)言的fork()函數(shù)在Linux中創(chuàng)建進(jìn)程的實(shí)例講解
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2209.html
您可能感興趣的文章
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用函數(shù)刪除字符
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)式函數(shù)庫(kù)
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)數(shù)怎么表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段函數(shù)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排序法函數(shù)
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段函數(shù)
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎么打出三角函數(shù)的值
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求階乘


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery


