马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
进程间通讯代码实例以及相干函数释义
练习:用户设计两个程序,要求进程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词条,用户可以自界说信号行为的有两个,SIGUSR1和SIGUSR2,这里自界说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
#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企服之家,中国第一个企服评测及商务社交产业平台。 |