与信号的Scanf [英] Scanf with Signals

查看:228
本文介绍了与信号的Scanf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个信号,阻止SIGINT,并基本上说对不起,你不能退出。\\\



问题是这可能发生在scanf



在scanf期间发生这种情况时,scanf将输入printf作为输入。



一个printf会使scanf基本上自动按下回车键。我不在乎我得到不好的输入。我只是想用一个printf或其他东西来编程完成scanf。



处理:



scanf获得东西)
- >用户可以输入的东西。



- > SIGINT发生并去我的处理程序。



- > Handler说Blah blah blah到stdout。



- > Scanf已经采取了这个blah blah blah, 。



如何使它在我返回scanf完成时(不管它收集什么,我只是希望它继续没有用户帮助)。



编辑:如果我发送两个信号,则scanf终止。我想以某种方式模拟scanf的结尾。

解决方案

你的问题表明你很困惑 -


我有一个信号阻止SIGINT,并基本上说对不起,你不能退出。\\\
p>

你可能有一个信号处理程序设置为响应SIGINT。此外,你可能使用'signal()'函数设置处理程序 - 但你应该使用POSIX标准的'sigaction()'函数来设置处理程序。


$ b


在上下文中,'this '可能是一个中断信号,由希望停止程序的用户输入。注意停止人们使用中断信号退出程序;如果你不让他们这样做,他们会更残酷。这可能意味着它们会产生SIGQUIT(也许是一个核心转储)如果你也阻止,还有一些其他的技巧,他们可以尝试,直到他们达到终极的杀9代,你的程序将没有机会作出反应。


当scanf发生这种情况时,scanf将输入printf作为输入。


是混乱的...你可能暗示从'printf()'语句(可能是说,你不能退出)的输出然后被视为输入到'scanf()'?这似乎是不可能的...这将需要一个非常,非常奇怪的设置I / O的过程,我仍然不相信它可能发生。


如何做一个printf,这会导致scanf基本上自动按下回车键。我不在乎我得到不好的输入。我只是想用一个printf或其他东西来编程完成scanf。


有几种可能性 - 它部分取决于O / S你正在使用(即使它是POSIX。)我不使用'scanf()';我觉得很难控制。如果您的系统在中断处理程序返回后恢复读取,那么您将有一个困难的时间。有时,尽管,'scanf()'将停止短,并返回已处理的项目数。如果'scanf()'你没有终止,你将需要考虑在一个简单调用'setjmp()'的函数中插入'sigsetjmp()',然后调用你的函数调用'scanf 。然后你的信号处理程序可以使用'siglongjmp()'返回到中间函数:

  sigjmp_buf trap; 

int intermediateary(int argument)
{
if(sigsetjmp(trap)== 0)
{
function_calling_scanf
return 0; // Success
}
//报告失败
return -1;
}

您的说明继续:


过程:

  scanf(get stuff) - >用户能够输入内容。

- > SIGINT发生并转到我的处理程序。



- > Handler对stdout说Blah blah blah。



- > Scanf采取了这个blah blah blah等待更多输入。


你怎么知道scanf已经阅读了blah blah blah?


我如何做到这样,当我返回scanf完成(不关心什么


使用 sigsetjmp()


编辑:如果我发送两个信号,那么scanf终止。我想以编程方式模拟scanf的结尾。


这表示你正在使用'signal你的信号处理程序而不是'sigaction()'。使用signal(),当中断发生时,信号处理程序将恢复为默认值。使用'sigaction()',你必须请求行为;默认情况下,接收到的信号(SIGINT)对于duraction被阻塞,并且信号处理程序保持有效。如果第一个中断发生在第一个中断运行时,第二个中断发生,直到第一个处理程序返回(此时处理程序将被重新输入)。


I have a signal that blocks SIGINT and basically says "Sorry, you can't quit.\n"

The issue is this can occur during a scanf.

When this occurs during a scanf, scanf takes in the printf as input.

How can I do a printf that will cause scanf to basically hit the enter key automatically. I don't care that I am getting bad input. I just want to programatically finish that scanf with a printf or something else.

Process:

scanf("get stuff") -> User is able to enter stuff in.

-> SIGINT occurs and goes to my handler.

-> Handler says "Blah blah blah" to stdout.

-> Scanf has taken this blah blah blah and is waiting for more input.

How do I make it so that when I return scanf is finished (don't care what it has gathered I just want it to continue without user help).

EDIT: if I send two signals then the scanf terminates. I want to emulate the ending of the scanf somehow programatically.

解决方案

Your question suggests you are confused - or perhaps English is not your native language.

I have a signal that blocks SIGINT and basically says "Sorry, you can't quit.\n"

What you might have is a signal handler that is set to respond to SIGINT. Further, you might be using the 'signal()' function to set the handler - but you should be aiming to use the POSIX standard 'sigaction()' function to set the handler instead.

The issue is this can occur during a scanf.

In context, 'this' is presumably an interrupt signal, typed by the user who wishes to stop your program. Be cautious about stopping people exiting a program with an interrupt signal; if you don't let them do that, they will be more brutal. That might mean they'll generate SIGQUIT (and perhaps a core dump) instead; if you block that too, there are a number of other tricks they can try until they get to the ultimate 'kill -9 pid' which your program will get no chance to react to.

When this occurs during a scanf, scanf takes in the printf as input.

This is confused...you are presumably implying that the output from a 'printf()' statement (presumably the one that says "You can't quit") is then being seen as input to the 'scanf()'? Which seems pretty improbable... It would require a very, very weird setup on the I/O of the process, and I'm still not convinced it can happen.

How can I do a printf that will cause scanf to basically hit the enter key automatically. I don't care that I am getting bad input. I just want to programatically finish that scanf with a printf or something else.

There are several possibilities - it depends in part on the O/S you are using (even if it is POSIX.) I don't use 'scanf()'; I find it too difficult to control. If your system resumes the reads after the interrupt handler returns, then you will have a difficult time. Sometimes, though, 'scanf()' will stop short and return the number of items it has processed. If the 'scanf()' you have does not terminate, then you will need to think about interposing a 'sigsetjmp()' in a function that simply calls 'setjmp()' and then invokes your function that calls 'scanf()'. Then your signal handler can use 'siglongjmp()' to return to that intermediate function:

sigjmp_buf trap;

int intermediary(int argument)
{
    if (sigsetjmp(trap) == 0)
    {
         function_calling_scanf(argument);
         return 0;  // Success
    }
    // Report failure
    return -1;
}

Your description continues:

Process:

   scanf("get stuff") -> User is able to enter stuff in.

-> SIGINT occurs and goes to my handler.

-> Handler says "Blah blah blah" to stdout.

-> Scanf has taken this blah blah blah and is waiting for more input.

How do you know that scanf has read the 'blah blah blah'? It seems very, very improbable to me.

How do I make it so that when I return scanf is finished (don't care what it has gathered I just want it to continue without user help).

Use sigsetjmp().

EDIT: if I send two signals then the scanf terminates. I want to emulate the ending of the scanf somehow programmatically.

This is what indicates that you are using 'signal()' to set your signal handler and not 'sigaction()'. With 'signal()', when the interrupt occurs, the signal handler is set back to the default. With 'sigaction()', you have to request that behaviour; by default, the received signal (SIGINT) is blocked for the duraction and the signal handler remains in effect. If a second interrupt occurs while the first is running, the second is held up until the first handler returns (at which point the handler will be re-entered).

这篇关于与信号的Scanf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆