23,217
社区成员




#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)
(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)