本文研究的主要是Linux進程函數fork(),vfork(),execX()的相關內容,具體介紹如下。
fork函數:創建一個新進程
1、fork()成功后,將為子進程申請PCB和用戶內存空間。
2、子進程會復制父進程用戶空間的所有數據(代碼段、數據段、BSS、堆、棧),文件描述符。
3、復制父親進程PCB中絕大多數信息。
4、雖然子進程復制了文件描述符,而對于文件描述符相關的文件表項(struct file結構),則采用共享的方式。
一個實例:
#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; }}
運行:
[root@localhost linux/168284.html">linux/198087.html">linux]# gcc -o fork fork.c [root@localhost linux]# ./fork child:i=2 parent:i=1
可以看到在子進程中改變了i的值,然而父進程i仍為1,所以說子進程和父進程有自己的用戶空間。而打開所創建的fork.txt可以得到hellowordIN,父子進程共同對一個文件操作寫入的數據是不交叉覆蓋的,說明父子進程共享文件偏移,一次共享文件表項。
與fork()函數不同,vfork()函數在創建進程是并不復制父進程的地址空間,而是在必要的時候才申請新的存儲空間,因此使得vfork()更有效率。
特別注意的是vfork()是共享父進程的代碼以數據段。
一個例子:
#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; }}
注意:上面的代碼中回收子進程用的是_exit(0),如果用return 0;的話它會回收用戶空間,因此在父進程調用的時候會出現段錯誤。
下面是調用輸出結果:
如果以fork()創建則會輸出: [root@localhost linux]# ./fork child:i=11 parent:i=10 如果改為vfork(),則: child:i=11 parent:i=11
用fork()函數創建紫禁城后,如果希望在當前子進程中運行新的程序,則可以調用execX系列函數。
注意:當進程調用exec函數后,該進程的用戶空間資源完全有新程序代替。
這些函數的區別在于:
1、指示新程序的位置是路徑還是文件名
2、在使用參數時是使用參數列表哈市使用argv[]數組
3、后綴有l(list)表示使用參數列表,v表示使用argv[]數組
具體如下所示:
#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[]);
一個實例:
#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");}
運行可以看到在子進程中執行了ls命令。
[yqtao@localhost linux]$ gcc -o exec execX.c[yqtao@localhost linux]$ ./exec /home father ok
//execlp()函數使用
#include <unistd.h>#include <stdio.h>#include <sys/types.h>int main(int argc ,char* argv[]) { execlp("ls","ls","-l","/home",(char*)0);}
//execv()函數的使用
#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()會從環境變量PATH所指定的目錄中查找文件名作為第一個參數,第二個及以后的參數由參數列表,注意最后一個成員必須為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);}
以上就是本文關于深入解讀Linux進程函數fork(),vfork(),execX()的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
新聞熱點
疑難解答