0% found this document useful (0 votes)
2 views6 pages

Exp 11 Python

The document outlines an experiment on demonstrating the concept of multi-threading in Python, detailing its aim, objectives, and theoretical background. It includes code examples for creating and managing threads, as well as discussing the benefits of multi-threading for efficient resource utilization. The conclusion emphasizes the effectiveness of Python's threading module in handling tasks and managing synchronization.

Uploaded by

ranatirth7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Exp 11 Python

The document outlines an experiment on demonstrating the concept of multi-threading in Python, detailing its aim, objectives, and theoretical background. It includes code examples for creating and managing threads, as well as discussing the benefits of multi-threading for efficient resource utilization. The conclusion emphasizes the effectiveness of Python's threading module in handling tasks and managing synchronization.

Uploaded by

ranatirth7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

‭Vidyavardhini’s College of Engineering & Technology‬

‭Department of Computer Engineering‬

‭ xperiment No. 11‬


E
‭Demonstrate the concept of Multi-threading‬
‭Date of Performance:02/04/25‬
‭Date of Submission:09/04/25‬
‭Vidyavardhini’s College of Engineering & Technology‬
‭Department of Computer Engineering‬

‭Experiment No. 11‬

‭Title:‬‭Demonstrate the concept of Multi-threading‬

‭Aim:‬‭To study and implement the concept of Multi-threading‬

‭Objective:‬‭To introduce the concept of Multi-threading‬‭in python‬

‭Theory:‬

‭Thread‬
I‭ n computing, a ‬‭process‬‭is an instance of a computer‬‭program that is being executed. Any‬
‭process has 3 basic components:‬
‭●‬ ‭An executable program.‬
‭●‬ ‭The associated data needed by the program (variables, work space, buffers, etc.)‬
‭●‬ ‭The execution context of the program (State of process)‬
‭A ‭t‬ hread‬‭is an entity within a process that can be‬‭scheduled for execution. Also, it is the‬
‭smallest unit of processing that can be performed in an OS (Operating System).‬

I‭ n simple words, a ‬‭thread‬‭is a sequence of such instructions‬‭within a program that can be‬
‭executed independently of other code. For simplicity, you can assume that a thread is simply‬
‭a subset of a process!‬
‭A thread contains all this information in a ‬‭Thread‬‭Control Block (TCB)‬‭:‬
‭●‬ ‭Thread Identifier:‬‭Unique id (TID) is assigned to‬‭every new thread‬
‭●‬ ‭Stack pointer:‬‭Points to thread’s stack in the process.‬‭Stack contains the local variables‬
‭under thread’s scope.‬
‭●‬ ‭Program counter:‬‭a register which stores the address‬‭of the instruction currently being‬
‭executed by thread.‬
‭●‬ ‭Thread state:‬‭can be running, ready, waiting, start‬‭or done.‬
‭●‬ ‭Thread’s register set:‬‭registers assigned to thread‬‭for computations.‬
‭●‬ ‭Parent process Pointer:‬‭A pointer to the Process control‬‭block (PCB) of the process that‬
‭the thread lives on.‬
‭Vidyavardhini’s College of Engineering & Technology‬
‭Department of Computer Engineering‬

‭ ode:‬
C
‭import threading‬
‭import os‬
‭import time‬
‭import concurrent.futures‬

‭def print_cube(num):‬
‭print("Cube: {}".format(num * num * num))‬

‭def print_square(num):‬
‭print("Square: {}".format(num * num))‬

‭if __name__ == "__main__":‬


‭t1 = threading.Thread(target=print_square, args=(10,))‬
‭t2 = threading.Thread(target=print_cube, args=(10,))‬
‭t1.start()‬
‭t2.start()‬
‭t1.join()‬
‭t2.join()‬
‭print("Done!")‬

‭def task1():‬
‭print("Task 1 assigned to thread: {}".format(threading.current_thread().name))‬
‭print("ID of process running task 1: {}".format(os.getpid()))‬

‭def task2():‬
‭print("Task 2 assigned to thread: {}".format(threading.current_thread().name))‬
‭print("ID of process running task 2: {}".format(os.getpid()))‬

‭if __name__ == "__main__":‬


‭print("ID of process running main program: {}".format(os.getpid()))‬
‭print("Main thread name: {}".format(threading.current_thread().name))‬

t‭1 = threading.Thread(target=task1, name='t1')‬


‭t2 = threading.Thread(target=task2, name='t2')‬

t‭1.start()‬
‭t2.start()‬

‭t1.join()‬
‭Vidyavardhini’s College of Engineering & Technology‬
‭Department of Computer Engineering‬

‭t2.join()‬

‭def worker():‬
‭print("Worker thread running")‬

‭ ool = concurrent.futures.ThreadPoolExecutor(max_workers=2)‬
p
‭pool.submit(worker)‬
‭pool.submit(worker)‬
‭pool.shutdown(wait=True)‬
‭print("Main thread continuing to run")‬

‭x = 0‬

‭def increment():‬
‭global x‬
‭x += 1‬

‭def thread_task():‬
‭for _ in range(100000):‬
‭increment()‬

‭def main_task():‬
‭global x‬
‭x = 0‬
‭t1 = threading.Thread(target=thread_task)‬
‭t2 = threading.Thread(target=thread_task)‬
‭t1.start()‬
‭t2.start()‬
‭t1.join()‬
‭t2.join()‬

‭if __name__ == "__main__":‬


‭for i in range(10):‬
‭main_task()‬
‭Vidyavardhini’s College of Engineering & Technology‬
‭Department of Computer Engineering‬

‭print("Iteration {0}: x = {1}".format(i, x))‬

‭class DummyThread(threading.Thread):‬
‭def __init__(self, name, priority):‬
‭threading.Thread.__init__(self)‬
‭self.name = name‬
‭self.priority = priority‬

‭def run(self):‬
‭name = self.name‬
‭time.sleep(1.0 * self.priority)‬
‭print(f"{name} thread with priority {self.priority} is running")‬

t‭1 = DummyThread(name='Thread-1', priority=4)‬


‭t2 = DummyThread(name='Thread-2', priority=1)‬

t‭1.start()‬
‭t2.start()‬

t‭1.join()‬
‭t2.join()‬
‭print('All Threads are executed')‬

‭Conclusion:‬‭Multithreading‬‭in‬‭Python‬‭turned‬‭out‬‭to‬‭be‬‭pretty‬‭effective.‬‭By‬‭running‬‭multiple‬
‭threads‬ ‭at‬ ‭the‬ ‭same‬ ‭time,‬ ‭the‬ ‭program‬ ‭was‬‭able‬‭to‬‭make‬‭better‬‭use‬‭of‬‭system‬‭resources‬‭and‬
threading‬‭module‬‭made‬‭it‬‭easier‬‭to‬
‭handle‬‭tasks‬‭more‬‭efficiently.‬‭Using‬‭Python’s‬‭built-in‬‭
‭manage‬‭synchronization‬‭and‬‭avoid‬‭issues‬‭like‬‭race‬‭conditions‬‭when‬‭multiple‬‭threads‬‭accessed‬
‭shared‬ ‭data.‬ ‭It‬ ‭worked‬ ‭well‬ ‭for‬ ‭both‬ ‭I/O-bound‬ ‭and‬ ‭CPU-heavy‬ ‭tasks,‬ ‭showing‬ ‭just‬ ‭how‬
‭Vidyavardhini’s College of Engineering & Technology‬
‭Department of Computer Engineering‬

‭flexible‬‭and‬‭reliable‬‭Python‬‭can‬‭be‬‭when‬‭it‬‭comes‬‭to‬‭multithreading.‬‭Overall,‬‭the‬‭experiment‬
‭highlighted how practical and useful this approach is in real-world Python programming.‬

You might also like