POSIX Threads in OS (original) (raw)

Last Updated : 10 Dec, 2022

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 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.