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相关推荐
- linux 进程sockfd fork,Linux下多进程服务端客户端模型一(单进程与多进程模型)...
本文将会简单介绍Linux下如何利用C库函数与系统调用编写一个完整的.初级可用的C-S模型. 一.基本模型: 1.1 首先服务器调用socket()函数建立一个套接字,然后bind()端口,开始l ...
- Unix/Linux fork后传-clone
来自<Fork三部曲之clone的诞生>.本文fork三部曲的后传,建议先阅读: 正传:<Linux fork那些隐藏的开销> 前传:<Unix/Linux fork前传 ...
- linux fork 函数,Linux的fork()系统调用
Linux的fork()系统调用,就是以父进程为模版创建子进程,是Linux系统的进程管理机制的核心API之一,另一个是调度器函数schedule(),它的用户态API就是之前说自旋锁时提到的sche ...
- linux编写多进程程序实验,实验7 编写多进程程序
实验七编写多进程程序 学生姓名:李亚军学号:6100412196 专业班级:卓越计科121班 1.实验目的 通过编写多进程程序,使读者熟练掌握fork().exec().wait()和waitpid( ...
- linux下多进程编程简介
两年前的文章,拿过来充充门面. ------------------------ linux下多进程编程简介 ( 作者:mikespook | 发布日期:2002-12-8 | 浏览次数:272 ) ...
- php linux fork进程 多个进程/线程共享一个 socket连接 出现多个进程响应串联
简单说来就是:因为fork,父子进程共享了一个redis连接.然后父子进程在发送了各自的redis请求分别获取到了对方的响应体. 复现示例代码: testFork.php <?php requi ...
- linux 下 多进程与多线程
[Linux]多进程与多线程之间的区别 http://blog.csdn.net/byrsongqq/article/details/6339240 网络编程中设计并发服务器,使用多进程与多线程 ,请 ...
- Linux fork隐藏的开销-过时的fork(正传)
本文来自<Linux fork那些隐藏的开销> fork是一个拥有50年历史的陈年系统调用,它是一个传奇!时至今日,它依旧灿烂. 一个程序员可以永远不用read/write,也可以不懂mm ...
- Unix/Linux fork/exec的前世今生
本文是<Linux fork那些隐藏的开销>的前传<Unix/Linix fork前传>.转载注明来自公众号"Linux阅码场". 昨天(好像是上周的事了, ...
最新文章
- python2x NameError: name ‘abc‘ is not defined
- 在pycharm中通过VCS快速提交代码
- k8s使用glusterfs存储报错type 'features/utime'
- Windows文件夹、文件源代码对比工具--WinMerge
- java ArrayList排序
- java getselecteditem_Java JComboBox.getSelectedItem方法代碼示例
- 计算机语言平均数怎么算,使用python怎么求三个数的平均值
- 学生机房管理服务器系统设计,广东工业大学数据库课程设计机房管理系统设计...
- mysql安装完只有黑框_MySQLMySQL的安装及安装中存在的问题
- java10个整数反向输出_输入一个整数,实现反转输出,如输入123,输出321。
- 工作总结 @{var sas = String.Format({0:yyyy-MM-dd}, Model.DemandTime.GetValueOrDefault());}
- 图像语义分析--深度学习方法
- css各种字体英文名称
- VUE生成二维码或条形码
- 短信平台系统 后台功能简介 网页版短信通道介绍 短信平台源代码搭建
- 苹果连接电脑 计算机不显示硬盘,苹果连接电脑没反应怎么办?苹果连接电脑没反应解决方法...
- lvm扩容lv扩容操作记录
- 蓝湖能导入html文件么,axure怎么导入蓝湖
- 智能合约升级原理01---起源
- u盘出现该项目不在计算机中怎么办,怎么解决U盘使用过程中出现的各种问题
热门文章
- Java代码服务器上下载图片_Java如何从服务器中下载图片
- 群晖docker签到京豆_利用Synology(群晖)Docker安装签到框架
- 【转】DICOM医学图像读取涉及到的医学坐标体系
- 【转】C++中#include包含头文件带 .h 和不带 .h 的区别
- 【转】人工智能教程-前言
- 一步步编写操作系统 31 cpu的分支预测 下
- python seaborn 热图 值对应颜色_基于行值的seaborn热图配色方案
- mysql 更新错误1062_mysql 出现1062错误怎么办
- CCNA-第十三篇-NAT-上
- 【Linux学习】常用指令-sortunique