導航:首頁 > 操作系統 > linuxvfork

linuxvfork

發布時間:2022-07-16 09:16:44

『壹』 linux vfork的子程序與父進程共享內存,那為什麼子進程執行exec就不會覆蓋父進程呢

Linux裡面好多內核代碼都是" on write",也就是你不用的時候並不會給你復制,但如果你要改的話就會分離。

類似的還有 malloc, 申請了內存,其實並沒有給你,除非你往裡寫了東西,內存才真正到手。呵呵

『貳』 關於linux的一些編程 (1)分別寫一個小程序使用fork()、vfork或clone()創建子進程。 (2)比較他們的運行

你說的問題我也不太懂,不過我這有些代碼可以給你參考下:
建立一個新的進程forkdemo1.c:
/* forkdemo1.c
* shows how fork creates two processes, distinguishable
* by the different return values from fork()
*/

#include <stdio.h>

main()
{
int ret_from_fork, mypid;

mypid = getpid(); /* who am i? */
printf("Before: my pid is %d\n", mypid); /* tell the world */

ret_from_fork = fork();

sleep(1);
printf("After: my pid is %d, fork() said %d\n",
getpid(), ret_from_fork);
}
子進程創建進程frokdemo2.c:
/* forkdemo2.c - shows how child processes pick up at the return
* from fork() and can execute any code they like,
* even fork(). Predict number of lines of output.
*/

main()
{
printf("my pid is %d\n", getpid() );
fork();
fork();
fork();
printf("After, my pid is %d\n", getpid() );
}
分辨父進程和子進程forkdemo3.c:
/* forkdemo3.c - shows how the return value from fork()
* allows a process to determine whether
* it is a child or process
*/

#include <stdio.h>

main()
{
int fork_rv;

printf("Before: my pid is %d\n", getpid());

fork_rv = fork(); /* create new process */

if ( fork_rv == -1 ) /* check for error */
perror("fork");

else if ( fork_rv == 0 )
printf("I am the child. my pid=%d\n", getpid());
else
printf("I am the parent. my child is %d\n", fork_rv);
}
父進程等待子進程的退出waitdemo.c:
/* waitdemo.c - shows how parent pauses until child finishes
*/

#include <stdio.h>

#define DELAY 2

main()
{
int newpid;
void child_code(), parent_code();

printf("before: mypid is %d\n", getpid());

if ( (newpid = fork()) == -1 )
perror("fork");
else if ( newpid == 0 )
child_code(DELAY);
else
parent_code(newpid);
}
/*
* new process takes a nap and then exits
*/
void child_code(int delay)
{
printf("child %d here. will sleep for %d seconds\n", getpid(), delay);
sleep(delay);
printf("child done. about to exit\n");
exit(17);
}
/*
* parent waits for child then prints a message
*/
void parent_code(int childpid)
{
int wait_rv; /* return value from wait() */
wait_rv = wait(NULL);
printf("done waiting for %d. Wait returned: %d\n", childpid, wait_rv);
}

『叄』 linux應該如何去學習

1、想要學習好Linux就要將它當成興趣,興趣才是最好的老師。良好的興趣才是學習成功好的開始,想要學習好首先就要足夠喜歡它。我們要了解市場上Linux的發展勢頭,了解Linux的相關情況,相信學習好Linux一定要是非常不錯的選擇。
2、學習合適的Linux發行版本,在開始學習Linux之前,首先就是選擇一個適合自己的版本,Linux旗下發行版本有伺服器版本以及桌面版本,而且桌面版本也可以做服務的,為了以後找工作需要的。常見的Linux發行版本有centos
redhat ubuntu等。
3、選擇好Linux學習版本之後,還需要合適的學習資料以及內容,可以到相關的Linux社群看看專業人員的學習機器以及學習手冊,了解真正相關的知識。
4、學習Linux架構以及命令,linux下都是通過命令來執行的,要學會用man和命令幫助,熟悉各種常見命令的操作。
5、學習shell腳本,對於專業的Linux人員來說,shell腳本是必須要掌握的。shell腳本是linux下強大的工具,可以通過各種命令組合完成自己的工作,提升自己的工作效率。

『肆』 linux中進程的vfork()和execl()函數

寫兩個測試程序測試一下,在程序裡面加log。
自己調試才會理解深刻。
很簡單的一個進程編寫問題。

『伍』 linux下的命令都是干什麼用的

前三個和最後一個是兩個類型。前三個主要是Linux用來創建新的進程(線程)而設計的,exec()系列函數則是用來用指定的程序替換當前進程的所有內容。所以exec()系列函數經常在前三個函數使用之後調用,來創建一個全新的程序運行環境。Linux用init進程啟動其他進程的過程一般都是這樣的。
下面說fork、vfork和clone三個函數。這三個函數分別調用了sys_fork、sys_vfork、sys_clone,最終都調用了do_fork函數,差別在於參數的傳遞和一些基本的准備工作不同。可見這三者最終達到的最本質的目的都是創建一個新的進程。在這里需要明確一下,Linux內核中沒有獨立的「線程」結構,Linux的線程就是輕量級進程,換言之基本控制結構和Linux的進程是一樣的(都是通過struct task_struct管理)。
fork是最簡單的調用,不需要任何參數,僅僅是在創建一個子進程並為其創建一個獨立於父進程的空間。fork使用COW(寫時拷貝)機制,並且COW了父進程的棧空間。
vfork是一個過時的應用,vfork也是創建一個子進程,但是子進程共享父進程的空間。在vfork創建子進程之後,父進程阻塞,直到子進程執行了exec()或者exit()。vfork最初是因為fork沒有實現COW機制,而很多情況下fork之後會緊接著exec,而exec的執行相當於之前fork復制的空間全部變成了無用功,所以設計了vfork。而現在fork使用了COW機制,唯一的代價僅僅是復制父進程頁表的代價,所以vfork不應該出現在新的代碼之中。在Linux的manpage中隊vfork有這樣一段話:It is rather unfortunate that Linux revived this specter from the past. The BSD man page states: "This system call will be eliminated when proper system sharing mechanisms are implemented. Users should not depend on the memory sharing semantics of vfork() as it will, in that case, be made synonymous to fork(2)."
clone是Linux為創建線程設計的(雖然也可以用clone創建進程)。所以可以說clone是fork的升級版本,不僅可以創建進程或者線程,還可以指定創建新的命名空間(namespace)、有選擇的繼承父進程的內存、甚至可以將創建出來的進程變成父進程的兄弟進程等等。clone和fork的調用方式也很不相同,clone調用需要傳入一個函數,該函數在子進程中執行。此外,clone和fork最大不同在於clone不再復制父進程的棧空間,而是自己創建一個新的。
關於Linux命令的介紹,看看《linux就該這么學》,具體關於這一章地址3w(dot)linuxprobe/chapter-02(dot)html

閱讀全文

與linuxvfork相關的資料

熱點內容
megawin單片機 瀏覽:685
以色列加密貨幣監督 瀏覽:907
程序員前端現在怎麼樣 瀏覽:497
伺服器和介面地址ping不通 瀏覽:555
linux命令返回上級目錄 瀏覽:897
移動花卡寶藏版為什麼不能選免流app 瀏覽:255
速騰carplay怎麼用安卓 瀏覽:13
紅塔銀行app怎麼樣 瀏覽:564
農行app怎麼開網銀 瀏覽:651
java迭代器遍歷 瀏覽:303
閩政通無法請求伺服器是什麼 瀏覽:48
怎麼做積木解壓神器 瀏覽:205
王者榮耀解壓玩具抽獎 瀏覽:49
12位是由啥加密的 瀏覽:871
程序員編迷你世界代碼 瀏覽:898
php取現在時間 瀏覽:248
單片機高吸收 瀏覽:430
怎麼區分五代頭是不是加密噴頭 瀏覽:247
hunt測試伺服器是什麼意思 瀏覽:511
2013程序員考試 瀏覽:641