IPC:Interrupts and Signals

本文介绍进程间通过信号进行通信的方法,包括信号的定义、类型、发送及处理方式,并提供使用kill()和signal()函数的实际代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In this section will look at ways in which two processes can communicate. When a process terminates abnormally it usually tries to send a signal indicating what went wrong. C programs (and UNIX) can trap these for diagnostics. Also user specified communication can take place in this way.

Signals are software generated interrupts that are sent to a process when a event happens. Signals can be synchronously generated by an error in an application, such as SIGFPE and SIGSEGV, but most signals are asynchronous. Signals can be posted to a process when the system detects a software event, such as a user entering an interrupt or stop or a kill request from another process. Signals can also be come directly from the OS kernel when a hardware event such as a bus error or an illegal instruction is encountered. The system defines a set of signals that can be posted to a process. Signal delivery is analogous to hardware interrupts in that a signal can be blocked from being delivered in the future. Most signals cause termination of the receiving process if no action is taken by the process in response to the signal. Some signals stop the receiving process and other signals can be ignored. Each signal has a default action which is one of the following:

  • The signal is discarded after being received
  • The process is terminated after the signal is received
  • A core file is written, then the process is terminated
  • Stop the process after the signal is received

Each signal defined by the system falls into one of five classes:

  • Hardware conditions
  • Software conditions
  • Input/output notification
  • Process control
  • Resource control

Macros are defined in <signal.h> header file for common signals.

These include:

SIGHUP 1 /* hangup */SIGINT 2 /* interrupt */
SIGQUIT 3 /* quit */SIGILL 4 /* illegal instruction */
SIGABRT 6 /* used by abort */SIGKILL 9 /* hard kill */
SIGALRM 14 /* alarm clock */
SIGCONT 19 /* continue a stopped process */
SIGCHLD 20 /* to parent on child stop or exit */

Signals can be numbered from 0 to 31.

Sending Signals -- kill(), raise()

There are two common functions used to send signals

int kill(int pid, int signal) - a system call that send a signal to a process, pid. If pid is greater than zero, the signal is sent to the process whose process ID is equal to pid. If pid is 0, the signal is sent to all processes, except system processes.

kill() returns 0 for a successful call, -1 otherwise and sets errno accordingly.

int raise(int sig) sends the signal sig to the executing program. raise() actually uses kill() to send the signal to the executing program:

          kill(getpid(), sig);

There is also a UNIX command called kill that can be used to send signals from the command line - see man pages.

NOTE: that unless caught or ignored, the kill signal terminates the process. Therefore protection is built into the system.

Only processes with certain access privileges can be killed off.

Basic rule: only processes that have the same user can send/receive messages.

The SIGKILL signal cannot be caught or ignored and will always terminate a process.

For examplekill(getpid(),SIGINT); would send the interrupt signal to the id of the calling process.

This would have a similar effect to exit() command. Also ctrl-c typed from the command sends a SIGINT to the process currently being.

unsigned int alarm(unsigned int seconds) -- sends the signal SIGALRM to the invoking process after seconds seconds.

Signal Handling -- signal()

An application program can specify a function called a signal handler to be invoked when a specific signal is received. When a signal handler is invoked on receipt of a signal, it is said to catch the signal. A process can deal with a signal in one of the following ways:

  • The process can let the default action happen
  • The process can block the signal (some signals cannot be ignored)
  • the process can catch the signal with a handler.

Signal handlers usually execute on the current stack of the process. This lets the signal handler return to the point that execution was interrupted in the process. This can be changed on a per-signal basis so that a signal handler executes on a special stack. If a process must resume in a different context than the interrupted one, it must restore the previous context itself

Receiving signals is straighforward with the function:

int (*signal(int sig, void (*func)()))() -- that is to say the function signal() will call the func functions if the process receives a signal sig. Signal returns a pointer to function func if successful or it returns an error to errno and -1 otherwise.

func() can have three values:

SIG_DFL
-- a pointer to a system default function SID_DFL(), which will terminate the process upon receipt of sig.
SIG_IGN
-- a pointer to system ignore function SIG_IGN() which will disregard the sig action (UNLESS it is SIGKILL).
A function address
-- a user specified function.

SIG_DFL and SIG_IGN are defined in signal.h (standard library) header file.

Thus to ignore a ctrl-c command from the command line. we could do:

signal(SIGINT, SIG_IGN);

TO reset system so that SIGINT causes a termination at any place in our program, we would do:

signal(SIGINT, SIG_DFL);

So lets write a program to trap a ctrl-c but not quit on this signal. We have a function sigproc() that is executed when we trap a ctrl-c. We will also set another function to quit the program if it traps the SIGQUIT signal so we can terminate our program:


#include <stdio.h>

void sigproc(void);

void quitproc(void); 

main()
{ signal(SIGINT, sigproc);
		 signal(SIGQUIT, quitproc);
		 printf(``ctrl-c disabled use ctrl- to quitn'');
		 for(;;); /* infinite loop */}

void sigproc()
{ 		 signal(SIGINT, sigproc); /*  */
		 /* NOTE some versions of UNIX will reset signal to default
		 after each call. So for portability reset signal each time */

		 printf(``you have pressed ctrl-c n'');
}

void quitproc()
{ 		 printf(``ctrl- pressed to quitn'');
		 exit(0); /* normal exit status */
}

sig_talk.c -- complete example program

Let us now write a program that communicates between child and parent processes using kill() and signal().

fork() creates the child process from the parent. The pid can be checked to decide whether it is the child (== 0) or the parent (pid = child process id).

The parent can then send messages to child using the pid and kill().

The child picks up these signals with signal() and calls appropriate functions.

An example of communicating process using signals is sig_talk.c:

/* sig_talk.c --- Example of how 2 processes can talk */
/* to each other using kill() and signal() */
/* We will fork() 2 process and let the parent send a few */
/* signals to it`s child  */

/* cc sig_talk.c -o sig_talk  */

#include <stdio.h>
#include <signal.h>

void sighup(); /* routines child will call upon sigtrap */
void sigint();
void sigquit();

main()
{ int pid;

  /* get child process */
  
   if ((pid = fork()) < 0) {
        perror("fork");
        exit(1);
    }
    
   if (pid == 0)
     { /* child */
       signal(SIGHUP,sighup); /* set function calls */
       signal(SIGINT,sigint);
       signal(SIGQUIT, sigquit);
       for(;;); /* loop for ever */
     }
  else /* parent */
     {  /* pid hold id of child */
       printf("/nPARENT: sending SIGHUP/n/n");
       kill(pid,SIGHUP);
       sleep(3); /* pause for 3 secs */
       printf("/nPARENT: sending SIGINT/n/n");
       kill(pid,SIGINT);
       sleep(3); /* pause for 3 secs */
       printf("/nPARENT: sending SIGQUIT/n/n");
       kill(pid,SIGQUIT);
       sleep(3);
     }
}

void sighup()

{  signal(SIGHUP,sighup); /* reset signal */
   printf("CHILD: I have received a SIGHUP/n");
}

void sigint()

{  signal(SIGINT,sigint); /* reset signal */
   printf("CHILD: I have received a SIGINT/n");
}

void sigquit()

{ printf("My DADDY has Killed me!!!/n");
  exit(0);
}

Other signal functions

There are a few other functions defined in signal.h:

int sighold(int sig) -- adds sig to the calling process's signal mask

int sigrelse(int sig) -- removes sig from the calling process's signal mask

int sigignore(int sig) -- sets the disposition of sig to SIG_IGN

int sigpause(int sig) -- removes sig from the calling process's signal mask and suspends the calling process until a signal is received

内容概要:本文档详细介绍了Analog Devices公司生产的AD8436真均方根-直流(RMS-to-DC)转换器的技术细节及其应用场景。AD8436由三个独立模块构成:轨到轨FET输入放大器、高动态范围均方根计算内核和精密轨到轨输出放大器。该器件不仅体积小巧、功耗低,而且具有广泛的输入电压范围和快速响应特性。文档涵盖了AD8436的工作原理、配置选项、外部组件选择(如电容)、增益调节、单电源供电、电流互感器配置、接地故障检测、三相电源监测等方面的内容。此外,还特别强调了PCB设计注意事项和误差源分析,旨在帮助工程师更好地理解和应用这款高性能的RMS-DC转换器。 适合人群:从事模拟电路设计的专业工程师和技术人员,尤其是那些需要精确测量交流电信号均方根值的应用开发者。 使用场景及目标:①用于工业自动化、医疗设备、电力监控等领域,实现对交流电压或电流的精准测量;②适用于手持式数字万用表及其他便携式仪器仪表,提供高效的单电源解决方案;③在电流互感器配置中,用于检测微小的电流变化,保障电气安全;④应用于三相电力系统监控,优化建立时间和转换精度。 其他说明:为了确保最佳性能,文档推荐使用高质量的电容器件,并给出了详细的PCB布局指导。同时提醒用户关注电介质吸收和泄漏电流等因素对测量准确性的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值