Unix - threads in UNIX.

1. What is a Thread?

  • A thread is the smallest unit of execution within a process.

  • A process may contain one or more threads.

  • All threads of a process share the same memory space (code, data, heap, files) but have their own stack and registers.

 You can think of a process as a container for resources, and threads as workers inside that container.


2. Why Threads?

  • Threads allow parallelism and faster execution on multi-core CPUs.

  • They are lighter than processes since creating a thread is cheaper than creating a process (fork() is heavy).

  • Used in applications like web servers, databases, games, browsers (where multiple tasks need to run at once).


3. Threads vs Processes

Feature Process Thread
Definition Independent program in execution Smallest execution unit within a process
Memory Space Each process has its own memory Threads share memory of process
Communication Via IPC (pipes, message queues, etc.) Directly (shared memory)
Creation Expensive (fork()) Lightweight (pthread_create())
Example Running vim, firefox Firefox tabs, web server worker threads

4. Types of Threads in UNIX

  1. User-level threads (ULT)

    • Managed by a user library (like pthreads).

    • Kernel doesn’t know about them.

    • Fast to create and manage.

  2. Kernel-level threads (KLT)

    • Managed by the kernel.

    • Each thread is scheduled by the OS.

    • More powerful (used in Linux).

In modern Linux/UNIX, POSIX Threads (pthreads) are the standard.


5. Example: Creating Threads in C (POSIX pthreads)

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

// Function executed by thread
void* worker(void* arg) {
    printf("Thread %d is running\n", *(int*)arg);
    sleep(1);
    return NULL;
}

int main() {
    pthread_t t1, t2;
    int id1 = 1, id2 = 2;

    // Create two threads
    pthread_create(&t1, NULL, worker, &id1);
    pthread_create(&t2, NULL, worker, &id2);

    // Wait for threads to finish
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    printf("Both threads finished.\n");
    return 0;
}

Output:

Thread 1 is running
Thread 2 is running
Both threads finished.

Here:

  • pthread_create() creates a new thread.

  • pthread_join() waits for a thread to finish.


6. Thread Life Cycle

  1. New – created but not started.

  2. Runnable – ready to run.

  3. Running – currently executing.

  4. Waiting/Blocked – waiting for I/O or event.

  5. Terminated – finished execution.


7. Diagram – Process vs Threads

+---------------------------+        +---------------------------+
|     Process A (PID 101)   |        |     Process B (PID 202)   |
|  +---------------------+  |        |  +---------------------+  |
|  |  Code / Data / Heap |  |        |  |  Code / Data / Heap |  |
|  +---------------------+  |        |  +---------------------+  |
|  | Thread 1 (stack)    |  |        |  | Thread 1 (stack)    |  |
|  | Thread 2 (stack)    |  |        |  | Thread 2 (stack)    |  |
|  | Thread 3 (stack)    |  |        |  | Thread 3 (stack)    |  |
+---------------------------+        +---------------------------+