linux c++(进程相关的函数 & 第一篇 fork)
2021/5/5 7:28:57
本文主要是介绍linux c++(进程相关的函数 & 第一篇 fork),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
进程相关的函数
创建一个新的线程 pid_t fork(void); * 失败 -1 * 成功,两次返回 父进程返回,子进程的id 子进程返回0 pid_t getpid(void); //获得当前进程的 pid_t getppid(void); //获得当前进程父进程的id #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main() { printf("Begin ....\n"); pid_t pid = fork(); if(pid < 0) { perror("fork err"); return -1; }else if(pid == 0) { //子进程 printf("I am child,pid = %d,ppid=%d\n",getpid(),getppid()); while(1) { printf("I am child\n"); sleep(1); } }else if(pid > 0) { //父进程的逻辑 printf("childpid = %d,self = %d,ppid=%d\n",pid,getpid(),getppid()); //父进程会比子进程死的早 while(1) {} } printf("End ....\n"); return 0; }
这篇关于linux c++(进程相关的函数 & 第一篇 fork)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-0600-macOS和Linux安装和管理多个Python版本
- 2024-03-30[译]漫画SELinux概念
- 2024-03-29linux 移动文件
- 2024-03-28linux .so file
- 2024-03-28Linux 磁盘管理
- 2024-03-28Linux学习笔记(十三)磁盘管理(一):磁盘分区
- 2024-03-26linux 创建 文件
- 2024-03-25使用SecureCRT对Linux vim进行颜色设置
- 2024-03-202019-2020-12 20199317 《Linux内核原理与分析》 第十二周作业
- 2024-03-20Linux运维的第二周总结