Simulation of Multi Threading On Single Threaded Operating Systems
Simulation of Multi Threading On Single Threaded Operating Systems
Operating Systems
Chandrajit G. Joshi
Fig. 1
2.3.1 Pseudo Code of main()
main ()
{
DECLARE and INITIALIZE local variables.
The SWITCH case that follows listener()
executes appropriate case depending on the type
CALL init_Process().
of request. The function thread_Schedule()
allocates a ThCB and execution stack for the
CALL init_Thread_Module().
appropriate function depending on the type of
CALL open_Files().
request received, and places the ThCB in the
ready list. This function will then be launched as
WHILE (TRUE)
thread. process_Unknown_Request() handles all
{
the undefined requests. Function main() returns if
CALL listner(). the request type is QUIT. Process then enters the
thread execution loop and does not exit from it
SWITCH (Request Type) till there are ThCBs in the ready list.
{ thread_Dispatch() launches and executes all the
READ : CALL thread_Schedule(READ). threads in the ready list and returns only when
WRITE : CALL thread_Schedule(WRITE). there are no threads in it.
UPDATE : CALL thread_Schedule(UPDATE). thread_IO_Completion() checks for any event
QUIT : RETURN. completion (e.g. IO or timeout). If there is any
. thread that is currently waiting for the event
. completion, then it takes the corresponding
DEFAULT: CALL process_Unknown_Request(). thread from the wait list and puts it in the ready
} /* End of SWITCH */ list according to its priority.
WHILE (There are ThCBs in the ready list)
{ In this pseudo code, thread_Dispatch() is the
CALL thread_Dispatch(). most important function because when it finds a
ThCB in the ready list, it launches the thread. In
CALL thread_IO_Completion(). other words, the execution jumps to the entry
} /* End of WHILE there are ThCBs */ point of the function. Following pseudo code
} /* End of WHILE TRUE */ illustrates a function that will execute as a thread.
} /* End of main() */
main() thread_Start()
process. In other words, these are long running
threads. The following pseudo code shows this. Main stack Thread stack
Fig. 2
PREV
TO = T2
PREV
TO = T3
PREV
. . . . . . . . TO = Tn
PREV
In Kernel mode, process will be responsible for IF ready list is NOT empty
establishing threading environment for the GOTO launch_The_Task.
process. In this mode, it will also be responsible
for detecting and processing I/O and timeout CALL listener().
completions, setting the next dispatch-states for
various tasks according to the result of last I/O or SWITCH (Request Type)
timeout completion and launching the tasks {
according to their respective dispatch states. READ: CALL process_Read().
WRITE: CALL process_Write().
UPDATE: CALL process_Update().
QUIT: RETURN.
E.g. for READ request, process_Read() is called.
.
process_Read() allocates a TCB for this request
.
and sets all the structure members of TCB like
DEFAULT: CALL process_Unknown_Request().
name of task, its priority, to appropriate values.
} /* End of SWITCH */
A check for TCBs in the ready list is again made,
IF ready list is NOT empty
and the tasks are launched if there are any.
GOTO launch_The_Task.
Read_From_DB
IF (CurTCB->step IS EQUAL TO 1) Other tasks PROCESS
task
DATABASE
{
CALL thread_ReadDB(FILE1).
Update_DB
task
SET CurTCB->step TO 2.
Fig. 4
6. REFERENCES
[1] Hewlett-Packard, “User Level Native Thread
Primitives”, www.hp.com
[2] “Advanced Linux Programming”,
www.advancedlinuxprogramming.com