深入解讀Linux進(jìn)程函數(shù)fork(),vfork(),execX()
本文研究的主要是Linux進(jìn)程函數(shù)fork(),vfork(),execX()的相關(guān)內(nèi)容,具體介紹如下。
函數(shù)fork()
fork函數(shù):創(chuàng)建一個(gè)新進(jìn)程
1、fork()成功后,將為子進(jìn)程申請(qǐng)PCB和用戶內(nèi)存空間。
2、子進(jìn)程會(huì)復(fù)制父進(jìn)程用戶空間的所有數(shù)據(jù)(代碼段、數(shù)據(jù)段、BSS、堆、棧),文件描述符。
3、復(fù)制父親進(jìn)程PCB中絕大多數(shù)信息。
4、雖然子進(jìn)程復(fù)制了文件描述符,而對(duì)于文件描述符相關(guān)的文件表項(xiàng)(struct file結(jié)構(gòu)),則采用共享的方式。
一個(gè)實(shí)例:
#include <unistd.h> //fork fuction #include <fcntl.h> //file operator #include <sys/types.h> #include <stdio.h> #include <stdlib.h> //exit fuction #include <string.h> int main() { pid_t pid; int i=1; int status; char *ch1="hello",*ch2="world",*ch3="IN"; int fd; if ((fd=open("fork.txt",O_RDWR|O_CREAT,0644))==-1) { perror("not open"); exit(EXIT_FAILURE); } if (write(fd,ch1,strlen(ch1))==-1) { //write in fork.txt perror("not write"); exit(EXIT_FAILURE); } if ((pid=fork())==-1) { perror("fork error"); exit(EXIT_FAILURE); } else if(pid==0) { //son process int i=2; //change i printf("child:i=%d\n",i); if (write(fd,ch2,strlen(ch2))==-1) perror("child write"); return 0; } else { sleep(1); printf("parent:i=%d\n",i); if (write(fd,ch3,strlen(ch3))==-1) perror("child write"); wait(&status); return 0; } }
運(yùn)行:
[root@localhost linux]# gcc -o fork fork.c [root@localhost linux]# ./fork child:i=2 parent:i=1
可以看到在子進(jìn)程中改變了i的值,然而父進(jìn)程i仍為1,所以說(shuō)子進(jìn)程和父進(jìn)程有自己的用戶空間。而打開(kāi)所創(chuàng)建的fork.txt可以得到hellowordIN,父子進(jìn)程共同對(duì)一個(gè)文件操作寫(xiě)入的數(shù)據(jù)是不交叉覆蓋的,說(shuō)明父子進(jìn)程共享文件偏移,一次共享文件表項(xiàng)。
函數(shù)vfork()
與fork()函數(shù)不同,vfork()函數(shù)在創(chuàng)建進(jìn)程是并不復(fù)制父進(jìn)程的地址空間,而是在必要的時(shí)候才申請(qǐng)新的存儲(chǔ)空間,因此使得vfork()更有效率。
特別注意的是vfork()是共享父進(jìn)程的代碼以數(shù)據(jù)段。
一個(gè)例子:
#include <unistd.h> //fork fuction #include <fcntl.h> //file operator #include <sys/types.h> #include <stdio.h> #include <stdlib.h> //exit fuction #include <string.h> int i=10; int main() { pid_t pid; if ((pid=fork())==-1) { perror("fork error"); exit(EXIT_FAILURE); } else if(pid==0) { //son process i++; printf("child:i=%d\n",i); _exit(0); } else { sleep(1); printf("parent:i=%d\n",i); return 0; } }
注意:上面的代碼中回收子進(jìn)程用的是_exit(0),如果用return 0;的話它會(huì)回收用戶空間,因此在父進(jìn)程調(diào)用的時(shí)候會(huì)出現(xiàn)段錯(cuò)誤。
下面是調(diào)用輸出結(jié)果:
如果以fork()創(chuàng)建則會(huì)輸出: [root@localhost linux]# ./fork child:i=11 parent:i=10 如果改為vfork(),則: child:i=11 parent:i=11
函數(shù)exec X()系列函數(shù)
用fork()函數(shù)創(chuàng)建紫禁城后,如果希望在當(dāng)前子進(jìn)程中運(yùn)行新的程序,則可以調(diào)用execX系列函數(shù)。
注意:當(dāng)進(jìn)程調(diào)用exec函數(shù)后,該進(jìn)程的用戶空間資源完全有新程序代替。
這些函數(shù)的區(qū)別在于:
1、指示新程序的位置是路徑還是文件名
2、在使用參數(shù)時(shí)是使用參數(shù)列表哈市使用argv[]數(shù)組
3、后綴有l(wèi)(list)表示使用參數(shù)列表,v表示使用argv[]數(shù)組
具體如下所示:
#include<unistd.h> int execl(const char *pathname,const char *arg0,.../*(char *) 0 */); int execv(const char *pathname,char *const argv[]); int execle(const char *pathname,const char *arg0,.../*(char *) 0 ,char *const envp[] */); int execve(const char *pathname,char *const argv[],char *const envp[]); int execlp(const char *filename,const char*arg0,.../*(char *) 0*/); int execvp(const char *filename, char *const argv[]); int fexecve(int fd,char *const argv[],char *const evnp[]);
一個(gè)實(shí)例:
#include <unistd.h> #include <stdio.h> #include <sys/types.h> int main(int argc ,char* argv[]) { pid_t pid; if ((pid=fork())==-1) printf("error"); else if (pid==0) execl("/bin/ls","ls","-l",argv[1],(char *)0); else printf("father ok\n"); }
運(yùn)行可以看到在子進(jìn)程中執(zhí)行了ls命令。
[yqtao@localhost linux]$ gcc -o exec execX.c [yqtao@localhost linux]$ ./exec /home father ok
//execlp()函數(shù)使用
#include <unistd.h> #include <stdio.h> #include <sys/types.h> int main(int argc ,char* argv[]) { execlp("ls","ls","-l","/home",(char*)0); }
//execv()函數(shù)的使用
#include <unistd.h> #include <stdio.h> #include <sys/types.h> int main(int argc ,char* argv[]) { char* argv1[]={"ls","-l","/home",0}; execv("/bin/ls",argv1); }
ecvp()會(huì)從環(huán)境變量PATH所指定的目錄中查找文件名作為第一個(gè)參數(shù),第二個(gè)及以后的參數(shù)由參數(shù)列表,注意最后一個(gè)成員必須為NULL
#include <unistd.h> #include <stdio.h> #include <sys/types.h> int main(int argc ,char* argv[]) { char* argv1[]={"ls","-l","/home",0}; execvp("ls",argv1); }
總結(jié)
以上就是本文關(guān)于深入解讀Linux進(jìn)程函數(shù)fork(),vfork(),execX()的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
詳解如何設(shè)置CentOS 7開(kāi)機(jī)自動(dòng)獲取IP地址
本例中以CentOS 7舉例說(shuō)明如何設(shè)置Linux開(kāi)機(jī)自動(dòng)獲取IP地址和設(shè)置固定IP地址。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Linux下如何對(duì)端口流量進(jìn)行統(tǒng)計(jì)
本篇文章主要介紹了Linux下如何對(duì)端口流量進(jìn)行統(tǒng)計(jì),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05linux抵御DDOS攻擊 通過(guò)iptables限制TCP連接和頻率
這篇文章主要介紹了linux抵御DDOS攻擊 通過(guò)iptables限制TCP連接和頻率,需要的朋友可以參考下2016-05-05Linux搭建DHCP服務(wù)器的詳細(xì)過(guò)程
DHCP動(dòng)態(tài)主機(jī)配置協(xié)議是一個(gè)局域網(wǎng)的網(wǎng)絡(luò)協(xié)議,指的是由服務(wù)器控制一段IP地址范圍,客戶機(jī)登錄服務(wù)器時(shí)就可以自動(dòng)獲得服務(wù)器分配的IP地址和子網(wǎng)掩碼,這篇文章主要介紹了Linux搭建DHCP服務(wù)器,需要的朋友可以參考下2022-10-10Centos 6.8編譯安裝LNMP環(huán)境(Nginx+MySQL+PHP)教程
這篇文章主要介紹了關(guān)于CentOS 6.8中編譯安裝LNMP環(huán)境的相關(guān)資料,LNMP即Linux,Nginx,MySQL,PHP,文中通過(guò)一步步的步驟介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-03-03