Completion of POSIX Streams
To end a thread, just go to the end of its thread function thread
thread _ fun () or call pthread_exit () from it:
#include
void pthread_exit (void * retval);
The retval parameter specifies the return code. Here is an example of
ending a stream:
void * turead _ fun ( void * num )
{
printf (" stream number % d \ n", (int) num);
pthread _ exit (0);
}
There are two different situations for the application startup code:
❖
Executing the return statement or executing all statements by the
end of main () completes the whole process with all threads also
completing their execution
❖
Executing the pthread _ exit () function inside the main () function
completes only the initial thread, this action does not affect other
threads in the program - they will continue executing until they are
completed
Calling the pthread _ exit () function is used only to join threads because it
does not release the flow resources - this is the responsibility of the
threaded joiner.
Waiting For Completion Of Threads
To await completion of threads, use the pthread _ join () function. It has the
following syntax:
int pthread_join (pthread_t the ,, void ** status);
This function blocks the thread where it was called until the thread
specified by the handle deserves its execution. If the status is not a null
pointer, it will point to the data returned by the stream after the call
(argument pthread _ exit ()). If the thread is already complete by the time
the pthread _ join () function is called, its information should be read
immediately.
Conclusions
❖
Processes and threads are the active resources of computer systems
that implement code execution. The thread is called a set of
sequentially executed processor commands. A process is a collection
of one or more threads and the protected address space in which they
are executed. Streams in the same address space can share data
together, and for streams of different processes without special means,
this is not possible. In traditional systems, each process can execute
only one thread code execution of processes. Modern operating
systems support the concept of multithreading.
❖
Using threads in an application means paralleling it - the ability to
perform actions simultaneously with different snippets of
code. Program parallelism reflects the asynchronous nature of the
outside world, its sources being code execution on multiple
processors, I / O operations, user interaction, and client application
requests. Bahatopotokovist allows applications to implement this
course and achieve parallelism and efficiency.
❖
There are user threads running in the slugger mode in the process
address space, and the kernel threads with which the OS kernel
works. The relationship between them determines the scheme of
implementation of the flow model. In practice, they often use the 1: 1
scheme, when each thread of the user corresponds to one thread of the
kernel, and the kernel itself is responsible for managing the threads of
the user.
❖
The flow can be in different states (execution, expectations,
readiness, etc.). The principles of moving from one state to another
depending on the principles of flow planning and processor time
planning. Moving the process from execution state to any other state
is about switching the context - transferring control from one thread to
another while preserving the state of the processor.
❖
Each process and flow in the system corresponds to its control unit
- a data structure containing all the necessary information. During the
creation process (usually by the system call fork ()) create one control
unit, allocate memory and launch the mainstream. Creating flow is
faster and easier because they do not need to allocate memory to the
new address.
|