Pthread Assignments (Difficulty Level: Intermediate)

6 min read

Below are a set of assignments that focus on pthread mutex and pthread semaphore for synchronization in multi-threaded programs:

1. Thread Synchronization with pthread mutex

Assignment:

  • Task: Write a C program that:

  • Creates multiple threads using pthread_create().

  • Each thread increments a shared global variable 1000 times.

  • Use a pthread mutex to ensure that the shared variable is accessed by only one thread at a time.

  • After all threads finish, print the final value of the shared variable.

Objective: Understand synchronization with pthread mutex for preventing race conditions in a multi-threaded environment.

2. Thread Synchronization with pthread semaphore

Assignment:

  • Task: Write a C program that:

  • Creates multiple threads using pthread_create().

  • Each thread will wait for a semaphore before proceeding.

  • Use a pthread semaphore initialized to zero, and post it in the main thread to allow the threads to proceed one by one.

  • Ensure that threads wait until the semaphore is posted, then perform a task and print a message.

Objective: Use pthread semaphore to control the execution order of threads and coordinate access.

3. Producer-Consumer Problem using pthread mutex and semaphore

Assignment:

  • Task: Implement a Producer-Consumer problem using pthread mutex and pthread semaphore:

  • Create a shared buffer of fixed size.

  • The producer thread will produce items and place them in the buffer.

  • The consumer thread will consume items from the buffer.

  • Use a pthread mutex to synchronize access to the shared buffer.

  • Use a pthread semaphore to signal when the buffer is full or empty.

Objective: Synchronize access to a shared buffer using pthread mutex and pthread semaphore to solve the Producer-Consumer problem.

4. Reader-Writer Problem using pthread mutex and pthread semaphore

Assignment:

  • Task: Implement the Reader-Writer problem using pthread mutex and pthread semaphore:

  • Allow multiple readers to access the shared resource concurrently.

  • Ensure that writers have exclusive access to the shared resource.

  • Use pthread mutex to protect the access count.

  • Use a pthread semaphore to allow readers to proceed while managing exclusive access for writers.

Objective: Implement Reader-Writer synchronization using pthread mutex and pthread semaphore to control concurrent access to a shared resource.

5. Thread Coordination using pthread mutex and semaphore

Assignment:

  • Task: Write a C program that:

  • Creates 3 threads: one for each task (Task 1, Task 2, Task 3).

  • Task 1 must execute first, followed by Task 2, then Task 3.

  • Use pthread semaphore to ensure that Task 2 waits for Task 1 to finish, and Task 3 waits for Task 2 to finish.

  • Use a pthread mutex to ensure that only one thread accesses a shared variable at a time (e.g., incrementing a counter).

Objective: Use pthread semaphore for coordinating thread execution order and pthread mutex for thread synchronization in accessing shared resources.

6. Counting Semaphore for Thread Synchronization

Assignment:

  • Task: Write a program where:

  • You create multiple threads, each doing a task such as printing a message.

  • Each thread should wait for a semaphore signal before printing.

  • Initialize the semaphore count to 0.

  • The main thread posts the semaphore multiple times to signal the threads to print their message.

  • After printing, each thread should decrement the semaphore count.

Objective: Use a counting pthread semaphore to control the execution of threads in a multi-threaded environment.

7. Mutex Lock for File Access in Multi-threading

Assignment:

  • Task: Write a C program that:

  • Creates multiple threads.

  • Each thread will write some data to the same file.

  • Use a pthread mutex to ensure that only one thread can write to the file at a time.

  • After all threads complete their tasks, ensure the file is properly closed and data is correctly written without corruption.

Objective: Implement pthread mutex to ensure proper synchronization when multiple threads access and modify the same file.

8. Barrier Synchronization Using pthread mutex and semaphore

Assignment:

  • Task: Write a program that:

  • Creates N threads (e.g., 5 threads).

  • Each thread performs a task, but they all need to synchronize at a barrier point.

  • Use a pthread mutex to protect the barrier, ensuring that all threads reach the barrier point before any can proceed.

  • Use a pthread semaphore to signal when all threads have reached the barrier and can proceed.

Objective: Implement barrier synchronization using pthread mutex and pthread semaphore to synchronize threads at a specific point in execution.

9. Task Scheduling with pthread mutex and semaphore

Assignment:

  • Task: Write a program with 3 threads, each performing a task:

  • Task 1: Print "Task 1 completed".

  • Task 2: Print "Task 2 completed" after Task 1.

  • Task 3: Print "Task 3 completed" after Task 2.

  • Use pthread semaphore to synchronize the order of tasks, ensuring Task 2 only runs after Task 1, and Task 3 only runs after Task 2.

  • Use a pthread mutex to ensure no two tasks print at the same time.

Objective: Use pthread semaphore to coordinate the order of task execution and pthread mutex for synchronizing printing in a multi-threaded environment.

10. Thread Synchronization with Multiple Mutex Locks

Assignment:

  • Task: Write a program that:

  • Creates multiple threads that each access two shared variables.

  • Use two pthread mutexes to protect access to the two shared variables.

  • Each thread will lock both mutexes in a specific order, perform calculations on the shared variables, and then unlock the mutexes.

  • Ensure no deadlocks occur by always locking the mutexes in the same order across threads.

Objective: Learn to use multiple pthread mutexes to protect shared variables and ensure safe access while avoiding deadlocks.

Conclusion:

These assignments cover a wide range of scenarios using pthread mutex and pthread semaphore to achieve thread synchronization in various multi-threaded programs. These tasks will help you understand how to control thread execution and manage concurrent access to shared resources effectively.

Feel free to ask for any clarifications or further explanations!

Enjoyed this article?

Subscribe to get the latest deep dives on Linux and Security delivered to your inbox.

Subscribe to Newsletter

Get the latest articles on Linux, cryptography, and security delivered to your inbox.

No spam, unsubscribe anytime.

Comments Coming Soon

We are building a privacy-focused, secure commenting system. Stay tuned!