当前位置:网站首页>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错误消失了。
边栏推荐
- Gbase 8C system table PG_ database
- Check log4j problems using stain analysis
- 错误Invalid bound statement (not found): com.ruoyi.stock.mapper.StockDetailMapper.xxxx解决
- Kubernetes cluster log and efk architecture log scheme
- SQL Server Query spécifie la structure de la table
- Check log4j problems using stain analysis
- where 1=1 是什么意思
- [shutter] banner carousel component (shutter_wiper plug-in | swiper component)
- sql server 查詢指定錶的錶結構
- Counter统计数量后,如何返回有序的key
猜你喜欢
[hcia]no.15 communication between VLANs
Pytest (6) -fixture (Firmware)
HW initial preparation
Classes and objects - initialization and cleanup of objects - constructor call rules
Random shuffle note
Kubernetes cluster log and efk architecture log scheme
Deep Reinforcement Learning for Intelligent Transportation Systems: A Survey 论文阅读笔记
random shuffle注意
[translation] the background project has joined the CNCF incubator
Add automatic model generation function to hade
随机推荐
xiaodi-笔记
[Hcia]No.15 Vlan间通信
《MATLAB 神经网络43个案例分析》:第43章 神经网络高效编程技巧——基于MATLAB R2012b新版本特性的探讨
Check log4j problems using stain analysis
Gbase 8C function / stored procedure parameters (I)
Wechat - developed by wechat official account Net core access
Cvpr2022 remove rain and fog
Pytest (6) -fixture (Firmware)
二维格式数组格式索引下标连续问题导致 返回json 格式问题
GBase 8c系统表-pg_aggregate
[translation] flux is safe. Gain more confidence through fuzzy processing
"Analysis of 43 cases of MATLAB neural network": Chapter 43 efficient programming skills of neural network -- Discussion Based on the characteristics of the new version of MATLAB r2012b
Use cve-2021-43893 to delete files on the domain controller
Gbase 8C system table PG_ amop
SQL server queries the table structure of the specified table
Use optimization | points that can be optimized in recyclerview
SqlServer行转列PIVOT
tensor中的append应该如何实现
簡單理解svg
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)