进程间通讯代码实例以及相关函数释义

进程间通讯代码实例以及相关函数释义

练习:用户设计两个程序,要求进程A中自定义信号SIGUSR1的相应借口,要求进程B每隔一段时间向进程A发送SIGUSR1信号,测试进程A是否可以执行关联的相应接口。

一、processA中设置信号处理handler

#include <signal.h>
#include <stdio.h>
// 信号处理函数
void handler(int sig)
{
 switch (sig)
 {
 case SIGUSR1:
 printf("收到user1信号\n");
 break;
 default:
 break;
 }
}
int main(int argc, char const *argv[])
{
 signal(SIGUSR1, handler); // 设置信号处理函数
 while (1);
 return 0;
}

linux手册第七章的signal词条,用户可以自定义信号行为的有两个,SIGUSR1SIGUSR2,这里自定义SIGUSR1

 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)

linux第2章说明了signal函数的使用:

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

NAME
signal - ANSI C signal handling

SYNOPSIS

 #include <signal.h> 
	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").

sighandler _t 规定了传入的信号处理函数的类型,内部为信号传参,返回值为void。当程序收到SIGUSR1信号时handler就会被触发。

二、processB中发送信号

#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
 while (1)
 {
 kill(4778, SIGUSR1); // 提前查询A进程的进程pid
 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/types.h>
#include <signal.h>

​ 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信号

...

测试成功

作者:小镇青年达师傅原文地址:https://www.cnblogs.com/town-master-da/p/18800754

%s 个评论

要回复文章请先登录注册