Linux編程:時間與檔案及訊號的操作

來源:互聯網
上載者:User
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <signal.h>#include <time.h>void fun(int x){ printf("hello world!\r\n"); int i; FILE *f=fopen("a.txt","rw+"); if(f==NULL){printf("file error\r\n");  exit(-1);}; for(i=0;i<10;i++) {  time_t a=time(NULL);  printf("%s\r\n",ctime(&a));  //功 能: 把日期和時間轉換為字串  //用 法: char *ctime(const time_t *time);  fprintf(f,"%s\r\n",ctime(&a));      struct tm *gmt;  gmt = gmtime(&a);     printf("%d/%d/%d  %d:%d:%d %d\r\n",gmt->tm_year+1900,gmt->tm_mon+1,gmt->tm_mday ,gmt->tm_hour,gmt->tm_min,gmt->tm_sec,gmt->tm_wday+1);     fprintf(f,"%d/%d/%d  %d:%d:%d %d\r\n\n",gmt->tm_year+1900,gmt->tm_mon+1,gmt->tm_mday ,gmt->tm_hour,gmt->tm_min,gmt->tm_sec,gmt->tm_wday+1);  sleep(2); } fclose(f); exit(1);}int main(){ signal(SIGINT,fun);     while(1);}

表標頭檔#include<signal.h>

功 能:設定某一訊號的對應動作

函數原型:void (*signal(int signum,void(* handler)(int)))(int);

或者:typedef void(*sig_t) ( int );

sig_t signal(int signum,sig_t handler);

參數說明:

第一個參數signum指明了所要處理的訊號類型,它可以取除了SIGKILL和SIGSTOP外的任何一種訊號。

第二個參數handler描述了與訊號關聯的動作,它可以取以下三種值:

(1)一個無傳回值的函數地址

此函數必須在signal()被調用前申明,handler中為這個函數的名字。當接收到一個類型為sig的訊號時,就執行handler 所指定的函數。這個函數應有如下形式的定義:

void func(int sig);

sig是傳遞給它的唯一參數。執行了signal()調用後,進程只要接收到類型為sig的訊號,不管其正在執行程式的哪一部分,就立即執行func()函數。當func()函數執行結束後,控制權返回進程被中斷的那一點繼續執行。

(2)SIG_IGN

這個符號表示忽略該訊號,執行了相應的signal()調用後,進程會忽略類型為sig的訊號。

(3)SIG_DFL

這個符號表示恢複系統對訊號的預設處理。

函數說明:

signal()會依參數signum 指定的訊號編號來設定該訊號的處理函數。當指定的訊號到達時就會跳轉到參數handler指定的函數執行。當一個訊號的訊號處理函數執行時,如果進程又接收到了該訊號,該訊號會自動被儲存而不會中斷訊號處理函數的執行,直到訊號處理函數執行完畢再重新調用相應的處理函數。但是如果在訊號處理函數執行時進程收到了其它類型的訊號,該函數的執行就會被中斷。

傳回值:返回先前的訊號處理函數指標,如果有錯誤則返回SIG_ERR(-1)。

下面的情況可以產生Signal:

按下CTRL+C產生SIGINT

硬體中斷,如除0,非法記憶體訪問(SIGSEV)等等

Kill函數可以對進程發送Signal

Kill命令。實際上是對Kill函數的一個封裝

軟體中斷。如當Alarm Clock逾時(SIGURG),當Reader中止之後又向管道寫資料(SIGPIPE),等等

程式例:

This example installs a signal handler routine for SIGFPE,catches an integer overflow condition, makes an adjustment to AX register, and returns. This example program MAY cause your computer to crash, and will produce runtime errors depending on which memory model is used.

#pragma inline#include <stdio.h>#include <signal.h>void Catcher(int sig, int type, int *reglist){printf("Caught it!\n");*(reglist + 8) = 3; // make return AX = 3}int main(void){signal(SIGFPE, Catcher);asm mov ax,07FFFH// AX = 32767asm inc ax    // cause overflowasm into// activate handlerThe handler set AX to 3 on return. If that hadn't happened,there would have been another exception when the next 'into'was executed after the 'dec' instruction.asm dec ax// no overflow nowasm into// doesn't activatereturn 0;}

2 Signals:

Signal

Description

SIGABRT

由調用abort函數產生,進程非正常退出

SIGALRM

用alarm函數設定的timer逾時或setitimer函數設定的interval timer逾時

SIGBUS

某種特定的硬體異常,通常由記憶體訪問引起

SIGCANCEL

由Solaris Thread Library內部使用,通常不會使用

SIGCHLD

進程Terminate或Stop的時候,SIGCHLD會發送給它的父進程。預設情況下該Signal會被忽略

SIGCONT

當被stop的進程恢複啟動並執行時候,自動發送

SIGEMT

和實現相關的硬體異常

SIGFPE

數學相關的異常,如被0除,浮點溢出,等等

SIGFREEZE

Solaris專用,Hiberate或者Suspended時候發送

SIGHUP

發送給具有Terminal的Controlling Process,當terminal被disconnect時候發送

SIGILL

非法指令異常

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/

SIGINFO

BSD signal。由Status Key產生,通常是CTRL+T。發送給所有Foreground Group的進程

SIGINT

由Interrupt Key產生,通常是CTRL+C或者DELETE。發送給所有ForeGround Group的進程

SIGIO

非同步IO事件

SIGIOT

實現相關的硬體異常,一般對應SIGABRT

SIGKILL

無法處理和忽略。中止某個進程

SIGLWP

由Solaris Thread Libray內部使用

SIGPIPE

在reader中止之後寫Pipe的時候發送

SIGPOLL

當某個事件發送給Pollable Device的時候發送

SIGPROF

Setitimer指定的Profiling Interval Timer所產生

SIGPWR

和系統相關。和UPS相關。

SIGQUIT

輸入Quit Key的時候(CTRL+\)發送給所有Foreground Group的進程

SIGSEGV

非法記憶體訪問

SIGSTKFLT

Linux專用,數學副處理器的棧異常

SIGSTOP

中止進程。無法處理和忽略。

SIGSYS

非法系統調用

SIGTERM

請求中止進程,kill命令預設發送

SIGTHAW

Solaris專用,從Suspend恢複時候發送

SIGTRAP

實現相關的硬體異常。一般是調試異常

SIGTSTP

Suspend Key,一般是Ctrl+Z。發送給所有Foreground Group的進程

SIGTTIN

當Background Group的進程嘗試讀取Terminal的時候發送

SIGTTOU

當Background Group的進程嘗試寫Terminal的時候發送

SIGURG

當out-of-band data接收的時候可能發送

SIGUSR1

使用者自訂signal 1

SIGUSR2

使用者自訂signal 2

SIGVTALRM

setitimer函數設定的Virtual Interval Timer逾時的時候

SIGWAITING

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.