• 详解linux进程间通信-信号


      前言:之前说看《C++ Primer 》暂时搁浅一下,迷上公司大神写的代码,想要明白,主要是socket、进程间通信!

      知道进程间通信:信号、信号量、管道、消息队列、共享内存(共享存储),也能写些简单代码进行通信,但不知道应用在哪?感觉很多小伙伴跟我有类似经历吧?

      一、应用实例:

      要去linux设备上去添加、改密用户:本地去linux设备添加用户,用socket实现,其实大神改的ssh源码来实现的,这不是我要讲的重点,而我要讲的是准备过程,去登陆linux设备,要准备好:管理员、密码等。

      简略说明中间过程:页面点击添加,调用cli(socket的客户端,socket本地通信),server.c(socket服务端)里fork子进程,而实现登录linux设备的是ssh进程,但是ssh进程需要管理员、密码等信息,那子进程怎么传给它,就需要进程间通信方式了,大神用的是消息队列。

      讲到这里不知道明白了么?有问题可以随时留言。

      接下来将详解进程间通信方式。

      二、进程间通信-信号:

      1、信号概念:

      信号的概念
      首先,每个信号都有一个名字。这些名字都以三个字符 S I G开头。 S I G A L R M是闹钟信号,当由a l a r m函数设置的时
    间已经超过后产生此信号。 

       产生信号条件:

      • 当用户按某些终端键时,产生信号。
      • 硬件异常产生信号:除数为0、无效的存储访问等等。
      • 进程用k i l l 函数可将信号发送给另一个进程或进程组。
      • 用户可用k i l l 命令将信号发送给其他进程。此程序是 k i l l函数的界面。
      • 当检测到某种软件条件已经发生,并将其通知有关进程时也产生信号。

      可以要求系统在某个信号出现时按照下列三种方式中的一种进行操作:
      (1) 忽略此信号。 
      (2) 捕捉信号。 
      (3) 执行系统默认动作。
      2、signal函数

      signal(int signum, sighandler_t handler); 

      格式 :signal(信号,信号处理函数)
      功能 :登记信号 当信号到来时 执行信号处理函数 而不采用默认的处理方式

      注:sighandler_t是函数指针,typedef void (*sighandler_t)(int);sighandler_t signal(int signum, sighandler_t handler);

      程序:检测信号,调用处理函数 

    #include "my.h"
    
    void sig_fun(int sig)
    {
    puts("ding-ling!");
    }
    
    int main()
    {
    signal(SIGALRM,sig_fun);
    alarm(5);
    for(;;) {} 
    return 0;
    
    }

    ---程序2.2---

    注:

      5秒之后产生产生SIGALRM信号,调用sig_fun函数。

      alarm函数:

      使用a l a r m函数可以设置一个时间值 (闹钟时间),在将来的某个时刻该时间值会被超过。当
    所设置的时间值被超过后,产生 S I G A L R M信号。

      #include <unistd.h>
      unsigned int alarm(unsigned int s e c o n ds) ;
      

      注:“my.h”是我自定义头文件,因为头文件方便书写,为了便于调试和理解,把头文件粘出来,如下: 

    #ifndef MY_H_
    #define MY_H_
    
    #include <signal.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    #include <sys/types.h> 
    #include <sys/socket.h>
    #include <pthread.h>
    #include <semaphore.h>
    #endif

      3、其他常用函数

      (1)killr a i s e函数
      k i l l函数将信号发送给进程或进程组。 r a i s e函数则允许进程向自身发送信号。  
      

      #include <sys/types.h>  

      #include <signal.h>

      int kill(pid_t p id, int s i g no) ;
      int raise(int s i g n o) ;
      两个函数返回:若成功则为 0,若出错则为-1

      (2)pause函数
      p a u s e函数使调用进程挂起直至捕捉到一个信号。
      #include <unistd.h>
      int pause(void);

      程序:优化“程序2.2”,用pause函数代替死循环,检测信号之后并退出,如下:

    #include "my.h"
    
    void sig_fun(int sig)
    {
    puts("ding-ling!");
    }
    
    int main()
    {
    signal(SIGALRM,sig_fun);
    alarm(5);
    printf("111
    ");
    pause();
    //for(;;) {} 
    printf("222
    ");
    return 0;
    
    }
    
    ---程序2.3---

    总结:以上介绍进程间通信应用实例,然后介绍了进程间通信的信号,接下来会介绍其余进程间通信方式。

     

      

     

      

      

      

      

  • 相关阅读:
    C语言I博客作业03
    C语言I博客作业02
    JSON Template
    Mac系统登录不进系统解决办法
    Mac怎么安装并配置Homebrew?
    tensorflow 安装命令
    删除表重复数据
    redhat安装docker
    Rabbit MQ 入门指南
    Maven 集成Tomcat7插件
  • 原文地址:https://www.cnblogs.com/liudw-0215/p/8946879.html
Copyright © 2020-2023  润新知