qidao123.com技术社区-IT企服评测·应用市场

标题: 进程间通讯代码实例以及相干函数释义 [打印本页]

作者: 半亩花草    时间: 2025-3-30 21:00
标题: 进程间通讯代码实例以及相干函数释义
进程间通讯代码实例以及相干函数释义

练习:用户设计两个程序,要求进程A中自界说信号SIGUSR1的相应借口,要求进程B每隔一段时间向进程A发送SIGUSR1信号,测试进程A是否可以执行关联的相应接口。
一、processA中设置信号处置惩罚handler
  1. #include <signal.h>
  2. #include <stdio.h>
  3. // 信号处理函数
  4. void handler(int sig)
  5. {
  6.     switch (sig)
  7.     {
  8.         case SIGUSR1:
  9.             printf("收到user1信号\n");
  10.             break;
  11.         default:
  12.             break;
  13.     }
  14. }
  15. int main(int argc, char const *argv[])
  16. {
  17.     signal(SIGUSR1, handler); // 设置信号处理函数
  18.     while (1);
  19.     return 0;
  20. }
复制代码
linux手册第七章的signal词条,用户可以自界说信号行为的有两个,SIGUSR1SIGUSR2,这里自界说SIGUSR1
  1.    Signal      Standard   Action   Comment
  2.    ────────────────────────────────────────────────────────────────────────
  3.    SIGABRT      P1990      Core    Abort signal from abort(3)
  4.    SIGALRM      P1990      Term    Timer signal from alarm(2)
  5.    SIGBUS       P2001      Core    Bus error (bad memory access)
  6.    SIGCHLD      P1990      Ign     Child stopped or terminated
  7.    SIGCLD         -        Ign     A synonym for SIGCHLD
  8.    SIGCONT      P1990      Cont    Continue if stopped
  9.    SIGEMT         -        Term    Emulator trap
  10.    SIGFPE       P1990      Core    Floating-point exception
  11.    SIGHUP       P1990      Term    Hangup detected on controlling terminal
  12.                                    or death of controlling process
  13.    SIGILL       P1990      Core    Illegal Instruction
  14.    SIGINFO        -                A synonym for SIGPWR
  15.    SIGINT       P1990      Term    Interrupt from keyboard
  16.    SIGIO          -        Term    I/O now possible (4.2BSD)
  17.    SIGIOT         -        Core    IOT trap. A synonym for SIGABRT
  18.    SIGKILL      P1990      Term    Kill signal
  19.    SIGLOST        -        Term    File lock lost (unused)
  20.    SIGPIPE      P1990      Term    Broken pipe: write to pipe with no
  21.                                    readers; see pipe(7)
  22.    SIGPOLL      P2001      Term    Pollable event (Sys V);
  23.                                    synonym for SIGIO
  24.    SIGPROF      P2001      Term    Profiling timer expired
  25.    SIGPWR         -        Term    Power failure (System V)
  26.    SIGQUIT      P1990      Core    Quit from keyboard
  27.    SIGSEGV      P1990      Core    Invalid memory reference
  28.    SIGSTKFLT      -        Term    Stack fault on coprocessor (unused)
  29.    SIGSTOP      P1990      Stop    Stop process
  30.    SIGTSTP      P1990      Stop    Stop typed at terminal
  31.    SIGSYS       P2001      Core    Bad system call (SVr4);
  32.                                    see also seccomp(2)
  33.    SIGTERM      P1990      Term    Termination signal
  34.    SIGTRAP      P2001      Core    Trace/breakpoint trap
  35.    SIGTTIN      P1990      Stop    Terminal input for background process
  36.    SIGTTOU      P1990      Stop    Terminal output for background process
  37.    SIGUNUSED      -        Core    Synonymous with SIGSYS
  38.    SIGURG       P2001      Ign     Urgent condition on socket (4.2BSD)
  39.    SIGUSR1      P1990      Term    User-defined signal 1
  40.    SIGUSR2      P1990      Term    User-defined signal 2
  41.    SIGVTALRM    P2001      Term    Virtual alarm clock (4.2BSD)
  42.    SIGXCPU      P2001      Core    CPU time limit exceeded (4.2BSD);
  43.                                    see setrlimit(2)
  44.    SIGXFSZ      P2001      Core    File size limit exceeded (4.2BSD);
  45.                                    see setrlimit(2)
  46.    SIGWINCH       -        Ign     Window resize signal (4.3BSD, Sun)
复制代码
linux第2章说明了signal函数的利用:
SIGNAL(2)                                                  Linux Programmer's Manual                                                 SIGNAL(2)
NAME
signal - ANSI C signal handling
SYNOPSIS
  1.     #include <signal.h>   
  2.         typedef void (*sighandler_t)(int);
  3.     sighandler_t signal(int signum, sighandler_t handler);
复制代码
DESCRIPTION
signal() sets the disposition of the signal signum to handler, which is either SIG_IGN, SIG_DFL, or the address of a programmer-defined function (a "signal handler").
sighandler _t 规定了传入的信号处置惩罚函数的类型,内部为信号传参,返回值为void。当程序收到SIGUSR1信号时handler就会被触发。
二、processB中发送信号
  1. #include <sys/types.h>
  2. #include <signal.h>
  3. #include <unistd.h>
  4. int main(int argc, char const *argv[])
  5. {
  6.     while (1)
  7.     {
  8.         kill(4778, SIGUSR1);  // 提前查询A进程的进程pid
  9.         sleep(1);
  10.     }
  11.     return 0;
  12. }
复制代码
man 2 kill:
KILL(2)                                                    Linux Programmer's Manual                                                   KILL(2)
NAME
kill - send signal to a process
SYNOPSIS
#include
#include
​       int kill(pid_t pid, int sig);
​       Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
​       kill(): _POSIX_C_SOURCE
DESCRIPTION
The kill() system call can be used to send any signal to any process group or process.
测试程序
dada@dada-virtual-machine:~/test$ ./processA
收到user1信号
收到user1信号
收到user1信号
收到user1信号
收到user1信号
收到user1信号
...
测试乐成

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.4