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
Thread in Operating System A thread is a single sequence stream within a process. Threads are also called lightweight processes as they possess some of the properties of processes. Each thread belongs to exactly one process.In an operating system that supports multithreading, the process can consist of many threads. But threa
7 min read
Thread Models in Operating System A thread is a light weight process which is similar to a process where every process can have one or more threads. Each thread contains a Stack and a Thread Control Block. There are four basic thread models : 1. User Level Single Thread Model : Each process contains a single thread.Single process is
2 min read
Threading Issues Multitasking is one of the key methods, which can be applied to improve the performance of operations. In a multithreading environment, there are many threading-related problems. We shall discuss in this article threading problems associated with system calls, cancellation of threads, signal handlin
11 min read
Threads in Distributed Systems Threads are essential components in distributed systems, enabling multiple tasks to run concurrently within the same program. This article explores threads' role in enhancing distributed systems' efficiency and performance. It covers how threads work, benefits, and challenges, such as synchronizatio
11 min read
Thread States in Operating Systems When a thread moves through the system, it is always in one of the five states: (1) Ready (2) Running (3) Waiting (4) Delayed (5) Blocked Excluding CREATION and FINISHED state. When an application is to be processed, then it creates a thread. It is then allocated the required resources(such as a net
2 min read