導航:首頁 > 源碼編譯 > pidc語言源碼

pidc語言源碼

發布時間:2024-11-21 15:48:37

A. 編寫一段c語言程序,使進程1創建出2 3 4 子進程,子進程3創建孫進程5 ,5在創建出重孫6

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
int pid1, pid2, pid3, pid4, pid5, pid6;

pid1 = getpid();
printf("PID = %d\n", pid1);

pid2 = fork();
if (pid2 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(30);
exit(0);
}

pid3 = fork();
if (pid3 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());

pid5 = fork();
if (pid5 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());

pid6 = fork();
if (pid6 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(30);
exit(0);
}

sleep(30);
exit(0);
}

sleep(30);
exit(0);
}

pid4 = fork();
if (pid4 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(30);
exit(0);
}

sleep(30);
return 0;
}
每一個進程都會在退出前 sleep 30秒,從而保證能夠用 ps 看到,
編譯 gcc testpid.c -o testpid
然後執行,可以看到
PID = 24913
PID = 24914, Parent PID = 24913
PID = 24916, Parent PID = 24913
PID = 24915, Parent PID = 24913
PID = 24917, Parent PID = 24915
PID = 24918, Parent PID = 24917

ps -ef 的結果
24913 24582 0 11:29 pts/19 00:00:00 ./testpid
24914 24913 0 11:29 pts/19 00:00:00 ./testpid
24915 24913 0 11:29 pts/19 00:00:00 ./testpid
24916 24913 0 11:29 pts/19 00:00:00 ./testpid
24917 24915 0 11:29 pts/19 00:00:00 ./testpid
24918 24917 0 11:29 pts/19 00:00:00 ./testpid

閱讀全文

與pidc語言源碼相關的資料

熱點內容
什麼app零粉分發視頻有收益 瀏覽:162
肯亞程序員 瀏覽:638
新科源碼 瀏覽:659
如何判斷伺服器有沒有帶寬 瀏覽:41
天正建築批量刪除命令 瀏覽:94
cad最下面的一排命令都什麼意思 瀏覽:456
pythonimportcpp 瀏覽:850
W10的系統怎麼給U盤加密 瀏覽:370
華為手機代碼編程教學入門 瀏覽:762
和彩雲沒會員怎樣解壓 瀏覽:634
androidimageview保存 瀏覽:387
新買店鋪什麼伺服器 瀏覽:883
文件夾能直接刻錄嗎 瀏覽:493
androidxmpp刪除好友 瀏覽:969
javac哪個前景好 瀏覽:428
中華英才網app為什麼不能搜索了 瀏覽:660
伺服器域名是什麼意思 瀏覽:52
Linux導出mysql命令 瀏覽:159
無詐建鄴是什麼app 瀏覽:229
python中的雙色球 瀏覽:168