Parallel Programming in C
Last Updated :
05 Jul, 2024
Parallel programming is a technique that allows multiple computations to be performed simultaneously, taking advantage of multi-core processors and distributed computing systems. Parallel programming can improve the system's performance by dividing the bigger task into smaller chunks and executing them parallelly. In this article, we will learn how we can implement parallel programming in C.
Parallelism in C
Before you learn to implement parallel programming in C make sure you are familiar with the following key concepts related to parallel programming in C:
- Concurrency: It is defined as managing multiple tasks that start, run, and complete in overlapping time periods.
- Parallelism: Running multiple tasks at the same time within the system to improve the overall performance.
- Process: The program under execution is known as a process. Every program within the system is independent of each other and is executed in isolation within its own memory space.
- Threads: Also known as lightweight processes, threads are defined as the smallest unit of processing that can be executed independently.
There are two methods through which parallel programming can be implemented in C:
- Using POSIX (Pthreads)
- Using OpenMP Library
Parallel Programming in C with POSIX
POSIX threads also known as pthreads is a standard interface used for creating threads in C. It provides a set of API's through which we can easily manage the threads and achieve parallel programming. To use POSIX threads in your program include the <pthreads.h> header file in your program and follow the below syntax:
Syntax of POSIX
pthread_t thread_id;
// Create thread
pthread_create(&thread_id, NULL, thread_function, NULL);
// Wait for threads to complete
pthread_join(thread_id, NULL);
// Terminate Thread
pthread_exit(NULL);
Here,
- pthread_t: It is used to declare thread Id variables.
- pthread_create: This method is used to create a thread
- pthread_join: This method is used to join two threads so that they can wait until each of them completes their execution.
- pthread_exit: This method is used to terminate a thread.
C Program to Implement Parallel Programming Using POSIX
The following program illustrates how we can implement parallel programming in C using POSIX.
C
// C Program to Implement Parallel Programming using POSIX
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
// Define the number of threads to be created
#define NUM_THREADS 4
// Function to be executed by each thread
void* perform_work(void* arg)
{
int index = *((int*)arg);
printf("Thread %d: starting work...\n", index);
// Perform some work here
printf("Thread %d: work done.\n", index);
// Return NULL to indicate the thread has finished
// execution
return NULL;
}
int main()
{
// Array to hold thread identifiers
pthread_t threads[NUM_THREADS];
// Array to hold arguments for each thread
int thread_args[NUM_THREADS];
// Variable to hold return code from pthread functions
int rc;
for (int i = 0; i < NUM_THREADS; ++i) {
// Set the thread argument to the current index
thread_args[i] = i;
// Create a new thread
rc = pthread_create(&threads[i], NULL, perform_work,
(void*)&thread_args[i]);
// Check if thread creation was successful
if (rc) {
// Print error message if thread creation failed
printf("Error: unable to create thread, %d\n",
rc);
exit(-1);
}
}
// Loop to join the threads
for (int i = 0; i < NUM_THREADS; ++i) {
// Wait for each thread to complete its execution
pthread_join(threads[i], NULL);
}
return 0;
}
Output
Thread 1: starting work...
Thread 1: work done.
Thread 0: starting work...
Thread 0: work done.
Thread 2: starting work...
Thread 2: work done.
Thread 3: starting work...
Thread 3: work done.
Explanation: In the above example, we have a defined a parallel program using POSIX threads (pthreads). It creates 4 threads, each executing the perform_work function. The main thread creates these worker threads, waits for them to complete using pthread_join, and then exits. Each worker thread prints a start and end message, demonstrating concurrent execution within the system.
Parallel Programming in C with OpenMP Library
OpenMp short for Open Multi-Processing is a popular library that provides a set of API's that supports multi-platform shared memory processing in C and C++. It used the compiler directives to achieve parallelism in the programs. To use the openMp library include the <omp.h> header file in your code and follow the below syntax.
Syntax of OpenMP
#pragma omp parallel
{
// Code inside this block will execute in parallel
}
C Program to Implement Parallel Programming Using OpenMP Library
The following program illustrates how we can implement parallel programming in C using openMP library.
C
// C Program to Implement Parallel Programming using OpenMP
// Library
#include <omp.h>
#include <stdio.h>
int main()
{
// Define the number of threads to be used
int num_threads = 4;
// Set the number of threads to use in parallel regions
omp_set_num_threads(num_threads);
// Parallel region begins
#pragma omp parallel
{
// Get the ID of the current thread
int thread_id = omp_get_thread_num();
printf("Hello from thread %d\n", thread_id);
}
return 0;
}
Output
Hello from thread 2
Hello from thread 0
Hello from thread 3
Hello from thread 1
Explanation: The above example demonstrates parallel programming using OpenMP library in C. It sets the number of threads to 4 and creates a parallel region using the #pragma omp parallel directive. Within this region, each thread prints a "Hello" message along with its unique thread ID, demonstrating simple parallel execution with OpenMP.
Similar Reads
Pattern Programs in C
Printing patterns using C programs has always been an interesting problem domain. We can print different patterns like star patterns, pyramid patterns, Floyd's triangle, Pascal's triangle, etc. in C language. These problems require the knowledge of loops and if-else statements.We will discuss the fo
15+ min read
String C/C++ Programs
C program to swap two StringsC Program to Sort an array of names or stringsC Program to Check if a Given String is PalindromeC/C++ Program for Return maximum occurring character in the input stringC/C++ Program for Remove all duplicates from the input string.C/C++ Program for Print all the duplicate
3 min read
C/C++ Programs
sArray C/C++ ProgramsC Program to find sum of elements in a given arrayC program to find largest element in an arrayRecursive C program to linearly search an element in a given arrayC program to multiply two matricesC/C++ Program for Given an array A[] and a number x, check for pair in A[] with sum
15+ min read
C Program to Implement Queue using Array
A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle which means the elements added first in a queue will be removed first from the queue. In this article, we will learn how to implement a Queue using Array in C. Implement Queue using Array in CTo implement Queue u
6 min read
Producer Consumer Problem in C
Concurrency is an important topic in concurrent programming since it allows us to completely understand how the systems work. Among the several challenges faced by practitioners working with these systems, there is a major synchronization issue which is the producer-consumer problem. In this article
5 min read
Array C/C++ Programs
C Program to find sum of elements in a given arrayC program to find largest element in an arrayC program to multiply two matricesC/C++ Program for Given an array A[] and a number x, check for pair in A[] with sum as xC/C++ Program for Majority ElementC/C++ Program for Find the Number Occurring Odd N
6 min read
C Programs
To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and
8 min read
C Program to Print Right Half Pyramid Pattern
The Right Half Pyramid Pattern is a triangular pattern consists of rows where each row contains an increasing number of characters. The number of characters starts from 1 and increases by 1 in each subsequent row. Characters are aligned to the left, resembling a right-angle triangle with its hypoten
2 min read
C Program to Traverse an Array
Write a C program to traverse the given array that contains N number of elements.ExamplesInput: arr[] = {2, -1, 5, 6, 0, -3} Output: 2 -1 5 6 0 -3Input: arr[] = {4, 0, -2, -9, -7, 1} Output: 4 0 -2 -9 -7 1Different Ways to Traverse an Array in CArrays are versatile data structures and C language pro
3 min read
Matrix Multiplication in C
A matrix is a collection of numbers organized in rows and columns, represented by a two-dimensional array in C. Matrices can either be square or rectangular. In this article, we will learn the multiplication of two matrices in the C programming language. ExampleInput: mat1[][] = {{1, 2}, {3, 4}} mat
3 min read