2. Process
• A process is basically a program in execution. The
execution of a process must progress in a sequential
fashion.
• A process is defined as an entity which represents the
basic unit of work to be implemented in the system.
• A Process Control Block is a data structure maintained by
the Operating System for every process.
3.
4. Thread
• A thread is a sequence of such instructions within a program
that can be executed independently of other code.
• Thread switching does not need to interact with operating
system.
• While one thread is blocked and waiting, a second thread in the
same task can run.
5.
6. S.N. Process Thread
1 Process is heavy weight or resource intensive. Thread is light weight, taking lesser resources than a
process.
2 Process switching needs interaction with operating system. Thread switching does not need to interact with operating
system.
3 In multiple processing environments, each process executes
the same code but has its own memory and file resources.
All threads can share same set of open files, child processes.
4 If one process is blocked, then no other process can execute
until the first process is unblocked.
While one thread is blocked and waiting, a second thread in
the same task can run.
5 Multiple processes without using threads use more
resources.
Multiple threaded processes use fewer resources.
6 In multiple processes each process operates independently
of the others.
One thread can read, write or change another thread's data.
8. Multithreading
• Running several threads is similar to running several different programs
concurrently.
• Multiple threads within a process share the same data space with the main thread
and can therefore share information or communicate with each other more easily
than if they were separate processes.
• Threads are sometimes called light-weight processes and they do not require much
memory overhead; they are cheaper than processes.
9. Types of Thread
• User level Thread-It is implemented by user.
• Kernel level Thread-It is implemented by os.
Race condition:
Two or more thread access same piece of data, inconsistent result may arise(because
of order of data access).So we are going for lock system
10. Threading Module
• Creating Thread Using Threading Module
• define a new subclass of the Thread class.
• Override the __init__(self [,args]) method to add additional arguments.
• Then, override the run(self [,args]) method to implement what the
thread should do when started.
11. Threading object Methods
• Start()- Begin thread execution.
• Run()- Method defining thread functionality.
• Join()- Suspend until started thread terminates blocks unless timeout is given
• getName() – Return name of thread
• setName()- set name of thread
• isAlive()- Boolean flag indicating whether thread is still running
• isDaemon()- Return Daemon flag of thread.
• SetDaemon()-Used to set the thread as daemon thread.
• Active_count-Return the number of threads currently running.
• Enumerate()-display all the current running thread names.
• Current_Thread()-display the current thread.
14. Sample Code:
import threading
def f1():
print(threading.active_count())
print(threading.current_thread())
print(threading.enumerate())
for i in range(3):
t=threading.Thread(target=f1)
t.start()
t.join()
15. Synchronizing Thread
• Locking mechanism that allows us to synchronize threads.
• New lock is created by calling the lock method(returns new lock)
• Acquire(blocking) of new lock object is used to force threads to run synchronously.
• Release() of new lock object is used to release the lock when it is no longer required.
19. Multithreaded Priority Queue
With the help of Queue module we can create a new queue object that can hold a
specific number of items and threads. Queue module have following method which
is given below.
1. get(): The get() removes and returns an item from the queue.
2.put(): The put adds item to a queue.
3.qsize() : The qsize() returns the number of items that are currently in the queue.
4.empty(): The empty( ) returns True if queue is empty; otherwise, False.
5.full(): the full() returns True if queue is full; otherwise, False.
20. Creating Queue
import queue
q=queue.Queue(3) #Queue base approach
#q=queue.LifoQueue()#--> stack base approach
for i in range(3):
q.put(i)
print(q.full())
print(q.qsize())
while not q.empty():
print(q.get())
https://www.geeksforgeeks.org/multithreading-python-set-1/