linux中fork()函数与vfork()函数的区别
生活随笔
收集整理的這篇文章主要介紹了
linux中fork()函数与vfork()函数的区别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
對于fork函數:
子進程只繼承父進程的文件描述表,不繼承但共享文件表項和i-node
父進程創建一個子進程之后,文件表項中的引用計數加1變為2,當父進程作close操作之后計數器減1,子進程還是可以使用文件表項,只有計數器減到0的時候才會釋放該文件表項
?
fork函數測試:
#include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <errno.h> #include <stdio.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> #include <sys/stat.h>int glob = 6; /* external variable in initialized data */int main(void) {int var; /* automatic variable on the stack */pid_t pid;var = 88;printf("before vfork\n"); /* we don't flush stdio */FILE *fp = fopen("s.txt", "w");int fd = open("s_fd.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG);char *s = "hello andrew";ssize_t size = strlen(s) * sizeof(char);//標準IO函數帶緩存功能-->全緩存fprintf(fp, "s: %s, pid : %d", s, getpid()); //實際上是先寫到緩存中去的//緩存沒有滿程序沒有結束,信息就會一直停留在緩存中//內核提供的IO系統調用(不帶緩存)write(fd, s, size);//直接寫入文件if ((pid = fork()) < 0) {perror("vfork error");} else if (pid == 0) { /* child */glob++; /* modify parent's variables */var++;//fprintf(fp, "pid = %d, glob = %d, var = %d\n", getpid(), glob, var);// _exit(0); /* child terminates */}else{//父進程}//父子進程都要執行fprintf(fp, "pid = %d, glob = %d, var = %d\n", getpid(), glob, var);//fprintf(fp, "s: %s, pid : %d\n", s, getpid()); close(fp);close(fd);sleep(1); exit(0); }?測試結果:
使用fork函數的緩存區會復制到子進程中,在父子進程都需要執行
//父子進程都要執行fprintf(fp, "pid = %d, glob = %d, var = %d\n", getpid(), glob, var);的時候父子進程中都含有上面:
fprintf(fp, "s: %s, pid : %d", s, getpid()); //實際上是先寫到緩存中去的fp緩存區中的內容,因此在執行的時候父子進程都能將 hello andrew輸出到自己的緩存中去,一旦程序結束父子進程緩存區中的內容都會寫入fp所指向的文件;
?
vfork函數測試:
#include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <errno.h> #include <stdio.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> #include <sys/stat.h>int glob = 6; /* external variable in initialized data */int main(void) {int var; /* automatic variable on the stack */pid_t pid;var = 88;printf("before vfork\n"); /* we don't flush stdio */FILE *fp = fopen("s.txt", "w");int fd = open("s_fd.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG);char *s = "hello andrew";ssize_t size = strlen(s) * sizeof(char);//標準IO函數帶緩存功能-->全緩存fprintf(fp, "s: %s, pid : %d", s, getpid()); //實際上是先寫到緩存中去的//緩存沒有滿程序沒有結束,信息就會一直停留在緩存中//內核提供的IO系統調用(不帶緩存)write(fd, s, size);//直接寫入文件if ((pid = vfork()) < 0) { //注意這里使用的是vfork函數perror("vfork error");} else if (pid == 0) { /* child */glob++; /* modify parent's variables */var++;//fprintf(fp, "pid = %d, glob = %d, var = %d\n", getpid(), glob, var);// _exit(0); /* child terminates */}else{//父進程}//父子進程都要執行fprintf(fp, "pid = %d, glob = %d, var = %d\n", getpid(), glob, var);//fprintf(fp, "s: %s, pid : %d\n", s, getpid()); close(fp);close(fd);sleep(1); exit(0); }測試結果:
在使用vfork的時候,可以看到,只有一個 hello andrew輸出,因為vfork函數是不為子進程創建單獨的分區的,而是和父進程共用一個,一旦子進程將緩存區中的內容輸出,那么另一位緩存區中也不會在有內容,因為兩者緩存區是相同的;
總結:vfork創建的子進程,子進程會先運行并且,不會復制父進程的內存空間。
總結
以上是生活随笔為你收集整理的linux中fork()函数与vfork()函数的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BDTC 2017 | 中国大数据技术大
- 下一篇: 配置ftp服务器亲测可用