How to multi-process exclusive control in C language

Overview

Memo when multi-process exclusion is performed when the executable file is different in C language

code

GitHub

Process flow

  1. Create a shared memory key
  2. Create shared memory
  3. Create a mutex object in shared memory
  4. Lock / unlock with mutex on shared memory

1. Create a shared memory key

If you don't care too much, the key used in shmget was IPC_PRIVATE, but in multi-process, shared memory is created for each. In that state, fork after creating mutex works well, but if the executable file is different, it cannot be excluded well.

Therefore, by specifying the key with shmget, the same shared memory can be used for another executable file.

ftok will create a key with an existing file path and a proj_id greater than or equal to 0.

2. Create shared memory

Use the created key to shmget.

If IPC_CREAT | IPC_EXCL is specified, the shared memory for key will be created if it does not exist, and an error will occur if it already exists. This makes it possible to determine whether or not to perform the subsequent initialization-related processing.

3. Create a mutex object in shared memory

Create a mutex object as usual.

4. Lock / unlock with mutex on shared memory

Lock / unlock where needed, as usual.

Summary

--If the executable file is different, you must specify the shared memory key. --If you have only one executable file, you can just use pthread_mutexattr_setpshared. --IPC_PRIVATE can be used.

Concern

--Confirmation of probability of ftok collision

Recommended Posts

How to multi-process exclusive control in C language
How to wrap C in Python
How to use Google Test in C
How to use the C library in Python
How to generate permutations in Python and C ++
Try to make a Python module in C language
Go language to see and remember Part 7 C language in GO language
Machine language embedding in C language
How to develop in Python
How to display the modification date of a file in C language up to nanoseconds
How to limit the API to be published in the C language shared library of Linux
[Go language] How to get terminal input in real time
How to temporarily implement a progress bar in a scripting language
How to create and use static / dynamic libraries in C
I tried to illustrate the time and time in C language
Try HeloWorld in your own language (with How to & code)
[Python] How to do PCA in Python
How to handle session in SQLAlchemy
Multi-instance module test in C language
How to use classes in Theano
How to write soberly in pandas
How to collect images in Python
Realize interface class in C language
How to update Spyder in Anaconda
Introduction to Protobuf-c (C language ⇔ Python)
How to convert 0.5 to 1056964608 in one shot
Post to slack in Go language
How to use computer language slowly 2
How to reflect CSS in Django
How to kill processes in bulk
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
How to build MongoDB C driver
How to run TensorFlow 1.0 code in 2.0
How to use computer language slowly
Segfault with 16 characters in C language
How to handle Japanese in Python
How to log in to Docker + NGINX
How to call PyTorch in Julia
Introduction to Socket API Learned in C Language Part 1 Server Edition
[C language] How to create, avoid, and make a zombie process
How to use calculated columns in CASTable
Linked list (list_head / queue) in C language
[Introduction to Python] How to use class in Python?
How to suppress display error in matplotlib
Exclusive control with lock file in Python
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
How to delete expired sessions in Django
[Itertools.permutations] How to put permutations in Python
[C language] I want to generate random numbers in the specified range
How to implement nested serializer in drf-flex-fields
How to work with BigQuery in Python
How to execute commands in jupyter notebook
How to do'git fetch --tags' in GitPython
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to reassign index in pandas dataframe