Get the App
SLTechnology News&Howtos  ›  Development  › 

How to synchronize threads among multiple processes using mutexes allocated by XSI IPC shared memory

Shulou Source: shulou.com Published: 2022-06-02 09:21:52 10月01日 Update

This article mainly introduces how to use XSI IPC shared memory allocation mutex to synchronize the threads between multiple processes in Camp Candle +. It has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article. Let's take a look at it.

# include

# include

# include

# include

# include

# include

# include

# include

# include

# define handle_error_en (en, msg)\

Do {errno = en; perror (msg); exit (EXIT_FAILURE);} while (0)

Static pthread_mutex_t * mtx

Static void * original_owner_thread (void * ptr) {

Printf ("[original owner] Setting lock...\ n")

Pthread_mutex_lock (mtx)

Printf ("[original owner] Locked. Now exiting without unlocking.\ n")

Pthread_exit (NULL); / / exit after lock addition does not release lock

}

Int main () {

Pid_t pid_robust

Int shmid

/ / use XSI IPC to allocate mutex memory

Shmid = shmget (IPC_PRIVATE,sizeof (pthread_mutex_t), IPC_CREAT | IPC_EXCL)

/ / get memory area pointer

Mtx=shmat (shmid,0,0)

Pid_robust = fork ()

Int proc_status

Int ret

If (pid_robust 0) {

Pthread_t thr_p

Pthread_mutexattr_t attr

Pthread_mutexattr_init & attr)

Pthread_mutexattr_setpshared & attr,PTHREAD_PROCESS_SHARED)

Pthread_mutexattr_setrobust (& attr, PTHREAD_MUTEX_ROBUST)

Pthread_mutex_init (mtx, & attr)

Pthread_create (& thr_p, NULL, original_owner_thread, NULL)

} else if (0==pid_robust) {

Int err_code

Sleep (2)

Printf ("[fork main thread] Attempting to lock the robust mutex.\ n")

Err_code = pthread_mutex_lock (mtx)

If (err_code = = EOWNERDEAD) {

Printf ("[fork main thread] pthread_mutex_lock () returned EOWNERDEAD\ n")

Printf ("[fork main thread] Now make the mutex consistent\ n")

Err_code = pthread_mutex_consistent (mtx); / / call the function to change the owner of the lock, that is, the lock is changed from the previous owner to the host thread

If (err_code! = 0) {

Handle_error_en (err_code, "pthread_mutex_consistent")

}

Printf ("[fork main thread] Mutex is now consistent; unlocking\ n")

Err_code = pthread_mutex_unlock (mtx)

If (err_code = = 0) {

Printf ("[fork main thread] pthread_mutex_lock () unexpectedly succeeded\ n")

} else {

Handle_error_en (err_code, "pthread_mutex_unlock")

}

} else {

Printf ("[fork main thread] lock success!% d\ n", err_code)

}

Exit (0)

}

Printf ("parent main thread waiting fork process\ n")

Sleep (2)

If (waitpid (pid_robust,&proc_status,0) < 0) {

Printf ("[parent main thread] waiting for fork process error.\ n")

Return 127

}

If (WIFEXITED (proc_status)) {

Return 0

}

Return 127

}

Compile:

Gcc-lpthread-o muti_proc_thread_lock muti_proc_thread_lock.c

Results:

Thank you for reading this article carefully. I hope the article "how to synchronize threads between multiple processes using XSI IPC shared memory allocation mutex" shared by the editor is helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Memory articles threads allocation between processes synchronization that is values interests functions at the same time owners pointers more friends knowledge results ribbons industries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft Redmi Shulou Information Apple