C语言 pause() 是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15992574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:01:48  来源:igfitidea点击:

How does pause() work?

c

提问by Luis Quesado

I'm completely noob in c. I have to write a function mypause()that should have a functionality similar to the pause()system call, and test the mypause()function in a program that repeatedly blocks waiting for a signal. How does te pause()function works?? Can't I just do a mypause()like this:

我完全是 c 的菜鸟。我必须编写一个函数mypause(),该函数应该具有类似于pause()系统调用的mypause()功能,并在重复阻塞等待信号的程序中测试该函数。tepause()函数是如何工作的??我不能这样做mypause()

fprintf( stderr, "press any key to continue\n" );

in order for the program to block and wait for an signal?

为了让程序阻塞并等待信号?

Have in mind that I can't ever use pause()or sigpause().

请记住,我永远不能使用pause()sigpause()

采纳答案by Halim Qarroum

The pause()function blocks until a signal arrives. User inputs are not signals. A signal can be emitted by another process or the system itself.

pause()功能会阻塞,直到信号到达。用户输入不是信号。信号可以由另一个进程或系统本身发出。

Pressing Ctrl-Cfor instance, causes your shell to send a SIGINTsignal to the current running process, which in normalcases causes the process to be killed.

Ctrl-C例如,按下会使您的 shell 向SIGINT当前正在运行的进程发送信号,这在正常情况下会导致该进程被终止。

In order to emulate the behaviour of pausein ISO C99 you could write something like the following. The code is commented, if you have a question about this implementation, please ask.

为了模拟pauseISO C99 中的行为,您可以编写如下内容。代码已注释,如果您对此实现有疑问,请提问。

#include <unistd.h>
#include <stdio.h>
#include <signal.h>

/**
 * The type sig_atomic_t is used in C99 to guarantee
 * that a variable can be accessed/modified in an atomic way
 * in the case an interruption (reception of a signal for example) happens.
 */
static volatile sig_atomic_t done_waiting = 0;

static void     handler()
{
  printf("Signal caught\n");
  done_waiting = 1;
}

void    my_pause()
{
  /**
   *  In ISO C, the signal system call is used
   *  to call a specific handler when a specified
   *  signal is received by the current process.
   *  In POSIX.1, it is encouraged to use the sigaction APIs.
   **/
  signal(SIGINT, handler);
  done_waiting = 0;
  while ( !done_waiting )
    ;
}

int     main()
{
  my_pause();
  printf("Hey ! The first call to my_pause returned !\n");
  my_pause();
  printf("The second call to my_pause returned !\n");
  return (0);
}

Note this example only works with the SIGINTsignal. To handle an additional set of signals, you can use other calls to signal()with different signal numbers or use sigaction()with a mask referencing all the desired signals.

请注意,此示例仅适用于SIGINT信号。要处理一组额外的信号,您可以使用signal()具有不同信号编号的其他调用,或sigaction()与引用所有所需信号的掩码一起使用。

A complete list of the signals availables on your system can be found in you <signal.h>include.

您可以在<signal.h>包含中找到系统上可用信号的完整列表。