UNIX系统中的进程间通信与程序分析调试
1. 进程间通信
在UNIX系统中,进程间通信是一个重要的话题。其中,无名管道在使用时,不相关的进程也可以发起请求。为了让程序能够找到服务器进程的进程ID,从而发送信号,还包含了一种连接机制。
以下是服务器进程的代码示例:
/*server.c - server process*/ #include <stdio.h> #include <signal.h> #include <fcntl.h> #include "packet.h" int datapipe, ctrlpipe, datafile, got_sig; char * dataname = "/usr/lib/tmac/tmac.s"; handler () { signal(SIGUSR1, handler); got_sig++; } main () { struct packet pk; struct packet sendpk; /*for telling people our pid*/ ctrlpipe = open(CNAME, O_RDONLY|O_NOELAY); datafile = open(dataname, 0); sendpk.pk_pid = getpid(); sendpk.pk_code = SENDPID; handler(); /*catch interrupts*/ for (; ;) { int n; while ((n