Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 84

Unit II - Process Description and Control

• Syllabus
• Process: Concept of a Process, Process States, Process Description, Process
Control (Process creation, Waiting for the process/processes, Loading
programs into processes and Process Termination), Execution of the
Operating System. Threads: Processes and Threads, Concept of
Multithreading, Types of Threads, Thread programming Using Pthreads.
Scheduling: Types of Scheduling, Scheduling Algorithms, and Thread
Scheduling
OS Management of Application Execution
• Resources are made available to multiple
applications
• The processor is switched among multiple
applications so all will appear to be progressing
• The processor and I/O devices can be used efficiently
What is a Process?
• A process is basically a program under execution.
• The execution of a process must progress in a sequential fashion.
• we write our computer programs in a text file and when we execute this
program, it becomes a process which performs all the tasks mentioned
in the program.
• A process utilizes CPU time and I/o resources.
• A process can be divided into four sections ─ stack, heap, text and data.
What is a Process?

Stack
The process Stack contains temporary data like
method/function parameters, return address and local
variables.
Heap
This is dynamically allocated memory to a process
during its run time.
Data
This section contains the global and static variables.

Text
This includes the current activity represented by the
value of Program Counter and the contents of the
processor's registers.
Process Elements

• While the program is executing, the process can be uniquely


characterized by a number of elements, including:

identifier

program
state priority
counter
memory context I/O status accounting
pointers data information information
Process Control Block

▪Contains the process elements


▪ It is possible to interrupt a running process and later
resume execution as if the interruption had not occurred
▪ Created and managed by the operating system
▪ Key tool that allows support for multiple processes
Two-State Process Model

• A process may be in one of the two states:


– running
– not-running
Five-State Process Model
Queuing Diagram
Multiple
Blocked
Queues
Process Creation

Process Parent Child


spawning process process
• when the • is the • is the new
OS creates original, process
a process at creating,
the explicit process
request of
another
process
Reasons for Process Creation
Process Creation

• Once the OS decides to create a new process it:

assigns a unique process identifier to the


new process

allocates space for the process

initializes the process control block

sets the appropriate linkages

creates or expands other data structures


Process Creation
• When a new process is created, the operating system assigns a unique Process
Identifier (PID) to it and inserts a new entry in the process table.
• The required memory space for all the elements of the process such as program,
data, and stack is allocated including space for its PCB.
• various values in PCB are initialized such as, The process identification part is
filled with PID assigned to it in step (1) and also its parent’s PID.
• The process state information would be set to ‘New’.
• Priority would be lowest by default, but the user can specify any priority during
creation.
• Then the operating system will link this process to the scheduling queue and the
process state would be changed from ‘New’ to ‘Ready’. Now the process is
competing for the CPU.
• Additionally, the operating system will create some other data structures such as
log files or accounting files to keep track of processes activity.
Process Creation
• Allocate a slot in the process table for the new process
1

• Assign a unique process ID to the child process


• Process 2

creation is by • Make a copy of the process image of the parent, with the
means of the 3 exception of any shared memory

kernel system • Increments counters for any files owned by the parent, to reflect
call, fork( ) 4 that an additional process now also owns those files

• This causes • Assigns the child process to the Ready to Run state
the OS, in 5

Kernel Mode, • Returns the ID number of the child to the parent process, and a 0
to: 6 value to the child process
After Creation

• After creating the process the Kernel can do one of the following, as
part of the dispatcher routine:
– stay in the parent process
– transfer control to the child process
– transfer control to another process
Fork() System Call
• The fork () system calls creates new process.
• When a process uses the fork() system call, it creates a replicate of
itself.
• The parent process is the existing process, and the child process is the
new process.
• When creating the child process, the parent states like open files,
address space, and variables are copied to the child process.
• The child and parent processes are located in separate physical
address spaces.
• As a result, the modification in the parent process doesn't appear in
the child process.
Fork() System Call
• Fork() system call takes no argument
• Fork() makes two identical copies of address spaces, one for the parent and
the other for the child.
• Fork() returns PID of the child process to the parent process
• After successfull creation of the process, both parent and child process
starts executing the next instruction.
• If Fork()>0, process creation is successful.
• If Fork()<0, process creation is Unsuccessful.
• If fork()=0, Child process
Fork() System Call
• int main()
• {
P
• fork();
• fork();
• printf("hello\n"); C1 P
• return 0;
• }
C2 C1 C3 P

• Output-
• hello
• hello
• hello
• hello
Fork() System Call
• int main()
• {
P
• fork();
• fork();
• printf("hello\n"); C1 P
• return 0;
• }
C2 C1 C3 P

• Output-
• hello
• hello
• hello
• hello
Fork() System Call

int main(){
pid_t p= fork(); //calling of fork system call
if(p==-1)
printf("Error occured while calling fork()");
else if(p==0)
printf("This is the child process");
else
printf("This is the parent process");
return 0;
}
Fork() System Call
Exec() System Call

• The exec() system call is used to make the new processes.


• When the exec() function is used, the currently running process is terminated
and replaced with the newly formed process.
• In other words, only the new process persists after calling exec().
• The parent process is shut down.
• This system call also substitutes the parent process's text segment, address
space, and data segment with the child process.
• When exec() is invoked, the program specified in the parameter to exec() will
replace the entire process including all threads.
• The process id will remain same, but the contents will be different.
Exec() System Call

Features Fork() Exec()


Definition It is a command that allows a process to copy It is a command that makes a new process by
itself. replacing the existing process.

Address Space The parent and child processes are in separate The child address space replaces the parent address
address spaces in the fork(). space in the exec().

Parent Process There is a child process and a parent process There is only a child process after calling the exec().
after calling the fork() function.

Result The fork() makes a child's process equal to the The exec() makes a child process and replaces it with
parent's process. the parent process.
Exec() System Call

fork() exec()
The fork() creates a new process that is an identical copy The exec() creates a new process in place of the original
of the original process. process.
Both the parent and the child processes are run at the Unless there is an error, control never goes back to the
same time. original process.
Parent and child processes are in different address parent address space is replaced by the child address
spaces. space.
EXEC
# include<stdio.h> # include<stdio.h>
Int main() Int main()
{ {
Printf(“Pid of process P1.c=%d\n”,getpid()); Printf(“We are in Process P2.c”);
Execv(“./P2.”,args); Printf(“Pid of process P2.c= %d,getpid());
Printf(“Back to Process P1”); Return(0);
Return(0); }
}

Output
Pid of process P1.c = 5962
We are in Process P2.c
Pid of process P2.c = 5962
Process state transition diagram
Suspended Processes
• Suspend ready
• A process in the ready state, which is moved to secondary memory from the main
memory due to lack of the resources (mainly primary memory) is called in the suspend
ready state.
• If the main memory is full and a higher priority process comes for the execution then
the OS have to make the room for the process in the main memory by throwing the
lower priority process out into the secondary memory.
• The suspend ready processes remain in the secondary memory until the main memory
gets available.
• Suspend wait
• Instead of removing the process from the ready queue, it's better to remove the
blocked process which is waiting for some resources in the main memory.
• Since it is already waiting for some resource to get available hence it is better if it waits
in the secondary memory and make room for the higher priority process. T
• hese processes complete their execution once the main memory gets available and
their wait is finished.
Characteristics of a Suspended Process

• The process is not • The process may or may not


immediately available for be waiting on an event
execution

• The process was placed in a • The process may not be


suspended state by an agent: removed from this state until
either itself, a parent process, the agent explicitly orders the
or the OS, for the purpose of removal
preventing its execution
Reasons for Process Suspension

Table 3.3 Reasons for Process Suspension


OS Modes of Execution

User Mode System Mode


– less-privileged mode – more-privileged mode
– user programs typically – also referred to as control
execute in this mode mode or kernel mode
– kernel of the operating
system
Kernel Mode vs user Mode
• Kernel Mode
✔ Can use the full instruction set of the CPU. Including:
✔ Enabling / disabling interrupts
✔ Setting special registers (page table pointer, interrupt table pointer,
etc…)
✔ Can modify any location in memory and modify page tables
• User Mode
✔ Cannot use privileged instructions.
✔ Can only modify the memory assigned to the process.
Interrupts
• An interrupt is an event that requires immediate attention from the CPU.
• Interrupt handler is a Procedure for handling interrupts
• Save all registers including the program counter to memory
• CPU looks up the interrupt handler on the interrupt vector table and calls
the interrupt handler
• Once the interrupt handler is completed, it restores all registers and
returns to the program counter.
• It may optionally retry the instruction that caused the interrupt (in the
case of a page fault).
• The program continues execution, not knowing anything has happened
Context Switching
• Context switches are caused by software and hardware interrupts
• Save the context of the process that is currently running on the CPU. Update the process control
block and other important fields.
• Move the process control block of the above process into the relevant queue such as the ready
queue, bloacked queue etc.
• Select a new process for execution.
• Update the process control block of the selected process. This includes updating the process
state to running.
• Update the memory management data structures as required.
• Restore the context of the process that was previously running when it is loaded again on the
processor. This is done by loading the previous values of the process control block and registers.
• Invoke the scheduler to find a ‘ready’ process and change its state to ‘running’
• Restore registers for the new process and switch back to user mode.
Change of Process State

• The steps in a update the process move the process


full process save the context of
the processor
control block of the
process currently in
control block of this
process to the
switch are: the Running state appropriate queue

If the currently running process is to be moved to another state


(Ready, Blocked, etc.), then the OS must make substantial select another
changes in its environment process for execution

restore the context of


the processor to that
which existed at the
time the selected update memory update the process
process was last management data control block of the
switched out structures process selected
Process Scheduling
• Scheduling allows one process to utilize the processor when the execution of
another process is on hold ( in waiting state)
• Any process can be on hold due to unavailability of any resource like I/O, files
hardware recourses etc,
• The aim of CPU scheduling is to make the system efficient, fast, and fair.
• Whenever the CPU becomes idle, the OS selects one of the processes in
the ready queue for the execution.
• The selection process is carried out by the short-term scheduler
Process Scheduling

• Time Quantum - is the allotted time slice for a program to run before a
scheduling decision. Typically less than or equal to a clock tick.
• Clock - one of two triggers for the scheduling algorithm. On most CPUs, the
clock ticks at a rate of 50-100Hz. Each clock tick issues a hardware interrupt
which permits the operating system to run the scheduler
Process Scheduling
• CPU scheduling decisions may take place under the following four
circumstances:
• When a process switches from the running state to
the waiting state(for I/O request or invocation of wait for the
termination of one of the child processes).
• When a process switches from the running state to the ready state
(for example, when an interrupt occurs).
• When a process switches from the waiting state to
the ready state(for example, completion of I/O).
• When a process terminates
Objectives of Process Scheduling

• Fairness: make sure each process gets its fair share of the CPU.
• Efficiency: keep the CPU as busy as possible
• Response time: minimize the response time for interactive users.
• Turnaround: minimize the time batch users must wait for output
• Throughput: maximize the number of jobs processed per hour
Process Scheduling

• Non-Preemptive Scheduling
• Under non-preemptive scheduling, once the CPU has been allocated
to a process, the process keeps the CPU until it releases the CPU
either by terminating or by switching to the waiting state.
• CPU is not interrupted in the middle of execution of the current
process. Instead, it waits till the process completes its CPU burst
time, and then after that it can allocate the CPU to any other process.
• Some Algorithms based on non-preemptive scheduling are:
⮚ Shortest Job First (Non preemptive version)
⮚ Priority Scheduling.(Non preemptive version)
Process Scheduling - Non Pre-emptive Shortest
Job First
• Consider the below processes available in the ready queue for
execution, with arrival time as 0 for all and given burst times.

Starvation
Priority Scheduling

• Non-Preemptive Priority Scheduling: If the new process with


higher priority than the currently running process, arrived at the
ready queue, the CPU will not be preempted, instead, the
incoming process is put at the head of the ready queue
Priority Scheduling (Non preemptive version)

Starvation can be
removed with “aging”
Priority Scheduling (Non preemptive version)
Pre-emptive Scheduling
• Pre-emptive Scheduling
• Processes are usually assigned with priorities.
• It is necessary to run a certain task that has a higher priority before
another task although it is running.
• Therefore, the running task is interrupted for some time and resumed
later when the priority task has finished its execution.
• This type of scheduling is used mainly when a process switches either
from running state to ready state or from waiting state to ready state.
• Some Algorithms that are based on preemptive scheduling are
⮚ Round Robin Scheduling (RR),
⮚ Shortest Remaining Time First (SRTF),
⮚ Priority Scheduling (preemptive version)
Round Robin Scheduling (Pre-emptive version)

• This algorithm is similar to FCFS scheduling, but in Round Robin(RR)


scheduling, preemption is added which enables the system to switch
between processes.
• A fixed time is allotted to each process, called a quantum, for execution.
• Once a process is executed for the given time period that process is
preempted and another process executes for the given time period.
• Context switching is used to save states of preempted processes.
• This algorithm is simple and easy to implement and the most important is is
this algorithm is starvation-free as all processes get a fair share of CPU.
Round Robin Scheduling (Pre-emptive version)
Round Robin Scheduling (RR) –
Advantages & Disadvantages
Advantages
• All the jobs get a fair allocation of CPU.
• There are no issues of starvation
• Deals with all processes without any priority.
• Cyclic in nature.
Disadvantages
• This algorithm spends more time on context switches.
• For small quantum, it is time-consuming scheduling.
• This algorithm offers a larger waiting time and response time.
• low throughput.
• If time quantum is less for scheduling then its Gantt chart seems to be too
big.
Shortest Remaining Time First (SRTF)
• On arrival of every process, the scheduler schedules those processes from the ready queue that
has the least remaining burst time.
Shortest Remaining Time First (SRTF)
• Advantages:
SRTF algorithm makes the processing of the jobs faster

• Disadvantages:
The context switch is done a lot more times which consumes CPU’s
valuable time for processing.
Process Termination

• There must be a means for a process to indicate its completion


• A batch job should include a HALT instruction or an explicit OS service
call for termination
• For an interactive application, the action of the user will indicate
when the process is completed (e.g. log off, quitting an application)
Reasons for
Process
Termination
Process Termination

• A process terminates when it finishes executing it’s


final statement and ask the OS to delete it by using
exit() system call.
• The process may return a status value (typically an
integer) to it’s parent by using wait() system call.
• All the resources of the process including physical and
virtual memory, open files, I/O buffers are deallocated
by the OS.
Zombie Process
• A process becomes a zombie process when it has completed execution
but one or some of its entries are still in the process table.
• If a process is ended by an "exit" call, the process’ entry in the process
table remains until the parent process acknowledges its execution, after
which it is removed.
• The time between the execution and the acknowledgment of the process
is the period when the process is in a zombie state(defunct process).
Threads

• Threads are also known as Lightweight processes.


• It is a basic unit of work.
• Each process can have one or more threads in it which are intended to
perform different tasks. Threads comprises of
⮚ Thread Id
⮚ Program counter
⮚ Register set
⮚ Stack
• A thread shares with other threads within the same process -
⮚ It’s code section
⮚ Data section
⮚ Open files
Threads

⮚ Threads are independent unit of execution within a process.


⮚ Execution of a thread has to be initiated by a process.
⮚ Threads are a popular way to improve the performance of an application
through parallelism.
⮚ The CPU switches rapidly back and forth among the threads giving the
illusion that the threads are running in parallel.
⮚ Each thread belongs to exactly one process and outside a process no
threads exist.
Threads

A traditional heavyweight process has a single thread. But if is has more than
one thread then it can perform more than one task at a time.
Single Threaded vs Multithreaded process
⮚ In a single thread or threading, the process contains only one thread.
⮚ That thread executes all the tasks related to the process.

⮚ Multithreading - In a multi-threaded application, multiple threads are


executed concurrently.
⮚ Each thread handles different tasks simultaneously by making optimal use
of the resources.
• The ability of an OS to support multiple, concurrent paths of execution
within a single process
◆ suspending a process involves suspending all threads of the process
◆ termination of a process terminates all threads within the process

Department of Information Technology, VIIT, Pune-48


Threads
Single Threaded Approaches
• A single execution path per
process, in which the
concept of a thread is not
recognized, is referred to
as a single-threaded
approach
• MS-DOS, some versions of
UNIX supported only this
type of process.

Department of Information Technology, VIIT, Pune-48


Multithreaded Approaches

• Most of the modern software applications in the system uses the


concept of multi threading.
• For eg. A web browser application can have multiple threads
– One thread does the task of taking input from the user.
– Second thread can send this request to the server
– Third thread can retrieve the results from the server
– Forth thread can display the images

Department of Information Technology, VIIT, Pune-48


Each thread has:

• An execution state (Running, Ready, etc.)


• saved thread context when not running (TCB)
• An execution stack
• Some per-thread static storage for local variables
• Access to the shared memory and resources of
its process (all threads of a process share this)

Any alteration of a resource by one thread affects the other threads


in the same process
Department of Information Technology, VIIT, Pune-48
Thread Execution States

Thread operations associated with a


The key states for change in thread state are:
a thread are:
■ Spawn (create)
■ Block
– Running ■ Unblock
– Ready ■ Finish
– Blocked

Department of Information Technology, VIIT, Pune-48


Thread Execution States
Process Thread
A Process simply means any program in execution. Thread simply means a segment of a process.
The process consumes more resources Thread consumes fewer resources.
Thread requires comparatively less time for
The process requires more time for creation.
creation than process.
The process is a heavyweight process Thread is known as a lightweight process
The process takes more time to terminate The thread takes less time to terminate.
Processes have independent data and code A thread mainly shares the data segment, code
segments segment, files, etc. with its peer threads.
The process takes more time for context switching. The thread takes less time for context switching.
Communication between processes needs more Communication between threads needs less
time as compared to thread. time as compared to processes.
For some reason, if a process gets blocked
In case if a user-level thread gets blocked, all of
then the remaining processes can continue
its peer threads also gets blocked.
their execution
Department of Information Technology, VIIT, Pune-48
Advantages of Threads
• Enhanced throughput of the system: When the process is split into many threads,
and each thread is treated as a job, the number of jobs done in the unit time
increases. That is why the throughput of the system also increases.
• Effective Utilization of Multiprocessor system: When you have more than one thread
in one process, OS can schedule more than one thread to more than one processor.
• Faster context switch: The context switching period between threads is less than the
process context switching. The process context switch means more overhead for the
CPU.
• Responsiveness: When the process is split into several threads, and when a thread
completes its execution, that process can be responded to as soon as possible.
• Communication: Multiple-thread communication is simple because the threads share
the same address space, while in process, we adopt just a few exclusive
communication strategies for communication between two processes.
• Resource sharing: Resources can be shared between all threads within a process,
such as code, data, and files. Note: The stack and register cannot be shared between
threads. There is a stack and register for each thread.
Department of Information Technology, VIIT, Pune-48
Advantages of Threads

Less time to Threads enhance


terminate a thread efficiency in
than a process communication
Switching between between programs
Takes less time two threads takes
to create a new less time than
thread than a switching between
process processes

Department of Information Technology, VIIT, Pune-48


Types of Threads

• Threads are implemented in following two ways −


• User Level Threads − User managed threads.
• Kernel Level Threads − Operating System managed threads acting on
kernel, an operating system core.

Department of Information Technology, VIIT, Pune-48


Types of Threads

User Level
Thread (ULT)
Kernel level
Thread (KLT)

Department of Information Technology, VIIT, Pune-48


User-Level Threads (ULTs)

• ULTs are created and managed by


using thread library, which contains
code and functions for creating and
destroying threads, for passing
messages between threads,
scheduling thread execution, context
switching etc.
• The kernel is not aware of the
existence of user threads
• Kernel is concerned about the
process only

Department of Information Technology, VIIT, Pune-48


Advantages of ULTs

ULTs
Scheduling can be can run
application specific on any
OS

Thread switching does not


require kernel mode
privileges (no mode
switches)

Department of Information Technology, VIIT, Pune-48


Disadvantages of ULTs

• In a typical OS, many system calls are blocking


▪ As a result, when a ULT executes a system call, not only that thread is
blocked, but all of the threads within the process are blocked because
blocking one thread may cause the whole process to go in suspended
state.

Department of Information Technology, VIIT, Pune-48


Overcoming ULT Disadvantages

Jacketing
• converts a blocking system call into a
non-blocking system call

Writing an application
as multiple processes
rather than multiple
threads

Department of Information Technology, VIIT, Pune-48


Kernel-Level Threads (KLTs)
⮚ Thread management is done by the
kernel
⮚ No thread management is done by the
application
⮚ Windows is an example of this
approach
⮚ In this case, thread management is
done by the Kernel. There is no
thread management code in the
application area. Kernel threads
are supported directly by the
operating system.

Department of Information Technology, VIIT, Pune-48


Kernel-Level Threads (KLTs)

The Kernel maintains context


information for the process as a whole
and for individuals threads within the
process.
Scheduling by the Kernel is done on a
thread basis.
The Kernel performs thread creation,
scheduling and management in Kernel
space.
Kernel threads are generally slower to
create and manage than the user
threads.
Department of Information Technology, VIIT, Pune-48
Advantages of KLTs

• If one thread in a process is blocked, the kernel can schedule another


thread of the same process
• Since Kernel can maintain full information about all threads, thread
management and scheduling can be done efficiently.

Department of Information Technology, VIIT, Pune-48


Disadvantage of KLTs

The transfer of control from one thread to another within the same process
requires a mode switch to the kernel

Department of Information Technology, VIIT, Pune-48


ULT vs KLT

User Level Threads Kernel Level Threads

ULTs are managed by user level KLTs are managed by OS


libraries
ULTs are typically fast KLTs are typically slower
Context switching is faster Context switching is slower

If one ULT is blocked, the entire If one KLT is blocked, it has no


process gets blocked effect on other threads and
process.

Department of Information Technology, VIIT, Pune-48


Thread Scheduling

⮚Round Robin Scheduling (RR),


⮚Shortest Remaining Time First (SRTF),
⮚Priority Scheduling (preemptive version)
⮚Shortest job First ( Non Preemptive version)
⮚Priority Scheduling(Non Preemtive version)

Department of Information Technology, VIIT, Pune-48


Thread programming Using Pthreads

• In a Unix/Linux operating system, the C/C++ languages provide the


POSIX thread(pthread) standard API(Application program Interface) for all
thread related functions.
• It allows us to create and manage the threads.
• We must include the pthread.h header file at the beginning of the script
to use all the functions of the pthreads library.
• To execute the c file, we have to use the -pthread or -lpthread in the
command line while compiling the file.

Department of Information Technology, VIIT, Pune-48


Thread programming Using Pthreads

#include <pthread.h>
pthread_create (thread, attr, start_routine, arg)

Parameter Description
thread unique identifier for the new thread returned by the subroutine.(addr of
thread variable)
attr attribute object that may be used to set thread attributes. You can specify a
thread attributes object, or NULL for the default values.
start_routine The C routine that the thread will execute once it is created.(name of the
function, thread will execute)
arg A single argument that may be passed to start_routine. It must be passed by
reference as a pointer cast of type void. NULL may be used if no argument
is to be passed.(data passed to the function)

Department of Information Technology, VIIT, Pune-48


Thread programming Using Pthreads

PThread_join
The pthread_join() function suspends the execution of
the calling thread until the target thread terminates.

PThread_exit
The pthread_exit() function terminates the calling thread

Department of Information Technology, VIIT, Pune-48


Program to create Thread Using Pthreads
Void *thread_function(void * arg)
Int i,n,j;
Int main(){
Pthread_t T1;
Pthread_create(&T1, NULL, thread_function, NULL); //main process waits for thread to finish
Pthread_join(T1, NULL); Output
Printf(“Inside main program\n);
Inside Thread
For(j=20;j<25;j++)
{
0
Printf(“%d\n”,j); 1
Sleep(1); 2
} 3
} 4
Void *thread_function(void *arg) Inside Main program
{ 20
Printf(“Inside Thread\n);
21
For(i=0;i<5;i++)
{ 22
Printf(%d\n”,i); 23
Sleep(1); 24
}
}
Department of Information Technology, VIIT, Pune-48
Program to create Thread Using Pthreads
Void *thread_function(void * arg)
Int i,n,j;
Int main(){
Pthread_t T1;
Pthread_create(&T1, NULL, thread_function, NULL); //main process waits for thread to finish
//Pthread_join(T1, NULL); Output
Printf(“Inside main program\n);
For( j=20; j<25; j++ )
{
Inside Thread
Printf( “%d\n”,j ); Inside main program
Sleep(1); 20
} 0
} 21
Void *thread_function(void *arg) 1
{ 22
Printf(“Inside Thread\n);
2
For( i=0; i<5; i++ )
{ 23
Printf( %d\n”, i ); 3
Sleep(1); 24
} 4
}
Department of Information Technology, VIIT, Pune-48
Thread Termination

The pthread_exit subroutine releases any thread-specific data, including the


thread's stack.
Any data allocated on the stack becomes invalid, because the stack is freed and
the corresponding memory may be reused by another thread. Therefore, thread
synchronization objects (mutexes and condition variables) allocated on a thread's
stack must be destroyed before the thread calls the pthread_exit subroutine.

Unlike the exit subroutine, the pthread_exit subroutine does not clean up system
resources shared among threads.
For example, files are not closed by the pthread_exit subroutine, because they
may be used by other threads.

Department of Information Technology, VIIT, Pune-48

You might also like