当前位置: 首页 > news >正文

Linux函数学习 fork

1、Linux fork 函数

pid_t fork(void);

pid_t : 对于子进程,返回0

pid_t : 对于父进程进程,返回子进程进程号

int pipe(int pipefd[2]);

 pipefd[0] 为读取管道

 pipefd[1] 为写入管道

返回值:-1失败   0 成功

2、函数实例

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>int main (int argc, char ** argv){int pipefd[2];int ret = 0;pid_t cpid;char buf;if(argc != 2) {printf("Usage pipe <command> \n");_exit(EXIT_SUCCESS);} else {printf("argv[1] = %s \n", argv[1]);}ret =  pipe(pipefd);if(ret == -1){perror("fork error \n");} else {printf("create pipe success !\n");}cpid = fork();	if (cpid == -1) {perror("fork error \n");}printf("pid1 = %d \n", getpid());if (cpid == 0) {	/*子进程从管道读取 */printf("child pid2 = %d cpid = %d \n", getpid(), cpid);/* 关闭不使用的写管道 */close(pipefd[1]);/*子进程读取管道数据,并输出打印*/while (read(pipefd[0], &buf, 1) > 0){write(STDOUT_FILENO, &buf, 1);}write(STDOUT_FILENO, "\n", 1);/*关闭管道*/close(pipefd[0]);printf("child pid2 = %d cpid = %d \n", getpid(), cpid);_exit(EXIT_SUCCESS);} else {printf("parent pid3 = %d cpid = %d \n", getpid(), cpid);/* 关闭不使用的读管道 */close(pipefd[0]);/* 父进程写arg[1]到管道 */write(pipefd[1], argv[1], strlen(argv[1]));close(pipefd[1]);/*等待子进程*/wait(NULL);printf("parent pid3 = %d \n", getpid());exit(EXIT_SUCCESS);}return 0;
}

3、代码运行效果

编译二进制文件,运行到手机端

相关文章:

  • RabbitMQ3.13.x之六_RabbitMQ使用场景
  • 【算法-数组】二分查找法
  • (27)4.8 习题课
  • 深入浅出 -- 系统架构之微服务架构选型参考图
  • 数字化智慧养老:引领老年人融入科技时代新生活
  • Ant Design Vue 表单验证手机号的正则
  • c#编程基础学习之方法
  • maven: 标签总结
  • 雄安建博会:中矿雄安新区的总部开工建设
  • Windows下docker-compose部署DolphinScheduler
  • 【原创】springboot+vue校园疫情防控管理系统设计与实现
  • 终极数据传输隐秘通道
  • 【鸿蒙开发】组件状态管理@Prop,@Link,@Provide,@Consume,@Observed,@ObjectLink
  • openssl3.2 - exp - zlib
  • 三种常见webshell工具的流量特征分析
  • Android优雅地处理按钮重复点击
  • Electron入门介绍
  • HTTP 简介
  • Java的Interrupt与线程中断
  • Java-详解HashMap
  • Python 基础起步 (十) 什么叫函数?
  • 从零开始的无人驾驶 1
  • 对超线程几个不同角度的解释
  • 分类模型——Logistics Regression
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 实战|智能家居行业移动应用性能分析
  • 算法之不定期更新(一)(2018-04-12)
  • 一起来学SpringBoot | 第三篇:SpringBoot日志配置
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • Salesforce和SAP Netweaver里数据库表的元数据设计
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • ​【已解决】npm install​卡主不动的情况
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • #DBA杂记1
  • #define、const、typedef的差别
  • #stm32整理(一)flash读写
  • #微信小程序:微信小程序常见的配置传值
  • (delphi11最新学习资料) Object Pascal 学习笔记---第5章第5节(delphi中的指针)
  • (JS基础)String 类型
  • (搬运以学习)flask 上下文的实现
  • (附源码)spring boot网络空间安全实验教学示范中心网站 毕业设计 111454
  • (附源码)springboot家庭装修管理系统 毕业设计 613205
  • (黑马出品_高级篇_01)SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
  • (五)关系数据库标准语言SQL
  • (一)python发送HTTP 请求的两种方式(get和post )
  • (一)RocketMQ初步认识
  • (原創) 未来三学期想要修的课 (日記)
  • (转)GCC在C语言中内嵌汇编 asm __volatile__
  • (转)jdk与jre的区别
  • (转)mysql使用Navicat 导出和导入数据库
  • (转)shell中括号的特殊用法 linux if多条件判断
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • .bat批处理(十一):替换字符串中包含百分号%的子串