Signal () function under Linux programming

Source: Internet
Author: User
Tags function prototype signal handler

Linux Programming the signal () function when the server close a connection, if the client side then sends the data. According to the provisions of the TCP protocol, you will receive a RST response, when the client sends data to this server, the system sends out a SIGPIPE signal to the process, telling the process that the connection has been disconnected and no more writing. According to the default processing rules of the signal sigpipe the default execution action of the signal is terminate (terminating, exiting), so the client exits. If you do not want the client exit can be sigpipe set to sig_ign such as: Signal (sigpipe,sig_ign), then sigpipe to the system processing. Server used fork, to collect garbage process, to prevent the generation of zombie process, can be handled as follows: Signal (sigchld,sig_ign); To the system init to recycle. There is no zombie process in the child process. Signal (SIGHUP, sig_ign); signal signal function, the first parameter represents the signal value to be processed (SIGHUP), the second parameter is a handler function, or is a representation, where sig_ign means to ignore the registered signal of SIGHUP. Sighup is related to console operation, when the console is closed, the system will send HUP signal to all processes that have console SessionID, the default HUP signal action is exit, if a remote login starts a service process and closes the connection when the program is running, it will cause the service process to exit , so the General Service process starts with the Nohup tool or writes a daemon. Process Organization in UNIX the session contains a foreground process group and one or more background process groups, and a process group contains multiple processes. A session may have a session first process, and a session header process may have a control terminal. A process group may have a process group first process. The process ID of the process group's first process is equal to the process group ID. Here is possible, under certain circumstances is not. The process of interacting with the terminal is the foreground process, or the background process sighup will be sent to the appropriate process in the following 3 scenarios:1, when the terminal shuts down, the signal is sent to the session first process and the process that is submitted as the job (&process of symbol submission)2, when the session first process exits, the signal is sent to each process in the foreground process group in the session3, if the parent process exits causing the process to make up an orphan process group, and there is a process in the process group that is in a stopped state (a sigstop or SIGTSTP signal is received), the signal is sent to each process in the process group. The system's default processing of the SIGHUP signal is to terminate the process of receiving the signal. So if the signal is not captured in the program, the process exits when the signal is received. Table header file #include<signal.h>function: Set the corresponding action function prototype of a signal:void(*signal (intSignumvoid(* handler) (int)))(int); Or: typedefvoid(*sig_t) (int); Sig_t Signal (intsignum,sig_t handler);  Parameter description: The first parameter, Signum, indicates the type of signal to be processed, and it can take any signal except Sigkill and sigstop. The second parameter, handler, describes the action associated with the signal, which can take the following three values: (1A function address that returns a positive value This function must be declared before signal () is called, Handler is the name of the function. When a signal of the type sig is received, the function specified by handler is executed. This function should be defined in the following form: IntFunc (intSIG); The sig is the only parameter passed to it. After the signal () call is executed, the process executes the Func () function as soon as it receives a signal of the type sig, regardless of which part of the program it is executing.  When the Func () function finishes executing, the control returns the point at which the process is interrupted to continue execution. (2Sigign This symbol means that the signal is ignored, and after the corresponding signal () call is executed, the process ignores the signal of the type sig. (3SIGDFL This symbol indicates the default processing of the system to the signal. Function Description: Signal () sets the processing function of the signal according to the signal number specified by the parameter signum. When the specified signal arrives, it jumps to the function execution specified by the parameter handler. When the signal processing function of a signal is executed, if the process receives the signal again, the signal is automatically stored without interrupting the execution of the signal processing function until the signal processing function completes and the corresponding handler function is called again.  However, if the process receives other types of signals when the signal processing function executes, the execution of the function is interrupted. Return value: Returns the previous signal handler pointer and returns SIG_ERR if there is an error (-1).  Additional instructions: After the signal has been jumped to the custom handler processing function execution, the system will automatically swap this processing function back to the original system preset processing mode, if you want to change this operation, use Sigaction (). The following conditions can produce signal:1. Press CTRL +C Generate SIGINT2. Hardware interrupts, such as except 0, illegal memory access (SIGSEV), etc.3. The KILL function can send a signal to a process4. Kill command. is actually a wrapper over the Kill function5. Software interrupts. such as when alarm clock timeout (Sigurg), when reader abort and then to the pipeline to write data (sigpipe), etc.2signals:signal DESCRIPTIONSIGABRT is generated by calling the Abort function, the process is not properly exited SIGALRM the timer timeout set with the alarm function, or setitimer time set by the interval function R Time-out Sigbus a specific hardware exception, usually caused by memory access Sigcancel used internally by the Solaris Thread Library, usually without using SIGCHLD process terminate or stop, SIGCHLD is sent to It's the parent process. By default, the signal is ignored Sigcont automatically sends SIGEMT and implements related hardware exceptions SIGFPE mathematical-related exceptions, such as being removed by 0, floating-point overflow, etc. sigfreeze Solaris when the stop process resumes running, H Iberate or suspended send Sighup sent to controlling Process with terminal, when terminal is disconnect send sigill illegal instruction exception Siginfo BSD s Ignal. Generated by status key, usually CTRL+T. Processes sent to all foreground group SIGINT are generated by interrupt key, usually CTRL+C or delete. Processes that are sent to all foreground group Sigio asynchronous IO events Sigiot implement related hardware exceptions, and generally correspond sigabrtsigkill cannot be processed and ignored.    Aborts a process SIGLWP is used internally by the Solaris Thread Libray Sigpipe sends Sigpoll when an event is sent to pollable device when a message is written to the pipe after reader abort sigprof Setitimer the specified profiling Interval timer produces SIGPWR and system-related. and UPS-related. Sigquit input Quit key (CTRL+\) Send to all foreground group processes SIGSEGV illegal memory Access Sigstkflt Linux dedicated, math coprocessor stack exception sigstop abort process. cannot be processed and ignored. Sigsys Illegal system call Sigterm request abort process, the KILL command is sent by default Sigthaw Solaris dedicated, sending SIGTRAP implementation-related hardware exceptions from suspend recovery time. Usually debug exception sigtstp Suspend Key, usually ctrl+Z. Process sent to all foreground group Sigttin when background group's process attempts to read terminal send Sigttou when background group's process attempts to write terminal Gurg when out-of-band data may be sent SIGUSR1 user custom signal when it is received1SIGUSR2 user-defined signal2the SIGVTALRM setitimer function is set by the virtual Interval timer timeout when the sigwaiting Solaris Thread Library implements private Sigwinch when terminal When the window size changes, all processes sent to foreground group SIGXCPU when the CPU time limit expires Sigxfsz the process exceeds the file size limit sigxres Solaris Private, when the process exceeds the resource limit.1, do not use low-level or STDIO.H IO functions2, do not use the action3, do not make system calls4, do not use longjmp when it is not a floating point signal5, the Singal function is defined by ISO C. Because ISO c does not involve multiple processes, process groups, and terminal i/O, so his definition of the signal is so vague that it is almost useless for UNIX systems. Note: Because Singal semantics are relevant to reality, it is best to use the Sigaction function instead of this function. http://blog.sina.com.cn/s/blog_4b226b92010119l5.html

Signal () function under Linux programming

Related Article

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.