日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

c語(yǔ)言退出程序命令exit c語(yǔ)言exit函數(shù)用法( 二 )


函數(shù)原型如下:
#include <stdlib.h>int on_exit(void (*function)(int, void *), void *arg);參數(shù)列表
– `function`: 用戶自定義的進(jìn)程退出清理函數(shù) 。
– `arg`: `void *`類(lèi)型的自定義參數(shù) 。
返回值
成功返回0,非0值則表示失敗 。
示例#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>void cleanup1() {fprintf(stderr, "[1]cleanup\n");sleep(1);}void cleanup2() {fprintf(stderr, "[2]cleanup\n");sleep(1);}void cleanup3(int status, void *arg) {fprintf(stderr, "[3]cleanup: %s\n", (char *)arg);sleep(1);}int main(int argc, char *argv[]) {if (argc != 2) {fprintf(stderr, "Usage: %s exit|_exit|_Exit|return\n", argv[0]);return EXIT_FAILURE;}// atexit注冊(cè)自定義清理函數(shù)atexit(cleanup1);atexit(cleanup2);atexit(cleanup2); // 多次注冊(cè)同一個(gè)函數(shù)// 非標(biāo)準(zhǔn)函數(shù)on_exit,僅Linux下有效// on_exit(cleanup3, (void *)"bye!!!");// on_exit(cleanup3, (void *)"bye!!!"); // 多次注冊(cè)同一個(gè)函數(shù)fprintf(stdout, "a newline!\n"); // 向stdout寫(xiě)入帶換行符的字符串(行緩沖,遇到換行符的情況下就會(huì)調(diào)用write系統(tǒng)調(diào)用輸出內(nèi)容)fprintf(stderr, "[stderr]a newline!"); // 向stderr寫(xiě)入不帶換行符的字符串(stderr默認(rèn)情況下無(wú)緩沖,直接調(diào)用write系統(tǒng)調(diào)用)fprintf(stdout, "[stdout]forgot a newline!"); // 向stdout寫(xiě)入不帶換行符的字符串(若不刷新緩沖區(qū),則該行內(nèi)容不會(huì)被輸出)if (strcmp("exit", argv[1]) == 0) {// 作用:執(zhí)行一些前置的清理操作并終止當(dāng)前進(jìn)程// 標(biāo)準(zhǔn)庫(kù)函數(shù)(C89)// #include <stdlib.h>// 調(diào)用exit函數(shù)會(huì)執(zhí)行以下操作:// 1、調(diào)用用戶注冊(cè)的清理函數(shù)// 2、刷新緩沖區(qū)并關(guān)閉所有標(biāo)準(zhǔn)IO流// 3、刪除臨時(shí)文件// 4、調(diào)用_exit系統(tǒng)調(diào)用exit(0);} else if (strcmp("_Exit", argv[1]) == 0) {// 作用:直接終止當(dāng)前進(jìn)程(含進(jìn)程的所有線程)// 標(biāo)準(zhǔn)庫(kù)函數(shù)(C99)// #include <stdlib.h>// 效果等同于_exit,但移植性更好 。_Exit(0);} else if (strcmp("_exit", argv[1]) == 0) {// 作用:直接終止當(dāng)前進(jìn)程(含進(jìn)程的所有線程)// 是對(duì)exit_group系統(tǒng)調(diào)用的包裝(可退出所有線程)// #include <unistd.h>_exit(0);}return EXIT_SUCCESS; // main函數(shù)return會(huì)調(diào)用exit函數(shù)}

推薦閱讀