日期:2014-05-16  浏览次数:20635 次

***********我又来求指点了*********
#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了。。。。?!
------解决方案--------------------
还有一种改法就是把vfork改成fork也是安全的。