c语言exit和return区别,在fork和vfork中使用(转)
原文:http://www.cnblogs.com/laojie4321/archive/2012/03/31/2426910.html
exit函数在头文件stdlib.h中。
exit(0):正常运行程序并退出程序;
exit(1):非正常运行导致退出程序;
return():返回函数,若在main主函数中,则会退出函数并返回一值,可以写为return(0),或return 0。
详细说:
1. return返回函数值,是关键字;exit是一个函数。
2. return是语言级别的,它表示了调用堆栈的返回;而exit是系统调用级别的,它表示了一个进程的结束。
3. return是函数的退出(返回);exit是进程的退出。
4. return是C语言提供的,exit是操作系统提供的(或者函数库中给出的)。
5. return用于结束一个函数的执行,将函数的执行信息传出个其他调用函数使用;exit函数是退出应用程序,删除进程使用的内存空间,并将应用程序的一个状态返回给OS,这个状态标识了应用程序的一些运行信息,这个信息和机器和操作系统有关,一般是 0 为正常退出,非0 为非正常退出。
6. 非主函数中调用return和exit效果很明显,但是在main函数中调用return和exit的现象就很模糊,多数情况下现象都是一致的。
下面是几个例子:
1 #include <unistd.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 int main(void) 6 { 7 pid_t pid; 8 int count=0; 9 10 pid=vfork(); 11 if(pid==0) 12 { 13 printf("child: count=%d\n",count); 14 printf("child: getpid=%d\n",getpid()); 15 count=1; 16 printf("child: count=%d\n",count); 17 // return 0;//会出现段错误 18 exit(0); //ok 19 } 20 else 21 { 22 printf("\nfather: pid=%d\n",pid); 23 printf("father: count=%d\n",count); 24 } 25 return(0); 26 }
运行结果、
1 [root@localhost part1_linux]# gcc fork2.c 2 [root@localhost part1_linux]# ./a.out 3 child: count=0 4 child: getpid=9911 5 child: count=1 6 7 father: pid=9911 8 father: count=1
运行结果说明:vfrok时父、子进程共享数据段,fork时是进行拷贝。如果,vfork子进程中,使用return返回时,出现段错误,结果如下:
1 [root@localhost part1_linux]# gcc fork2.c 2 [root@localhost part1_linux]# ./a.out 3 child: count=0 4 child: getpid=10864 5 child: count=1 6 7 father: pid=10864 8 father: count=0 9 段错误
2. 为什么执行结果子进程打印出来 我的父亲是id:1,与父进程id不同
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <unistd.h> 4 5 int main() 6 { 7 int i=0; 8 pid_t pid; 9 printf("还没创建子进程\n"); 10 i++; 11 pid = fork(); 12 if(pid==-1) 13 { 14 printf("fork error!\n"); 15 } 16 else if(pid==0) 17 { 18 i++; 19 printf("我是子进程,id%d\n",getpid()); 20 printf("我的父亲是id:%d\n",getppid()); 21 printf("-----i=%d-----\n",i); 22 } 23 else 24 { 25 i++; 26 printf("我是父进程,id:%d\n",getpid()); 27 printf("-----i=%d-----\n",i); 28 } 29 exit(0); 30 }
子进程在打印第一句时,父进程也在打印第一句,但是子进程在执行第二句时,父进程已经直接over了(这只是个简单的说法,实际过程可能并不如此,我要说的是,父进程先于子进程的打印语句之前就结束)。因此此时的子进程成了孤儿进程,会被init也就是1号进程领养,成为init的子进程。 为了避免这样的情况,父进程最后可以执行wait来等待子进程的返回。
3. 用vfork()创建子进程,执行后程序一直不断地重复运行,不断创建子进程,结尾用exit(0)代替return(0)后问题就能解决
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <unistd.h> 4 5 int main() 6 { 7 int i=0; 8 pid_t pid; 9 printf("还没创建子进程\n"); 10 i++; 11 pid = vfork(); 12 if(pid==-1) 13 { 14 printf("fork error!\n"); 15 } 16 else if(pid==0) 17 { 18 i++; 19 printf("我是子进程,id%d\n",getpid()); 20 printf("我的父亲是id:%d\n",getppid()); 21 printf("-----i=%d-----\n",i); 22 } 23 else 24 { 25 i++; 26 printf("我是父进程,id:%d\n",getpid()); 27 printf("-----i=%d-----\n",i); 28 } 29 return(0); 30 }