This article will record the process of learning the fork and wait (waitpid) functions.
1. Fork
In Linux, the fork function is often used to create a new process. It is very special. When executed once, two values are returned, which is very different from the general function, this has aroused my high attention.
Prototype: pid_t fork (void)
Return: 0 -- sub-process
> 0 (child process ID) -- parent proces
not possible to have a negative number if there are other ready processes in the queue. It should be noted here that the permissions of all the other processes in the team have dropped to 0, indicating that the scheduling policy for these processes is sched_other, because if a policy is SCHED_FIFO or sched_rr the process exists, then the weight value is at least 100.Note 3:For_each_task () is a loop over all processes, not just the ready process queue. For non-real-
Tags: sig while return value types signal int main get runThe Linux wait function is detailedWhy wait and waitpid occur sigchld--when a child process exits, the kernel sigchld the signal to the parent process, and the child process exits with an asynchronous event (the child process can terminate at any time the parent
Wait () and Waitpid ()
Function description
The wait () function is used to block the parent process (that is, the process that invokes the ()) until a child process finishes or the process receives a specified signal. The wait () function returns immediately if the parent process has no child processes or its child processes have ended.
The role of Waitpid ()
following function is not executedwake_up_interruptible,abovewait_event_interruptibleand will continue to hibernate .)And clears the __wait from the wait queue so that the process can schedule the run. If the process currently has an asynchronous signal (POSIX), the-erestartsys is returned.
Wake-Up Q Specifies the process that is registered on the wait queue. The function does not immediately awa
)
Related functions
Wait,execve
Table header File
#include
Defining functions
pid_t vfork (void);
Function description
Vfork () produces a new subprocess whose child processes replicate the data and stack space of the parent process and inherit the parent process's user code, group code, environment variables, open file code, working directory, resource limits,
Background: When reading UNIX Network programming volume 1, the fifth chapter server processes SIGCHLD signals. and multiple clients shutting down the socket connection at the same time, multiple sub-processes of the service-side master process end almost simultaneously.Use the wait condition:void sig_chld (int signo) { pid_t pid; int stat; = Wait (st
You cannot attach a USB flash disk to Linux on the vmwarevm. Please help us and wait online. Thank you very much! -- Linux general technology-Linux technology and application information. For details, refer to the following section. You cannot attach a USB flash disk to Linux
basically at the same time, resulting in 5 SIGCHLD signals are delivered to the server parent process almost simultaneously. As shown below:That is, almost at the same time, 5 SIGCHLD signals are delivered to the parent process, which in turn will cause the zombie process to appear. Because UNIX generally does not queue the signal, which resulted in 5 SIGCHLD submitted, only one sig_chld function, the rema
Linux Shell sleep/wait (reprinted) 2007-04-27 18:12Bash's basic configuration is made up of configuration files./etc/profile is called a global configuration file for the shell.Another file in the personal directory under the personal directory/.bash-profileThere's a file in the personal directory./ETC/BASHRC is the second global profile, which holds (function aliases, etc.) also has a configuration file in
In Linux drivers, it is best not to use continuous loops to do the work of waiting for some kind of event, but to use another method, sleep and wake.Waiting for the queue to work is the process of sleep and wakeThe basic principle of the sleep and wake process, when the driver waits for an event to enter hibernation, wakes up the driver code of the hibernation state when the event occurs. Use:Static Declare_wait_queue_head (BUTTON_WAITQ); Defining th
Linux system monitoring, diagnostic tools Tophttp://my.oschina.net/leejun2005/blog/157910Linux system Monitoring, diagnostic tool IO waithttp://my.oschina.net/leejun2005/blog/355915Introduction to the Lsof usage of Linux system monitoring and diagnostic toolshttp://my.oschina.net/leejun2005/blog/153584Analysis and solution of high CPU load problemhttp://yikebocai.com/2014/11/cpu-load-too-high/
Zombie does not take up memory and does not occupy the CPU, on the surface we can not care about their existence, however, in fact, the UNIX system limits the maximum number of processes that can exist at one time at a time. If the program does not clean up the zombie in time, it will end up with too many processes, and there will be an error when the new process
) assumes that the child process normally ends with a value other than 0.Wexitstatus (status) Gets the end code returned by the child process exit (), usually preceded by theWifexited to infer whether the ability to end gracefully uses this macro.Wifsignaled (status) assumes that this macro value is true when the child process is terminated by a signalWtermsig (status) Gets the signal code that the child process has aborted because of the signal, and usually uses wifsignaled to infer it before u
Original URL: http://www.2cto.com/os/201203/124851.htmlWait (waits for the child process to break or end)Header file #include "Go" Linux waitpid and wait usage
int status;pid_t t = fork (), if (t) {waitpid (T, status, 0);}else{System ("VI temp.txt"); Exit (0);} After both the parent and child processes have finished executing the parsing process: if and else or select branch.The main reason is that thefork () function is called once and returned two times . The difference of two returns is that the return value of the child process is 0, and the parent process returns the process ID of the new child process. When returned, the parent process executes w
)Background execution: ""Users sometimes take a long time to execute commands, which can affect other things. The best way to do this is to put it in the background. Programs running in the background can continue to execute after the user logs off. When you want to place the command in the background, add "" after the command.Linux Shell | |When the shell executes a command, it returns a return value that is saved in the shell variable $? In When $?
Recently found that most of the production server used in the work, through the Securecrt/xshell/putty terminal software to SSH connection, waiting for the prompt to enter the password for a long time, always need more than 5 seconds, after observation found that the original is Sshd_ The cause of configuration in config;Vim Open/etc/ssh/sshd_config1. Find "Gssapiauthentication yes" to modify it to "Gssapiauthentication no"2. Find "Usedns yes" to modi
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.