V
V
Venot2021-11-19 13:17:10
linux
Venot, 2021-11-19 13:17:10

How do processes use a mutex?

A mutex is a synchronization primitive that provides mutual exclusion of the execution of critical sections of code.
If I write my own application, I can create a mutex for threads, and the thread will check if it can access this section, and if there are 2 different processes, how can another process understand whether it has access or not?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Cheremisin, 2021-11-19
@Venot

There are three options (in posix), configured via pthread_mutexattr_settype()
- PTHREAD_MUTEX_NORMAL blocking when trying to capture - the thread will wait until the resource is freed
- PTHREAD_MUTEX_RECURSIVE recursive capture - the same thread can re-capture the resource
- PTHREAD_MUTEX_ERRORCHECK crashes with an error if the resource is busy
Well and it is unnecessary to forget that there are also semaphores that have a counter. For example, block access if 5 threads or processes have already captured the semaphore.
And, I also forgot, if there are processes, then there is shared memory for this - semaphores and mutexes should be placed there. With mutexes, I'm not sure exactly, but semaphores are needed there!

A
Armenian Radio, 2021-11-19
@gbg

To do this, you need to learn how to do two things:
1) Make meaningful headings with your question, so that it is not like here - the heading is irrelevant to the body of the question.
2) Place the created mutexes in memory shared between processes - then the processes will be able to synchronize on them .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question