c/c++ linux 进程 fork wait函数
linux 进程 fork wait函数
fork:创建子进程
wait:父进程等待子进程结束,并销毁子进程,如果父进程不调用wait函数,子进程就会一直留在linux内核中,变成了僵尸进程。
fork函数的详细说明:fork
wait函数详细说明参考:wait
例子1:不注释掉exit(0)的话,子进程不会执行到printf("end pid: %d\n", getpid());这行。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <wait.h>//wait function
int main(){
pid_t pid;
pid = fork();
if(pid == 0){
printf("child process : %d\n", getpid());
//exit(0);
}
else{
int status;
pid_t waitpid;
printf("parent process : childpid=%d , mypid=%d\n",pid, getpid());
waitpid = wait(&status);
printf("waitpid:%d\n", waitpid);
}
printf("end pid: %d\n", getpid());
return 0;
}
例子2:父进程和子进程之间,值是不共有的,你是你的,我是我的。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(){
pid_t pid;
int i = 100;
pid = fork();
if(pid == 0){
printf("child process : %d\n", getpid());
i += 500;
}
else{
printf("parent process : childpid=%d , mypid=%d\n",pid, getpid());
i += 1024;
}
printf("i=%d\n", i);
return 0;
}
例子3:线程之间,值是共有的。
#include <stdio.h>
#include <unistd.h>//sleep function
#include <pthread.h>
int global_val = 0;
void* sub_thread(void *data){
int* val = (int*)data;
printf("sub_thread : val=%d\n", *val);
for(int i = 0; i < 10; ++i){
global_val++;
printf("sub_thread : i=%d, g=%d\n", i, global_val);
sleep(1);
}
return NULL;
}
int main(){
pthread_t th;
void* th_ret;
int arg = 200;
if(pthread_create(&th, NULL, sub_thread, (void*)&arg) != 0){
perror("pthread_create");
return 1;
}
for(int i = 0; i < 10; ++i){
global_val++;
printf("main: i=%d, g=%d\n", i, global_val);
sleep(1);
}
if(pthread_join(th, &th_ret) != 0){
perror("pthread_join");
return 1;
}
return 0;
}
编译时需要加 -pthread
g++ -g process-5-thread.cpp -std=c++11 -pthread
c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854
c/c++ linux 进程 fork wait函数的更多相关文章
- Linux进程的创建函数fork()及其fork内核实现解析【转】
转自:http://www.cnblogs.com/zengyiwen/p/5755193.html 进程的创建之fork() Linux系统下,进程可以调用fork函数来创建新的进程.调用进程为父进 ...
- Linux进程的创建函数fork()及其fork内核实现解析
进程的创建之fork() Linux系统下,进程可以调用fork函数来创建新的进程.调用进程为父进程,被创建的进程为子进程. fork函数的接口定义如下: #include <unistd.h& ...
- linux创建进程fork的方法步骤
fork创建进程 函数原型如下 #include// 必须引入头文件,使用fork函数的时候,必须包含这个头文件,否则,系统找不到fork函数 pid_t fork(void); //void代表没有 ...
- linux进程编程:子进程创建及执行函数简介
linux进程编程:子进程创建及执行函数简介 子进程创建及执行函数有三个: (1)fork();(2)exec();(3)system(); 下面分别做详细介绍.(1)fork() 函数定 ...
- 【Linux下进程机制】从一道面试题谈linux下fork的运行机制
今天一位朋友去一个不错的外企面试linux开发职位,面试官出了一个如下的题目: 给出如下C程序,在linux下使用gcc编译: #include "stdio.h" #includ ...
- Linux环境fork()函数详解
Linux环境fork()函数详解 引言 先来看一段代码吧, 1 #include <sys/types.h> 2 #include <unistd.h> 3 #include ...
- system()、exec()、fork()三个与进程有关的函数的比较
启动新进程(system函数) system()函数可以启动一个新的进程. int system (const char *string ) 这个函数的效果就相当于执行sh –c string. 一般 ...
- [转帖]Linux下fork函数及pthread函数的总结
Linux下fork函数及pthread函数的总结 https://blog.csdn.net/wangdd_199326/article/details/76180514 fork Linux多进程 ...
- [转帖]system()、exec()、fork()三个与进程有关的函数的比较
system().exec().fork()三个与进程有关的函数的比较 https://www.cnblogs.com/qingergege/p/6601807.html 启动新进程(system函数 ...
随机推荐
- ubuntu中环境变量的几个问题思考
问题一:export PATH=$PATH:/usr/local和export PATH=/usr/local:$PATH这两个的区别是什么?可以随便用吗? 这两个都是要把该目录加到环境变量中,一般的 ...
- [Abp 源码分析]四、模块配置
0.简要介绍 在 Abp 框架当中通过各种 Configuration 来实现模块的配置,Abp 本身提供的很多基础设施功能的一些在运行时的行为是通过很多不同的 Configuration 来开放给用 ...
- zabbix系列之九——添加钉钉告警
一.添加钉钉机器人 1. 2. 复制webhook后面脚本用到:https://oapi.dingtalk.com/robot/send?access_token=36e69dd50bbcc54b7b ...
- asp.net core系列 25 EF模型配置(隐藏属性)
一. 隐藏属性概述 隐藏属性也叫影子属性,该属性不是在.net实体类中定义的属性,而是在EFCore模型中为该实体类型定义的属性.这些属性的值和状态完全在变更跟踪器中维护.它有二个功能:(1)当数据库 ...
- Latex文件分别用Texwork和Winedt打开时,产生中文乱码的解决方法
中文兼容方法(能保证编译成功) \usepackage{CJK} \begin{document} \begin{CJK}{GBK}{kai} ... 中文 ... \end{CJK} \end{do ...
- 从零开始学习PYTHON3讲义(三)写第一个程序
<从零开始PYTHON3>第三讲 本页面使用了公式插件,因博客主机过滤无法显示的表示抱歉,并建议至个人主页查看原文. 我见过很多初学者,提到编程都有一种恐惧感,起源是感觉编程太难了.其 ...
- 【Python3爬虫】selenium入门
selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Fire ...
- C#2.0导航
主要特性 泛型 类型和方法的参数化 可空类型 值类型可为null 委托 更简化的方式 迭代器 简单的foreach,不简单的状态机
- 使用MaxCompute Java SDK运行安全相关命令
使用MaxCompute Console的同学,可能都使用过MaxCompute安全相关的命令.官方文档上有详细的MaxCompute安全指南,并给出了安全相关语句汇总. 简而言之,权限管理.列级 ...
- Python并发编程之谈谈线程中的“锁机制”(三)
大家好,并发编程 进入第三篇. 今天我们来讲讲,线程里的锁机制. 本文目录 何为Lock( 锁 )?如何使用Lock( 锁 )?为何要使用锁?可重入锁(RLock)防止死锁的加锁机制饱受争议的GIL( ...