[Description] threads and processes are a common topic. What are the differences and advantages of threads and processes? How many threads can be opened at most?
[Resolution]
1 difference
A thread is the smallest unit of CPU scheduling and a process is the smallest unit of resource allocation. A process is a thread container, and the real completion of code execution is a thread, and the process is used as the thread execution environment. In a 32-bit Windows operating system, the system allocates a private 232 = 4 GB virtual address space for each process. But in fact, only 2 GB space is used by user partitions, and 2 GB space is used for Kernel partitions such as kernel code and device drivers. In the process, the thread shares the address space of the user partition.
2 advantages and disadvantages
In Windows, for multi-process programs, the system allocates private 4 GB virtual address space for processes, occupying a large amount of resources. For multi-threaded programs, multithreading shares the address space of the same process, so it consumes less resources. During process switching, the whole address space needs to be exchanged, while switching between threads only changes the execution environment, so the efficiency is high. In Windows, multithreading is better than multi-process. After all, Windows begins with multithreading.
In Linux, the POSIX standard is adopted, and the Unix family comes from multiple processes. There is no significant difference between the scheduling overhead of multiple processes and that of multiple threads.
An image metaphor,
Multi-process is a three-dimensional transportation system. Although the cost is high, it consumes more oil on the uphill and downhill slopes, but there is no congestion.
Multithreading is a plane traffic system with low cost, but there are too many traffic lights and traffic jams.
The so-called oil consumption mostly refers to the CPU clock speed, but the clock speed is no longer a problem, and the memory is getting bigger and bigger. We hope that the driving process will be unobstructed.
3. Thread Stack
The thread stack is used to maintain all function parameters and local variables required by the thread to execute code. In a 32-bit system, how many threads can be opened in a process?
There is no fixed answer,
Thread size = process user space/thread stack size.
In Windows, the default stack size is 1 M. in this calculation, 2 GB/1 M = 2048 threads can be opened theoretically. But in fact, it cannot be done. Because of the consumption of other parts.
On the Linux platform, such as Fedora 10, you can use the ulimit-a command to view the stack size. The result is
Stack size (kbytes,-s) 10240
That is, 10 M. In this calculation, a maximum of 2 GB/10 MB = 204 threads can be created. In addition, refer to the document, which indicates that the default stack size of the Linux platform should be 8192KB rather than 10 MB.
[Instance profiling]
Test the number of available ECs instances:
Test environment: VM + 512 MB (estimated virtual memory is 1024 MB)
Test. c
[Html]
# Include <stdio. h>
# Include <stdlib. h>
# Include <pthread. h>
# Include <assert. h>
Void * start_routine (void * param)
{
Int data = * (int *) param;
Printf ("% s: % d \ n", _ func __, data );
Return NULL;
}
# Define THREADS_NR 1024
Void create_test_threads ()
{
Int I = 0;
Int ret1 = 0;
Void * ret2 = NULL;
Pthread_t ids [THREADS_NR] = {0 };
For (I = 0; I <THREADS_NR; I ++)
{
Ret1 = pthread_create (ids + I, NULL, start_routine, & I );
}
For (I = 0; I <THREADS_NR; I ++)
{
Pthread_join (ids [I], & ret2 );
}
Return;
}
Int main (int argc, char ** argv)
{
Create_test_threads ();
Return 0;
}
# Include <stdio. h>
# Include <stdlib. h>
# Include <pthread. h>
# Include <assert. h>
Void * start_routine (void * param)
{
Int data = * (int *) param;
Printf ("% s: % d \ n", _ func __, data );
Return NULL;
}
# Define THREADS_NR 1024
Void create_test_threads ()
{
Int I = 0;
Int ret1 = 0;
Void * ret2 = NULL;
Pthread_t ids [THREADS_NR] = {0 };
For (I = 0; I <THREADS_NR; I ++)
{
Ret1 = pthread_create (ids + I, NULL, start_routine, & I );
}
For (I = 0; I <THREADS_NR; I ++)
{
Pthread_join (ids [I], & ret2 );
}
Return;
}
Int main (int argc, char ** argv)
{
Create_test_threads ();
Return 0;
}
Makefile
[Html]
Src = test. c
Target = test
Temp = $ (wildcard *~)
Flag = PTHREAD_TEST
All: $ (src)
Gcc-g-pthread-D $ (flag) $ ^-o $ (target)
Clean:
Rm $ (temp) $ (target)
Src = test. c
Target = test
Temp = $ (wildcard *~)
Flag = PTHREAD_TEST
All: $ (src)
Gcc-g-pthread-D $ (flag) $ ^-o $ (target)
Clean:
Rm $ (temp) $ (target)
Running result
[Html]
Start_routine: 300
Start_routine: 301
Start_routine: 302
Start_routine: 303
Start_routine: 304
Segment Error
Start_routine: 300
Start_routine: 301
Start_routine: 302
Start_routine: 303
Start_routine: 304
Segment Error
Experiments show that up to 305 threads can be opened in this environment. Debug with GDB:
Gdb test
[Html]
Start_routine: 303
[Thread 0xbe409b90 (LWP 11075) exited]
[New Thread 0xbee0ab90 (LWP 11076)]
Start_routine: 304
[Thread 0xbee0ab90 (LWP 11076) exited]
[New Thread 0xbf80bb90 (LWP 11077)]
Start_routine: 565
[Thread 0xbf80bb90 (LWP 11077) exited]
Program received signal SIGSEGV, Segmentation fault.
0x00581b07 in pthread_join () from/lib/libpthread. so.0
Missing separate debuginfos, use: debuginfo-install glibc-2.9-2.i686
(Gdb) bt
#0 0x00581b07 in pthread_join () from/lib/libpthread. so.0
#1 0x0804859f in create_test_threads () at test1.c: 29
#2 0x080485d1 in main () at test1.c: 37
(Gdb) frame 1
#1 0x0804859f in create_test_threads () at test1.c: 29
29 pthread_join (ids [I], & ret2 );
(Gdb) p ret1
$4 = 11
(Gdb) p ret2
$5 = (void *) 0x0
Start_routine: 303
[Thread 0xbe409b90 (LWP 11075) exited]
[New Thread 0xbee0ab90 (LWP 11076)]
Start_routine: 304
[Thread 0xbee0ab90 (LWP 11076) exited]
[New Thread 0xbf80bb90 (LWP 11077)]
Start_routine: 565
[Thread 0xbf80bb90 (LWP 11077) exited]
Program received signal SIGSEGV, Segmentation fault.
0x00581b07 in pthread_join () from/lib/libpthread. so.0
Missing separate debuginfos, use: debuginfo-install glibc-2.9-2.i686
(Gdb) bt
#0 0x00581b07 in pthread_join () from/lib/libpthread. so.0
#1 0x0804859f in create_test_threads () at test1.c: 29
#2 0x080485d1 in main () at test1.c: 37
(Gdb) frame 1
#1 0x0804859f in create_test_threads () at test1.c: 29
29 pthread_join (ids [I], & ret2 );
(Gdb) p ret1
$4 = 11
(Gdb) p ret2
$5 = (void *) 0x0
The error occurred at creation with the error code 11.
View errno. h
Use find to view Lu Jin
(1) find/-name errno. h 2>/dev/null
[Html]
/Usr/src/kernels/2.6.27.5-117. fc10.i686/include/linux/errno. h
/Usr/src/kernels/2.6.27.5-117. fc10.i686/include/asm-x86/errno. h
/Usr/src/kernels/2.6.27.5-117. fc10.i686/include/asm-generic/errno. h
/Usr/src/kernels/2.6.27.5-117. fc10.i686/include/linux/errno. h
/Usr/src/kernels/2.6.27.5-117. fc10.i686/include/asm-x86/errno. h
/Usr/src/kernels/2.6.27.5-117. fc10.i686/include/asm-generic/errno. h
(2) view source files
Run
Cat/usr/src/kernels/2.6.27.5-117. fc10.i686/include/asm-generic/errno. h
Found 11 errno-base.h files defined in the same level directory
View
Cat/usr/src/kernels/2.6.27.5-117. fc10.i686/include/asm-generic/errno-base.h
[Html] view plaincopyprint? # Define EAGAIN 11/* Try again */
# Define EAGAIN 11/* Try again */
Use man pthread_create to view,
[Html]
ERRORS
The pthread_create () function shall fail if:
EAGAIN The system lacked the necessary resources to create another
Thread, or the system-imposed limit on the total number
Threads in a process {PTHREAD_THREADS_MAX} wocould be exceeded.
EINVAL The value specified by attr is invalid.
EPERM The caller does not have appropriate permission to set
Required scheduling parameters or scheduling policy.
The pthread_create () function shall not return an error code
[EINTR].
The following sections are informative.
ERRORS
The pthread_create () function shall fail if:
EAGAIN The system lacked the necessary resources to create another
Thread, or the system-imposed limit on the total number
Threads in a process {PTHREAD_THREADS_MAX} wocould be exceeded.
EINVAL The value specified by attr is invalid.
EPERM The caller does not have appropriate permission to set
Required scheduling parameters or scheduling policy.
The pthread_create () function shall not return an error code
[EINTR].
The following sections are informative.