Linux Programming: Time and file and signal operation

Source: Internet
Author: User
Tags exit function prototype include printf signal handler linux
#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));
  Function: convert date and time to string
  //Usage: 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);
}

Table header File #include<signal.h>

Function: Set the corresponding action of a signal

Function prototype: void (*signal int signum,void (* handler) (int)) (int);

Or: typedef void (*sig_t) (int);

sig_t signal (int signum,sig_t handler);

Parameter description:

The first parameter signum indicates the type of signal to be processed, and it can take any signal except Sigkill and sigstop.

The second parameter handler describes the action associated with the signal, which can take the following three values:

(1) A function address with no return value

This function must be declared before the signal () is invoked, and handler is the name of the function. When a signal of type sig is received, the function specified by handler is executed. This function should have the following form of definition:

void func (int sig);

The sig is the only parameter passed to it. After the signal () call is executed, the process simply receives a signal of type sig, and executes the Func () function immediately, regardless of which part of the program is executing. When the Func () function completes, control returns to the point at which the process is interrupted.

(2) Sig_ign

This symbol indicates that the signal is ignored, and after the corresponding signal () call is performed, the process ignores the type sig signal.

(3) SIG_DFL

This symbol indicates that the recovery system defaults to processing the signal.

Function Description:

Signal () Sets the processing function of the signal according to the signal number specified by the parameter signum. When the specified signal arrives, it jumps to the function execution specified by the parameter handler. When the signal processing function of a signal executes, if the process receives the signal again, the signal is automatically stored without interrupting the execution of the signal processing function until the signal processing function is finished and the corresponding handler function is called again. However, if the process receives other types of signals when the signal processing function executes, execution of the function is interrupted.

Return value: Returns the previous signal processing function pointer and returns SIG_ERR (-1) If there is an error.

The following conditions can produce signal:

Press CTRL + C to produce SIGINT

Hardware interrupts, such as 0, illegal memory access (SIGSEV), etc.

The KILL function can send a signal to a process

Kill command. is actually a wrapper over the kill function.

Software interruption. If the alarm clock timeout (Sigurg), write the data to the pipe after the reader is aborted (sigpipe), and so on

program Example:

This example installs a signal handler routine for sigfpe,catches an integer overflow condition, makes an adjustment to AX Register, and returns. This example the cause your computer to crash, and would produce runtime errors on depending which the 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 = 32767
ASM Inc. AX
    
//Cause overflow
asm into
//activate handler
Th E handler set AX to 3 on return. If that's hadn ' t happened, there would have been another exception when the
next ' into ' is executed after the
' de C ' instruction.
ASM Dec AX
/No overflow now ASM to//
doesn ' t activate return
0;
}

2 Signals:

Signal

Description

Sigabrt

Generated by calling the Abort function, the process does not exit normally

Sigalrm

The timer timeout or Setitimer function set by the alarm function sets the interval timer timeout

Sigbus

A specific hardware exception that is typically caused by memory access

Sigcancel

Used internally by the Solaris Thread Library and is not typically used

Sigchld

When a process terminate or stops, SIGCHLD sends it to its parent process. The signal is ignored by default

Sigcont

When the stop process resumes running, it automatically sends

Sigemt

and implementation-related hardware exceptions

Sigfpe

Math-related exceptions, such as being removed by 0, floating point overflow, etc.

Sigfreeze

Solaris dedicated, hiberate or suspended time to send

Sighup

Sent to the controlling Process with terminal, when terminal was disconnect

Sigill

Illegal instruction exception

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/

Siginfo

BSD signal. Generated by the status key, usually ctrl+t. Processes sent to all foreground group

SIGINT

Generated by interrupt key, usually CTRL + C or delete. Processes sent to all foreground group

Sigio

Asynchronous IO Events

Sigiot

Implementation of related hardware anomalies, generally corresponding to SIGABRT

SIGKILL

cannot be processed and ignored. Abort a process

Siglwp

Used internally by the Solaris Thread Libray

Sigpipe

Send the pipe when the reader is aborted

Sigpoll

Send when an event is sent to pollable device

Sigprof

Generated by the Setitimer specified profiling Interval timer

Sigpwr

and system-related. associated with UPS.

Sigquit

When you enter Quit key (ctrl+\) the process sent to all foreground group

SIGSEGV

Illegal memory access

Sigstkflt

Linux-specific, math coprocessor stack exception

SIGSTOP

Abort the process. cannot be processed and ignored.

Sigsys

Illegal system calls

Sigterm

Request abort process, kill command sent by default

Sigthaw

Solaris dedicated, sent from suspend recovery

Sigtrap

Implement the associated hardware exception. Typically debug exceptions

Sigtstp

Suspend Key, is generally ctrl+z. Processes sent to all foreground group

Sigttin

Sent when the process of background group attempts to read terminal

Sigttou

Send when background group's process tries to write terminal

Sigurg

may be sent when Out-of-band data is received

SIGUSR1

User-defined Signal 1

SIGUSR2

User-defined Signal 2

Sigvtalrm

Setitimer function set the virtual Interval timer timed out

Sigwaiting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.