C language in the pause of function and alarm of function and sleep of function

  • 2020-04-02 03:21:30
  • OfStack

C pause() : allows a process to pause until a signal appears
The header file:


#include <unistd.h>

Definition function:


int pause(void);

The pause() function causes the current process to pause(go to sleep) until it is interrupted by a signal.

Return value: only -1.

Error code: EINTR has a signal arriving that interrupts this function.


C language alarm() function: sets the alarm clock for signaling
The header file:


#include <unistd.h>

Definition function:


unsigned int alarm(unsigned int seconds);

Alarm () is used to set the signal SIGALRM to be sent to the current process after the number of seconds specified by the parameter seconds.

Return value: returns the number of seconds remaining from the previous alarm, or 0 if the alarm was not previously set.

sample


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

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 sleep() function: allows a process to pause for a period of time
The header file:


 #include <unistd.h>

Definition function:


unsigned int sleep(unsigned int seconds);

Sleep () causes the current process to pause until the time specified in the parameter seconds is reached or is interrupted by a signal.

Return 0 if the process pauses for the time specified in the parameter seconds, or the number of remaining seconds if there is a signal interrupt.


Related articles: