Linux_ Process Control

Source: Internet
Author: User
Tags terminates

Linux Process Model
System is used to invoke the shell and execute a specified command "Basically not on Linux"

Fork is used to create a new process that is almost a full copy of the current process

exec can replace the currently running process with another program in the process



Process identifier:
The properties of the process PID (non-zero integer), parent process Ppid

PID indicates the only process
The most ' old ' parent process for all processes is the INIT process
The init process is the first process to execute after the Linux kernel starts
-init boot the system, start the daemon and run the necessary programs

System work:
Power-on, fixed code [operating system boot]64k (self-checking CPU and other hardware, which disk boot)--load kernel---boot init (pid:1), other programs


The common use of PID is to create a unique file or directory name, PID write log

Head File Yourself man

PID and Ppid acquisition
unight int getpid ();
unight int getppid ();

Each user has a UID,
The GetLogin function returns the user login name of the executing program, passing the login name to the Getpwnam function, which returns the corresponding information for that user in the/etc/passed file.

#include <pwd.h>
Char *login = GetLogin ();//Get Login name
struct passwd *getpwnam (const char *name);//Get information based on login name


To create a process:
Rarely used under Linux
int system ("string");
Passed to/bin/sh-c to execute the command specified by string (cmd)

If no/bin/sh is found, return 127 If another error is returned-1, success is 0, but if string is null, a non-0 value is returned
Such as:
System ("Ls-l");


Heavily used under Linux
pid_t fork (void);
Cloning only the same process as yourself, some do not inherit [PID is not the same]

Successful father process put back the child process PID, return 0 to the child process, call the fork once, put back two times (one in the parent process, one in the child process)

The parent process cannot be expected to run before or after its child processes, and their execution is unordered, asynchronous
--The asynchronous nature means that you should not execute code that relies too much on the parent process in the child process, and vice versa
--fork may also fail, exceeding the maximum number of processes allowed to execute
--fork execution failed, put back to parent process 1, not creating child process

★ Determine whether the parent or child process is based on the return value
Special attention:
After the fork is called, the child process inherits all the code and memory of the parent process, [but the child process starts executing after the fork]
The fork is divided into two processes, with the same memory and memory values, running alone after the fork

Example
main{
printf ("Begin");
pid_t = fork ();
if (pid_t = = 0) {
printf ("I am a Child");}
else {
printf ("I am the Father");
}
printf ("End");
}
Print results
Begin
Father
Child
End
End

Fork is used:
Server service program, do not rely on the console (with the console detached from the relationship) daemon, no TTY program,
The same file descriptor is manipulated in different processes (shared file descriptor)//inherits the file descriptor of the parent process

Fork
UNIX uses Vfork to create a new process, but does not produce a copy of the parent process.

Execve
Fork can only create its own generation of new PID, completely independent, two processes are in
Execve ("path", parameter, NULL)//replaces the process that called it, the process becomes the calling process, the resource is unchanged, the executable code is changed, and only one process
Note: The parameters must conform to the parameters in main argv[]
Such as:
Execution MyFunc 1 2 argv[] The parameters are
MyFunc 1 2


This usage is more commonly used
★//fork after one of the processes execve two different processes to share the same file descriptor, sharing the resources of the system
★//fork after the parent process exit (0), the processing process into a background program



Wait and Waitpid
Collects the child process's exit status (frees the resource) and waits
pid_t Wait (int *status);
pid_t waitpid (pid_t pid, int *status, int options);//specify which child process to end the wait

Status Exit code:
Wexitstatus (status)//Translation exit code
Gets the value of the child process exit return



The parent process must wait for the child process
A child process can be unequal to the parent process

Parent Kill Live (orphan process)
A resource in a child process is not freed by the parent process that created it, but by the kernel (kernel).
When the parent process of a child process exits, the Init process becomes the parent of the child process, and when the child process exits the INIT process calls wait to release the resources that the child process occupies in the process table.
Init collects exit status after the parent process exits

Child Kill Parent Live (zombie process may occur)
Parent process requires wait child process, no wait child process becomes zombie
And the memory leak is the same, he is the kernel PID leak
When the parent process is kill, the kernel also scans the child process, releasing


Termination of the process:
Main function return; You can only exit return 10 in the main function;
Call exit (); In the child function can also exit well exit (10);
Call _exit;
Call the Abort function; Abnormal exit, generate core file, tell OS program error, do not generate exit code abort ()//is a rude call, the last means to use

Top 4 Normal termination
signal termination send kill signal Kill (PID, SIGKILL)//Send kill signal to SIG
Not normal.

--as long as the process exits, kernel processing is the same, closing open files, freeing memory resources, and other cleanup work





What's the difference between _exit and exit? (No content)
_exit terminates the calling process, but does not close the file, does not clear the output cache, and does not call the egress function.
The exit function terminates the calling process. All files are closed before exiting the program, buffered output content
The definition is refreshed and all flushed "exit functions" (defined by atexit) are called.
The definition is refreshed and all flushed "exit functions" (defined by atexit) are called.



Linux_ Process Control

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.