In Linux, it does not work for background processes to pass signals through kill.

Source: Internet
Author: User

When reading the APUE signal chapter, I metIn Linux, it does not work for background processes to pass signals through kill.

The specific situation is the same as that in the following link:

Http://bbs.csdn.net/topics/390335913? Page = 1 # post-395188120

Compile and execute the following signal program, and then shell command line: First let the program run in the background, get the process ID, then send the signal to the process using kill: kill-USR1 ID, but no result
#include "apue.h"static void sig_usr(int);int main(void){if(signal(SIGUSR1,sig_usr)==SIG_ERR)  err_sys("can not catch SIGUSR1");if(signal(SIGUSR2,sig_usr)==SIG_ERR)  err_sys("can not catch SIGUSR2");for( ; ; )    pause();}static void sig_usr(int signo){if(signo==SIGUSR1)   printf("received sIGUSR1\n");elseif(signo==SIGUSR2)   printf("received sIGUSR2\n");else   err_dump("received signal %d\n",signo);}

Here is my solution:

This problem should be implemented in the background of linux processes. You can try this experiment in another way:
1. Add the print getpid () function to the program.
2. Run the program on the foreground
3. In the new terminal window, enter "kill-USR1 [pid]" in the new terminal window.
4. Return to the original terminal and view the experiment results.


Kill

Function Description: delete a program or job in progress.

Syntax: kill [-s <information name or number>] [Program] or kill [-l <information number>]

Note: kill can send specified information to the program. The preset information is SIGTERM (15). You can terminate the specified program. If you still cannot terminate the program, you can use SIGKILL (9) Information to try to forcibly Delete the program. The program or job number can be viewed using the ps or jobs commands.
Parameters:
-L <Information No.> If the <Information No.> option is not added, the-l parameter lists all information names.
-S <information name or number> specifies the information to be sent.
[Program] [Program] can be the PID or PGID of the program, or the work number.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.