Page 65 - DCAP403_Operating System
P. 65

Operating System




                    Notes          4.7 Concept of Thread

                                   Threads are a way for a program to fork (or split) itself into two or more simultaneously (or
                                   pseudo-simultaneously) running tasks. A thread is a single sequence stream within in a process.
                                   Because threads have some of the properties of processes, they are sometimes called lightweight
                                   processes. In a process, threads allow multiple executions of streams. In many respect, threads
                                   are popular way to improve application through parallelism.
                                   The CPU switches rapidly back and forth among the threads giving illusion that the threads are
                                   running in parallel. Like a traditional process i.e., process with one thread, a thread can be in any
                                   of several states (Running, Blocked, Ready or Terminated). Each thread has its own stack. Since
                                   thread will generally call different procedures and thus a different execution history. This is why
                                   thread needs its own stack.

                                                                 Figure 4.6: Threading

                                        Scheduler

                                                                                      Context Switcher
                                                     .....
                                                                                                  Mega32 State

                                                                                                   R1, R2, R3
                                                                                                   R4, ...





                                                                                                   R30, R31,
                                                                                                   etc.

                                                      T1          T2          T3
                                                    Thread      Thread      Thread
                                                 P1          P2          P1
                                                     Msg         Msg         Msg
                                           Sleep=50ms  Box Running  Box  Blocked  Box
                                                     Stack       Stack       Stack
                                                                                        Semaphore  Semaphore

                                                                                         Held by   Held by


                                                                                         Waiting   Waiting

                                                                                                   Waiting
                                                          Msg    Msg     Msg
                                                                                                   Waiting
                                                          Msg    Msg


                                                          Msg    Msg     Msg


                                   An operating system that has thread facility, the basic unit of CPU utilization is a thread. A
                                   thread has or consists of a program counter (PC), a register set, and a stack space. Threads are not
                                   independent of one other like processes as a result threads shares with other threads their code

                                   section, data section, OS resources also known as task, such as open files and signals.

          58                               LOVELY PROFESSIONAL UNIVERSITY
   60   61   62   63   64   65   66   67   68   69   70