c语言exit和return区别,在fork和vfork中使用

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>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  pid_t pid;
  int count=0;

  pid=vfork();
  if(pid==0)
  {
    printf("child: count=%d\n",count);
    printf("child: getpid=%d\n",getpid());
    count=1;
    printf("child: count=%d\n",count);
    // return 0;//会出现段错误
    exit(0); //ok
  }
  else
  {
    printf("\nfather: pid=%d\n",pid);
    printf("father: count=%d\n",count);
  }
  return(0);
}

 

运行结果

[root@localhost part1_linux]# gcc fork2.c 
[root@localhost part1_linux]# ./a.out 
child: count=0
child: getpid=9911
child: count=1

father: pid=9911
father: count=1

运行结果说明:vfrok时父、子进程共享数据段,fork时是进行拷贝。如果,vfork子进程中,使用return返回时,出现段错误,结果如下:

[root@localhost part1_linux]# gcc fork2.c 
[root@localhost part1_linux]# ./a.out 
child: count=0
child: getpid=10864
child: count=1

father: pid=10864
father: count=0
段错误
2. 为什么执行结果子进程打印出来 我的父亲是id:1,与父进程id不同
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
	int i=0;
	pid_t pid;
	printf("还没创建子进程\n");
	i++;
	pid = fork();
	if(pid==-1)
	{
	  printf("fork error!\n");
	}
	else if(pid==0)
	{
	  i++;
	  printf("我是子进程,id%d\n",getpid());
  	  printf("我的父亲是id:%d\n",getppid());
   	  printf("-----i=%d-----\n",i);
	}
	else
	{
	  i++;
	  printf("我是父进程,id:%d\n",getpid());
	  printf("-----i=%d-----\n",i);
	}
	exit(0);
}
子进程在打印第一句时,父进程也在打印第一句,但是子进程在执行第二句时,父进程已经直接over了(这只是个简单的说法,实际过程可能并不如此,我要说的是,父进程先于子进程的打印语句之前就结束)。因此此时的子进程成了孤儿进程,会被init也就是1号进程领养,成为init的子进程。 为了避免这样的情况,父进程最后可以执行wait来等待子进程的返回。
3. 用vfork()创建子进程,执行后程序一直不断地重复运行,不断创建子进程,结尾用exit(0)代替return(0)后问题就能解决
return 0在一个函数中是正常的返回过程,它会使得程序返回到函数被调用处,回复之前的执行流程,return 语句不会被执行。而exit 一般是在任意位置和使用的,执行到exit 0时,整个进程就over了(这也是为什么在多线程程序中不要随意使用exit的原因),用来使程序退出运行,一般用来处理(不可挽回的)错误状态。
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
  int i=0;
  pid_t pid;
  printf("还没创建子进程\n");
  i++;
  pid = vfork();
  if(pid==-1)
  {
    printf("fork error!\n");
  }
  else if(pid==0)
  {
    i++;
    printf("我是子进程,id%d\n",getpid());
    printf("我的父亲是id:%d\n",getppid());
    printf("-----i=%d-----\n",i);
  }
  else
  {
    i++;
    printf("我是父进程,id:%d\n",getpid());
    printf("-----i=%d-----\n",i);
  }
  return(0);
}

posted on 2012-03-31 14:00  n哖苡逅  阅读(8130)  评论(1编辑  收藏  举报

导航