In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.