c语言exit和return区别,在fork和vfork中使用
轉(zhuǎn)自c語(yǔ)言exit和return區(qū)別,在fork和vfork中使用
exit函數(shù)在頭文件stdlib.h中。
簡(jiǎn)述:
? ? exit(0):正常運(yùn)行程序并退出程序;
? ??exit(1):非正常運(yùn)行導(dǎo)致退出程序;
? ??return():返回函數(shù),若在main主函數(shù)中,則會(huì)退出函數(shù)并返回一值,可以寫為return(0),或return 0。
詳述:
? ??1. return返回函數(shù)值,是關(guān)鍵字;exit是一個(gè)函數(shù)。
? ??2. return是語(yǔ)言級(jí)別的,它表示了調(diào)用堆棧的返回;而exit是系統(tǒng)調(diào)用級(jí)別的,它表示了一個(gè)進(jìn)程的結(jié)束。
? ??3. return是函數(shù)的退出(返回);exit是進(jìn)程的退出。
? ??4. return是C語(yǔ)言提供的,exit是操作系統(tǒng)提供的(或者函數(shù)庫(kù)中給出的)。
? ??5. return用于結(jié)束一個(gè)函數(shù)的執(zhí)行,將函數(shù)的執(zhí)行信息傳出個(gè)其他調(diào)用函數(shù)使用;exit函數(shù)是退出應(yīng)用程序,刪除進(jìn)程使用的內(nèi)存空間,并將應(yīng)用程序的一個(gè)狀態(tài)返回給OS,這個(gè)狀態(tài)標(biāo)識(shí)了應(yīng)用程序的一些運(yùn)行信息,這個(gè)信息和機(jī)器和操作系統(tǒng)有關(guān),一般是?0 為正常退出,非0 為非正常退出。
? ??6. 非主函數(shù)中調(diào)用return和exit效果很明顯,但是在main函數(shù)中調(diào)用return和exit的現(xiàn)象就很模糊,多數(shù)情況下現(xiàn)象都是一致的。
?下面是幾個(gè)例子:
例一:
#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;//會(huì)出現(xiàn)段錯(cuò)誤exit(0); //ok}else{printf("\nfather: pid=%d\n",pid);printf("father: count=%d\n",count);}return(0);
}運(yùn)行結(jié)果:[root@localhost part1_linux]# gcc fork2.c
[root@localhost part1_linux]# ./a.out
child: count=0
child: getpid=9911
child: count=1father: pid=9911
father: count=1 運(yùn)行結(jié)果說(shuō)明:vfrok時(shí)父、子進(jìn)程共享數(shù)據(jù)段,fork時(shí)是進(jìn)行拷貝。如果,vfork子進(jìn)程中,使用return返回時(shí),出現(xiàn)段錯(cuò)誤,結(jié)果如下:
[root@localhost part1_linux]# gcc fork2.c
[root@localhost part1_linux]# ./a.out
child: count=0
child: getpid=10864
child: count=1father: pid=10864
father: count=0
段錯(cuò)誤例二: #include <stdio.h>
#include <sys/types.h>
#include <unistd.h>int main()
{int i=0;pid_t pid;printf("還沒(méi)創(chuàng)建子進(jìn)程\n");i++;pid = fork();if(pid==-1){printf("fork error!\n");}else if(pid==0){i++;printf("我是子進(jìn)程,id%d\n",getpid());printf("我的父親是id:%d\n",getppid());printf("-----i=%d-----\n",i);}else{i++;printf("我是父進(jìn)程,id:%d\n",getpid());printf("-----i=%d-----\n",i);}exit(0);
} 為什么執(zhí)行結(jié)果子進(jìn)程打印出來(lái) 我的父親是id:1,與父進(jìn)程id不同?
子進(jìn)程在打印第一句時(shí),父進(jìn)程也在打印第一句,但是子進(jìn)程在執(zhí)行第二句時(shí),父進(jìn)程已經(jīng)直接over了(這只是個(gè)簡(jiǎn)單的說(shuō)法,實(shí)際過(guò)程可能并不如此,我要說(shuō)的是,父進(jìn)程先于子進(jìn)程的打印語(yǔ)句之前就結(jié)束)。因此此時(shí)的子進(jìn)程成了孤兒進(jìn)程,會(huì)被init也就是1號(hào)進(jìn)程領(lǐng)養(yǎng),成為init的子進(jìn)程。 為了避免這樣的情況,父進(jìn)程最后可以執(zhí)行wait來(lái)等待子進(jìn)程的返回。
例三:#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>int main()
{int i=0;pid_t pid;printf("還沒(méi)創(chuàng)建子進(jìn)程\n");i++;pid = vfork();if(pid==-1){printf("fork error!\n");}else if(pid==0){i++;printf("我是子進(jìn)程,id%d\n",getpid());printf("我的父親是id:%d\n",getppid());printf("-----i=%d-----\n",i);}else{i++;printf("我是父進(jìn)程,id:%d\n",getpid());printf("-----i=%d-----\n",i);}return(0);
} 用vfork()創(chuàng)建子進(jìn)程,執(zhí)行后程序一直不斷地重復(fù)運(yùn)行,不斷創(chuàng)建子進(jìn)程,結(jié)尾用exit(0)代替return(0)后問(wèn)題就能解決。
return 0在一個(gè)函數(shù)中是正常的返回過(guò)程,它會(huì)使得程序返回到函數(shù)被調(diào)用處,恢復(fù)之前的執(zhí)行流程。而exit 一般是在任意位置使用的,執(zhí)行到exit 0時(shí),整個(gè)進(jìn)程就over了(這也是為什么在多線程程序中不要隨意使用exit的原因),用來(lái)使程序退出運(yùn)行,一般用來(lái)處理(不可挽回的)錯(cuò)誤狀態(tài)。
轉(zhuǎn)載于:https://www.cnblogs.com/noble/p/4144165.html
總結(jié)
以上是生活随笔為你收集整理的c语言exit和return区别,在fork和vfork中使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 求一个处女座个性签名
- 下一篇: 微软2014校园招聘笔试试题