Synchronize Multithreaded Printing with Mutex

CCBeginner
Practice Now

Introduction

In this project, you will learn how to use mutex to synchronize the printing of strings in a multi-threaded environment. You will modify an existing "Chaotic Typewriter" program to ensure that the strings are printed in the correct sequence without any intermixing of letters.

👀 Preview

$ gcc printer.c -o printer -lpthread
$ ./printer
helloworld

ðŸŽŊ Tasks

In this project, you will learn:

  • How to initialize a mutex in the init() function
  • How to lock and unlock the mutex in the printer() function to ensure exclusive access to the printing process
  • How to compile and run the modified program to observe the correct output

🏆 Achievements

After completing this project, you will be able to:

  • Understand the importance of mutex in synchronizing access to shared resources in a multi-threaded program
  • Apply mutex locking and unlocking techniques to coordinate the execution of multiple threads
  • Troubleshoot and fix issues in a multi-threaded program to achieve the desired output

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/ControlFlowGroup -.-> c/while_loop("`While Loop`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-301495{{"`Synchronize Multithreaded Printing with Mutex`"}} c/variables -.-> lab-301495{{"`Synchronize Multithreaded Printing with Mutex`"}} c/data_types -.-> lab-301495{{"`Synchronize Multithreaded Printing with Mutex`"}} c/if_else -.-> lab-301495{{"`Synchronize Multithreaded Printing with Mutex`"}} c/while_loop -.-> lab-301495{{"`Synchronize Multithreaded Printing with Mutex`"}} c/pointers -.-> lab-301495{{"`Synchronize Multithreaded Printing with Mutex`"}} c/function_parameters -.-> lab-301495{{"`Synchronize Multithreaded Printing with Mutex`"}} c/function_declaration -.-> lab-301495{{"`Synchronize Multithreaded Printing with Mutex`"}} end

Initialize the Mutex

In this step, you will learn how to initialize the mutex in the init() function. Follow the steps below to complete this step:

  1. Open the printer.c file located in the /home/labex/project/ directory.
  2. In the code already given for the function printer, the mutex is locked (pthread_mutex_lock(&mutex)) at the start to ensure exclusive access to the printing process. Once the string is fully printed, the mutex is unlocked (pthread_mutex_unlock(&mutex)), allowing the other thread to print.
  3. Locate the init() function and add the following code to initialize the mutex:
void init()
{
    pthread_mutex_init(&mutex, NULL);
}

This initializes the mutex variable using the pthread_mutex_init() function, which is part of the POSIX Threads (pthreads) library. The second argument NULL specifies that we are using the default mutex attributes.

Compile and Run the Program

Now that you have completed the modifications to the printer.c file, you can compile and run the program.

  1. Open a terminal and navigate to the /home/labex/project/ directory.
  2. Compile the program with the -lpthread flag to link the pthreads library:
gcc printer.c -o printer -lpthread
  1. Run the program:
./printer

You should now see the output:

helloworld

The strings "hello" and "world" are printed in sequence without any intermixing of the letters, as expected.

Congratulations! You have successfully modified the "Chaotic Typewriter" program to work as intended.

âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other C Tutorials you may like