zoukankan      html  css  js  c++  java
  • APUE_1.9Signals Figure1.10ReadCommandsFromStandardInputAndExecuteThem

    ~H09R47ZZRPVJ(MH7)YC)DR

    HA51$(QVPD~XM6ZB64V71QX

    I$6K9KOBT1}7DQO{K3B1}CV

    /*
     * 1.10ReadCommandsFromStandardInputAndExecuteThem.cpp
     *
     *  Created on: Feb 11, 2015
     *      Author: sunyj
     */
    
    #include "../apuesunyj.h"
    #include <sys/wait.h>
    
    static void sig_int(int); // our signal-catching function,static limit this function in this file
    
    int main(void)
    {
        char buf[MAXLINE];
        pid_t pid;
        int status;
        if (signal(SIGINT, sig_int) == SIG_ERR) // catch the ctrl + c signal
        {
            err_sys("signal error");
        }
    
        printf("%% "); /* print prompt (printf requires %% to print %) */
        // ctrl + c invoke function sig_int, ctrl + d end this loop
        while (fgets(buf, MAXLINE, stdin) != NULL)
        {
            if (buf[strlen(buf) - 1] == '
    ')
            {
                buf[strlen(buf) - 1] = 0; /* replace newline with null */
            }
            if ((pid = fork()) < 0)
            {
                err_sys("fork error");
            }
            else if (pid == 0)
            {   /* child */
                execlp(buf, buf, (char *) 0);
                err_ret("couldn't execute: %s", buf);
                exit(127);
            }
            /* parent */
            if ((pid = waitpid(pid, &status, 0)) < 0)
                err_sys("waitpid error");
            printf("%% ");
        }
        printf("EOF(ctrl + d) received
    ");
        printf("bye bye
    ");
        exit(0);
    }
    
    void sig_int(int signo)
    {
        printf("interrupt by signal ctrl + c 
    %% ");
    }
    
     

    #include "../apuesunyj.h"

    I8B4$NERCX@4CF)6)_V05(W

    IMG_1338IMG_1340IMG_1342IMG_1349IMG_1350IMG_1355IMG_1356IMG_1359IMG_1360IMG_1362IMG_1363IMG_1364IMG_1365IMG_1366IMG_1367

  • 相关阅读:
    11前端css动画
    10前端css文本和字体
    09前端css3渐变
    08前端css3背景
    07前端css3边框和圆角
    06前端css3增加选择器
    堡垒机Teleport
    Sublime Text 3注册及中文乱码问题解决
    Linux部署KMS激活Windows 10和Office 2016
    服务器维护实施步骤
  • 原文地址:https://www.cnblogs.com/sunyongjie1984/p/4320805.html
Copyright © 2011-2022 走看看