Skip to content

threadLib is a multithreading library with three types of mapping models. One-One, Many-One and Many-Many. threadLib can be used to achieve parallel flow of execution of user program. It is available for Unix-like POSIX conformant operating systems.

Notifications You must be signed in to change notification settings

suraj2439/ThreadLib-user_threads

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 

Repository files navigation

threadLib : Multi-Threading Library

Overview:

threadLib is a multithreading library with three types of mapping models.
1. One-One
2. Many-One
3. Many-Many
threadLib can be used to achieve parllel flow of execution of user program. It is available for Unix-like POSIX operating systems.

1. One-One Model:

Each user thread maps to one kernel thread. Kernel thread is created with clone system call.
Newly created thread is inserted in the linked list of threads. 
Each node of the linked list contains the metadata of corresponding thread.

2. Many-One Model:

All the user threads created by user will run on only one kernel thread.
Scheduler is use to schedule the many user threads on one kernel thread. 
Scheduler has its own context. Here main is also treated like other threads.

Flow of execution:

    1. Suppose thread T1 is executing, after completing its time quantum it would receive an Timer Interrupt.
    2. User defined signal handler of corresponding thread T1 will be invoked.
    3. In signal handler the context of current thread is saved and we jump to Scheduler context.
    4. Scheduler finds a RUNNABLE process and jumps to its context.
    5. If newly selected thread has executed before for some time then the thread would resume execution in the 
       signal handler where context was stored previously in step 3. 
       Otherwise thread will start execution in wrapper function which is an argument to clone.
    6. This thread will execute for its time quantum and the same process will be repeated.

    Wrapper Function(execute_me in code):
            Helps in identifying when thread is over. 
            It simply calls the user function corresponding to thread.

3. Many-Many Model:

    In this model 'm' user threads run on 'n' kernel threads.
    Each kernel thread has its own Scheduler i.e. there are 'n' instance of scheduler stored in the
    array (scheduler_node_array[] in code).
    There is single linked list of threads. Each contain metadata of corresponding thread. 
    If any of the kernel thread gets Timer Interrupt then that kernel thread will pick its next thread 
    from this linked list for execution.

Flow of execution:

    1. If user creates new thread then each user thread will be created on seperate Kernel Thread 
       till NO_OF_KTHREADS(max k-threads) exceeds.
    2. If this limit exceeds, newly created thread will be inserted in the thread linked list for further execution.
    3. On Timer Interrupt(SIGALARM) user defined signal_handler gets invoked.
    NOTE: Here SIGALARM signal can interrupt any Kernel Thread(may not be the same by whome SIGALARM was set).
          To resolve this constraint, SIGALARM handler will take next Kernel Thread in round-robin fashion and 
          SIGVTALARM signal will be delivered to that specific Kernel Thread, 
          Now right Kernel Thread got interrupted to pick the next thread for its execution.
    4. Next flow of execution for each Kernel Thread will be the same as in Many-One Model flow of execution. 

Usage:

  1. git clone https://github.com/suraj2439/threadLib-user_threads.git
  2. To run the testcases execute ./run_me located in /src folder.
  3. To run specific model provide user program and compile it with /src/one_one or /src/many_one or /src/many_many files.

Contributors

About

threadLib is a multithreading library with three types of mapping models. One-One, Many-One and Many-Many. threadLib can be used to achieve parallel flow of execution of user program. It is available for Unix-like POSIX conformant operating systems.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published