linux fork多進程 demo

注釋

  • 使用系統調用fork()創建三個子進程;
  • 各個子進程顯示和輸出一些提示信息和自己的進程標識符;
  • 父進程顯示自己的進程ID和一些提示信息,然后調用waitpid()等待多個子進程結束,并在子進程結束后顯示輸出提示信息表示程序結束。

代碼

#include <iostream>
#include <unistd.h>
#include <cstdarg>
#include <wait.h>
#include "crypto_util.h"int get_file(const std::string file_name){size_t get_file_id = 0;std::cout << hsm::common::get_md5_digest_hex(file_name) << std::endl;get_file_id = strtol(reinterpret_cast<const char *>(hsm::common::get_md5_digest_hex(file_name).substr(0,7).c_str()), nullptr, 16);return get_file_id;
}
int tprintf(const char *fmt,...){va_list args;struct tm* t_struct;time_t t_sec;t_sec = time(nullptr);t_struct = localtime(&t_sec);printf("%02d:%02d:%02d:%5d|",t_struct->tm_hour,t_struct->tm_min,t_struct->tm_sec,getpid());va_start(args,fmt);return vprintf(fmt,args);
}
int main(void) {std::cout << "Start!" << std::endl;printf("Father progress,PID is %d.\n",getpid());pid_t pid1 = fork();if(pid1 == 0){printf("Children progress 1,PID is %d.\n",getpid());exit(1);printf("Children progress 1 over.\n");}pid_t pid2 = fork();if(pid2 == 0){printf("Children progress 2,PID is %d.\n",getpid());exit(1);printf("Children progress 1 over.\n");}pid_t pid3 = fork();if(pid3 == 0){printf("Children progress 3,PID is %d.\n",getpid());exit(1);printf("Children progress 1 over.\n");}else if (pid1 != -1 ){tprintf("Parent forked children process-- %d.\n",pid1);tprintf("Parent is waiting for child to exit.\n");waitpid(pid1,NULL,0);waitpid(pid2,NULL,0);waitpid(pid3,NULL,0);tprintf("Child Process had exited.\n");tprintf("Parent had exited.\n");} else{tprintf("Everything was done without error.\n");}return 0;
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/446795.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/446795.shtml
英文地址,請注明出處:http://en.pswp.cn/news/446795.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Android WebView 與 JS 交互

目錄二、具體分析2.1 Android通過WebView調用 JS 代碼方式1&#xff1a;通過WebView的loadUrl()方式2&#xff1a;通過WebView的evaluateJavascript()方法對比使用建議2.2、JS通過WebView調用 Android 代碼2.2.1、方法分析方式1&#xff1a;通過 WebView的addJavascriptInterfa…

關于鎖的注意事項

文件鎖 Linux 提供了 fcntl 系統調用&#xff0c;可以鎖定文件但是文件鎖是和進程相關聯的&#xff0c;一個進程中的多個線程/協程對同一個文件進行的鎖操作會互相覆蓋掉&#xff0c;從而無效。fcntl 創建的鎖是建議性鎖&#xff0c;只有寫入的進程和讀取的進程都遵循建議才有效…

安卓實現登錄與注冊界面

使用Intent與Bundle傳遞數據 登錄界面login.xml 1.使用Relativelayout相對布局 <?xml version"1.0" encoding"utf-8"?> <RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"mat…

Android Button字母自動全部大寫的問題

兩種解決方案&#xff1a; 方法一&#xff1a; 在 xml 布局中設置屬性 android:textAllCaps"false" <Buttonandroid:layout_width"wrap_content"android:layout_height"match_parent"android:text"添加動作組"android:textAllCap…

安卓Activity與intent跳轉

Activity生命周期 Activity啟動模式 Intent跳轉 _________startActivity() 1.Intent intentnew Intent(A.this,B.class); startActivity(intent); 2.startActivity(new Intent(A.this,B.class)); _________startActivityForResult() Intent intentnew Intent(A.this,B.class…

將讀寫鎖放到共享內存中,實現進程之間對數據的讀寫訪問控制

代碼 #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <assert.h> #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <fstream> #include <…

Android WebView 使用漏洞

目錄一、類型二、具體分析2.1、WebView任意代碼執行漏洞2.1.1、addJavascriptInterface 接口引起遠程代碼執行漏洞漏洞產生原因解決方案關于該方法的其他細節總結2.1.2、searchBoxJavaBridge_接口引起遠程代碼執行漏洞漏洞產生原因解決方案2.1.3、accessibility和 accessibilit…

將讀寫鎖放到共享內存,實現進程之間對于同一文件的讀寫操作

思路 將讀寫鎖和讀寫鎖的屬性以及一個用于存儲共享內存的地址的int型變量三者封裝成一個struct結構將這個結構體放到共享內存中&#xff0c;以及將讀寫鎖的屬性設置成全局性質&#xff0c;然后使用這個屬性初始化鎖&#xff0c;以及將鎖的地址關聯到結構體的內存地址這個變量定…

Android Studio 查看頁面布局層次結構

Android Studio有個可以查看手機上app頁面布局層次結構的工具。可以協助我們對布局進行優化&#xff0c;去掉沒有必要的節點等&#xff0c;通過這個工具可以清晰的看見頁面整個結構&#xff1b;廢話少說直接上圖&#xff0c;再說過程。 這就是我們想要看到的&#xff0c;每個節…

Java web后端 第一章框架搭建

Redis 通用Mapper 通用Mapper->MyBatis動態SQL封裝包,增刪改查 0 SQL語句 PageHelper PageHelper–>實現分頁操作,不需要limit,直接使用靜態方法 電商系統技術特點 分布式(數據很多,一臺電腦存儲一部分數據) 高并發,集群(并發量很高,后臺不只一個電腦) ,海量數據 主…

進程鎖 讀寫文件的小例子 C++代碼

代碼 #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <cassert> #include <pthread.h> #include <cstdio> #include <cstdlib> #include <fstream> #include <io…

Java 中sleep()與wait()的區別

目錄一、原理不同二、鎖的處理機制不同三、使用的區域不同四、異常捕獲不同五、總結一、原理不同 sleep()是Thread類的靜態方法&#xff0c;是線程用來控制自身流程的&#xff0c;它會使此線程暫停執行指定的時間&#xff0c;而把執行機會讓給其他的線程&#xff0c;等到計時時…

android--地圖定位打卡

獲取位置信息 1)位置信息 GPS衛星定位,在室外適用 基站(3個基站交叉,鎖定手機位置)–基站定位不平均,有些地方實現不了3點定位 網絡定位–通過手機IP地址,去鎖定位置(消耗流量,對網絡有要求) 谷歌地圖的大致實現思路(通用) 2)實現定位功能的重要類 在百度地圖和高德地圖中不…

Android 將整形顏色值轉換成String類型

轉換方法&#xff1a; val hexColor String.format("#%06X", [0xFFFFFF or intColor]);轉換結果&#xff1a; #F2EADA

MacOS 的 zsh 和 bash 切換

目錄一、從 bash 切換到 zsh1、使用系統自帶的 zsh2、使用第三方的 zsh2.1、Clone代碼到本地2.2、備份你已存在的 ~/.zshrc 文件2.3、新建一份新的 zsh 配置文件2.4、改變默認的shell腳本二、從 zsh 切換回 bash三、zsh 和 bash 的環境變量zsh、bash 都是shell&#xff0c;zsh …

android--在命令行中生成Android的數字證書keystore文件

標題 生成 密鑰口令為 13458977480 密鑰庫口令為 13458977480 存放位置 查看證書的相關資料

linux查看系統日志

cd /var/log/gscubuntu:/var/log$ tail -f syslog

IDEA 創建 SpringBoot 項目

目錄一、新建Springboot項目第一步&#xff1a;新建一個Springboot項目第二步&#xff1a;選擇項目模板第三步&#xff1a;設置項目配置第四步&#xff1a;設置項目依賴第五步&#xff1a;設置項目名稱及路徑第六步&#xff1a;創建完成二、測試及運行1、測試代碼2、設置默認端…

VC++軟件

一個main fatal error LNK1169: 找到一個或多個多重定義的符號–報錯 一個項目即一個程序&#xff0c;多個文件只能有一個main函數 刪除掉多余的main 控制臺按enter鍵閃退 在代碼中加上 #include<stdlib.h> getchar();//讓控制臺停留 system("pause");//讓…