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;
}

github源代码

例子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;
}

github源代码

例子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;
}

github源代码

编译时需要加 -pthread

g++ -g process-5-thread.cpp -std=c++11 -pthread

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

c/c++ linux 进程 fork wait函数的更多相关文章

  1. Linux进程的创建函数fork()及其fork内核实现解析【转】

    转自:http://www.cnblogs.com/zengyiwen/p/5755193.html 进程的创建之fork() Linux系统下,进程可以调用fork函数来创建新的进程.调用进程为父进 ...

  2. Linux进程的创建函数fork()及其fork内核实现解析

    进程的创建之fork() Linux系统下,进程可以调用fork函数来创建新的进程.调用进程为父进程,被创建的进程为子进程. fork函数的接口定义如下: #include <unistd.h& ...

  3. linux创建进程fork的方法步骤

    fork创建进程 函数原型如下 #include// 必须引入头文件,使用fork函数的时候,必须包含这个头文件,否则,系统找不到fork函数 pid_t fork(void); //void代表没有 ...

  4. linux进程编程:子进程创建及执行函数简介

    linux进程编程:子进程创建及执行函数简介 子进程创建及执行函数有三个: (1)fork();(2)exec();(3)system();    下面分别做详细介绍.(1)fork()    函数定 ...

  5. 【Linux下进程机制】从一道面试题谈linux下fork的运行机制

    今天一位朋友去一个不错的外企面试linux开发职位,面试官出了一个如下的题目: 给出如下C程序,在linux下使用gcc编译: #include "stdio.h" #includ ...

  6. Linux环境fork()函数详解

    Linux环境fork()函数详解 引言 先来看一段代码吧, 1 #include <sys/types.h> 2 #include <unistd.h> 3 #include ...

  7. system()、exec()、fork()三个与进程有关的函数的比较

    启动新进程(system函数) system()函数可以启动一个新的进程. int system (const char *string ) 这个函数的效果就相当于执行sh –c string. 一般 ...

  8. [转帖]Linux下fork函数及pthread函数的总结

    Linux下fork函数及pthread函数的总结 https://blog.csdn.net/wangdd_199326/article/details/76180514 fork Linux多进程 ...

  9. [转帖]system()、exec()、fork()三个与进程有关的函数的比较

    system().exec().fork()三个与进程有关的函数的比较 https://www.cnblogs.com/qingergege/p/6601807.html 启动新进程(system函数 ...

随机推荐

  1. 呵呵,Python操作MSSQL的帮助类

    从网上找的,估计原文是:Python操作SQLServer示例 本文主要是Python操作SQLServer示例,包括执行查询及更新操作(写入中文). 需要注意的是:读取数据的时候需要decode(' ...

  2. Echarts图标自适应问题(已解决)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 基础才是重中之重~Dictionary<K,V>里V的设计决定的性能

    回到目录 字典对象Dictionary<K,V>我们经常会用到,而在大数据环境下,字典使用不当可能引起性能问题,严重的可能引起内在的溢出! 字典的值建议为简单类型,反正使用Tuple< ...

  4. 从jvm角度看懂类初始化、方法重写、重载。

    类初始化 在讲类的初始化之前,我们先来大概了解一下类的声明周期.如下图 类的声明周期可以分为7个阶段,但今天我们只讲初始化阶段.我们我觉得出来使用和卸载阶段外,初始化阶段是最贴近我们平时学的,也是笔试 ...

  5. Chapter 5 Blood Type——26

    "I saw his face — I could tell." “我看到他的脸了 —— 我知道.” "How did you see me? I thought you ...

  6. 痞子衡嵌入式:ARM Cortex-M内核那些事(4)- 性能指标

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是ARM Cortex-M性能指标. 1.处理器的性能指标 用于评价CPU的性能指标非常多,不同的性能侧重点下的测试标准可能得出的指标值不 ...

  7. 史上最走心webpack4.0中级教程——配置之外你应该知道的事

    <webpack4.0各个击破系列>适合不满足于只会配置webpack但一时间又看不懂源码的中级读者.我没法保证这个系列是最好的,但至少能保证每一篇博文都跟那些Ctrl+C和Ctrl+V的 ...

  8. 第31章 日志 - Identity Server 4 中文文档(v1.0.0)

    IdentityServer使用ASP.NET Core提供的标准日志记录工具.Microsoft文档有一个很好的介绍和内置日志记录提供程序的描述. 我们大致遵循Microsoft使用日志级别的指导原 ...

  9. linux yum配置代理

    yum里面可以单独设置代理就是yum源的参数加proxy=“http://ip:PORT”即在/etc/yum.conf中加入下面几句.proxy=http://210.45.72.XX:808pro ...

  10. 做一个开源的小程序登录模块组件(token)

    先了解下SSO 对于单点登陆浅显一点的说就是两种,一种web端的基于Cookie.另一种是跨端的基于Token,一般想要做的都优先做Token吧,个人建议,因为后期扩展也方便哦. 小程序也是呢,做成t ...