Linux programming process (3): Process Replication fork, orphan process, botnet Process

Source: Internet
Author: User

Objectives of this section:

  • Copy process image
  • Fork system call
  • Orphan and botnets
  • Copy at write time
1. Process replication (or generation)

The child process obtained by using the fork function inherits the address space of the entire process from the parent process, including: process context, process stack, memory information, open file descriptor, signal control settings, process priority, process group number, current working directory, root directory, resource limit, control terminal, etc.

The difference between a child process and a parent process is:

1. The sub-process does not inherit the lock set by the parent process (because the sub-process is inherited due to exclusive locks)

2. The process IDs and parent process IDs are different.

3. Pending alarms of sub-processes are cleared;

4. the pending Signal Set of the sub-process is set to an empty set.

Ii. fork system call

Contains the header file <sys/types. h> and <unistd. h>

Function: create a sub-process

Function prototype

Pid_t fork (void );

Parameter: No parameter.

Return Value:

  • If a child process is successfully created, the child process ID is returned for the parent process.
  • If a child process is successfully created, the return value is 0 for the child process.
  • -1 indicates creation failed.

Flowchart:

<Unistd. h> <stdlib. h> ERR_EXIT (m) \ (main (= (pid =-(pid = (pid> printf (

Running result:

<Unistd. h> <stdlib. h> ERR_EXIT (m) \ (main (= (pid =-(pid = (pid>

The above program is basically the same as the previous one, that is, to let the parent process sleep for 100 seconds so that the child process exits first.

Running result:

<Unistd. h> <stdlib. h> ERR_EXIT (m) \ (main (val = pid = (pid =-(pid = ++ printf (pid> printf (

When fork is called:

Running result:

<Unistd. h> <stdlib. h> <fcntl. h> ERR_EXIT (m) \ (main (= open (fd =-= (pid =-(pid =, (pid> write (fd ,,

Running result:

It can be seen that the parent-child process shares the file offset pointer. After the parent process writes the file offset to the parent, the child process starts to write.

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.