Thread In OSs

Comparing how Windows and Linux manage processes and muti-threaded applications , fair comparison highlighting the pros and cons of the two giant OSs.

Threads implementation in Windows and Linux

Elements that are set to execute within a program are known as threads. Threads enhance overall system performance by allowing concurrent operations to occur during program execution. On multi-processor systems, several instances (threads) can run in parallel on several processors; enabling a program to run faster. Multi-threaded applications have gained popularity over their native single-threaded programs in favor of adding more system functionalities for users.

Threads in Windows

When a program runs, Windows creates a process and threads within to execute the various areas of the program as per the functionality it was developed for. Threads make use of the same memory address space allocated to their parent process in addition to other resources. To identify various threads within a process; each has a thread identifier, a priority scheduler, a local storage and a thread context. Windows creates threads using the “CreateThread” function that returns a handler to identify the thread and allow it to spawn child threads. The number of threads created in Windows is limited by the amount of memory (main and virtual) available on the system. A thread can create other worker threads, also known as a “thread pool”, and use public variables to share vital data or pass any necessary stack sizing, arguments and security attributes through the “CreateThread” function parameters. The thread pool is assigned small tasks by the main thread to asynchronously manage work on behalf of the application. The technology is been constantly developed by Microsoft to include waiter threads, a work queue and, a worker factory that manages the worker threads.
Threads are dispatched to the processor for execution; the scheduler code is within the system kernel, there is no pre-defined module or procedure. To change the execution order of a thread; Windows changes the priority of the parent process then sets a new priority for the thread. A thread can suspend or resume another thread, since threads do not run in synchronous; a flag is sent using the “Resume Thread” function call with the suspended thread’s handler as a parameter. This represents a key benefit in multi-threading, in that it implements simultaneous processing. When the task is completed the thread calls the function “ExitThread” to terminate, also terminating the parent process will result in terminating any thread belonging to it. The operating system does some house cleaning by freeing any resources allocated to that thread, setting the thread exit code, and terminating the main process if that thread was the only one. A thread can exit through a call to the “Terminate Thread ” function by another thread, this will not allow any cleaning to be done, leaving resources and objects still in association with the thread. Windows uses preemptive multitasking, in which a thread shares the memory space and voluntarily gives up the processor time allocated to it for another thread to execute. If any thread misbehaved due to errors in programming, a thread can be forced to terminate using the designated terminate function.

Leave a Reply

Your email address will not be published. Required fields are marked *