当前位置:网站首页>vfork执行时出现Segmentation fault
vfork执行时出现Segmentation fault
2022-07-03 02:46:00 【黑旋风大李逵】
1.首先明白vfork是干什么用的?
Linux命令行下输入man vfork回车
看红色框里的说明,翻译过来是vfork - 创建子进程并且阻塞父进程,block n.块 v.阻塞。
在手册的最底部出现下面红框的BUGS,说明vfork是由BUG的。翻译为信号处理的细节是有模糊的,在系统之间有差异(不同)。BSD手册页指出:“为了避免死锁情况,vfork()从不发送SIGTTOU(后台进程企图从控制终端写信号)或SIGTTIN(后台进程企图从控制终端读信号)信号;相反,允许输出或ioctl,并且输入会导致文件结束指令。”
2.出现Segmentation fault错误的代码如下:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid=vfork();
if(pid==-1)
{
printf("Create process is fault.\n");
}
if(pid==0)
{
printf("Child process %d is running.\n",getpid());
}
else
{
printf("Parent process %d is running.\n",getpid());
}
return 0;
}
程序执行结果如下:
从程序结果可以看出,vfork是先执行子进程,然后执行父进程的,只不过这段代码出现了Segmentation fault。
3.使用strace命令跟踪一下这个程序
在Linux终端输入strace -f -s 65500 -i -T ./vfork
从跟踪结果中看只调用一次exit_group(0),正确的情况下应该执行两次return 0;
如何修改程序?
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t pid=vfork();
if(pid==-1)
{
printf("Create process is fault.\n");
}
if(pid==0)
{
printf("Child process %d is running.\n",getpid());
}
else
{
printf("Parent process %d is running.\n",getpid());
}
exit(0);//将return 0;替换了,exit()函数在stdlib.h头文件中
}
执行结果为:
从结果看出Segmentation fault错误消失了。
边栏推荐
- [principles of multithreading and high concurrency: 1_cpu multi-level cache model]
- sql server数据库添加 mdf数据库文件,遇到的报错
- Cvpr2022 remove rain and fog
- sql server 查询指定表的表结构
- Matlab tips (24) RBF, GRNN, PNN neural network
- Kubernetes family container housekeeper pod online Q & A?
- Source code analysis | resource loading resources
- GBase 8c触发器(三)
- Tensorflow to pytorch notes; tf. gather_ Nd (x, y) to pytorch
- [tutorial] chrome turns off cross domain policies CORS and samesite, and brings cookies across domains
猜你喜欢

Today, it's time to copy the bottom!

Kubernetes cluster log and efk architecture log scheme
![[shutter] banner carousel component (shutter_wiper plug-in | swiper component)](/img/a6/5c97ef3f34702b83ebf0511501d757.gif)
[shutter] banner carousel component (shutter_wiper plug-in | swiper component)

Random shuffle note

random shuffle注意

where 1=1 是什么意思

HW-初始准备

The Linux server needs to install the agent software EPS (agent) database

Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)

random shuffle注意
随机推荐
Hcip137-147 title + analysis
[C语言]给账号密码进行MD5加密
cvpr2022去雨去雾
Principle and application of database
leetcode540
Can netstat still play like this?
SQL server queries the table structure of the specified table
怎么将yolov5中的PANet层改为BiFPN
What does it mean when lambda is not entered?
面试项目技术栈总结
Use optimization | points that can be optimized in recyclerview
[fluent] JSON model conversion (JSON serialization tool | JSON manual serialization | writing dart model classes according to JSON | online automatic conversion of dart classes according to JSON)
[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)
GBase 8c 函数/存储过程参数(二)
Pytest (6) -fixture (Firmware)
yii2 中andWhere多个or查询 orm条件
tensor中的append应该如何实现
Deep reinforcement learning for intelligent transportation systems: a survey paper reading notes
MATLAB小技巧(24)RBF,GRNN,PNN-神经网络
javeScript 0.1 + 0.2 == 0.3的问题