POSIX Threads in OS :
The POSIX thread libraries are a C/C++ thread API based on standards. It enables the creation of a new concurrent process flow. It works well on multi-processor or multi-core systems, where the process flow may be scheduled to execute on another processor, increasing speed through parallel or distributed processing. Because the system does not create a new system, virtual memory space and environment for the process, threads needless overhead than "forking" or creating a new process. While multiprocessor systems are the most effective, benefits can also be obtained on uniprocessor systems that leverage delay in I/O and other system processes that may impede process execution.
To utilise the PThread interfaces, we must include the header pthread.h at the start of the CPP script.
#include <pthread.h>
PThreads is a highly concrete multithreading system that is the UNIX system's default standard. PThreads is an abbreviation for POSIX threads, and POSIX is an abbreviation for Portable Operating System Interface, which is a type of interface that the operating system must implement. PThreads in POSIX outline the threading APIs that the operating system must provide.
Why is Pthreads used?
- The fundamental purpose for adopting Pthreads is to improve programme performance.
- When compared to the expense of starting and administering a process, a thread requires far less operating system overhead. Thread management takes fewer system resources than process management.
- A process's threads all share the same address space. Inter-thread communication is more efficient and, in many circumstances, more user-friendly than inter-process communication.
- Threaded applications provide possible performance increases and practical advantages over non-threaded programmes in a variety of ways.
- Multi-threaded programmes will run on a single-processor system but will automatically make use of a multiprocessor machine without the need for recompilation.
- The most significant reason for employing Pthreads in a multiprocessor system is to take advantage of possible parallelism. This will be the major emphasis of the rest of this lesson.
- In order for a programme to use Pthreads, it must be divided into discrete, independent tasks that may run concurrently.
The new thread is made runnable, and it will begin performing the start routine using the arg argument as the argument. The arg parameter is a void pointer that can point to any type of data. Casting this pointer into a scalar data type (such as int) is not advised since the casts may not be portable.
Let's have a look at a C example of a better implementation approach :
C
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function( void *ptr );
main()
{
pGeeksforGeeks_t GeeksforGeeks1, GeeksforGeeks2;
char *message1 = "GeeksforGeeks 1";
char *message2 = "GeeksforGeeks 2";
int geeky1, geeky2;
geeky1 = pthread_create( &GeeksforGeeks1, NULL, print_message_function, (void*) message1);
geeky2 = pthread_create( &GeeksforGeeks2, NULL, print_message_function, (void*) message2);
pthread_join( GeeksforGeeks1, NULL);
pthread_join( GeeksforGeeks2, NULL);
printf("GeeksforGeeks 1 returns: %d\n",geeky1);
printf("GeeksforGeeks 2 returns: %d\n",geeky2);
exit(0);
}
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
}
This programme spawns five threads, each of which runs the perform work function, which publishes the thread's unique number to standard output. If a programmer wanted the threads to interact with each other, he or she would have to create a global variable that was defined outside of the scope of any of the functions. The following command may be used to compile this programme with the gcc compiler.
gcc pthreads_demo.c -lpthread -o pthreads_demo
Because the pthreads standard is not supported natively on Windows, the Pthreads-w32 project aims to create a portable and open-source wrapper implementation. It may also be used to transfer Unix applications (that utilizes pthreads) to Windows with little to no changes to the platform. The latest version 2.8.0 is compatible with 64-bit Windows computers after some extra updates.
Winpthreads, a wrapper implementation of pthreads in the mingw-w64 project, seeks to use more native system calls than the Pthreads-w32 project.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Operating System Tutorial An Operating System(OS) is a software that manages and handles hardware and software resources of a computing device. Responsible for managing and controlling all the activities and sharing of computer resources among different running applications.A low-level Software that includes all the basic fu
4 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Types of Operating Systems Operating Systems can be categorized according to different criteria like whether an operating system is for mobile devices (examples Android and iOS) or desktop (examples Windows and Linux). Here, we are going to classify based on functionalities an operating system provides.8 Main Operating System
11 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read