其他分享
首页 > 其他分享> > 【Operating system】Threads

【Operating system】Threads

作者:互联网

A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack. It shares with other threads belonging to the same process its code section, data section, and other operating-system resources, such as open fifiles and signals. A traditional (or heavyweight) process has a single thread of control. If a process has multiple threads of control, it can perform more than one task at a time.

The reasons that we have threads:

①Only now with threads, we add a new element: the ability for the parallel entities to share an address space and all of its data among themselves. 

②They are lighter weight than processes, they are easier(i.e., faster) to create and destroy than processes.

③A performance argument. Threads yield no performance gain when all of them are CPU bound, but when there is substantial computing and also substantial I/O, having threads allows these activities to overlap, thus speeding up the application. 

 

When threads add to the process model is to allow multiple executions to take place in the same process environment, to a large degree independent of one another. Having multiple threads running in parallel in one process is analogous to having multiple processes running in parallel in one computer.

Every thread can access every memory address within the process' address space, one thread can read, write, or even wipe out another thread's stack.In addition to sharing an address space, all the threads can share the same set of open files, child processes, alarms, and signals, and so on.

Like a traditional process, a thread can be in any one of several states: running, blocked, ready, or terminated.

 

The advantages and disadvantages of implementing threads in user space and in kernel

User space:

Ads:

1, a user-level threads package can be implemented on an operating system that does not support threads. 

2,They allow each process to have its own customized scheduling algothrim. 

3, They scale better, since kernel threads invariably require some table space and stack space in the kernel. 

Disads:

1, If a thread starts running, no other thread in that process will ever run unless the first thread voluntarily gives up the CPU. 

2, Programmers generally want threads precisely in applications where the threads block often, as for example, in a multithreaded Web server. 

Kernel:

Ad:When a thread blocks, the kernel, as its option, can run either another thread from the same process(if one is ready)or a thread from a different process.

Main disad: the cost of a sysrtem call is substantial, so if thread operations(creation, termination,etc.)a common, much more overhead will be incurred.

 

标签:processes,thread,space,process,system,Threads,Operating,threads,its
来源: https://www.cnblogs.com/yjx-7355608/p/15965983.html