日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C語言中花式退出程序的方式總結(jié)_C 語言

作者:一無是處的研究僧 ? 更新時間: 2022-11-27 編程語言

前言

在本篇文章當(dāng)中主要給大家介紹C語言當(dāng)中一些不常用的特性,比如在main函數(shù)之前和之后設(shè)置我們想要執(zhí)行的函數(shù),以及各種花式退出程序的方式。

main函數(shù)是最先執(zhí)行和最后執(zhí)行的函數(shù)嗎

C語言構(gòu)造和析構(gòu)函數(shù)

通常我們在寫C程序的時候都是從main函數(shù)開始寫,因此我們可能沒人有關(guān)心過這個問題,事實(shí)上是main函數(shù)不是程序第一個執(zhí)行的函數(shù),也不是程序最后一個執(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ù)并不是第一個被執(zhí)行的函數(shù),那么程序第一次執(zhí)行的函數(shù)是什么呢?很簡單我們看一下程序的調(diào)用棧即可。

從上面的結(jié)果可以知道,程序第一個執(zhí)行的函數(shù)是_start,這是在類Unix操作系統(tǒng)上執(zhí)行的第一個函數(shù)。

那么main函數(shù)是程序執(zhí)行的最后一個函數(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ù)

我們可以使用上面兩個函數(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ù)生效了,但是需要注意一個問題,先注冊的函數(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ù),我們可以使用這個函數(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是一個系統(tǒng)調(diào)用,當(dāng)執(zhí)行這個方法的時候程序會被直接終止,我們看下面的代碼:

#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); // 只改了這個函數(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;
}

原文鏈接:https://www.cnblogs.com/Chang-LeHung/p/16804703.html

欄目分類
最近更新