Talk C chestnuts together (78th back: C language instance-create process)

Source: Internet
Author: User

Talk C chestnuts together (78th back: C language instance-create process)

Hello, everyone. The last time we talked about the example of the DIY ls command continued, the example we mentioned here is:Create Process. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

Let's give a brief introduction to the process concept:A process is a program running in the system.. For example, the vim we are using is a process. Run the ps command to view the result. Enter the ps x command in the terminal to obtain the following result:

Pid tty stat time command 1550? Ssl 0: 00 cinnamon-session -- session cinnamon... // other system-related processes, do not list 5276 pts/2 Ss 0: 00 bash // The terminal we use is also a process 5297 pts/2 S + 0: 00 vim // The terminal we use is also a process 5298 pts/1 R + ps x // ps command itself is also a process

Process is a relatively abstract concept. As shown above, I think you should have an intuitive impression on the process. When I heard about the process, I was still using windows, so I opened the task manager to watch the process. Now let's think about it. This is also a good practice at the time. If any viewer is using a Windows system, try it like this, haha.

Enter a command in the terminal and press enter to create a process. Just now, we used the ps COMMAND to check that a process was created in the system. The process name is ps, that is, the COMMAND column in the preceding running result. The process names are all letters and sometimes long (for example, the first process name in the preceding running results is long). Therefore, the system manages the process numbers.The process ID is called PID, that is, the process ID.. The first column in the preceding running result is the process ID. Process IDs are integers starting from 0. Different systems have limits on the number of Created processes. Therefore, process IDs are finite integers. Among these IDs, 0 and 1 are special IDs, which are retained by the system for your own use. Based on how to use it, we will introduce it later.

Do you know how to create a process? "Enter the command and execute". This viewer is completely correct. Next, let's take a look at how the process is created in the code.Use the fork () function to create a process in actual code. When the function runs successfully, a value greater than 0 is returned. The value is the PID of the newly created process. If the operation fails,-1 is returned. Next we will illustrate the example.

Write the following code into the file and save it. then compile and run the code.

#include
  
   #include
   
    int main(){    pid_t pid;    pid = fork();    sleep(5);    if(pid > 0)        printf("pid is: %d \n",pid);    else        return 0;}
   
  

The following is the running result of the program.For more information, see:

./S // The compiled program pid is: 5456 // display the PID of the newly created Process

Open a terminal and run the ps command to view the processes in the current system. The result is as follows:

Pid tty stat time command 1550? Ssl 0: 00 cinnamon-session -- session cinnamon... // other system-related processes are not listed one by one: 5375 pts/1 S + 0: 00 vim Ex060_CreatProcess.c 5434 pts/3 Ss 0: 00 bash 5455 pts/2 S + 0: 00. /s // This process is the compiled program s 5456 pts/2 S + 0: 00. /s // you can see from the PID that this process is the new process created by the program s 5457 pts/3 R + 0: 00 ps x

The readers will not write the code in the body, and the detailed code will be put into my resources. You can click here to download and use it.

Let's talk about the example of creating a process. 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.