当前位置:网站首页>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错误消失了。
边栏推荐
- Counter统计数量后,如何返回有序的key
- Mathematical statistics -- Sampling and sampling distribution
- Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
- [C语言]给账号密码进行MD5加密
- 【翻译】后台项目加入了CNCF孵化器
- [principles of multithreading and high concurrency: 1_cpu multi-level cache model]
- Add automatic model generation function to hade
- GBase 8c 触发器(一)
- Kubernetes cluster log and efk architecture log scheme
- Gbase 8C function / stored procedure parameters (I)
猜你喜欢

Kubernetes family container housekeeper pod online Q & A?

Xiaodi notes

Deep Reinforcement Learning for Intelligent Transportation Systems: A Survey 论文阅读笔记
![ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc](/img/cb/145937a27ef08050a370d5a255215a.jpg)
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc

Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
![ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc](/img/cb/145937a27ef08050a370d5a255215a.jpg)
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc

Practice of traffic recording and playback in vivo

where 1=1 是什么意思

Build a private cloud disk cloudrev

Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
随机推荐
Practice of traffic recording and playback in vivo
Check log4j problems using stain analysis
疫情当头,作为Leader如何进行代码版本和需求开发管控?| 社区征文
面试八股文整理版
为什么会选择框架?选择什么样的框架
How to implement append in tensor
Joking about Domain Driven Design (III) -- Dilemma
GBase 8c系统表-pg_aggregate
Monitoring and management of JVM
Gbase 8C system table PG_ am
tensorflow转pytorch笔记;tf.gather_nd(x,y)转pytorch
GBase 8c触发器(二)
Javescript 0.1 + 0.2 = = 0.3 problem
Sqlserver row to column pivot
[fluent] listview list (map method description of list set | vertical list | horizontal list | code example)
MATLAB小技巧(24)RBF,GRNN,PNN-神经网络
Xiaodi notes
Informatics Olympiad one general question bank 1006 a+b questions
SQL statement
Compréhension simple de SVG