如果我用scanf函数程序终止 [英] Program terminates if I use scanf

查看:152
本文介绍了如果我用scanf函数程序终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现在code定时器,我发现使用而(1)的所有示例( ;;)但是当我用试过 scanf函数我的程序终止。难道是混得标准输入任何价值,因为如果我使用 scanf函数然后两次定时从程序退出之前被称为两个时间。
下面是我的示例code:

I am trying to implement the timer in code, All the example that I found are using while(1) or for(;;) but when I tried with using scanf my program terminates. Is it getting any value on stdin because if I use scanf two times then timer is called two time before exiting from program. Here is my sample code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <netinet/in.h>
#include <signal.h>
#include <time.h>

#include <linux/socket.h>
#include <time.h>


#define SIGTIMER (SIGRTMAX)
#define SIG SIGUSR1
static timer_t     tid;
static timer_t     tid2;

void SignalHandler(int, siginfo_t*, void* );
timer_t SetTimer(int, int);

int main(int argc, char *argv[]) {

    int t;

    printf("SIGRTMAX %d\n", SIGRTMAX);
        printf("SIGRTMIN %d\n", SIGRTMIN);

    struct sigaction sigact;
    sigemptyset(&sigact.sa_mask);
    sigact.sa_flags = SA_SIGINFO;
    sigact.sa_sigaction = SignalHandler;
    // set up sigaction to catch signal
    if (sigaction(SIGTIMER, &sigact, NULL) == -1) {
        perror("sigaction failed");
        exit( EXIT_FAILURE );
    }

    // Establish a handler to catch CTRL+c and use it for exiting.
    sigaction(SIGINT, &sigact, NULL);
    tid=SetTimer(SIGTIMER, 1000);

    struct sigaction sa;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = SignalHandler;
    // set up sigaction to catch signal
    if (sigaction(SIG, &sa, NULL) == -1) {
        perror("sa failed");
        exit( EXIT_FAILURE );
    }

    // Establish a handler to catch CTRL+c and use it for exiting.
    sigaction(SIGINT, &sa, NULL);
    tid2=SetTimer(SIG, 1000);

   // for(;;);  or while(1) Working properly

    scanf("%d", &t); /// Program terminates
    return 0;
}

void SignalHandler(int signo, siginfo_t* info, void* context)
{


    if (signo == SIGTIMER) {
        printf("Command Caller has ticked\n");

    }else if (signo == SIG) {
        printf("Data Caller has ticked\n");

    } else if (signo == SIGINT) {
        timer_delete(tid);
        perror("Crtl+c cached!");
        exit(1);  // exit if CRTL/C is issued
    }
}
timer_t SetTimer(int signo, int sec)
{
    static struct sigevent sigev;
    static timer_t tid;
    static struct itimerspec itval;
    static struct itimerspec oitval;

    // Create the POSIX timer to generate signo
    sigev.sigev_notify = SIGEV_SIGNAL;
    sigev.sigev_signo = signo;
    sigev.sigev_value.sival_ptr = &tid;

    if (timer_create(CLOCK_REALTIME, &sigev, &tid) == 0)
    {

        itval.it_value.tv_sec = sec / 1000;
        itval.it_value.tv_nsec = (long)(sec % 1000) * (1000000L);
        itval.it_interval.tv_sec = itval.it_value.tv_sec;
        itval.it_interval.tv_nsec = itval.it_value.tv_nsec;



        if (timer_settime(tid, 0, &itval, &oitval) != 0)
        {
            perror("time_settime error!");
        }
    }
    else
    {
        perror("timer_create error!");
        return NULL;
    }
    return tid;
}

所以,我怎样才能解决这个问题呢?
任何帮助将是鸭preciated。
谢谢,Yuvi

So, How can I resolve this problem ? Any help would be Appreciated. Thanks, Yuvi

推荐答案

信号中断scanf函数。如果添加 PERROR('scanf函数')后scanf函数输出将是:

Signal interrupts scanf. If you add perror('scanf') after scanf output will be:


SIGRTMAX 64
SIGRTMIN 34
Command Caller has ticked
Data Caller has ticked
scanf: Interrupted system call

如果您替换 scanf函数


do {
    errno = 0;
    scanf("%d", &t);
} while(errno == EINTR);

当它失败,中断的系统调用 scanf函数将重试。

scanf will be retried when it fail with Interrupted system call.

这篇关于如果我用scanf函数程序终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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