Linux programming signal (4): alarm and reentrant Functions

Source: Internet
Author: User

1. alarm ()

Before you reentrant a function, let's take a look at the usage of the alarm () function:

# Include <unistd. h>

Unsigned int alarm (unsigned int seconds)

The system calls alarm to schedule the kernel to send a SIGALRM signal to the calling process after the specified seconds. If the specified seconds parameter is 0, the SIGALRM signal is no longer sent. The last setting will cancel the previous setting. The returned value is the time remaining between the last scheduled call and sending, or 0 is returned because there is no previous scheduled call.

Note: in use, alarm is only set to send a signal once. If you want to send a signal multiple times, you must use alarm for multiple calls.

Man help description:


DESCRIPTION
Alarm () arranges for a SIGALRM signal to be delivered to the calling
Process in seconds.

If seconds is zero, no new alarm () is scheduled.

In any event any previusly set alarm () is canceled.

RETURN VALUE
Alarm () returns the number of seconds remaining until any previusly
Scheduled alarm was due to be delivered, or zero if there was no previ-
Ously scheduled alarm.

Example:

#include <unistd.h><sys/stat.h><sys/wait.h><sys/types.h><fcntl.h><stdlib.h><stdio.h><errno.h><.h><signal.h> ERR_EXIT(m) \    ( handler( main( argc,  * (signal(SIGALRM, handler) ==  handler(

Result:

<Sys/stat. h> <sys/wait. h> <sys/types. h> <fcntl. h> <stdlib. h> <stdio. h> <errno. h> <. h> <signal. h> ERR_EXIT (m) \ (handler (main (argc, * ={,={, (signal (SIGALRM, handler) ==== handler (

Result:

The program also creates a struct, sets a global variable, and then uses two local variables in the main function to assign values to the global variables respectively, because this assignment operation can be interrupted, for example, the assignment of each struct can be considered as two steps:

G_data.a = zeros.;

G_data. B = zeros. B;

So when g_data.a = one. a. After the operation is complete and interrupted, run to execute the processing function and call unsafe_fun () in the processing function to print the global variable value. The result is that the value of global variable a has changed, the B value is still not here and changed, so 1, 0 appears.

So the results are uncertain.

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.