Tuesday, April 10, 2012

Threads


  • A thread is a basic unit of CPU utilization, consisting of a program counter, a stack, and a set of registers, ( and a thread ID. )


the difference between a Thread and a Process?

A process is an executing instance of an application.
For example, when you double-click the Microsoft Word icon, you start a process that runs Word.
A process is a collection of virtual memory space, code, data, and system resources.
A processor executes threads, not processes, so each application has at least one process, and a process always has at least one thread of execution, known as the

primary thread.
A process can have multiple threads in addition to the primary thread
a process is an instance of a computer program that is being sequentially executed


A thread is a path of execution within a process.
a process can contain multiple threads. When you start Word, the operating system creates a process and begins executing the primary thread of that process.
A thread is code that is to be serially executed within a process
Thread – is stream of executable code within process. They are light weight process.
A single process may contain several executable programs (threads) that work together as a coherent whole



http://www.cs.jhu.edu/~yairamir/cs418/os2/index.htm
http://siber.cankaya.edu.tr/ozdogan/OperatingSystems/ceng328/node99.html
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/4_Threads.html





  • For example in a word processor, a background thread may check spelling and grammar while a foreground thread processes user input ( keystrokes ), while yet a third


thread loads images from the hard drive, and a fourth does periodic automatic backups of the file being edited.

Another example is a web server - Multiple threads allow for multiple requests to be satisfied simultaneously, without having to service requests sequentially or to

fork off separate processes for every incoming request.
( The latter is how this sort of thing was done before the concept of threads was developed. A daemon would listen at a port, fork off a child for every incoming

request to be processed, and then go back to listening to the port. )

A single threaded process can only run on one CPU, no matter how many may be available, whereas the execution of a multi-threaded application may be split amongst

available processors.


There are two types of threads to be managed in a modern system: User threads and kernel threads.

User threads are supported above the kernel, without kernel support. These are the threads that application programmers would put into their programs.

Kernel threads are supported within the kernel of the OS itself. All modern OSes support kernel level threads, allowing the kernel to perform multiple simultaneous

tasks and/or to service multiple kernel system calls simultaneously.





Thread libraries provide programmers with an API for creating and managing threads.

There are three main thread libraries in use today:

POSIX Pthreads - may be provided as either a user or kernel library, as an extension to the POSIX standard.

Win32 threads - provided as a kernel-level library on Windows systems.

Java threads - Since Java generally runs on a Java Virtual Machine, the implementation of threads is based upon whatever OS and hardware the JVM is running on, i.e. either Pthreads or Win32 threads depending on the system.



http://www2.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/4_Threads.html







  • An application typically is implemented as a separate process with several threads of control.

A web browser might have one thread display images or text while another thread retrieves data from the network.

A word processor may have a thread for displaying graphics, another thread for responding to keystrokes from the user, and a third thread for performing spelling

and grammar checking in the background.

Consider the Web server could be written in the absence of threads. The net result is that many fewer requests/sec can be processed.

The better approach would multithread the web-server process. Thus threads gain considerable performance.
The server would create a separate thread that would listen for client requests;
when a request was made, rather than creating another process, the server would create another thread to service the request.

Many OS kernels are now multithreaded; several threads operate in the kernel, and each thread performs a specific task, such as managing devices or interrupt handling.

http://siber.cankaya.edu.tr/ozdogan/OperatingSystems/ceng328/node100.html




  • Three main thread libraries are in use today:


POSIX Pthreads. Pthreads, the threads extension of the POSIX standard, may be provided as either a user- or kernel-level library.

Win32. The Win32 thread library is a kernel-level library available on Windows systems.

Java. The Java thread API allows thread creation and management directly in Java programs. However, because in most instances the JVM is running on top of a host OS, the Java thread API is typically implemented using a thread library available on the host system.
http://siber.cankaya.edu.tr/OperatingSystems/ceng328/node106.html

  • concurrency is related to how an application handles multiple tasks it works on. An application may process one task at at time (sequentially) or work on multiple tasks at the same time (concurrently).

Parallelism on the other hand, is related to how an application handles each individual task. An application may process the task serially from start to end, or split the task up into subtasks which can be completed in parallel.
 As you can see, an application can be concurrent, but not parallel. This means that it processes more than one task at the same time, but the tasks are not broken down into subtasks.

An application can also be parallel but not concurrent. This means that the application only works on one task at a time, and this task is broken down into subtasks which can be processed in parallel.

Additionally, an application can be neither concurrent nor parallel. This means that it works on only one task at a time, and the task is never broken down into subtasks for parallel execution.
http://tutorials.jenkov.com/java-concurrency/concurrency-vs-parallelism.html

  • The Java Concurrency Utilities framework is a library of types that are designed to be used as building blocks for creating concurrent classes or applications. These types are thread-safe, have been thoroughly tested, and offer high performance.

Examples include semaphores, barriers, thread pools, and concurrent hashmaps.


https://www.javaworld.com/article/2078809/java-concurrency/java-concurrency-java-101-the-next-generation-java-concurrency-without-the-pain-part-1.html

  • Lesson: Concurrency

Computer users take it for granted that their systems can do more than one thing at a time. They assume that they can continue to work in a word processor, while other applications download files, manage the print queue, and stream audio. Even a single application is often expected to do more than one thing at a time. For example, that streaming audio application must simultaneously read the digital audio off the network, decompress it, manage playback, and update its display. Even the word processor should always be ready to respond to keyboard and mouse events, no matter how busy it is reformatting text or updating the display. Software that can do such things is known as concurrent software.
https://docs.oracle.com/javase/tutorial/essential/concurrency/

  • A computer system normally has many active processes and threads. This is true even in systems that only have a single execution core, and thus only have one thread actually executing at any given moment. Processing time for a single core is shared among processes and threads through an OS feature called time slicing.

Processes
A process has a self-contained execution environment. A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space.
Processes are often seen as synonymous with programs or applications. However, what the user sees as a single application may in fact be a set of cooperating processes. To facilitate communication between processes, most operating systems support Inter Process Communication (IPC) resources, such as pipes and sockets. IPC is used not just for communication between processes on the same system, but processes on different systems.


Threads
Threads are sometimes called lightweight processes. Both processes and threads provide an execution environment, but creating a new thread requires fewer resources than creating a new process.
Threads exist within a process — every process has at least one. Threads share the process's resources, including memory and open files

https://docs.oracle.com/javase/tutorial/essential/concurrency/procthread.html

No comments:

Post a Comment