Talk C Chestnut Bar (81st: C-language instance-process stop)

Source: Internet
Author: User

Ladies and gentlemen, good crossing, the last time we talked about the process of mutual exclusion of the sample, this time we say the example is: The process stopped . Gossip Hugh, words return to the positive.

Let's talk C chestnuts together!

We described in the previous Zhanghuizhong how to create the process, but did not introduce the stop process, there are already some crossing in question. How do we stop the process? Crossing, let's talk about how to stop the process today.

There are several ways to stop a process :

    • Stop at the normal end of the process;
    • Use the Exit function to stop the process.
    • Forcing the process to cease;
    • The process is stopped after an error occurs during process execution.

Next, let's describe the few ways we're going to stop:

Stop at normal end of process

The program we wrote has a main function that returns a value to the system using the return statement in the main function. The process with the main function will stop.

The method is the most common method, which may be that we see more, but think very ordinary.

As the saying goes: the matter to dilute for you say is this truth.

The following is a simple code structure:

int main(){     //do something     return0;        //通过返回停止进程 }
To stop a process using the Exit function

This function can be called directly in the program, and the number of parameters passed to it is typically 1. Indicates that the program has an exception and stops. For example, we often infer whether the pointer is empty in the function, and if it is null, stop executing the program.

if(NULL == p)     exit(1);   //通过exit函数停止进程
Force Stop Process

Use the KILL command in the terminal to send a signal to the process. Like what:

kill34567

The example above indicates that a term signal is sent to the PID 34567 process, which stops when the process receives a signal.

Of course. The ability to use the KILL command to send other signals to the process so that the process stops. We only used the default signal in the sample.

In addition Linux provides a kill system call that can be used to send a stop signal to a process when we want to force a process to stop in code. The process is then forced to stop. Like what:

kill(34567,SIGKILL)

The example above represents a process that stops PID 34567.

You need administrator privileges to use this method. Otherwise, there is no permission to signal the process.

Stop process after an error occurs during process execution

When a critical error occurs during the execution of the process, the system stops the process. This method is used by the system and we cannot use it. All we have to do is check for possible anomalies. Avoid the occurrence of serious errors, such as memory leaks.

Crossing, the way the process stops is not uncommon and easy to understand, so we don't write code.

You crossing, the example of the process stop is here. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (81st: C-language instance-process stop)

Related Article

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.