Linux process Management (3): summary

Source: Internet
Author: User
Tags call back

7. The difference between exit and _exit
In order to understand the differences between these two system calls, we first discuss the problem of the file memory buffer. In Linux, standard input and output (I/O) functions are processed as files. Corresponds to each open file, there is a corresponding cache in memory, each time the file is read, read more records into the cache, so that the next time the file is read in the cache, but also in the file is written in the corresponding cache, is not directly written to the hard disk file, A certain condition (such as reaching a certain number, encountering a newline character \ n or the end-of-file sign EOF) will actually write the data to the file. The advantage of this is that it speeds up file read and write. But this also poses some problems, such as having some data that we think has been written to the file, but actually does not meet certain conditions and still resides in the memory cache, so if we directly terminate the process with the _exit () function, it will result in data loss. If you change to exit, there will be no data loss problem, which is the difference between them. To explain the problem, it involves the steps of their work.
Exit (): The previous source code analysis shows that when the function is executed, the process checks for file opening, cleans the I/O cache, and if there is data in the cache, writes them to the appropriate file, preventing the loss of file data and then terminating the process.
_exit (): When the function is executed, the standard input output cache is not cleaned up, but the memory space is cleared, and of course the data in the file cache that has not yet been written to the file is destroyed. This shows that using the exit () function is more secure.
In addition, there are different header files for their differences. Exit () in Stdlib.h, _exit () is in unistd.h. In general, exit (0) indicates normal exit, exit (1), Exit (-1) for exception exit, 0, 1,-1 is the return value, the specific meaning can be customized. Also note that return returns the function call and, if the main function is returned, exits the program. Exit is forced to exit the program at the point of invocation, and runs the program at the end.

The following is the complete Linux process run process:

[Plain]View PlainCopy
  1. Arch/x86/include/asm/unistd_32.h:fork () User space to invoke (e.g. C program)
  2. --->int $0x80 generates 0x80 soft interrupt
  3. --->arch/x86/kernel/entry_32.s:entry (system_call) Interrupt handler System_call ()
  4. ---> Execute save_all macro to save all CPU register values
  5. --->arch/x86/kernel/syscall_table_32.s:entry (sys_call_table) system calls a multi-path decomposition table
  6. --->arch/x86/kernel/process_32.c:sys_fork ()
  7. --->kernel/fork.c:do_fork () copy the original process into another new process
  8. --->kernel/fork.c:copy_process ()
  9. --->struct task_struct *p; Defining a new Process descriptor (PCB)
  10. Legality check of--->clone_flags sign
  11. --->security_task_create () security check (SELinux mechanism)
  12. --->kernel/fork.c:dup_task_struct () copy process descriptor
  13. --->struct thread_info *ti; Defining the thread information structure
  14. --->alloc_task_struct () allocates memory for the new PCB
  15. --->kernel/fork.c:arch_dup_task_struct () Copy the PCB of the parent process
  16. --->atomic_set (&tsk->usage,2) Sets the PCB usage counter to 2, indicating the active state
  17. --->copy_creds () Copy Permissions and identity information
  18. ---> Detect whether the total number of processes exceeds max_threads
  19. ---> Initialize each field in the PCB
  20. --->sched_fork () Scheduler Related Settings
  21. ---> Copy process All information Copy_semundo (), Copy_files (),
  22. --->copy_signal (), copy_mm ()
  23. --->copy_thread () replication thread
  24. --->alloc_pid () assigning PID
  25. ---> Update properties and Count of processes
  26. --->kernel/sched.c:wake_up_new_task () put the process on the run queue and let the scheduler Dispatch
  27. --->kernel/sched.c:select_task_rq () Select the best CPU (multiple CPUs in SMP)
  28. --->p->state = task_running set to task_running status
  29. --->activate_task ()
  30. --->enqueue_task () inserts the current process into the runqueue of the corresponding CPU
  31. ---> Clone_vfork flag: Wait_for_completion () blocks the parent process and waits for the child process to end
  32. ---> Return the assigned PID
  33. Kernel/sched.c:schedule () scheduling a newly created process
  34. Process in operation
  35. Exit () User space to invoke (e.g. C program)
  36. --->0x80 interrupt jumps to Include/linux/syscalls.h:sys_exit ()
  37. --->kernel/exit.c:do_exit () is responsible for the process exit
  38. --->struct task_struct *tsk = current; Get my PCB
  39. --->set_fs (user_ds) settings using the file system mode
  40. --->exit_signals () clears the signal processing function and sets the PF_EXITING flag
  41. ---> Purge process a series of resources exit_mm (), Exit_files ()
  42. --->exit_fs (), Exit_thread ()
  43. --->kernel/exit.c:exit_notify () exit notification
  44. --->forget_original_parent () adoptive all my sub-processes to the INIT process
  45. --->kill_orphaned_pgrp () sends a pending signal to processes within the process group Sighup and Sigcont
  46. --->tsk->exit_signal = SIGCHLD; Send a SIGCHLD signal to my parent process
  47. --->kernel/exit.c:do_notify_parent () notifies the parent process
  48. ---> returns DEATH_REAP if the parent process processes the SIGCHLD signal
  49. ---> If the parent process does not process the SIGCHLD signal, returns the signal value when it is passed in
  50. --->__wake_up_parent () wakes the parent process
  51. ---> Notifications return death_reap, set Exit_state to Exit_dead I quit and die.
  52. ---> Otherwise set me up for Exit_zombie I quit but not dead, become a zombie process
  53. ---> If I clean up the resources myself for Death_reap:release_task ()
  54. ---> If I am a zombie, I will adopt the INIT process when my parent process exits, and the Init is responsible for cleanup
  55. --->exit_io_context () cleanup IO context
  56. --->preempt_disable () to disable preemption
  57. --->tsk->state = task_dead; Set me up for process death status
  58. --->kernel/sched.c:schedule () release my PCB, dispatch another new process
  59. Cleanup Zombie Process: Wait system call waits for child process to end
  60. --->0x80 interrupt last reached kernel/exit.c:do_wait ()
  61. --->do_wait_thread ()
  62. --->wait_consider_task ()
  63. ---> If the child process is exit_dead, return the 0,wait call back, and the subprocess cleans itself
  64. ---> If the child process is Exit_zombie:wait_task_zombie ()
  65. --->xchg () sets the zombie subprocess to Exit_dead
  66. --->release_task () cleanup Zombie subprocess

Here is a basic diagram of the execution process:


Figure 1 Execution flow of Linux process management

Linux process Management (3): summary

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.