Hello world 6

Source: Internet
Author: User

Loaded from: http://www.cnblogs.com/xuqiang/archive/2010/03/29/1953689.html

Uninstall the hello program

Now let's take a look at our hello World Program:

# Include <stdio. h>

Int main ()
{

Printf ("Hello world \ n ");

Return 0;

}

After the process finishes running, we will display the call exit () or return to exit the running process. If you call return, the compiler will add exit (). in this case, save

Some information is necessary because the parent process can read the messages and get the exit status of the child process. if the sub-process exits. however, the parent process does not use wait (), which is what we often say.

The exit () system calls the corresponding interface in the kernel to sys_exit (). Let's trace and see how the kernel handles this process.

 

Fastcall noret_type void do_exit (long code)

{

...

// The function cannot be used in the interrupt context or process no. 0.

If (unlikely (in_interrupt ()))

Panic ("aiee, killing interrupt handler! ");

If (unlikely (! Tsk-> PID ))

Panic ("attempted to kill the idle task! ");

...

// Set pf_exiting: indicates that the process is exiting.

Tsk-> flags | = pf_exiting;

 

...

 

// Exit status code

Tsk-> exit_code = code;

Taskstats_exit (tsk, group_dead );

 

// Space occupied by exiting the process

Exit_mm (TSK );

 

If (group_dead)

Acct_process ();

// Remove the signal from the IPC queue

Exit_sem (TSK );

//

_ Exit_files (TSK );

// Close the opened file

_ Exit_fs (TSK );

Check_stack_usage ();

Exit_thread ();

Cgroup_exit (tsk, 1 );

Exit_keys (TSK );

 

// All Process Groups exit and the current process is the lead of the Process Group

If (group_dead & tsk-> signal-> leader)

// Disconnect the current TTY and send the sighup and sigcont messages to the terminal group of the Process display.

Disassociate_ctty (1 );

 

 

// Reduce the reference count of the module

Module_put (task_thread_info (TSK)-> exec_domain-> module );

If (tsk-> binfmt)

Module_put (tsk-> binfmt-> module );

 

Proc_exit_connector (TSK );

// Update the kinship of the process and send the corresponding signal to the parent process

Exit_notify (TSK );

 

...

// Process exit is complete. Set pf_exitpidone

Tsk-> flags | = pf_exitpidone;

 

...

// Set the process status to task_dead

Tsk-> state = task_dead;

 

// Schedule another process to run

Schedule ();

 

...

}

 

Obviously, the above function first checks whether the process exit conditions are met, ends the resources related to the process, and marks the current process as task_dead.

Delete task_struct. When will the process descriptor be deleted?

 

In Linux, the process is allowed to query the status of its parent process or the status of any sub-process. Therefore, after a process is executed, it cannot be deleted immediately. Only when

The parent process is allowed to delete the task_struct structure from the memory after a wait system call is made. But what if the child process ends before the parent process? In Linux, the parent process of such sub-processes is automatically set to the INIT process. In this case, use the wait system call in the INIT process to end such a sub-process.

 

 

See Linux kernel Programming

When a child process terminates, the parent process receives the kernel signal "sigchld". The parent process can call the wait system call at any event location. You can use:

Pid_t wait (int * Status)

Pid_t waitpid (pid_t PID, int * status, int options)

Pid_t wait3 (int * status, int options, struct rusage * rusage)

Pid_t wait4 (pid_t PID, int * status, int options, struct rusage * rusage)

These functions call sys_wait4 again.

asmlinkage int sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru){
...
/* Is the parameter valid? */
If (options &~ (Wnohang | wuntraced |__ wclone) Return-einval;/* Add the process to the waiting process */add_wait_queue (& Current-> wait_chldexit, & wait); repeat: flag = 0;/* First initialize the flag to 0. If the PID parameter is found to be the same as the PID of a sub-process, change the PID */
/* P_cptr is the sub-process of the process, and p_osptr is the sub-process sibling process of the process */For (P = Current-> p_cptr; P = p-> p_osptr) {... flag = 1; Switch (p-> state) {/* Find the process PID that is equal to the function parameter PID */case task_stopped:/* The task_stopped process enters the sleep state, in this case, the process can be awakened */If (! P-> exit_code) continue; If (! (Options & wuntraced )&&! (P-> flags & pf_ptraced) continue; If (Ru! = NULL) getrusage (p, rusage_both, Ru); If (stat_addr) put_user (P-> exit_code <8) | 0x7f, stat_addr); P-> exit_code = 0; retval = p-> PID; goto end_wait4; Case task_zombie:/* the process is in task_zombie, in this case, task_struct */current-> cutime + = p-> utime + P-> cutime; Current-> cstime + = p-> stime + P-> cstime; if (Ru! = NULL) getrusage (p, rusage_both, Ru); If (stat_addr) put_user (p-> exit_code, stat_addr); retval = p-> PID; If (p-> p_opptr! = P-> p_pptr) {remove_links (p); P-> p_pptr = p-> p_opptr; set_links (p); policy_parent (p);} elserelease (P ); /* release the structure of the process's memory */...}}... end_wait4:/* end function sys_wait4 */remove_wait_queue (& Current-> wait_chldexit, & wait); Return retval ;}

Obviously, the memory structure of the process is eliminated in the sys_wait4 function.

So far, the hello World Program has finally completed the mission of a simple printf ("Hello world \ n. In Linux, the complete life cycle of a program (or process) is like this.

Finally Hello world is finished...

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.