C語言中花式退出程序的方式總結(jié)
前言
在本篇文章當(dāng)中主要給大家介紹C語言當(dāng)中一些不常用的特性,比如在main函數(shù)之前和之后設(shè)置我們想要執(zhí)行的函數(shù),以及各種花式退出程序的方式。
main函數(shù)是最先執(zhí)行和最后執(zhí)行的函數(shù)嗎
C語言構(gòu)造和析構(gòu)函數(shù)
通常我們在寫C程序的時(shí)候都是從main函數(shù)開始寫,因此我們可能沒人有關(guān)心過這個(gè)問題,事實(shí)上是main函數(shù)不是程序第一個(gè)執(zhí)行的函數(shù),也不是程序最后一個(gè)執(zhí)行的函數(shù)。
#include <stdio.h>
void __attribute__((constructor)) init1() {
printf("before main funciton\n");
}
int main() {
printf("this is main funciton\n");
}
我們編譯上面的代碼然后執(zhí)行,輸出結(jié)果如下圖所示:
? code git:(main) ./init.out
before main funciton
this is main funciton
由此可見main函數(shù)并不是第一個(gè)被執(zhí)行的函數(shù),那么程序第一次執(zhí)行的函數(shù)是什么呢?很簡單我們看一下程序的調(diào)用棧即可。

從上面的結(jié)果可以知道,程序第一個(gè)執(zhí)行的函數(shù)是_start,這是在類Unix操作系統(tǒng)上執(zhí)行的第一個(gè)函數(shù)。
那么main函數(shù)是程序執(zhí)行的最后一個(gè)函數(shù)嗎?我們看下面的代碼:
#include <stdio.h>
void __attribute__((destructor)) __exit() {
printf("this is exit\n");
}
void __attribute__((constructor)) init() {
printf("this is init\n");
}
int main() {
printf("this is main\n");
return 0;
}
上面程序的輸出結(jié)果如下:
? code git:(main) ./out.out
this is init
this is main
this is exit
由此可見main函數(shù)也不是我們最后執(zhí)行的函數(shù)!事實(shí)上我們除了上面的方法之外我們也可以在libc當(dāng)中注冊一些函數(shù),讓程序在main函數(shù)之后,退出執(zhí)行前執(zhí)行這些函數(shù)。
on_exit和atexit函數(shù)
我們可以使用上面兩個(gè)函數(shù)進(jìn)行函數(shù)的注冊,讓程序退出之前執(zhí)行我們指定的函數(shù)
#include <stdio.h>
#include <stdlib.h>
void __attribute__((destructor)) __exit() {
printf("this is exit\n");
}
void __attribute__((constructor)) init() {
printf("this is init\n");
}
void on__exit() {
printf("this in on exit\n");
}
void at__exit() {
printf("this in at exit\n");
}
int main() {
on_exit(on__exit, NULL);
atexit(at__exit);
printf("this is main\n");
return 0;
}
this is init
this is main
this in at exit
this in on exit
this is exit
我們可以仔細(xì)分析一下上面程序執(zhí)行的順序。首先是執(zhí)構(gòu)造函數(shù),然后執(zhí)行 atexit 注冊的函數(shù),再執(zhí)行 on_exit 注冊的函數(shù),最后執(zhí)行析構(gòu)函數(shù)。從上面程序的輸出我們可以知道我們注冊的函數(shù)生效了,但是需要注意一個(gè)問題,先注冊的函數(shù)后執(zhí)行,不管是使用 atexit 還是 on_exit 函數(shù)。我們現(xiàn)在看下面的代碼:
#include <stdio.h>
#include <stdlib.h>
void __attribute__((destructor)) __exit() {
printf("this is exit\n");
}
void __attribute__((constructor)) init() {
printf("this is init\n");
}
void on__exit() {
printf("this in on exit\n");
}
void at__exit() {
printf("this in at exit\n");
}
int main() {
// 調(diào)換下面兩行的順序
atexit(at__exit);
on_exit(on__exit, NULL);
printf("this is main\n");
return 0;
}
上面的代碼輸出如下:
this is init
this is main
this in on exit
this in at exit
this is exit
從輸出的結(jié)果看確實(shí)和上面我們提到的規(guī)則一樣,先注冊的函數(shù)后執(zhí)行。這一點(diǎn)再linux程序員開發(fā)手冊里面也提到了。

但是這里有一點(diǎn)需要注意的是我們應(yīng)該盡可能使用atexit函數(shù),而不是使用on_exit函數(shù),因?yàn)閍texit函數(shù)是標(biāo)準(zhǔn)規(guī)定的,而on_exit并不是標(biāo)準(zhǔn)規(guī)定的。
exit和_exit函數(shù)
其中exit函數(shù)是libc給我們提供的函數(shù),我們可以使用這個(gè)函數(shù)正常的終止程序的執(zhí)行,而且我們在前面注冊的函數(shù)還是能夠被執(zhí)行。比如在下面的代碼當(dāng)中:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void __attribute__((destructor)) __exit1() {
printf("this is exit1\n");
}
void __attribute__((destructor)) __exit2() {
printf("this is exit2\n");
}
void __attribute__((constructor)) init1() {
printf("this is init1\n");
}
void __attribute__((constructor)) init2() {
printf("this is init2\n");
}
void on__exit1() {
printf("this in on exit1\n");
}
void at__exit1() {
printf("this in at exit1\n");
}
void on__exit2() {
printf("this in on exit2\n");
}
void at__exit2() {
printf("this in at exit2\n");
}
int main() {
// _exit(1);
on_exit(on__exit1, NULL);
on_exit(on__exit2, NULL);
atexit(at__exit1);
atexit(at__exit2);
printf("this is main\n");
exit(1);
return 0;
}
上面的函數(shù)執(zhí)行結(jié)果如下所示:
this is init1
this is init2
this is main
this in at exit2
this in at exit1
this in on exit2
this in on exit1
this is exit2
this is exit1
可以看到我們的代碼被正常執(zhí)行啦。
但是_exit是一個(gè)系統(tǒng)調(diào)用,當(dāng)執(zhí)行這個(gè)方法的時(shí)候程序會(huì)被直接終止,我們看下面的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void __attribute__((destructor)) __exit1() {
printf("this is exit1\n");
}
void __attribute__((destructor)) __exit2() {
printf("this is exit2\n");
}
void __attribute__((constructor)) init1() {
printf("this is init1\n");
}
void __attribute__((constructor)) init2() {
printf("this is init2\n");
}
void on__exit1() {
printf("this in on exit1\n");
}
void at__exit1() {
printf("this in at exit1\n");
}
void on__exit2() {
printf("this in on exit2\n");
}
void at__exit2() {
printf("this in at exit2\n");
}
int main() {
// _exit(1);
on_exit(on__exit1, NULL);
on_exit(on__exit2, NULL);
atexit(at__exit1);
atexit(at__exit2);
printf("this is main\n");
_exit(1); // 只改了這個(gè)函數(shù) 從 exit 變成 _exit
return 0;
}
上面的代碼輸出結(jié)果如下所示:
this is init1
this is init2
this is main
可以看到我們注冊的函數(shù)和最終的析構(gòu)函數(shù)都沒有被執(zhí)行,程序直接退出啦。
花式退出
出了上面的_exit函數(shù)之外,我們還可以使用其他的方式直接退出程序:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>
void __attribute__((destructor)) __exit1() {
printf("this is exit1\n");
}
void __attribute__((destructor)) __exit2() {
printf("this is exit2\n");
}
void __attribute__((constructor)) init1() {
printf("this is init1\n");
}
void __attribute__((constructor)) init2() {
printf("this is init2\n");
}
void on__exit1() {
printf("this in on exit1\n");
}
void at__exit1() {
printf("this in at exit1\n");
}
void on__exit2() {
printf("this in on exit2\n");
}
void at__exit2() {
printf("this in at exit2\n");
}
int main() {
// _exit(1);
on_exit(on__exit1, NULL);
on_exit(on__exit2, NULL);
atexit(at__exit1);
atexit(at__exit2);
printf("this is main\n");
syscall(SYS_exit, 1); // 和 _exit 效果一樣
return 0;
}
出了上面直接調(diào)用函數(shù)的方法退出函數(shù),我們還可以使用內(nèi)聯(lián)匯編退出函數(shù),比如在64位操作系統(tǒng)我們可以使用下面的代碼退出程序:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>
void __attribute__((destructor)) __exit1() {
printf("this is exit1\n");
}
void __attribute__((destructor)) __exit2() {
printf("this is exit2\n");
}
void __attribute__((constructor)) init1() {
printf("this is init1\n");
}
void __attribute__((constructor)) init2() {
printf("this is init2\n");
}
void on__exit1() {
printf("this in on exit1\n");
}
void at__exit1() {
printf("this in at exit1\n");
}
void on__exit2() {
printf("this in on exit2\n");
}
void at__exit2() {
printf("this in at exit2\n");
}
int main() {
// _exit(1);
on_exit(on__exit1, NULL);
on_exit(on__exit2, NULL);
atexit(at__exit1);
atexit(at__exit2);
printf("this is main\n");
asm(
"movq $60, %%rax;"
"movq $1, %%rdi;"
"syscall;"
:::"eax"
);
return 0;
}
上面是在64位操作系統(tǒng)退出程序的匯編實(shí)現(xiàn),在64為系統(tǒng)上退出程序的系統(tǒng)調(diào)用號為60。下面我們使用32位操作系統(tǒng)上的匯編實(shí)現(xiàn)程序退出,在32位系統(tǒng)上退出程序的系統(tǒng)調(diào)用號等于1:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>
void __attribute__((destructor)) __exit1() {
printf("this is exit1\n");
}
void __attribute__((destructor)) __exit2() {
printf("this is exit2\n");
}
void __attribute__((constructor)) init1() {
printf("this is init1\n");
}
void __attribute__((constructor)) init2() {
printf("this is init2\n");
}
void on__exit1() {
printf("this in on exit1\n");
}
void at__exit1() {
printf("this in at exit1\n");
}
void on__exit2() {
printf("this in on exit2\n");
}
void at__exit2() {
printf("this in at exit2\n");
}
int main() {
// _exit(1);
on_exit(on__exit1, NULL);
on_exit(on__exit2, NULL);
atexit(at__exit1);
atexit(at__exit2);
printf("this is main\n");
asm volatile(
"movl $1, %%eax;"
"movl $1, %%edi;"
"int $0x80;"
:::"eax"
);
return 0;
}
以上就是C語言中花式退出程序的方式總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于C語言退出程序的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語言中設(shè)置用戶識別碼的相關(guān)函數(shù)的簡單講解
這篇文章主要介紹了C語言中設(shè)置用戶識別碼的相關(guān)函數(shù)的簡單講解,包括setuid()函數(shù)和setreuid()函數(shù)以及setfsuid()函數(shù),需要的朋友可以參考下2015-08-08
C語言數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)字符串分割的實(shí)例
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)字符串分割的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10
C++求解二叉樹的下一個(gè)結(jié)點(diǎn)問題
本文將通過C++求解以下問題:給定一個(gè)二叉樹其中的一個(gè)結(jié)點(diǎn),請找出中序遍歷順序的下一個(gè)結(jié)點(diǎn)并且返回。文中示例代碼講解詳細(xì),感興趣的可以了解一下2022-04-04
C++ string字符串的使用和簡單模擬實(shí)現(xiàn)
C語言中,字符串是以'\0'結(jié)尾的一些字符的集合,為了操作方便,C標(biāo)準(zhǔn)庫中提供了一些str系列的庫函數(shù),但是這些庫函數(shù)和字符串是分離的,本文給大家介紹了C++ string字符串的使用和簡單模擬實(shí)現(xiàn),感興趣的朋友可以參考下2024-06-06
C語言用棧模擬實(shí)現(xiàn)隊(duì)列問題詳解
本片文章帶你分析如何用兩個(gè)棧,并且只使用棧的基本功能來模擬實(shí)現(xiàn)隊(duì)列,其中同樣只實(shí)現(xiàn)隊(duì)列的基本功能,感興趣的朋友來看看吧2022-04-04

