深入解讀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,所以說子進(jìn)程和父進(jìn)程有自己的用戶空間。而打開所創(chuàng)建的fork.txt可以得到hellowordIN,父子進(jìn)程共同對(duì)一個(gè)文件操作寫入的數(shù)據(jù)是不交叉覆蓋的,說明父子進(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()的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
centOS7 橋接模式設(shè)置靜態(tài)Ip的方法步驟
這篇文章主要給大家介紹了關(guān)于centOS7 橋接模式設(shè)置靜態(tài)Ip的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用centOS7具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
openwrt中taiscale自動(dòng)安裝腳本方式
這篇文章介紹了在OpenWrt系統(tǒng)中自動(dòng)安裝Tailscale的腳本,腳本分為三個(gè)部分:etc/init.d/tailscale用于控制Tailscale守護(hù)進(jìn)程的啟動(dòng)和停止,usr/bin/tailscale負(fù)責(zé)下載和運(yùn)行Tailscale,而usr/bin/tailscaled則專門用于檢查和下載最新版本的Tailscale2025-03-03
在Linux系統(tǒng)中使用Vim讀寫遠(yuǎn)程文件的命令詳解
這篇文章主要介紹了在Linux系統(tǒng)中使用Vim讀寫遠(yuǎn)程文件的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
實(shí)現(xiàn)一臺(tái)或者多臺(tái)Linux實(shí)例解綁SSH密鑰對(duì)
在本篇文章中我們給大家分享了一篇如何為一臺(tái)或者多臺(tái) Linux 實(shí)例解綁 SSH 密鑰對(duì)的相關(guān)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2018-10-10
linux如何利用crontab添加定時(shí)任務(wù)詳解
這篇文章主要給大家介紹了關(guān)于linux如何利用crontab添加定時(shí)任務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用linux具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Linux中對(duì)mysql用戶進(jìn)行管理的操作代碼
這是在Linux里面對(duì)MySQL數(shù)據(jù)庫的用戶進(jìn)行管理,其中包括查看全部用戶以及權(quán)限、賦予增刪改查權(quán)限、賦予遠(yuǎn)程連接%權(quán)限、刪除用戶的權(quán)限、刪除用戶、修改密碼等,本文給大家詳細(xì)介紹了Linux中對(duì)mysql用戶進(jìn)行管理的操作代碼,需要的朋友可以參考下2024-11-11
在Linux系統(tǒng)中查看所有正在運(yùn)行服務(wù)的方法小結(jié)
在 Linux 系統(tǒng)中所有正在運(yùn)行的服務(wù)都是用什么查看的?其實(shí)使用系統(tǒng)自帶的工具和很多方法都可以查看到系統(tǒng)所有正在運(yùn)行的服務(wù),本文就和大家探討這個(gè)問題,感興趣的小伙伴跟著小編一起來看看吧2024-07-07

