作者Keitaro (動き出す時間...)
看板C_and_CPP
標題[問題] 請教fork/exec關於memory release
時間Wed Oct 14 18:04:02 2020
開發平台(Platform): (Ex: Win10, Linux, ...)
Linux
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
gcc
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
無
問題(Question):
請教各位 假設我要在linux底下執行CreateProcess的動作
如底下程式碼所示
如果fork產生的child process在exec之前還是跑從
parent process拷貝而來的程式碼
exec之後就跑要執行的程式了
child process應該也有new了一份char array
那為何child process不需要delete[] p呢?
我這個char array是要傳給exec的參數(把p_sArg/p_sEnv做處理然後傳送)
在底下的example就省略parser argument的部分
只用個p示意而已
感謝各位先進的指導
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
example:
void CreateProcess(char* p_sAppName, char* p_sArg, char* p_sEnv)
{
char *p = new char[10];
int pid = fork();
......
......
if (pid == 0)
{
exec(...);
}
else if (pid > 0)
{
waitpid(pid , 0, 0);
}
else
{
printf("error");
}
delete[] p;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.242.38.175 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1602669844.A.CAF.html
推 b0920075: kernel 會幫你處理好,沒差,反正等等execve 就會把記10/14 19:09
→ b0920075: 憶體內容洗成新的 process 要用的10/14 19:09
感謝說明
※ 編輯: Keitaro (223.137.137.101 臺灣), 10/14/2020 20:02:04
※ 編輯: Keitaro (223.137.137.101 臺灣), 10/14/2020 20:03:09
→ ucrxzero: fter-malloc-in-parent-does-the-child-process-need-t 10/15 10:34
→ ucrxzero: o-free-it/23440176 10/15 10:34
→ ucrxzero: 這篇說exec要 execvp不用 10/15 10:35
推 LPH66: 樓上應該漏看了敘述, 他是說如果沒有呼叫 exec* 系列函數 10/15 10:48
→ LPH66: 的話那就需要, 而如果呼叫了 exec* 系列 (如 execvp) 就免 10/15 10:48
→ LPH66: (exec* 系列底層都會歸到 execve 系統呼叫所以可以一起看) 10/15 10:49
瞭解了 非常感謝以上幾位版友
※ 編輯: Keitaro (60.251.156.103 臺灣), 10/15/2020 13:11:24