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 - 命令式编程与符号编程

    原文链接:https://zh.d2l.ai/chapter_computational-performance/hybridize.html本文是对原文内容的摘取和扩展. 命令式编程(imperat ...

  2. iOS模拟器使用

    在iOS开发过程中一直都是使用模拟器进行调试,在模拟器上有很多不适应的地方,但是其实在模拟器上也有很多其他的功能,在本文中主要对模拟器的一些基本功能进行总结一下. 1 首先,我们了解一下模拟器中常用的 ...

  3. mysql之delete语法

    一:DELETE语法 以往用delect删除表数据是都是单表(一个表)删除.对于关联表,往往都是先删除第一个表的数据,然后再写另一个delect语句删除另一个表的数据(浪费时间,又影响性能,与数据库交 ...

  4. http初探

    http超文本传输协议 一.版本差异: 版本分0.9   1.0   1.1    2.0 http0.9/1.0已经过时:目前主要是1.1版本的,2.0版本的还没普及. http1.1 ----在同 ...

  5. J2EE-tomcat的配置

    修改web.xml文件里面的内容:  路径:D:\software\apache-tomcat-8.0.44\webapps\ROOT\WEB-INF\web.xml: 内容:<?xml ver ...

  6. Docker系列教程02-MongoDB默认开启鉴权

    说明,我这里使用的是compose的版本的1.17.0格式是3,但是这和compose版本无关,你只需要添加MONGO_INITDB_ROOT_USERNAME和MONGO_INITDB_ROOT_P ...

  7. 设置radio选中

    选中: $('.viewradio:input[name="istop"][value="' + getSelected().istop + '"]').pro ...

  8. SmartSql 类型处理器

    Nuget 安装 Install-Package SmartSql.TypeHandler -Version 3.0.1 SmartSql.TypeHandler 包括了俩种类型的类型处理程序: Js ...

  9. 通过 Ansible 安装 Docker

    本文的演示环境为 ubuntu 16.04. 先在 Ansible Galaxy 搜索 docker,由 geerlingguy 贡献的 docker role 是目前最受欢迎的: 通过 ansibl ...

  10. Servlet主要相关类核心类 容器调用的过程浅析 servlet解读 怎么调用 Servlet是什么 工作机制

      WEB简介   Web项目 是 B/S结构 浏览器/服务器模式的 浏览器发起请求,服务器作出响应   请求的发起和响应使用HTTP协议进行通讯 所谓协议也就是一种固定格式   而Socket是应用 ...