Exit () _ exit () terminate the process

Source: Internet
Author: User

/*

* _ Exit () stop the process immediately and clear the space used and various data structures in the kernel.

* Before exit (), call the exit handler to clear the I/0 buffer.

*/

# Include <stdio. h> // file preprocessing, including the standard input/output Library

# Include <unistd. h> // file preprocessing, including the fork function library
# Include <sys/types. h> // file preprocessing, including the fork function library
# Include <stdlib. h> // file preprocessing, including the exit function library
Void terminal_progress ()
{
Pid_t result; // The PID used to store the fork returned sub-process
Result = fork (); // call the fork function. The returned values are stored in the result variable.
If (result =-1) // you can use the result value to determine the return status of the fork function,
{
Perror ("failed to create sub-process! \ N ");
Exit (0 );
}
Else if (result = 0) // The value 0 indicates the sub-process.
{
Printf ("test termination process _ exit ()! \ N ");
Printf ("We use cache for this line! ");
_ Exit (0 );
}
Else // If the returned value is greater than 0, the parent process is returned.
{Sleep (10 );
Printf ("test termination process exit ()! \ N ");
Printf ("We use cache for this line! ");
Exit (0 );
}

}

/*
* This program shows how to avoid zombie processes.
*/
# Include <stdio. h> // file preprocessing, including the standard input/output Library
# Include <unistd. h> // file preprocessing, including the fork function library
# Include <sys/types. h> // file preprocessing, including fork, wait, and waitpid function libraries
# Include <sys/Wait. H> // file preprocessing, including the wait and waitpid function libraries
Void zombie_progress ()
{
Pid_t PID, wpid;
Int status, I;
PID = fork (); // call the fork function to copy the process
If (pid = 0) // If the returned value is 0, it indicates the sub-process.
{
Printf ("this is a sub-process, process number (PID) is: % d \ n", getpid ());
Sleep (5); // The sub-process waits for 5 seconds.
Exit (6 );
}
Else
{
Printf ("this is the parent process, waiting for the child process... \ n ");
Wpid = wait (& status); // The parent process calls the wait function to eliminate zombie processes.
I = wexitstatus (Status); // obtain the status when the sub-process exits through the integer pointer status
Printf ("the process Number of the waiting process is: % d, end status: % d \ n", wpid, I );
}
}

# Include <stdio. h> // file preprocessing, including standard input/output functions
# Include <unistd. h> // file preprocessing, including the fork function library
# Include <sys/types. h> // file preprocessing, including fork, wait, and waitpid function libraries
# Include <sys/Wait. H> // file preprocessing, including the wait and waitppid function libraries
Void display0 ();
Void display1 ();
Void display2 ();
Void Cisco ()
{
Pid_t result;
Int status, select, num;
Void (* Fun [3]) (); // use the function pointer to create three subprograms
Fun [0] = display0;
Fun [1] = display1;
Fun [2] = display2;
Printf ("1. Copy process \ n 2, do not copy process \ n please input your choice :");
Scanf ("% d", & select );
If (select = 1) // if the user inputs 1, the replication process
{
Result = fork (); // call the fork function to copy the process. The returned value exists in the // variable result
If (result =-1)
{
Perror ("replication process error ");
Exit (1 );
}
If (result = 0) // sub-process
{
Printf ("this is a sub-process (process number: % d, parent process number: % d):", getpid (), getppid ());
Printf ("go to the Cisco (Cisco) 1912 switch boot interface. \ n ");
Printf ("1 user (s) now active on Management Console. \ n ");
Printf ("\ t uer interface menu \ n ");
Printf ("\ t [0] Menu \ n ");
Printf ("\ t [1] command line \ n ");
Printf ("\ t [2] IP configuration \ n ");
Printf ("Enter Selection :");
Scanf ("% d", & num);/* use the function pointer to run the corresponding program */
If (Num> = 0 & num <= 2)
(* Fun [num]) ();
Exit (0 );
}
Else
{
Waitpid (result, & status, wnohang);/* wnohang does not block the parent process */
/* The parent process calls the waitppid function to remove zombie processes */
Printf ("this is the parent process (process number: % d, parent process number: % d) \ n", getpid (), getppid ());
If (wifexited (Status) = 0)
Printf ("sub-process terminated abnormally, sub-process terminated status: % d \ n", wifexited (Status ));
Else
Printf ("sub-process terminated normally, sub-process terminated status: % d \ n", wifexited (Status ));
Exit (0 );
}
}
}
Void display0 ()
{
Printf ("you have selected to enter the menu mode \ n ");
}
Void display1 ()
{
Printf ("you have chosen to enter the command line mode \ n ");
}
Void display2 ()
{
Printf ("you choose to enter IP Address Configuration Mode \ n ");
}

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.