- This solution demonstrates thread synchronization using mutexes within shared memory, graceful signal handling for SIGINT, and automated control through a bash script.
This application spawns two threads to share data through a shared memory segment. Processes the SIGINT signal to terminate the application cleanly.
- Use
shm_open
to create a shared memory object, andftruncate
to set the memory size. - Use
mmap
to map shared memory. - Implement inter-thread synchronization using
pthread_mutex
.
- The two threads take turns increasing the counter value in shared memory.
- Use
pthread_mutex_lock
andpthread_mutex_unlock
to maintain data consistency.
- Use the
signal
function to handle the SIGINT signal. - When the SIGINT signal is received, the shared memory is freed, the mutex is destroyed, and the application is terminated.
- The script runs the application in the background and sends a SIGINT signal after a specified amount of time.
- Check whether the application terminates cleanly through the SIGINT signal.
-
Ensure you have GCC installed on your Linux distribution.
-
Compile the C application using
make
. -
Use the bash script to launch the application:
./launch_app.sh <duration>
.chmod +x launch_app.sh
make
./main &
ctrl + c
./launch_app.sh &
- The environment is a Linux system with support for gcc, pthread, and standard POSIX libraries.
- The application and the script are executed with sufficient permissions, especially for allocating shared memory.
- The duration provided to the bash script is a positive integer.