• 中断处理


    主要调用singal()捕捉信号,指定函数处理

    #include <stdio.h>
    #include <sys/wait.h>
    #include <string.h>
    #include <errno.h>

    #define MAXLINE 20
    static void sig_int(int);

    int main(void)
    {
    char buf[MAXLINE]; /* from apue.h */
    pid_t pid;
    int status;

    //捕捉信号
    if(signal(SIGINT, sig_int) == SIG_ERR)
    puts("signal error");

    printf("%% "); /* print prompt (printf requires %% to print %) */
    while (fgets(buf, MAXLINE, stdin) != NULL) {
    if (buf[strlen(buf) - 1] == '\n')
    buf[strlen(buf) - 1] = 0; /* replace newline with null */

    if ((pid = fork()) < 0) {
    puts("fork error");
    } else if (pid == 0) { /* child */
    printf("pid:%d\n", pid);

    execlp(buf, buf, (char *)0);
    printf("couldn't execute: %s\n", buf);
    return 1;
    }

    /* parent */
    if ((pid = waitpid(pid, &status, 0)) < 0)
    {
    printf("waitpid error !status:%d\n", status);
    }
    printf("%% ");
    }
    return 0;
    }
    void sig_int(int signo){
    printf("%s\n", strerror(EINTR));
    }



  • 相关阅读:
    Gin+Gorm小项目
    python实现监控信息收集
    Vue引入Stylus
    Go搭建一个Web服务器
    saltstack高效运维
    04-01 Django之模板层
    03-01 Django之视图层
    02-01 Django之路由层
    HTTP协议
    01-01 Web应用
  • 原文地址:https://www.cnblogs.com/xiangzi888/p/2247754.html
Copyright © 2020-2023  润新知