Critical section problem:
Code 1:
#include <stdio.h> // Include the standard input/output library for
printf function
#include <pthread.h> // Include the pthread library for using threads
and mutexes
pthread_mutex_t mutex; // Declare a mutex variable for synchronizing
access to shared resource
int shared_resource = 0; // Initialize a shared resource that will be
accessed by multiple threads
// Define the function to be executed by threads
void* thread_function(void* arg) {
// Lock the mutex before entering the critical section
pthread_mutex_lock(&mutex); // Lock the mutex to ensure exclusive
access to shared resource
// Print which thread is currently executing the critical section
printf("Thread %ld is in critical section.\n", arg);
shared_resource++; // Increment the shared resource
// Print the current value of the shared resource
printf("Shared resource value: %d\n", shared_resource);
// Unlock the mutex after leaving the critical section
pthread_mutex_unlock(&mutex); // Unlock the mutex to allow other
threads to access the shared resource
return NULL; // Return NULL as this thread function does not need to
return a value
//main function (entry point of the program)
int main() {
pthread_t threads[2]; // Declare an array to hold thread identifiers
pthread_mutex_init(&mutex, NULL); // Initialize the mutex
// Create two threads, passing their index as the argument to
thread_function
for (long i = 0; i < 2; i++) {
pthread_create(&threads[i], NULL, thread_function, (void*)i);
// Wait for both threads to complete execution
for (int i = 0; i < 2; i++) {
pthread_join(threads[i], NULL);
}.
pthread_mutex_destroy(&mutex); // Destroy the mutex to clean up
resources
return 0; // Return 0 to indicate successful completion of the program
Output:
Thread 0 is in critical section.
Shared resource value: 1
Thread 1 is in critical section.
Shared resource value: 2
Or
Thread 1 is in critical section.
Shared resource value: 1
Thread 0 is in critical section.
Shared resource value: 2
Code 2:
#include <stdio.h> // Include the standard I/O library for printf
#include <pthread.h> // Include the pthread library for thread
functions and mutexes
int shared_resource = 0; // Define a global variable that will be shared
among threads
pthread_mutex_t mutex; // Declare a mutex to protect access to the
shared resource
// Thread function that increments the shared_resource 100 times
void* thread_function(void* arg) {
// Lock the mutex before entering the critical section
for (int i = 0; i < 100; ++i) { // Loop 100 times
pthread_mutex_lock(&mutex); // Lock the mutex before
accessing shared_resource
shared_resource++; // Increment the shared_resourcep
// Unlock the mutex after exiting the critical section
pthread_mutex_unlock(&mutex); // Unlock the mutex after accessing
shared_resource
return NULL; // Return NULL when the thread function
completes
int main() {
pthread_t thread1, thread2; // Declare two thread identifiers
pthread_mutex_init(&mutex, NULL); // Initialize the mutex with
default attributes
pthread_create(&thread1, NULL, thread_function, NULL); // Create the
first thread to run thread_function
pthread_create(&thread2, NULL, thread_function, NULL); // Create the
second thread to run thread_function
pthread_join(thread1, NULL); // Wait for the first thread to finish
pthread_join(thread2, NULL); // Wait for the second thread to
finish
pthread_mutex_destroy(&mutex); // Destroy the mutex to clean
up resources
printf("Final value of shared_resource: %d\n", shared_resource); // Print
the final value of shared_resource
return 0; // Return 0 to indicate successful completion
Output:
Final value of shared_resource: 200