Open In App

Parallel Programming in C

Last Updated : 05 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article

Similar Reads