Location>code7788 >text

Inter-process communication code examples and related functions interpretations

Popularity:359 ℃/2025-03-30 16:12:39

Inter-process communication code examples and related functions interpretations

Exercise: The user designs two programs, requiring the corresponding excuse of custom signal SIGUSR1 in process A, and requiring process B to send a SIGUSR1 signal to process A every once in a while, testing whether process A can execute the associated corresponding interface.

1. Set up signal processing handler in processA

#include <>
 #include <>

 // Signal processing function
 void handler(int sig)
 {
     switch (sig)
     {
         case SIGUSR1:
             printf("user1 signal received\n");
             break;
         default:
             break;
     }
 }

 int main(int argc, char const *argv[])
 {
     signal(SIGUSR1, handler); // Set signal processing function
     while (1);
     return 0;
 }

There are two sign entries in Chapter 7 of Linux Manual. Users can customize signal behavior.SIGUSR1andSIGUSR2, customize it hereSIGUSR1

   Signal      Standard   Action   Comment
   ────────────────────────────────────────────────────────────────────────
   SIGABRT      P1990      Core    Abort signal from abort(3)
   SIGALRM      P1990      Term    Timer signal from alarm(2)
   SIGBUS       P2001      Core    Bus error (bad memory access)
   SIGCHLD      P1990      Ign     Child stopped or terminated
   SIGCLD         -        Ign     A synonym for SIGCHLD
   SIGCONT      P1990      Cont    Continue if stopped
   SIGEMT         -        Term    Emulator trap
   SIGFPE       P1990      Core    Floating-point exception
   SIGHUP       P1990      Term    Hangup detected on controlling terminal
                                   or death of controlling process
   SIGILL       P1990      Core    Illegal Instruction
   SIGINFO        -                A synonym for SIGPWR
   SIGINT       P1990      Term    Interrupt from keyboard
   SIGIO          -        Term    I/O now possible (4.2BSD)
   SIGIOT         -        Core    IOT trap. A synonym for SIGABRT
   SIGKILL      P1990      Term    Kill signal
   SIGLOST        -        Term    File lock lost (unused)
   SIGPIPE      P1990      Term    Broken pipe: write to pipe with no
                                   readers; see pipe(7)
   SIGPOLL      P2001      Term    Pollable event (Sys V);
                                   synonym for SIGIO
   SIGPROF      P2001      Term    Profiling timer expired
   SIGPWR         -        Term    Power failure (System V)
   SIGQUIT      P1990      Core    Quit from keyboard
   SIGSEGV      P1990      Core    Invalid memory reference

   SIGSTKFLT      -        Term    Stack fault on coprocessor (unused)
   SIGSTOP      P1990      Stop    Stop process
   SIGTSTP      P1990      Stop    Stop typed at terminal
   SIGSYS       P2001      Core    Bad system call (SVr4);
                                   see also seccomp(2)
   SIGTERM      P1990      Term    Termination signal
   SIGTRAP      P2001      Core    Trace/breakpoint trap
   SIGTTIN      P1990      Stop    Terminal input for background process
   SIGTTOU      P1990      Stop    Terminal output for background process
   SIGUNUSED      -        Core    Synonymous with SIGSYS
   SIGURG       P2001      Ign     Urgent condition on socket (4.2BSD)
   SIGUSR1      P1990      Term    User-defined signal 1
   SIGUSR2      P1990      Term    User-defined signal 2
   SIGVTALRM    P2001      Term    Virtual alarm clock (4.2BSD)
   SIGXCPU      P2001      Core    CPU time limit exceeded (4.2BSD);
                                   see setrlimit(2)
   SIGXFSZ      P2001      Core    File size limit exceeded (4.2BSD);
                                   see setrlimit(2)
   SIGWINCH       -        Ign     Window resize signal (4.3BSD, Sun)

Chapter 2 of linux explains the use of signal functions:

SIGNAL(2) Linux Programmer's Manual SIGNAL(2)

NAME
signal - ANSI C signal handling

SYNOPSIS

    #include <>   
	typedef void (*sighandler_t)(int);

    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").

signalshandler _t specifies the type of incoming signal processing function, internally as signal parameters, and the return value is void. When the program receivesSIGUSR1The handler will be triggered when the signal is signaled.

2. Send signals in processB

#include <sys/>
 #include <>
 #include <>

 int main(int argc, char const *argv[])
 {

     While (1)
     {
         kill(4778, SIGUSR1); // Query the process pid of process A in advance
         sleep(1);
     }
     return 0;
 }

man 2 kill:

KILL(2) Linux Programmer's Manual KILL(2)

NAME
kill - send signal to a process

SYNOPSIS
#include <sys/>
#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.

Testing procedures

dada@dada-virtual-machine:~/test$ ./processA
Received user1 signal
Received user1 signal
Received user1 signal
Received user1 signal
Received user1 signal
Received user1 signal

...

Test successfully