Process creation and termination

Process creation and termination
Most systems can process concurrently, they can be dynamically created and deleted. Therefore, the operating system must provide a mechanism for creating processes and terminate the process.

Process Creation

Process may create several new processes in the implementation process. Create a process called the parent process, and the new process is called the child process. Each new process can then create other processes, forming process tree.
Most operating systems (including UNIX, Linux and Windows) identification of the process uses a unique process identifier (pid), pid is usually an integer value. Each process in the system has a unique pid, it can be used as an index to access various properties of the kernel process.
A process tree typical Linux system
A process tree in Figure 1 a typical Linux system
Figure 1 shows the Linux operating system, a typical process tree, including the process name and pid (we usually use the term process, but prefer Linux "mission" of the term). Process init (pid it's always 1), as the root of all user processes or process the parent process. Once the system is started, the init process can create a variety of users, such as Web servers, print servers, ssh servers.
In Figure 1, kthreadd and sshd into two sub init process. kthreadd process responsible for creating additional processes to perform core tasks (here khelper and pdflush). ssh sshd process responsible for managing connected to the system by the client. login process is responsible for managing log in directly to the client system. In this example, the customer has logged in and using the bash shell, it was allocated pid 8416. Using bash command line interface, this process also creates the process of ps and emacs editor.
For UNIX and Linux systems, we can get a list of processes by the ps command. For example, the command
ps -the
Complete information can list all currently active processes in the system. By recursively tracing parent process until the process init, you can easily construct a similar process tree shown in Figure 1.
In general, when a process creates a child process, the child process requires resources (CPU time, memory, files, I / O devices, etc.) to complete the task. The child can get there directly from the operating system resources, resources can also be obtained from only a subset of the parent process there. The parent may have to allocate resources among sub-processes or shared resources (such as memory or file). Limit the child process the parent process can only use resources, can prevent the creation of too much process, cause the system to overload.
In addition to providing physical and logical resources, the parent process may initialize a data transfer (or input) to the child process. For example, assume there is a process which is a function of the state image.jpg file as on the terminal screen. When the process is created, it will get input from the parent process at that file name image.jpg. By this name, it opens the file, and then write the content. It can also output the device name. In addition, some operating system will transfer resources to the child process. For such a system, the new process to obtain two open file, i.e., the terminal device and image.jpg, and for data transmission between the two.
When the process of creating a new process, there are two possible execution:
  1. Parent and child processes concurrently.
  2. The parent process to wait until some or all of the sub-processes executed.
The address space of the new process, there are two possibilities:
  1. The child process is a copy of the parent process (parent process which has the same programs and data).
  2. New child process to load another program.
To illustrate these differences, first of all look at the UNIX operating system. In UNIX, as mentioned previously, each process by a unique integer identifier to identify the process. System call fork (), you can create a new process. Copy the address space of the new process address space of the original process. This mechanism allows the parent to easily communicate with the child process. These two processes (parent and child) are to continue in command after the system calls fork (), but there is one difference: the new (child) process, the system returns a value of 0 calling fork (); and for the parent process, return the process identifier (non-zero) value of the child process.
Typically, after the system call fork (), there is a process of using the system call exec (), in order to replace the memory space of the process with a new program. System call exec () to load a binary file into memory (destroying the memory content includes system calls exec () of the original program), and begin execution. In this manner, the two processes can communicate with each other, and each is capable of operating methods. The parent process can create more child process, or if there is nothing to do in the child process is running, it uses the system call wait () out of his own ready queue until the child process terminates. Because the call exec () with a new program covering the process address space, so call exec () unless an error occurs, control is not returned.
  1. #include <sys/types.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. int main ()
  5. {
  6. pid_t pid;
  7. /* fork a child process */
  8. pid = fork();
  9. if (pid < 0) { /* error occurred */\
  10. fprintf(stderr, "Fork Failed");
  11. return 1;
  12. }
  13. else if (pid == 0) { /* child process */
  14. execlp("/bin/ls","ls",NULL);
  15. }
  16. else { /* parent process */
  17. /* parent will wait for the child to complete */
  18. wait(NULL);
  19. printf("Child Complete");
  20. }
  21. return 0;
  22. }
C program shown above illustrate the above-described example UNIX system calls. There are two different processes, but running the same program. The only difference between these two processes are: pid of the child process is 0, while the value of the parent process pid is greater than 0 (in fact, it is the child pid process). Child inherits permissions from the parent process, scheduling attributes, and certain resources, such as opening a file.
System call execlp () (which is a version of the system call exec ()), the child process using the UNIX command / bin / ls (used to list directory listing) to cover its address space. Through the system call wait (), the parent process waits for the child process to complete. When the child process is completed (by displaying or implicit call exit ()), the parent process will () call to continue at the beginning of the wait, and will call at the end of the system call exit (). This can be represented by FIG. 2.
Figure 2 through the system calls fork () to create a process
Of course, nothing prevents the child does not call exec (), but continues to perform as a copy of the parent process. In this case, the parent and child processes will be executed concurrently, and using the same code instructions. Since the child process is a copy of the parent process, both processes have their own copy of the data.
As another example, take a look at the next Windows process creation. Create a process using the Windows API function CreateProcess (), which is similar to fork () (which is the parent process used to create the child process). However, fork () so that the child inherits the parent process address space, and CreateProcess () requires a specific program to be loaded when the child process is created in the process's address space. Furthermore, the fork () does not require any parameters passed, and CreateProcess () need to pass at least 10 parameters.
  1. #include <stdio.h>
  2. #include <windows.h>
  3. int main(VOID)
  4. {
  5. STARTUPINFO you;
  6. PR0CESS_INF0RMATI0N pi;
  7. /* allocate memory */
  8. ZeroMemory(&si, sizeof(si));
  9. si.cb = sizeof (si);
  10. ZeroMemory(&pi, sizeof(pi));
  11. /* create child process */
  12. if (!CreateProcess(NULL, /* use command line */
  13. "C: \\WIND0WS\\system32\\mspaint. exe" , /* command */ NULL, /* don,t inherit process handle */
  14. NULL, /* don^ inherit thread handle */
  15. FALSE, /* disable handle inheritance */
  16. 0, /* no creation flags */
  17. NULL, /* use parentJs environment block */
  18. NULL, /* use parent1s existing directory */
  19. &si,
  20. &pi))
  21. {
  22. fprintf (stderr, "Create Process Failed");
  23. return -1;
  24. }
  25. /* parent will wait for the child to complete */
  26. WaitForSingleObject(pi.hProcess,INFINITE);
  27. printf("Child Complete");
  28. /* close handles */
  29. CloseHandle(pi.hProcess);
  30. CloseHandle(pi.hThread);
  31. }
C program shown above demonstrates the function CreateProcess (), it creates a child process, and loaded application mspaint.exe. Here a number of default values ​​selected parameter 10 for delivery to CreateProcess ().
Two parameters passed to CreateProcess (), the structural and STARTUPINFO Examples PROCESS-INFORMATION:
  • Many characteristics of the structure of the new process STARTUPINFO specified, such as window size and appearance, and other standard file handles input and output;
  • PR0CESS_INF0RMATI0N structure with a handle and identifier of the new process and its threads.
Before CeateProcess (), the function call ZeroMemory () to allocate memory for these structures.
Function CreateProcess () of the first two parameters are the application name and command line parameters. If the application name is NULL (this is NULL), then the command line parameter to specify the application to be loaded. In this case, the load is mspaint.exe Microsoft Windows applications.
In addition to these two initial parameters, where the system default parameters to inherit process and thread handles, and specifies not create a logo; in addition, there is also the use of the existing environment and block the startup directory of the parent process. Finally, a structural STARTUPINFO two points to the beginning of the program created and PROCESS_INFORMATION pointer.
In the UNIX system call example, by calling the parent calls wait () system waits for the child to complete the process; and in Windows, this is equivalent WaitForSingleObject (), wait for the process to complete, its parameter specifies the child process handle That pi.hProcess. Once the child process exits, control returns to the parent process from the function WaitForSingleObject ().

The process is terminated

When the process is complete and the implementation of the final statement calling exit through the system () request to delete the operating system itself, the process is terminated. At this time, the process can return status value (usually an integer) to parent (via system calls wait ()). All resources processes, such as physical and virtual memory, open files, and I / O buffers, etc., will be freed by the operating system.
It will also appear in other cases the process is terminated. Process calls (such as Windows-Terminate-Process ()), can be terminated by another process through the appropriate system. Typically, only the parent process to terminate the process of implementation of this system call. Otherwise, the user can arbitrarily terminate each other's work. Remember, if the child process is terminated, the parent needs to know the child process identifier. Therefore, when a process creates a new process, the newly created process identifier to be passed to the parent process.
The reason to terminate the parent process child process are many, such as:
  1. Child process uses more than its allocated resources. (To determine whether or not this happens, the parent should have a mechanism to check the status of the child).
  2. Assign tasks to the child process is no longer needed.
  3. The parent process is exiting, and the operating system does not allow the child without the parent process continues.
Some systems do not allow the presence of the child in the case of the parent process has been terminated. For such systems, if a process terminates (normal or abnormal), then all child processes should also be terminated. This phenomenon, known as cascade termination, usually initiated by the operating system.
To illustrate the process execution and termination, Linux and UNIX systems with the following example: You can terminate the process calls exit () through the system, exit status can also be provided as a parameter.
/* exit with status 1 */
exit(1);
In fact, normal termination, Exit () can be called directly (as shown above), may be invoked indirectly (through main return statement () in).
The parent process can call wait through the system (), wait for the child process terminates. System call wait () parameters can be, let the parent process to obtain the exit status of the child process; this system call also returns the process identifier terminator, so the parent can know which child process has been terminated:
pid_t pid;
int status;
pid = wait(festatus);
When a process terminates, the operating system will release its resources. However, it is located in an entry in the process table or in until its parent process calls wait (); this is because the process table contains the exit status of the process. When the process has been terminated, but the parent process has not been called wait (), this process is called a zombie process.
All processes are terminated when the transition to this state, but in general the zombie short-lived existence. Once the parent process calls wait (), process zombie process identifier and its entry in the process table will be released.
If the parent does not call wait () terminates, so that the child process orphaned process, then this what happens? Linux and UNIX handling of this situation is: the init process as parent process orphan process. Init process regularly call wait (), in order to collect any exit status of orphan process, and release orphaned identifiers and process table entries.
 

Guess you like

Origin www.cnblogs.com/cyx-b/p/12163199.html