Talk C chestnuts together (81st back: C language instance-process stopped)

Source: Internet
Author: User

Talk C chestnuts together (81st back: C language instance-process stopped)

Hello, everyone. In the previous example, we talked about the process mutex. This example is as follows:Process stopped. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

We introduced how to create a process in the previous chapter, but did not introduce how to stop the process. Some readers have already asked questions. How can we stop the process? Today, let's talk about how to stop the process.

The following methods are used to stop a process::

Stop the process when the process ends normally. Use the exit function to stop the process. Force stop the process. Stop the process when an error occurs during the process.

Next, we will introduce the methods for stopping these stopovers:

Stops when the process ends normally.

In the program we write, there is a main function. In the main function, the return statement is used to return a value to the system. At this time, the process with the main function will stop. This method is the most common method. It may be because we have seen more, but we think it is quite common. As the saying goes: this is the truth.

The following is a simple code structure:

Int main () {// do something return 0; // stop the process by returning}
Use the exit function to stop a process

You can directly call this function in a program. The parameter passed to it is usually 1. Indicates that the program stops when an exception occurs. For example, in a function, we often judge whether the pointer is null. If it is null, the program is stopped.

If (NULL = p) exit (1); // stop the process through the exit function
Force stop Process

Use the kill command in the terminal to send a signal to the process. For example:

kill 34567

The preceding example indicates that a TERM signal is sent to a process with a PID of 34567, and the process stops after receiving the signal. Of course, you can use the kill command to send other signals to the process to stop the process. We only use the default signal in the example.

In addition, Linux provides a kill system call. When we want to force stop a process in the code, we can use it to send a stop signal to a process and then force stop the process. For example:

kill(34567,SIGKILL)

The preceding example indicates that the process whose PID is 34567 is stopped. You must have the Administrator permission to use this method. Otherwise, you do not have the permission to send signals to the process.

Stop the process after an error occurs during process running

When a serious error occurs during the process, the system stops the process. This method is used by the system and cannot be used. All we need to do is check various possible exceptions to avoid serious errors, such as memory leakage.

The process stop method is common and easy to understand, so we will not write code.

You can refer to the process stop example here. I want to know what examples will be provided later, and I will try again.

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.