***********我又来求指点了*********

生活简单到无聊 2014-02-18 10:21:18
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/types.h>

int main(int argc, char* argv[])
{

int i = 0;

pid_t pid = -1;

fprintf(stdout, "parent pid[%d], i[%d]\n", getpid(), i);

if((pid = vfork()) < 0)
{
fprintf(stderr, "fork failed!! errorno = %d\n", errno);
return 0;
}
else if(pid > 0)
{
fprintf(stdout, "after fork, parent pid[%d], i[%d]\n", getpid(), i);
}
else
{
system("find . -type f");
}


fprintf(stdout, "process end. pid[%d], i[%d]\n", getpid(), i);

return 0;
}



运行结果:
parent pid[20000], i[0]
./core.17552
./libshow.a
./dup2.c
./csdn.c
./main
./base/baseMath.o
./base/baseMath.c
./base/libbaseMath.a
./base/libbaseMath.so
./base/baseMath.h
./test.cpp
./stat.c
./power.c
./tag.c
./libshow.so
./unlink
./unlink.c
./book.c
./show.o
./a.bat
./show.c
./reservword.c
./fileop.c
./code.c
process end. pid[20001], i[0]
after fork, parent pid[20000], i[0]
process end. pid[20000], i[0]
Segmentation fault (core dumped)



为什么最后core了。。。。?!
...全文
85 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 2 楼 Idle_Cloud 的回复:
你把最后那个return 0,改成exit(0)就好了,子进程用return 是不安全的。
额,vfork的问题,vfork不继承父进程的数据,我改成fork就不core了。
Carl_CCC 2014-02-18
  • 打赏
  • 举报
回复
还有一种改法就是把vfork改成fork也是安全的。
Carl_CCC 2014-02-18
  • 打赏
  • 举报
回复
你把最后那个return 0,改成exit(0)就好了,子进程用return 是不安全的。
  • 打赏
  • 举报
回复
GDB结果如下:
(gdb) l
3       #include<stdlib.h>
4       //#include<memory.h>
5       //#include<dirent.h>
6       #include<errno.h>
7       #include<sys/types.h>
8
9       int main(int argc, char* argv[])
10      {
11                
12          int i = 0;
(gdb) 
13          
14          pid_t pid = -1;
15          
16          fprintf(stdout, "parent pid[%d], i[%d]\n", getpid(), i);
17          
18          if((pid = vfork()) < 0)
19          {
20              fprintf(stderr, "fork failed!!  errorno = %d\n", errno);
21              return 0;
22          }
(gdb) 
23          else if(pid > 0)
24          {
25              fprintf(stdout, "after fork, parent pid[%d], i[%d]\n", getpid(), i);
26              //waitpid(pid);
27          }
28          else
29          {
30              /*if(execl("/bin/find", ".", "-type", "f", (char*)NULL) < 0)
31              {
32                  fprintf(stderr, "execl failed!!  errorno = %d\n", errno);
(gdb) 
33              }*/
34              system("find . -type f");
35          }
36          
37          
38          fprintf(stdout, "process end. pid[%d], i[%d]\n", getpid(), i);
39          
40          return 0;
41      }(gdb) b 38
Breakpoint 1 at 0x10000830: file code.c, line 38.
(gdb) run main
Starting program: /data/cyc/cyccode/main main
parent pid[20117], i[0]
./libshow.a
./dup2.c
./csdn.c
./main
./base/baseMath.o
./base/baseMath.c
./base/libbaseMath.a
./base/libbaseMath.so
./base/baseMath.h
./test.cpp
./core.20000
./stat.c
./power.c
./tag.c
./libshow.so
./unlink
./unlink.c
./book.c
./show.o
./a.bat
./show.c
./reservword.c
./fileop.c
./code.c
process end. pid[20120], i[0]
after fork, parent pid[20117], i[0]

Breakpoint 1, main (argc=2, argv=0xffffffff4d8) at code.c:38
38          fprintf(stdout, "process end. pid[%d], i[%d]\n", getpid(), i);
(gdb) p i
$1 = 0
(gdb) p getpid()
$2 = 20117
(gdb) p stdout
$3 = (struct _IO_FILE *) 0xfffb7f606a0 <_IO_2_1_stdout_>
(gdb) s
process end. pid[20117], i[0]
40          return 0;
(gdb) s
41      }(gdb) s
0x00000fffb7ddbd00 in .generic_start_main () from /lib64/libc.so.6
(gdb) s
Single stepping until exit from function .generic_start_main,
which has no line number information.

Program received signal SIGSEGV, Segmentation fault.
0x00000fffb7ddbd04 in .generic_start_main () from /lib64/libc.so.6
(gdb)

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧