Assignments on IPC, Synchronisation and Linux system calls (Difficulty Level: Intermediate+)

6 min read

These assignments will help you understand different IPC methods and how they can be synchronized.

1. Process Management with Synchronization

Assignment:

  • Task: Write a C program that:

  • Creates a parent process and two child processes using fork().

  • The first child will compute a value (e.g., a sum or a factorial) and store it in a shared memory region.

  • The second child will read the computed value from the shared memory region.

  • Use a semaphore or mutex to synchronize access to the shared memory, ensuring the second child only reads after the first child has written.

  • Print the result of the computation after the second child has read it.

Objective: Understand synchronization between multiple processes, memory sharing, and mutual exclusion.

2. File Operations with Synchronization

Assignment:

  • Task: Write a C program that:

  • Creates two child processes using fork().

  • The first child writes some data to a file (e.g., appends data to a log file).

  • The second child reads the data from the file and prints it.

  • Use a mutex to ensure only one process writes or reads from the file at any time.

  • Ensure the file is properly closed and the synchronization prevents data races.

Objective: Learn to synchronize file operations in a multi-process environment.

3. Memory Management with Synchronization

Assignment:

  • Task: Write a C program that:

  • Allocates shared memory using shmget() and shmat() (shared memory) for two child processes.

  • The first child will write some data to the shared memory.

  • The second child will read the data from shared memory.

  • Use a semaphore or mutex to synchronize access to the shared memory region, preventing concurrent read/write issues.

  • Clean up the shared memory after both processes are done.

Objective: Understand memory sharing between processes and proper synchronization to prevent race conditions.

4. Signal Handling with Synchronization

Assignment:

  • Task: Write a C program that:

  • Creates a child process.

  • The parent process will send a signal (e.g., SIGUSR1) to the child using kill().

  • The child process will handle the signal using a custom signal handler (signal() or sigaction()).

  • Use a mutex to synchronize the signal handling with the execution flow of the child process, ensuring no race conditions.

  • Print messages indicating when the signal is received and handled.

Objective: Combine signal handling with synchronization to manage process flow in a controlled manner.

5. Pipes with Synchronization

Assignment:

  • Task: Write a C program that:

  • Creates a pipe using pipe().

  • Fork two child processes: the first will write data into the pipe, and the second will read from it.

  • Use a mutex or semaphore to ensure that both processes do not simultaneously access the pipe.

  • The second child reads the message and prints it after the first child finishes writing.

Objective: Use pipes for inter-process communication and synchronize the read/write operations.

6. Message Queues with Synchronization

Assignment:

  • Task: Write a C program that:

  • Creates a message queue using msgget().

  • The first child sends a message to the queue using msgsnd().

  • The second child receives the message from the queue using msgrcv().

  • Use a semaphore to synchronize the read and write operations to the queue.

  • Print the message received by the second child.

Objective: Learn how to use message queues for communication between processes and ensure synchronization.

7. Shared Memory with Synchronization

Assignment:

  • Task: Write a C program that:

  • Allocates a shared memory region using shmget() and attaches it using shmat().

  • The first child writes some data to the shared memory.

  • The second child reads from the shared memory.

  • Use a mutex or semaphore to synchronize the shared memory access, ensuring only one process accesses it at a time.

  • Print the data after both processes are done.

Objective: Understand how shared memory works and synchronize its access.

8. Sockets with Synchronization

Assignment:

  • Task: Write a C program that:

  • Implements a server and a client using sockets.

  • The server listens for incoming connections, and the client sends a message.

  • Use mutexes or semaphores to synchronize the message transfer between server and client, ensuring message integrity.

  • The server responds with a confirmation message.

  • Both the server and the client properly close their sockets after communication is complete.

Objective: Implement socket-based communication and synchronize access to ensure data integrity.

9. Semaphores with Synchronization

Assignment:

  • Task: Write a C program that:

  • Creates a named semaphore using sem_open() for synchronizing two processes.

  • The first process writes to a shared file or memory region.

  • The second process reads the data after the first process has completed.

  • Ensure that the second process waits for the semaphore to be released by the first process before it proceeds.

  • Clean up the semaphore using sem_close() and sem_unlink().

Objective: Learn how to use semaphores for synchronizing access to shared resources.

10. File System Management with Synchronization

Assignment:

  • Task: Write a C program that:

  • Creates a file and writes data to it using open() and write().

  • Creates two child processes using fork().

  • The first child appends data to the file, and the second child reads the data.

  • Use a semaphore to synchronize the file access so that both children do not try to access the file simultaneously, preventing data corruption.

  • The program should print the contents of the file after both operations are complete.

Objective: Learn to synchronize file system operations between multiple processes using semaphores.

Bonus Challenge: Advanced Multi-Process Synchronization Using IPC

Assignment:

  • Task: Write a C program that:

  • Creates multiple child processes (e.g., 5 processes) using fork().

  • Each child performs a portion of a complex task (e.g., calculating different parts of a matrix multiplication).

  • Use shared memory for each child to communicate results to the parent.

  • Use semaphores to ensure that only one child updates the shared memory at a time.

  • The parent process aggregates the results and prints the final output.

Objective: Use multiple IPC mechanisms (shared memory, semaphores) and synchronization techniques to coordinate several processes.

Conclusion:

This list includes all major IPC mechanisms, each with a specific focus on synchronization using mutexes or semaphores where appropriate. This will allow you to learn about different IPC methods and apply them with synchronization techniques to avoid race conditions and ensure correct process coordination.

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!