• IO进程间通信:信号


    3、信号

    用户进程对信号的响应方式:

    * 忽略信号:对信号不做任何处理,但是有两个信号不能忽略:即SIGKILL及SIGSTOP。

    * 捕捉信号:定义信号处理函数,当信号发生时,执行相应的处理函数。

    * 执行缺省操作:Linux对每种信号都规定了默认操作

    1、kill: 向进程发射信号

        #include

        int kill(pid_t pid, int sig);

          参数:

            pid: 进程号, 0:同一个进程组, -1:信号发给所有的进程表中的进程(除了进程号最大的进程外)

            sig: 信号

        返回值:

            成功: 0

            失败: -1, 并设置errno

               

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. int main(int argc, char *argv[])
    8. {
    9. pid_t pid = fork();
    10. if (pid < 0)
    11. {
    12. perror("fork");
    13. return -1;
    14. }
    15. else if (0 == pid) //child
    16. {
    17. sleep(5);
    18. printf("child: pid = %d\n", getpid());
    19. exit(10);
    20. }
    21. printf("parent!\n");
    22. sleep(2);
    23. if (0 > kill(pid, SIGKILL)) //向进程发射信号
    24. {
    25. perror("kill");
    26. return -1;
    27. }
    28. int wstatus;
    29. pid = wait(&wstatus); //获取子进程退出状态
    30. if (WIFEXITED(wstatus)) //判断是否是正常退出
    31. {
    32. printf("Child process exit normally!\n");
    33. printf("exit: %d\n", WEXITSTATUS(wstatus)); //获取退出值
    34. }
    35. else
    36. printf("Chils process exit unnormally!\n");
    37. printf("wait: pid = %d\n", pid);
    38. return 0;
    39. }

    2、raise: 向自己进程发射信号

        #include

          int raise(int sig);

          参数:

            sig: 信号

        返回值:

            成功: 0

            失败: 非0

               

    3、alarm: 闹钟函数

        #include

          unsigned int alarm(unsigned int seconds);

          参数:

            seconds:

    返回值:

    1. #include
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. alarm(3);
    6. sleep(2);
    7. printf("%u\n", alarm(5));
    8. while (1)
    9. {
    10. sleep(1);
    11. printf("1s go out...\n");
    12. }
    13. return 0;
    14. }

    4、signal:注册信号的处理方式

        #include

        typedef void (*sighandler_t)(int);

        sighandler_t signal(int signum, sighandler_t handler);

        参数:

            signum:  信号

            handler:

                 SIG_IGN: 忽略信号

    1. #include
    2. #include
    3. #include
    4. int main(int argc, char *argv[])
    5. {
    6. signal(SIGINT, SIG_IGN);
    7. while (1)
    8. {
    9. printf("hello world!\n");
    10. sleep(1);
    11. }
    12. return 0;
    13. }

                SIG_DFL: 缺省信号

                函数的入口地址: 捕获信号

    signal捕获信号

    1. #include
    2. #include
    3. #include
    4. void handler(int sig)
    5. {
    6. if (SIGINT == sig)
    7. printf("I got SIGINT signal!\n");
    8. else if (SIGQUIT == sig)
    9. printf("I got SIGQUIT signal!\n");
    10. }
    11. int main(int argc, char *argv[])
    12. {
    13. signal(SIGINT, handler);
    14. signal(SIGQUIT, handler);
    15. while (1)
    16. {
    17. printf("hello world!\n");
    18. sleep(1);
    19. }
    20. return 0;
    21. }

    用父进程来捕获子进程的死亡状态信号:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. void handler(int sig)
    8. {
    9. wait(NULL);
    10. printf("wait end!\n");
    11. }
    12. int main(int argc, char *argv[])
    13. {
    14. signal(SIGCHLD, handler);
    15. pid_t pid;
    16. if (0 > (pid = fork()))
    17. {
    18. perror("fork");
    19. return -1;
    20. }
    21. else if (0 == pid)
    22. {
    23. sleep(3);
    24. printf("Child exit!\n");
    25. exit(0);
    26. }
    27. while (1)
    28. {
    29. printf("Parent!\n");
    30. sleep(1);
    31. }
    32. return 0;
    33. }

  • 相关阅读:
    Vue+SpringBoot打造考研专业课程管理系统
    Python —— UI自动化之Page Object模式
    [思维]Theramore 2022杭电多校第8场 1001
    vue-router(基本使用、路由重定向、多级路由、路由命名、路由的query和params参数、路由的props配置)(十一)
    力扣-python-两数相加
    Xavier(7):ubuntu主机减少 NVIDIA sdkmanager 及相关文件的占用空间
    CMake (零基础一)
    python 中文字符转换unicode及Unicode 编码转换为中文
    Liinux——(网络)socket编程
    一款支持功能安全车规级 线性PMIC稳压器 NCV4274CDS50R4G 解决方案:高效率、更智能、强功能安全
  • 原文地址:https://blog.csdn.net/qq_63626307/article/details/126515631