Linux下C語言驗證多程式
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
pid_t pid1;
pid_t pid2;
cout<<"main: pid1="<<pid1<<"; pid2="<<pid2<<endl;
pid1=fork();
cout<<"pid1= "<<pid1<<endl;
cout<<"pid2= "<<pid2<<endl;
cout<<"the current process is: "<<getpid()<<" the parent process is: "<<getppid()<<endl;
return 0;
}
上面是父程式的執行結果
下面是子程式的執行結果。
fork功能
建立一個子程式,父子程式併發執行
子程式複製父程式的如下屬性
程式碼段、資料段的內容,父子程式擁有相同的程式碼和資料
開啟檔案列表
不復制程式的PID屬性
fork返回值
父程式從fork返回處繼續執行,在父程式中,fork返回子程式PID
子程式從fork返回處開始執行,在子程式中,fork返回0
getpid()得到當前程式號
getppid()得到父程式程式號
參考:
相關文章
- 【C語言】linux下多檔案編譯C語言Linux編譯
- Linux下C語言程式設計(轉)LinuxC語言程式設計
- Linux下C語言程式設計簡介(轉)LinuxC語言程式設計
- 利用C語言驗證哥德巴赫猜想C語言
- c語言程式實驗——實驗報告五C語言
- c語言程式實驗————實驗報告十C語言
- c語言程式實驗————實驗報告十二C語言
- Linux下C語言程式設計基礎知識LinuxC語言程式設計
- linux下傳送email的c語言程式碼(轉)LinuxAIC語言
- 聊聊C語言/C++—程式和程式語言C語言C++
- Linux下C語言程式設計基礎知識(轉)LinuxC語言程式設計
- linux下使用mysql的C語言APILinuxMySqlC語言API
- C語言實驗1C語言
- C語言實驗二C語言
- C語言程式C語言
- 用C語言在Linux系統下建立守護程式(Daemon)C語言Linux
- Linux下關於時間概念的C語言程式設計LinuxC語言程式設計
- linux平臺下C語言按程式名查詢程式號pidLinuxC語言
- c語言程式設計--實驗報告一C語言程式設計
- c語言程式設計——實驗報告六C語言程式設計
- # c語言程式設計——實驗報告一C語言程式設計
- c語言程式設計——實驗報告一C語言程式設計
- c語言程式設計——實驗報告二C語言程式設計
- C語言程式設計—實驗報告四C語言程式設計
- C語言程式設計-實驗報告4C語言程式設計
- Linux下跨語言呼叫C++實踐LinuxC++
- Linux下C語言編譯的問題LinuxC語言編譯
- C語言 之 多執行緒程式設計C語言執行緒程式設計
- C語言實驗作業C語言
- Linux-C語言LinuxC語言
- linux下c語言學習筆記——操作mysqlLinuxC語言筆記MySql
- go語言簽發和驗證licenseGo
- windows下ping程式使用C語言實現WindowsC語言
- linux作業系統下c語言程式設計入門(1)(轉)Linux作業系統C語言程式設計
- linux作業系統下c語言程式設計入門(2)(轉)Linux作業系統C語言程式設計
- c語言多檔案編譯C語言編譯
- C語言呼叫 Java(Linux)C語言JavaLinux
- 實驗3 C語言函式應用程式設計C語言函式程式設計