日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux fork多进程 demo

發布時間:2023/12/13 linux 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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; }

?

總結

以上是生活随笔為你收集整理的linux fork多进程 demo的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。