C語言trap 可重新進入和安全執行緒

來源:互聯網
上載者:User
#include <signal.h>#include <stdio.h>#include <time.h>#include <unistd.h>void handler(int signum){        char result[100];        time_t now;//      struct tm time1;        now = time(NULL);//      localtime_r(&now, &time1);//      ctime_r(&now,result);        strcpy(result,ctime(&now));//      strftime(result, 100, "%T", &time1);        printf("At %s, user pressed Ctrl-C\n", result);}int main (void){        time_t now;//      struct tm ltime;        char result[100];        if (signal(SIGINT, handler) == SIG_IGN)                signal(SIGINT, SIG_IGN);        now = time(NULL);        while(1) {#ifdef UNSAFE        //      localtime_r(&now, <ime);                ctime(&now);#endif        }        return 0;}

 對於訊號處理函數,盡量做到簡單,如果調用了不可重新進入函數,可能引發死結問題。

#16 0x0000003257e7230f in _int_free () from /lib64/libc.so.6#17 0x0000003257e7276b in free () from /lib64/libc.so.6#18 0x0000003257e8c7ca in tzset_internal () from /lib64/libc.so.6#19 0x0000003257e8cf2e in __tz_convert () from /lib64/libc.so.6#20 0x0000003257e8b529 in ctime () from /lib64/libc.so.6

strace -p 1202Process 1202 attached - interrupt to quitfutex(0x3258154fc0, FUTEX_WAIT_PRIVATE, 5, NULL

gcc -D UNSAFE test1.c -o test_unsafe

gcc -D UNSAFE test1.c -o test

編譯上面這個程式可以重現這個問題。

線程中也不要調用非安全執行緒系統調用,否則也會出現死結。

關於futex的文章

http://www.cnblogs.com/yysblog/archive/2012/11/03/2752728.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.