#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
// Function to be executed by each thread
void* thread_function(void* arg) {
int thread_num = *((int*)arg); // Retrieve the thread number
printf(“Hello from thread %d!\n”, thread_num);
return NULL;
}
int main() {
pthread_t threads[5]; // Array to hold thread IDs
int thread_args[5]; // Array to hold thread arguments (thread numbers)
// Creating 5 threads
for (int i = 0; i < 5; i++) {
thread_args[i] = i + 1; // Assigning thread number starting from 1
if (pthread_create(&threads[i], NULL, thread_function, &thread_args[i]) != 0) {
perror(“Failed to create thread”);
exit(1);
}
}
// Wait for all threads to finish execution
for (int i = 0; i < 5; i++) {
if (pthread_join(threads[i], NULL) != 0) {
perror(“Failed to join thread”);
exit(1);
}
}
printf(“All threads have finished executing.\n”);
return 0;
}