Linux Process Learning

Source: Internet
Author: User

Wait function and waitpid function. function prototype: pid_t wait (int * Status)

Status is an integer pointer pointing to an object to protect the status of the child process when it exits. Example: pid = wait (& Status)

 

This status is assumed to be 0x0400, indicating that the first byte stores the number of the interrupt signal, and the last and second byte stores the number in exit.

If the return value of the function is successful, the process Number of the sub-process is returned. If the return value is failed,-1 is returned;

The wait function is called to block a process until any sub-process ends or the process directly receives a signal. If the process does not have

The child process or its child process has ended, and the wait function will return immediately.

Waitpid (pid_t PID, int * status, int options)

The waitpid function is similar to the wait function. The waitpid function can specify to wait for a subprocess.

PID =-1, waiting for any sub-process; = 0, waiting for sub-processes in the group ID;-1, other sub-Processes

Options: wnohong if the process specified by the PID is not immediately available, the waitpid is not blocked, and the returned value is 0.

Wuntraced

0, equivalent to wait

 

 

 

The Linux daemon process, also known as the Linux daemon process, is a Linux interrupt background service process. It is a long-term surviving process and is usually independent of the control terminal.

And periodically execute a task or wait for processing some events.

The daemon often starts running at system startup and ends only when the system is shut down.

To write a Linux daemon, follow these steps:

1. Create a child process and the parent process exits.

2. Create a new session in the sub-process

3. Change the current directory to the root directory.

4. Reset the File Permission mask.

5. Disable the file descriptor.

 

1. PID = fork ()

If (pid> 0)

{

Exit (0 );

}

Setsid (); // used to create a new session and make the current process the leader of the new session group. The process is completely independent from other processes.

Chdir ("/"); // during process running, the file system of the current directory cannot be uninstalled.

Unmask (0); // sets the File Permission to 0, which can increase the flexibility of the daemon process.

Fdablesize = getdtablesize ();

For (FD = 0; FD <fdtablesize; FD ++)

Close (FD );

End!

 

 

 

 

 

 

 

 

 

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.