Threads

Definition of threads: a part of a program that can execute independently of other parts. Operating systems that support multithreading enable programmers to design programs whose threaded parts can execute concurrently.

Notes about threads:

1. Lightweight process (moving a second person into a already built house)

2. Multiple execution activities at the same time

3. Has its own program counter, stack, and set of registers

4. Extremely cheaper in terms of CPU costs

5. Two types of threads:

A. User threads

B. Kernel threads

Advantages of Threads:

Because in many applications, there may be multiple jobs processing at once, it is beneficial to separate these activities into threads that can run in quasi-parallel. This simplifies the programming model.

It takes less time to create a thread than a process because the thread will share the address space of the process—a new address does not have to be created. Threads only need a stack and storage for registers.

It takes less time to terminate a thread than a process

It takes less time to switch between two threads in the same process because they share the same address space. (i.e.,Context Switching is fast.)

Because threads share common data, it is easier to communicate between threads. Data produced in one thread is available to other threads in same process.

Speeds up applications. When there are substantial computing and I/O within a process, these activities can overlap.

If system has multiple CPUs, the use of threads allows for true parallelism.

Example:

Imagine typing an 800-page report on a word processor program. The process has three threads. One thread interacts with the user (input), one handles reformatting (changing page layouts as items are added or deleted from pages), and one to handle disk backups.

The user can be typing in information or deleting a paragraph. As this is being done the program can be reformatting the pages. The user does not have to stop and wait on the program to do this—they can continue working. At the same time, the information can be saved every few minutes to the disk to protect the changes. If there were not different threads for each of these activities within the same process, the user would not be able to continue working while the pages are being reformatted or while the backup is being done. The keyboard and mouse would be ignored while the other activities were working. Likewise, no backup could be done while the pages are being reformatted.

In addition, because all three threads are working on the same document, three processes would not work. Having three threads allow them to share memory and have access to the document.

User-level threads:

All management is done by the application by using a thread library rather than use of system calls.

Advantage of User-level threads:

Thread switching does not involve the kernel—does not call the OS and cause an interrupt to the kernel. OS not aware of the thread’s existence.

Managed as if they were single-threaded processes.

\Scheduling can be application specific—choose the best algorithm

Can run on any OS – only needs a thread library

Each thread only needs a program counter, registers, stack and small control block which are all stored in the user process address space

Performance is better in user-level threads. Fast and efficient.

Disadvantages of User-level threads:

Lack of coordination between threads and OS kernel. A process as a whole gets a time slice no matter how many threads within the process. It is up to each thread to give up control to other threads.

Thread cannot block a I/O system call by changing to kernel mode

If one thread is blocked, the entire process and all other threads in the process are blocked (even if the other threads are runable)

If multiprocessors – Only the kernel can assign processes to the CPU. Two threads within the same process cannot run simultaneously on two processors

Kernel-level threads:

All scheduling is done by the kernel. No thread library. The kernel has a thread table that keeps track of all the threads in the system in addition to maintaining a process table to keep track of all of the processes. System calls are used to manage threads.

Advantages of kernel-level threads:

Threads are independent and operate in the OS

Because the kernel is in control, the scheduler may give more time to a process that has a large number of threads as opposed to a process with few threads

Blocking is done on a thread level. If one thread is blocked, doesn’t block entire process. Therefore, kernel-level threads are good for applications that are block frequently.

Disadvantages of kernel-level threads:

Kernel threads are more like regular processes – under protection mechanisms such as system calls, interrupts, etc. Required a full thread control block for each thread to maintain the information about the threads. This increases the complexity in the kernel.

Must use the scheduling policy the OS provides

<Switching within the process involves the kernel which results in significant slow down.

Starvation may occur because of thread priority inversion.

Round Robin Scheduling

One of the oldest, simplest, fairest, and most widely used algorithms is round robin. Each process is assigned a time interval, called its quantum, which it is allowed to run. If the process is still running at the end of the quantum, the CPU is preempted and given to another process. If the process has blocked or finished before the quantum has elapsed, the CPU switching is done when the process blocks, of course. Round robin is easy to implement. All the scheduler needs to do is maintain a list of runnable processes, as shown in Fig. 1. When the process uses up its quantum, it is put on the end of the list, as shown in Fig. 2.

 

RR DiagramRound-robin scheduling. (a) The list of runnable processes. (b) The list of runnable processes after A uses up its quantum.

A peculiar issue with Round Robin scheduling is the length of the Quantum. Switching from process to process requires a certain amount of time for “administration (saving and loading registers etc.).” Suppose that this process switch or context switch takes 1 msec, including administrative tasks, etc. Also, suppose that the quantum is set at 4 msec. With these parameters, after doing 4 msec of useful work, the CPU will have to spend 1 msec on process switching. Twenty percent of the CPU time will be wasted on administrative overhead. Clearly this is too much.

To improve the CPU efficiency, we could set the quantum to, say, 100 msec. Now the wasted time is only 1 percent. But, consider what happens on a timesharing system if ten interactive users hit the carriage return key at roughly the same time. Ten processes will be put on the list of runnable processes. If the CPU is idle, the first one will start immediately, the second one may not start until 100 msec later, and so on. The unlucky last one may have to wait 1 sec before getting a chance, assuming all the others use their full quanta. Most users will perceive a 1-sec response to a short command as sluggish. So, setting the quantum too short causes too many process switches and lowers the CPU efficiency, but setting it too long may cause poor response to short interactive requests. A quantum in this scenario, set around 20-50 msec is often a reasonable compromise.

CONTEXT SWITCHING

 A context switch (aka process or task switch) “is the switching of the CPU from one process or thread to another process or thread. A context is the contents of a CPU’s registers and program counter at any point in time”, as defined by the Linux Info Project at http://www.linfo.org/context_switch.html. Context switches take place in kernel mode. Switching occurs for multitasking, interrupt handling, user and kernel mode switching, preemption, a process’ quantum is complete, a process is blocked or a system call is made. The following steps are carried out during a switch: suspension of a process’ progress, secure storing of all data related to the process for later retrieval, accessing the data in memory for the next process to run, and locating the exact position to begin or resume the next process or thread. Both hardware and software can generate a context switch. Software switching is preferred over hardware switching mainly because floating register data is not saved during a hardware switch. Time consumption during a context switch is great while registers are loaded and saved and tables, lists and memory of the time allotted to a running process are preserved. Therefore, to prevent reduced efficiency with too many process switches, it is necessary to set the quantum time slice appropriately and to eliminate preemption.

 

Click here to view the questions

Session 1

The operating system is the “go-between” between the hardware and software.

Because computers have disks, processors, printers, network interfaces, keyboards, and many other components, there needs to be a way to manage all of these medium so the computer works as efficiently as possible.

The operating system has two primary functions:

Ø     To provide an abstract interface between the software and the hardware.

Device drivers in the OS allows for interface with the hardware systems.

Hardware systems have different drivers.  Each computer cannot know how to

control every device.  The purpose of the OS is to tell each device how it should

operate.

Runs system calls which provides a variety of services

Ø     To be a resource manager

Act as a “traffic light” for all of the resources (printers, keyboards, disks, etc.)

Tells the computer when and in what order processes will be executed (job or process management).  Most operating systems allow many processes or programs to be executed at the same time.  This is called multitasking. Process management is how an operating system handles running multiple processes.

Memory management – Reading and writing data and keeping up with the location of where the files are stored.

Provide security: Provides security by providing backup and recovery routines.  Also, provides security by protecting data.

Multiplexing recources:

Share time: allow different programs or users to take turns using the resource.

Share space: hold several programs in memory at once instead of giving all of the memory to one program at a time.

Leave a Reply