In Linux, why does a multi-process program use less threads?

Source: Internet
Author: User
In Linux, the General Technology of Linux-Linux programming and kernel information is used for programming with less threads. For details, see the following section. I have been engaged in Linux/UNIX programming for many years and have been using fork to Write multi-process programs. When someone asks why Linux seldom uses Thread Programming, they find that they really need to study this problem. View fork in man manual and get the following prompt:

Fork creates a child process that differs from the parent process only in its PID and PPID, and in the fact that resource utilizations are set to 0. File locks and pending signals are not inherited.

Under Linux, fork is implemented using copy-on-write pages, so the only penalty incurred by fork is the time and memory required to duplicate the parent's page tables, and to create a unique task structure for the child.

That is to say, fork only needs to copy the page table and create the task structure. In addition, the only difference between the parent and child processes is PID and PPID, and the resource consumption is 0.

Check PTHREAD_CREATE (3) of LinuxThreads to create only one thread, and do not mention the relevant details.

What is the actual situation? I have compiled the following two sections of code:

/* Forktest. c code :*/
# Include
# Include
# Include
# Include
# Include

Int main (int argc, char ** argv)
{
Pid_t pid;
Char * mem;
If (argc <4) {printf ("\ r \ nusage: % s time1 time2 memory-size \ n", argv [0]); return 1 ;}
Mem = (char *) malloc (atoi (argv [3]);/* intentionally input 1 GB or more memory requirements to test memory usage */
Pid = fork ();
If (pid = 0 ){
Sleep (atoi (argv [1]);
Exit (0 );
}
Sleep (atoi (argv [2]);
Return 0;
}

/* Compile the code to generate the executable program a-fork */

/* The pthread_create.c code is as follows :*/
# Include
# Include
Void * start_routine (void *)
{
Sleep (30 );
}

Int main (int argc, char ** argv)
{
Int ret;
Pthread_t pth;
Ret = pthread_create (& pth, NULL, start_routine, NULL );
Sleep (35 );
Return 0;
}

/* Compile the code to generate the executable program a-thread */

Then run the above two programs and observe them with the ps command and top command. The following table is displayed:

1. The CPU usage of both instances is 0.

2. memory usage % MEM: a-fork is 0.5, and a-thread is 0.6.

3. After the two programs run, ps can see two processes (that is, the-fork process has two, and the-thread process has two)

In-depth understanding of the symptoms:
The reason for the above result 3 is that LinuxThreads adopts the "one-to-one" model of threads and processes, and the scheduling is handed over to the core, A user-level thread management mechanism, including signal processing, is implemented.

If the above result 1 is displayed, the program is sleep. It is understandable that the CPU usage is not used.

But why is there 2nd results, that is, the memory usage of a-thread is higher than that of a-fork? Although this superficial phenomenon can answer why we like process programming and use less thread programming, why? Is this test method correct? (I am also using my own PC for testing) it seems that I have to carefully study the kernel source code to know.

According to some materials, due to the unique advantages of Linux, the process of Linux is more efficient and lightweight, so process programming is preferred for Linux.

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.