Pause () function and alarm () function and sleep () function _c language in C language

Source: Internet
Author: User
Tags sleep

C language Pause () function: Let the process pause until the signal appears
header file:

#include <unistd.h>

To define a function:

int pause (void);

Function Description: Pause () will suspend the current process (go to sleep state) until it is interrupted by a signal (signal).

Return value: Returns only-1.

Error code: EINTR has a signal arrival interrupt this function.


C language Alarm () function: Set signal transmission alarm Clock
header file:

#include <unistd.h>

To define a function:

unsigned int alarm (unsigned int seconds);

Function Description: Alarm () is used to set the signal SIGALRM to the current process after the number of seconds specified by the parameter seconds. If the parameter seconds is 0, the previous set alarm is canceled and the remaining time is returned.

Return value: Returns the number of seconds left before the alarm, and returns 0 if no previous alarm is set.

Example

#include <unistd.h>
#include <signal.h>
void handler ()
{
  printf ("hello\n");
}

Main ()
{
  int i;
  Signal (SIGALRM, handler);
  Alarm (5);
  for (i = 1; i < 7; i++)
  {
    printf ("Sleep%d ... \ n", i);
    Sleep (1);
  }
}

Perform:

Sleep 1
... Sleep 2
... Sleep 3
... Sleep 4
... Sleep 5 ... hello sleep
6 ...

C language Sleep () function: Let a process suspend execution for a period of time
header file:

 #include <unistd.h>

To define a function:

unsigned int sleep (unsigned int seconds);

Function Description: Sleep () pauses the current process until it reaches the time specified by the parameter seconds or is interrupted by a signal.

Return value: Returns 0 if the process pauses to the specified time of the parameter seconds, and returns the number of seconds if a signal is interrupted.

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.