Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Parallel programming is a technique that allows multiple processes to run simultaneously, significantly improving the performance and efficiency of applications, especially on multi-core systems. In the Linux environment, parallel programming is highly applicable and can be achieved using various tools and libraries such as Pthreads, OpenMP, and MPI. This article will guide you through the basics of parallel programming in Linux, demonstrating how to create and run parallel programs using these tools.
Examples:
Pthreads (POSIX threads) is a POSIX standard for threads, providing a set of C programming language types, functions, and constants. Here is a simple example of a parallel program using Pthreads.
Install the necessary development tools:
sudo apt-get install build-essential
Create a C program using Pthreads:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5
void *print_hello(void *threadid) {
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++) {
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, print_hello, (void *)t);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
Compile the program:
gcc -o pthread_example pthread_example.c -lpthread
Run the program:
./pthread_example
OpenMP (Open Multi-Processing) is an API that supports multi-platform shared memory multiprocessing programming in C, C++, and Fortran. Here is a simple example using OpenMP.
Install the necessary development tools:
sudo apt-get install build-essential
Create a C program using OpenMP:
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
#pragma omp parallel
{
int ID = omp_get_thread_num();
printf("Hello(%d) ", ID);
printf("World(%d)\n", ID);
}
return 0;
}
Compile the program:
gcc -o openmp_example -fopenmp openmp_example.c
Run the program:
./openmp_example
MPI (Message Passing Interface) is a standardized and portable message-passing system designed to function on parallel computing architectures. Here is a simple example using MPI.
Install the necessary development tools:
sudo apt-get install mpich
Create a C program using MPI:
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
MPI_Init(NULL, NULL);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
printf("Hello world from processor %d of %d\n", world_rank, world_size);
MPI_Finalize();
return 0;
}
Compile the program:
mpicc -o mpi_example mpi_example.c
Run the program:
mpirun -np 4 ./mpi_example