PROCESS AND THREADS
Process
A process is an instance of a program in execution. It is a dynamic entity, meaning that it represents the actual execution of code, as opposed to a program, which is a static set of instructions.
Process Control Block (PCB):
A data structure maintained by the OS for each process, containing important information about the process such as its state, program counter, registers, memory management information, and I/O status.
Process States:
New: The process is being created.
Ready: The process is ready to run but is waiting for CPU time.
Running: The process is currently being executed by the CPU.
Waiting: The process is waiting for some event to occur (e.g., I/O completion).
Terminated: The process has finished execution.
Inter-Process Communication (IPC):
Mechanisms that allow processes to communicate and synchronize their actions, such as message passing, shared memory, and semaphores.
Threads
A thread is the smallest unit of processing that can be scheduled by an OS. Threads are often called lightweight processes because they share the same address space and resources of the process they belong to but can be executed independently.
Thread States:
New: The thread is being created.
Ready: The thread is ready to run.
Running: The thread is currently executing.
Waiting: The thread is waiting for some event.
Terminated: The thread has finished execution.
Types of Threads:
User Threads: Managed by user-level libraries, without OS kernel involvement in scheduling.
Kernel Threads: Managed and scheduled directly by the OS kernel.
Multithreading:
The ability of a CPU or a single process to manage multiple threads concurrently. There are different models of multithreading:
import threading
def handle_client(client_socket):
# Handle client request
client_socket.send(b'HTTP/1.1 200 OK\r\n\r\nHello, world!')
client_socket.close()
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('0.0.0.0', 8080))
server_socket.listen(5)
while True:
client_socket, addr = server_socket.accept()
client_thread = threading.Thread(target=handle_client, args=(client_socket,))
client_thread.start()