Linux processes and Signals

Source: Internet
Author: User
Linux processes and signals-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1. Process
Process concept: the address space where one or more threads are running and the system resources required by these threads.

1.1 start a new process using the system function:

# Include
Int system (const char * string );

The system function starts a new shell, which then executes the command.

1.2 Replace process image exec Function Series:

# Include

Char ** environ;
Int execl (const char * path, const char * arg0 ,..., (Char *) 0 );
Int execlp (const char * file, const char * arg0 ,..., (Char *) 0 );
Int execle (cont char * path, const char * arg0 ,..., (Char *) 0, char * const envp []);
Int execv (const char * path, char * const argv []);
Int execvp (const char * file, char * const argv []);
Int execve (const cha * path, char * const argv [], char * const envp []);

In general, the exec function does not return unless an error occurs. The exec function returns-1, and errno indicates an error.

The new process started by exec inherits many features of the original process, especially the file descriptor opened in the original process remains open in the new process, unless they use fcntl to set close on exec flag.

1.3 copy process image fork function:

# Include
# Include
Pid_t fork (void );

Fork creates a new process. This system calls to copy the current process and create a new table item in the Table. Many attributes of the new table item are the same as those of the current process. In combination with fork and exec, you can create everything you need for a new process.

1.4 wait, waitpid function and zombie Process

Wait functions:

# Include
# Include
Pid_t wait (int * stat_loc)

Wait system calls will suspend the parent process until its child process ends. This call returns the pid of the child process, which is usually the pid of the child process that has ended running. If stat_loc is not a null pointer, the status information is written to the position indicated by it. The status information allows the parent process to know the exit status of the child process, that is, the value returned by the main function of the child process or the exit code of the exit function in the child process.

Macro for interpreting status information:

Macro Definition
WIFEXITED (stat_val) Nonzero if the child is terminated normally.
WEXITSTATUS (stat_val) If WIFEXITED is nonzero, this returns child exit code.
WIFSIGNALED (stat_val) Nonzero if the child is terminated on an uncaught signal.
WTERMSIG (stat_val) If WIFSIGNALED is nonzero, this returns a signal number.
WIFSTOPPED (stat_val) Nonzero if the child has stopped.
WSTOPSIG (stat_val) If WIFSTOPPED is nonzero, this returns a signal number.

Waitpid Function

# Include
# Include
Pid_t waitpid (pid_t pid, int * stat_loc, int options)

Wiatpid can be used to wait for the completion of a specific process. The pid parameter specifies the pid of the child process to wait. If the value is-1, waitpid returns information about any sub-process. If stat_loc is a non-null pointer, waitpid writes the status information to the position it points. The Option parameter allows us to change the waitpid behavior. The WNOHANG Option prevents waitpid calling from suspending the invocation of the caller. If you want to periodically check whether a specific sub-process is terminated, you can call the following:

Waitpid (child_pid, (int *) 0, WNOHANG)

If the child process is not terminated or unexpectedly terminated, it returns 0; otherwise, it returns child_pid.

Botnets

If the child process is no longer running, but it still exists in the system, because its exit code also needs to be saved for future wait calls of the parent process. Then it will become a dead or zombie process. Therefore, you must use the wait or waitpid function to generate sub-processes using fork to prevent zombie processes.

If the parent process terminates abnormally, the child process automatically uses the process (init) with pid 1 as its parent process. The zombie process remains in the progress table until the init process discovers and releases it.

2. Signal
2.1 send signals to processes
You can press the interrupt character (for example, Ctrl + C) on the keyboard to send a signal to the foreground process. You can also use the kill command to send commands, including to background processes. Kill sends the SIGTERM signal by default. Other signals can be sent as follows. For example, the SIGHUP signal sent to a process with a pid of 512 is:

Kill? HUP 512

Sending signal function: kill Function

# Include
# Include
Int kill (pid_t pid, int sig );

The alarm function sends a SIGALARM signal after the specified time.

# Include
Unsigned int alarm (unsigned int seconds );

2.2 signal processing functions
There are two ways to set the signal processing function: signal and sigaction. We recommend using the sigaction function, which is robust.

Signal handler for signal library function installation:

# Include
Void (* signal (int sig, void (* func) (int );

This function is quite complex. signal is a function with sig and func parameters. The signal to be captured or ignored is provided by the sig parameter. The function to be called after receiving the specified signal is provided by the func parameter. Func can also replace the signal processing function SIG_IGN with two special values, indicating that the signal is ignored; SIG_DFL, indicating the default behavior.

Install the signal processing function using the sigaction function:

# Include
Int sigaction (int sig, const struct sigaction * act, struct sigaction * oact );

Structure Definition of struct sigaction:

Struct sigaction {
Void (* sa_handler) (int);/* function, SIG_DFL or SIG_IGN
Void (* sa_sigaction) (int, siginfo_t *, void *);
Sigset_t sa_mask;/* signals to block in sa_handler
Int sa_flags;/* signal action modifiers
Void (* sa_restorer) (void );
}

If oact is not a null pointer, sigaction writes the original action on the signal to the position it points. If oact is a null pointer, The sigaction function does not need to perform other settings.

Sa_handler in act is a signal processing function and can also be SIG_IGN and SIG_DFL.

Sa_mask in act is the signal shielding set, which will be blocked when sa_handerl calls it.

Parameters of sa_flags in act:

SA_NOCLDSTOP Don't generate SIGCHLD when child processes stop.
SA_RESETHAND Reset signal action to SIG_DFL on receept.
SA_RESTART Restart interruptible funble rather than error with EINTR.
SA_NODEFER Don't add the signal to the signal mask when caught.

SA_RESTAT is used to restart the interrupted system call after the signal processing function is complete. Many system calls can be interrupted.

SA_RESETHAND sets the signal processing function to SIG_DFL after the signal processing function is processed.

Functions related to signal Sets

# Include
Int sigaddset (sigset_t * set, int signo );
Int sigemptyset (sigset_t * set );
Int sigfillset (sigset_t * set );
Int sigdelset (sigset_t * set, int signo );

The basic function is the same as the function name. Not much.

The sigismember function is used to determine whether signo is in the set signal set.

# Include
Int sigismember (sigset_t * set, int signo );

Set and check the signal shielding word sigpromask function of the process

# Include
Int sigprocmask (int how, const sigset_t * set, sigset_t * oset );

The values and functions of how are as follows:

SIG_BLOCK The signals in set are added to the signal mask.
SIG_SETMASK The signal mask is set from set.
SIG_UNBLOCK The signals in set are removed from the signal mask.

If the set parameter is a null pointer, the value of how is meaningless. The role of sigpromask is to save the value of the current signal shielding word to the oset.

Sigpending Function

View the pending status of blocked Processes

# Include
Int sigpending (sigset * set );

Sigsuspend Function

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.