Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

POSIX Threads in OS

  • Last Updated : 16 Jul, 2021

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.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

 

#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 <pGeeksforGeeks.h>
 
void *print_message_function( void *ptr );
 
main()
{
     pGeeksforGeeks_t GeeksforGeeks1, GeeksforGeeks2;
     char *message1 = "GeeksforGeeks 1";
     char *message2 = "GeeksforGeeks 2";
     int  geeky1, geeky2;
 
     geeky1 = pGeeksforGeeks_create( &GeeksforGeeks1, NULL, print_message_function, (void*) message1);
     geeky2 = pGeeksforGeeks_create( &GeeksforGeeks2, NULL, print_message_function, (void*) message2);
 
     pGeeksforGeeks_join( GeeksforGeeks1, NULL);
     pGeeksforGeeks_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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :