Zookeeper
1 fork () function
The sub-process copies the 0-3g space of the parent process and the PCB in the parent process kernel, but the ID number is different. A fork call returns two times at a time, which has the following features:
A: The child process ID is returned from the parent process.
B: The sub-process returns 0.
C: shared during read and copy during write
2 fork ()-dependent header file
# Include <unistd. h>
3 fork () function description:
Pid_tfork (void );
Use this function to create a sub-process
4 case study:
Summary:
A because of read sharing and write replication, the child process will copy the code of the parent process, but the execution starts from fork. However, when the parent and child processes define the variables used by the parent and child processes before fork, the Parent and Child processes do not operate the variables at the same time, but are independent of each other, that is, the Child processes specify their own N, does not operate on N of the parent process.
5. getpid/getppid
Header file on which a depends
# Include <unistd. h>
# Include <sys/types. h>
B function declaration
Uid_t getuid (void); // return the actual user ID
Uid_t geteuid (void); // return a valid user ID
C: Case Study
6. getuid () function
Header file on which a depends
# Include <unistd. h>
# Include <sys/types. h>
B function declaration
Uid_t getuid (void); // obtain the user ID
Uid_t geteuid (void); // obtain a valid user ID
Function Description:
Use the getuid () and geteuid () functions to obtain the user ID.
Case study:
Running result:
7. getgid () function
A: header file of dependency
# Include <unistd. h>
# Include <sys/types. h>
B function declaration
Gid_t getgid (void );
Gid_t getegid (void );
Function Description:
Use this function to obtain the group ID.
C Case study:
Running result:
Vfork
Call the exec function immediately after fork is used.
The Parent and Child processes share the same address space. If the child process does not immediately exec, it modifies the variable value obtained by the parent process. This modification takes effect in the parent process.
Designed to improve system efficiency and reduce unnecessary overhead
Now, fork has a mechanism to share data during read and write, and vfork is gradually deprecated.
4 Process primitives: fork () function, getpid () function, getppid () function, getuid () function, getgid () function, vfork ()