1> Differences between threads and processes
The thread mechanism supports concurrent programming technology and ensures parallel processing on the multi-processor. In Linux, threads are very special. in Linux, all threads are implemented as threads.
In Linux, the thread looks like a normal process (only the process shares resources with other processes, such as address space ). The above mechanism works with Microsoft Windows or sun
Solaris implementation
Great differences. These systems provide dedicated support for thread mechanisms (lightweight processes ).
In modern operating systems, processes support multithreading. A process is the minimum unit for resource management and allocation, while a thread is the minimum unit for program execution. A process consists of two parts: a thread set and a resource.
Set. A thread in a process is a dynamic object that represents the execution process of a process instruction. Resources, including address space, opened files, and user information, are shared by threads in the process. The thread has its own private data:
Program counters, stack spaces, and registers.
In reality, there are many tasks that require concurrent processing, such as database servers, network servers, and large-capacity computing.
If the multi-process method is used, the following problems occur:
1> fork consumes a lot of sub-processes, and fork is an expensive system call.
2> each process has its own address space. inter-process collaboration requires complex IPC technologies, such as message passing and shared memory.
Threads promote the concept of processes so that a process can contain multiple activities (or execution sequences ). The advantages and disadvantages of multithreading are actually the unity of opposites. The advantages of using threads are:
1> improve the real-time response capability of the program;
2> more effective use of multi-processor, real parallelism (Parallelism );
3> improve the program structure and have multiple control flows;
4> easy communication, because the code and global data of the shared process;
5>
Reduce the use of system resources. Scheduling and switching between threads of the same process does not need to be called by the system. Therefore, this will reduce the additional consumption. A process can usually start thousands of threads and has no
Problem.
Disadvantages:
Because each thread shares the address space of the process, it may lead to competition. Therefore, some synchronization technology is required for data that has multiple threads to access.
2> thread Classification
2.1 kernel threads
The Linux kernel can be seen as a service process (managing hardware and software resources and responding to various reasonable and unreasonable requests of user processes ). The kernel requires multiple execution streams in parallel. To prevent possible blocking, multithreading
Is necessary. The kernel thread is the separation of the kernel, which can process a specific thing. Linux Kernel uses kernel threads to divide the kernel into several functional modules, such as kswapd,
Kflushd is particularly useful in processing asynchronous events such as Asynchronous Io. The use of kernel threads is cheap. The only resource used is the space for storing registers during kernel stack and context switching. Multi-line support
Multi-threads Kernel
). Scheduling of kernel threads is the responsibility of the kernel. When a kernel thread is in the blocking state, it does not affect other kernel threads because it is the basic unit of scheduling. This is different from the user thread.
2.2 user thread
The user thread is implemented in the user space. The kernel does not directly schedule the user thread process. The scheduling object of the kernel is the same as that of the traditional process. The kernel does not know the existence of the user thread.
Because the Linux kernel does not have the concept of lightweight processes (threads), you cannot schedule user threads independently. Instead, a thread Runtime Library organizes thread scheduling. Its main task is
Inter-stack scheduling. If a thread in a process calls a blocked system call, the entire process will be switched to the waiting state by the scheduler, and other threads will not be able to run. Therefore, Linux
Asynchronous I/O mechanism is used.
3> kernel threads
The kernel thread or daemon occupies a large proportion in the operating system. After the Linux operating system is started
After windows is started, you can run the "PS-Ef" command to view the processes in the system. At this time, you will find many process names ending with "D". Specifically, the names are added to the display.
"[]", These processes are kernel threads. The system starts from hardware-> kernel-> User-State processes, and PID allocation is a forward loop.
The PID of the Core Thread is usually small.
Uid pid ppid C stime tty time cmd
Root 1 0 0 09:42? 00:00:01/sbin/init
Root 2 0 0 09:42? 00:00:00 [kthreadd]
Root 3 2 0 09:42? 00:00:00 [migration/0]
Root 4 2 0 09:42? 00:00:00 [ksoftirqd/0]
Root 5 2 0 09:42? 00:00:00 [watchdog/0]
Root 6 2 0 09:42? 00:00:00 [events/0]
Root 7 2 0 09:42? 00:00:00 [khelper]
Root 40 2 0 09:42? 00:00:00 [kblockd/0]
Root 64 2 0 09:42? 00:00:00 [kseriod]
Root 110 2 0 09:42? 00:00:00 [pdflush]
Root 111 2 0 09:42? 00:00:00 [pdflush]
Root 112 2 0 09:42? 00:00:00 [kswapd0]
Root 151 2 0 09:42? 00:00:00 [AIO/0]
Root 1327 2 0 09:42? 00:00:00 [ksuspend_usbd]
Root 1330 2 0 09:42? 00:00:00 [khubd]
Root 1362 2 0 09:42? 00:00:00 [ATA/0]
Root 1370 2 0 09:42? 00:00:00 [ata_aux]
Root 1428 2 0 09:42? 00:00:00 [scsi_eh_0]
Root 1430 2 0 09:42? 00:00:01 [scsi_eh_1]
Root 2304 2 0 09:42? 00:00:00 [kjournald]
Root 2507 1 0 09:42? 00:00:00/sbin/udevd -- daemon
Root 2787 2 0 09:42? 00:00:00 [kgameportd]
Root 3698 2 0 09:42? 00:00:00 [kapmd]
Root 3872 2 0 09:42? 00:00:00 [kjournald]
3.1 kernel threads
Events processes kernel events. Many software and hardware events (such as power outages and file changes) are converted to events and distributed to threads interested in the events for response.
Ksoftirqd handles soft interruptions
Hardware interruption processing usually requires a disconnection, which cannot be too long; otherwise, new interruptions may be lost. Therefore, a large part of the interrupted work is removed and transferred to the hard-working ksoftirqd.
. For example, a network packet may need to be disconnected from the NIC, but the TCP/IP protocol does not need to be disconnected.
Kblockd manages disk block read/write
Kjournald ext3 File System Log Management usually each _ mounted _ ext3 partition has
The logs of each partition are independent.
Pdflush dirty memory page write-back
Too many dirty pages mean risks, such as the loss of content during a fault and the response to a burst of physical memory requests (a large number of write-back operations may lead to bad response times)
Kswapd memory recycling ensures that the number of idle physical memory is within a proper Range
AIO replaces user Process Management Io to support user-mode AIO
3.2 user process
Crond executes scheduled tasks
Init is the first thread created by the kernel. Guides user space services, manages orphan threads, and converts execution levels
Mingetty waits for the user to log on from tty
A bash shell process, a system interface in the form of command lines. It accepts user commands and carries out explanations and executions.
Service Process for sshd SSH logon, file transfer, command execution, and other operations
Klogd obtains print information from the kernel information buffer. When the kernel detects an exception, it usually outputs some messages to the user, which is useful for troubleshooting.
Syslogd System Log Process
Udevd supports user-mode device operations)
4> Create a thread named clone_flags
The initial process definition consists of three parts: Program, resource, and execution. A program usually refers to code. resources generally include memory resources, Io resources, and signal processing at the operating system level, and Program Execution
It is often understood as the execution context, including CPU usage, and later evolved into a thread. Prior to the appearance of the thread concept, in order to reduce the overhead of process switching, the operating system designer gradually corrected the process concept and gradually allowed
The resources occupied by a process are separated from the main body, allowing some processes to share some resources, such as files, signals, data memory, and even code. Applications can be tuned through a unified clone () System
You can use different parameters to specify whether to create a simple process or a common process. In the kernel, the clone () call will call do_fork () after passing and interpreting the parameters. This core function is also
Fork ():
Long do_fork (unsigned long clone_flags,
Unsigned long stack_start,
Struct pt_regs * regs,
Unsigned long stack_size,
Int _ User * parent_tidptr,
Int _ User * child_tidptr)
Clone_flags is taken from the "or" value of the following macro:
In do_fork (), different clone_flags will lead to different behaviors. For linuxthreads, it uses (clone_vm |
Clone_fs | clone_files |
Clone_sighand) parameter to call clone () to create a "Thread", indicating shared memory, shared file system access count, shared file descriptor table, and shared signal processing method. Ben
To see how the Linux kernel shares these resources.
1. clone_vm
Do_fork () needs to call copy_mm () to set the mm and active_mm items in task_struct. The two mm_struct data are associated with the process.
Corresponding to the memory space. If the clone_vm switch is specified during do_fork (), copy_mm () will
Set active_mm
Increase the number of users of the mm_struct (mm_struct: mm_users ). That is to say, the Lightweight Process shares the memory with the parent process.
Address space. The position of mm_struct in the process can be seen from the diagram:
2. clone_fs
Use FS (struct fs_struct) in task_struct
*) Records the root directory and current directory information of the file system where the process is located. When do_fork () is used, copy_fs () is called to copy the structure. For lightweight processes, only
FS-> count, shares the same fs_struct with the parent process. That is to say, a lightweight process does not have information related to an independent file system, and any thread in the process changes the current
Directory, root directory, and other information will directly affect other threads.
3. clone_files
A process may open some files and use files (struct files_struct) in the process structure task_struct.
*) To save the file structure opened by the process (struct
File) information. In do_fork (), copy_files () is called to process this process attribute. Lightweight processes and parent processes share this structure. copy_files ()
Only files-> count is added. This sharing allows any thread to access open files maintained by the process, and their operations are directly reflected in other threads in the process.
4. clone_sighand
Each Linux Process can customize the signal processing method.
Use a struct in signal_struct)
The array of k_sigaction structure is used to save the configuration information. copy_sighand () in do_fork () is used to copy the information. Lightweight processes do not copy, but only
Only the signal_struct: Count count is added. This structure is shared with the parent process. That is to say, the subprocess and the parent process have the same signal processing method and can be changed to each other.
Although Linux supports lightweight processes, it does not support Core-level threads, because Linux "Threads" and "processes" are actually at a scheduling level, sharing a process identifier space.
Restrictions make it impossible to implement POSIX thread mechanism in a full sense on Linux. Therefore, many implementations of Linux thread libraries can only implement the vast majority of POSIX semantics as much as possible, and
Function approaching as much as possible.
This article is from the chinaunix blog. If you want to view the original text, click:
Http://blog.chinaunix.net/u1/55599/showart_1271977.html