Talk C chestnuts together (107th back: C language instance-thread overview)

Source: Internet
Author: User
Tags overview example

Talk C chestnuts together (107th back: C language instance-thread overview)

Hello, everyone. In the previous session, we talked about examples of producer and consumer issues. The example is as follows:Thread Overview. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

Seeing officials, the main character in our chapter today is thread. What is a thread? We still remember that we introduced the process concept in the previous chapter:A process is a program running in the system. A thread is a unit in a process, usually called a lightweight process..

A thread is created by a process.It only has its own stack, and other resources are shared with the process that created it.. Next, let's take a concrete example to see the true sense of the thread ".

Let's take a look at the process of the system.Run the following command on the terminal: ps xThe following result is displayed:

Pid tty stat time command 1501? Ssl 0: 00 cinnamon-session -- session cinnamon... // omitting information about other processes 3375? Sl gnome-terminal 3383 pts/1 Ss bash 3740? Sl 0: 02/usr/lib/firefox // This is the process of the firefox browser 3823 pts/1 R + 0: 00 ps x

From the preceding running results, we can see that the firefox process is running in the system,Execute the command in the terminal: ps xHThe following result is displayed:

Pid tty stat time command 1501? Ssl 0: 00 cinnamon-session -- session cinnamon 1501? Ssl 0: 00 cinnamon-session -- session cinnamon 1501? Ssl 0: 00 cinnamon-session -- session cinnamon 1501? Ssl 0: 00 cinnamon-session -- session cinnamon... // omitting information about other processes 3375? Sl gnome-terminal 3375? Sl 0: 00 gnome-terminal 3375? Sl 0: 00 gnome-terminal 3375? Sl 0: 00 gnome-terminal 3383 pts/1 Ss 0: 00 bash 3740? Sl 0: 02/usr/lib/firefox // The following are firefox threads 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? SNl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? SNl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3740? Sl/usr/lib/firefox 3833 pts/1 R + ps xH

As you can see from the above resultsThe process firefox has many threads. How do you know what an official said? In fact, we can see from the PID column that they all have the same PID. This again shows that the thread shares resources with the process that created it. Strictly speaking, the thread is not marked as a process PID. The above result shows the process PID. We canRun the ps md 3740 command to view the thread where the PID is 3740.The result is as follows:

Ps mp 3740 // view the thread pid tty stat time command 3740 where the PID is 3740 processes? -0: 02/usr/lib/firefox--Sl 0: 01-// The following shows the firefox thread---Sl 0: 00--0: Sl 00---Sl 0: 00 -- -Sl 0: 00---Sl 0: 00---Sl 0: 00---Sl 0: 00---Sl 0: 00---Sl 0: 00--Sl 0: 00---Sl 0: 00 -- -SNl 0: 00---Sl 0: 00---Sl 0: 00---Sl 0: 00---Sl 0: 00---Sl 0: 00---Sl 0: 00--Sl 0: 00 -- -Sl 0: 00---Sl 0: 00---Sl 0: 00---Sl 0: 00---Sl 0: 00---Sl 0: 00--Sl 0: 00---Sl 0: 00 -- -Sl 0: 00---Sl 0: 00---Sl 0: 00---SNl 0: 00---Sl 0: 00---Sl 0: 00---Sl 0: 00--Sl 0: 00 -- -Sl 0: 00---Sl 0: 00---Sl 0: 00---Sl 0: 00---Sl 0: 00---Sl 0: 00--Sl 0: 00-

From the preceding running results, we can see that the thread whose PID is 3740 has no PID.

Let's talk about the thread overview example. 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.