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

无涯教程-进程 - 创建终止

到现在为止,我们知道无论何时执行程序,都会创建一个进程,并且该进程将在执行完成后终止,如果我们需要在程序中创建一个进程,并且可能希望为其安排其他任务,该怎么办。能做到吗?是的,显然是通过进程创建的,当然,工作完成后,它将自动终止,或者您可以根据需要终止它。

进程创建是通过 fork()系统调用实现的,新创建的进程称为子进程,而启动它的进程(或开始执行时的进程)称为父进程。在fork()系统调用之后,现在我们有两个进程-父进程和子进程。如何区分它们?很简单,就是通过它们的返回值。

System Call

创建子进程后,让我们看到fork()系统调用详细信息。

#include <sys/types.h>
#include <unistd.h>pid_t fork(void);

创建子进程,此调用之后,有两个进程,现有的一个称为父进程,而新创建的一个称为子进程。

fork()系统调用返回以下三个值之一:

  • 负值  - 表示错误,即创建子进程失败。

  • 0       - 表示为子进程。

  • 正值 - 表示新创建的子进程的进程ID。

让我们考虑一个简单的程序。

File name: basicfork.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>int main() {fork();printf("Called fork() system call\n");return 0;
}

执行步骤

汇编

gcc basicfork.c -o basicfork

执行/输出

Called fork() system call
Called fork() system call

注意-通常在fork()调用之后,子进程和父进程将执行不同的任务。如果需要运行相同的任务,则对于每个fork()调用,它将运行2次幂n次,其中 n 是fork()被调用的次数。

看到fork()创建了子进程之后,就该查看父进程和子进程的详细信息了。

文件名:pids_after_fork.c

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>int main() {pid_t pid, mypid, myppid;pid = getpid();printf("Before fork: Process id is %d\n", pid);pid = fork();if (pid < 0) {perror("fork() failure\n");return 1;}//Child processif (pid == 0) {printf("This is child process\n");mypid = getpid();myppid = getppid();printf("Process id is %d and PPID is %d\n", mypid, myppid);} else { //Parent process sleep(2);printf("This is parent process\n");mypid = getpid();myppid = getppid();printf("Process id is %d and PPID is %d\n", mypid, myppid);printf("Newly created process id or child pid is %d\n", pid);}return 0;
}

汇编&执行

Before fork: Process id is 166629
This is child process
Process id is 166630 and PPID is 166629
Before fork: Process id is 166629
This is parent process
Process id is 166629 and PPID is 166628
Newly created process id or child pid is 166630

进程可以通过以下两种方式之一终止:

  • 通常在传递某些信号(如终止信号)时发生。

  • 通常,使用_exit()系统调用(或_Exit()系统调用)或exit()库函数。

_exit()和exit()之间的区别主要是清理活动, exit()在将控件返回内核之前会进行一些清理,而 _exit()(或_Exit())会将控件立即返回内核。 

考虑以下带有exit()的示例程序。

文件名称:atexit_sample.c

#include <stdio.h>
#include <stdlib.h>void exitfunc() {printf("Called cleanup function - exitfunc()\n");return;
}int main() {atexit(exitfunc);printf("Hello, World!\n");exit (0);
}

汇编&执行

Hello, World!
Called cleanup function - exitfunc()

考虑以下带有_exit()的示例程序。

文件名称:at_exit_sample.c

#include <stdio.h>
#include <unistd.h>void exitfunc() {printf("Called cleanup function - exitfunc()\n");return;
}int main() {atexit(exitfunc);printf("Hello, World!\n");_exit (0);
}

汇编&执行

Hello, World!

进程 - 创建&终止 - 无涯教程网无涯教程网提供到现在为止,我们知道无论何时执行程序,都会创建一个进程,并且该进程将在执行完成后...https://www.learnfk.com/process/inter-process-communication-process-creation-termination.html

http://www.lryc.cn/news/139369.html

相关文章:

  • LLMs参考资料第一周以及BloombergGPT特定领域的训练 Domain-specific training: BloombergGPT
  • LeetCode字符串数组最长公共前缀
  • Git gui教程---第八篇 Git gui的使用 创建一个分支
  • Docker修改daemon.json添加日志后无法启动的问题
  • QT6编译的文件分布情况
  • 2023中国算力大会 | 中科驭数加入DPU推进计划,探讨DPU如何激活算网融合新基建
  • leetcode 115. 不同的子序列
  • gradio应用transformer模块部署生成式人工智能应用程序
  • 【目标检测】“复制-粘贴 copy-paste” 数据增强实现
  • 深度学习知识总结2:主要涉及深度学习基础知识、卷积神经网络和循环神经网络
  • Spring Boot 集成 WebSocket 实现服务端推送消息到客户端
  • vr游乐场项目投资方案VR主题游乐馆互动体验
  • chrom扩展开发配合百度图像文字识别实现自动登录(后端.net core web api)
  • 香港服务器怎么打开SSH
  • 【LeetCode】437.路径总和Ⅲ
  • Mybatis-plus中操作JSON字段
  • 第十五课、Windows 下打包发布 Qt 应用程序
  • 【php】windows下php运行已有php web项目环境配置教程
  • 【mybatis】 mybatis在mysql 更新update 操作 更新时间字段按照年月日时分秒格式 更新为当前时间...
  • C++动态规划经典案例解析之合并石子
  • go MongoDB
  • 算法与数据结构(八)--优先队列
  • React 全栈体系(三)
  • 腾讯云下一代CDN -- EdgeOne加速MinIO对象存储
  • GitLab-CI 指南
  • MyBatis的核心技术掌握,简单易懂(上)
  • Redisson自定义序列化
  • MongoDB Long 类型 shell 查询
  • 回归预测 | MATLAB实现GA-APSO-IBP改进遗传-粒子群算法优化双层BP神经网络多输入单输出回归预测
  • Spring cache整合Redis使用介绍