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