Simple use of sigaction and Signal

Source: Internet
Author: User

# Include <unistd. h>
# Include <stdio. h>
# Include <signal. h>
# Include <sys/time. h>

Typedef struct _ delete_file_para_str
{
Int intdays;
Char * strpaths;
} Deletefileparastr;
// Sigaction = Signal
Deletefileparastr strpara = {9876, "/root/test |/var/Data "};
Void handle (int s, siginfo_t * info, void * D)
{
Deletefileparastr * punionpara = (deletefileparastr *) Info-> si_ptr;
Printf ("SIGUSR1 handle parameter: % d, % s \ n", punionpara-> intdays, punionpara-> strpaths );
}
Void handle_timer (int s)
{
Printf ("handle_timer begin... \ n ");
Union sigval val;
 
Val. sival_ptr = & strpara;

Sigqueue (getpid (), SIGUSR1, Val );
}
Main ()
{
Struct sigaction act = {};
Act./* 2 */sa_sigaction = handle; // use non-traditional processing functions
Sigemptyset (& (Act. sa_mask ));
Act. sa_flags =/* 1 */sa_siginfo; // 0 indicates that the traditional processing function is used.
// Sa_siginfo indicates that the data transmission processing function is used.
Sigaction (SIGUSR1, & act, 0); // register the signal and signal behavior
 
/* Timer operation */
Struct itimerval Val = {};
Signal (sigalrm, handle_timer );
Val. it_interval. TV _sec = 2;
Val. it_value. TV _usec = 1;
Setitimer (itimer_real, & Val, 0 );

 

While (1 );
}

 

 

/* Output result

Handle_timer begin...
SIGUSR1 handle parameter: 9876,/root/test |/var/Data

Handle_timer begin...
SIGUSR1 handle parameter: 9876,/root/test |/var/Data

Handle_timer begin...
SIGUSR1 handle parameter: 9876,/root/test |/var/Data

Handle_timer begin...
SIGUSR1 handle parameter: 9876,/root/test |/var/Data

Handle_timer begin...
SIGUSR1 handle parameter: 9876,/root/test |/var/Data

 

*/

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.