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 16.04 更改默认Python版本
一般Ubuntu默认的Python版本都为2.x, 如何改变Python的默认版本呢?假设我们需要把Python3.5设置为默认版本: 首先查看Python默认版本: ubuntu@user~$:py ...
- 面试前必知Redis面试题—缓存雪崩+穿透+缓存与数据库双写一致问题
今天来分享一下Redis几道常见的面试题: 如何解决缓存雪崩? 如何解决缓存穿透? 如何保证缓存与数据库双写时一致的问题? 一.缓存雪崩 1.1什么是缓存雪崩? 回顾一下我们为什么要用缓存(Redis ...
- 2.Django路由规则
路由规则 1.基于正则的url 在templates目录下创建index.html.detail.html文件 (1)index.html <!DOCTYPE html> <html ...
- Object Pooling(对象池)实现
在文章开始之前首先要思考的问题是为什么要建立对象池.这和.NET垃圾回收机制有关,正如下面引用所说,内存不是无限的,垃圾回收器最终要回收对象,释放内存.尽管.NET为垃圾回收已经进行了大量优化,例如将 ...
- Python爬虫入门教程 15-100 石家庄政民互动数据爬取
石家庄政民互动数据爬取-写在前面 今天,咱抓取一个网站,这个网站呢,涉及的内容就是 网友留言和回复,特别简单,但是网站是gov的.网址为 http://www.sjz.gov.cn/col/14900 ...
- 使用dotnet build时复制引用dll到生成目录
默认配置下dotnet build只会输出项目代码的dll,依赖用的是dotnet缓存中的dll,只有dotnet publish才会把依赖的dll一起输出到生成目录. 在项目csproj文件中添加以 ...
- Android 解压zip文件(支持中文)
过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...
- Win 7 家庭普通版系统升级密钥
VQB3X-Q3KP8-WJ2H8-R6B6D-7QJB7 (高级版)FJGCP-4DFJD-GJY49-VJBQ7-HYRR2 (旗舰版)要先升级到高级版再升级旗舰版,不然(可能)会出错.
- SmartSql For Asp.Net Core 最佳实践
常规操作 安装 SmartSql Install-Package SmartSql 安装 SmartSql.DIExtension Install-Package SmartSql.DIExtensi ...
- Oracle学习笔记四
一.PL/SQL编程 游标(光标Cursor) 为什么使用游标 在写java程序中有集合的概念,那么在pl/sq中也会用到多条记录,这时候我们就要用到游标,游标可以存储查询返回的多条数据. 语法: C ...