In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "Linux how to achieve inter-process synchronization", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Linux how to achieve inter-process synchronization" this article.
# # Mutex mutex
We already know that mutexes can be used for synchronization between threads, but in fact, mutexes can also be used for synchronization between processes. To achieve this, you can change the property of pthread_mutex_init to inter-process sharing before initializing it. The main attribute modification functions of mutex are as follows:
Main application functions:
Pthread_mutexattr_t mattr type: property pthread_mutexattr_init function used to define mutexes: initialize a mutex property object pthread_mutexattr_destroy function: destroy mutex property object (not lock) pthread_mutexattr_setpshared function: modify the mutex property.
Int pthread_mutexattr_setpshared (pthread_mutexattr_t * attr, int pshared)
Let's focus on the second parameter: pshared, which has the following two values:
Thread lock: PTHREAD_PROCESS_PRIVATE (the default property of mutex is thread lock, private between processes)
Process lock: PTHREAD_PROCESS_SHARED
To achieve inter-process synchronization, you need to change the property of mutex to PTHREAD_PROCESS_SHARED.
# include # include struct mt {int num; pthread_mutex_t mutex; pthread_mutexattr_t mutexattr;}; int main (void) {int i; struct mt * mm; pid_t pid; mm = mmap (NULL, sizeof (* mm), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON,-1,0); memset (mm, 0, sizeof (* mm)) Pthread_mutexattr_init (& mm- > mutexattr); / initialize the mutex attribute object pthread_mutexattr_setpshared (& mm- > mutexattr, PTHREAD_PROCESS_SHARED); / / modify the attribute to inter-process sharing pthread_mutex_init (& mm- > mutex, & mm- > mutexattr); / / initialize a mutex pid = fork () If (pid = = 0) {for (I = 0; I)
< 10; i++) { sleep(1); pthread_mutex_lock(&mm->Mutex); (mm- > num) + +; pthread_mutex_unlock (& mm- > mutex); printf ("- child-num++% d\ n", mm- > num);} else if (pid > 0) {for (I = 0; I)
< 10; i++) { sleep(1); pthread_mutex_lock(&mm->Mutex); mm- > num+=2; pthread_mutex_unlock (& mm- > mutex); printf ("- parent---num+=2% d\ n", mm- > num);} wait (NULL);} pthread_mutexattr_destroy (& mm- > mutexattr); / / destroy mutex attribute object pthread_mutex_destroy (& mm- > mutex) / / destroy mutex munmap (mm,sizeof (* mm)); / / release return 0;}
# # File Lock
As the name implies, the locking mechanism is implemented through files. Specifically, the locking mechanism is implemented with the help of the fcntl function. When the process that manipulates the file does not acquire the lock, although the file can be opened, it cannot perform read or write operations on the file.
# fcntl function:
Function prototype: int fcntl (int fd, int cmd,... / arg /)
Function: get and set file access control properties.
Parameter description: parameter cmd has the following values: F_SETLK (struct flock) setting file lock (trylock) F_SETLKW (struct flock) setting file lock (lock) W-> wait F_GETLK (struct flock *) getting file lock data type flock prototype as follows: struct flock {. Short lumped type; lock type: F_RDLCK, F_WRLCK, F_UNLCK short lumped where; offset: SEEK_SET, SEEK_CUR, SEEK_END off_t lumped start; start offset: 1000 off_t lumped; length: 0 indicates that the whole file is locked pid_t lumped; the process that holds the lock ID: (F_GETLK only).}
# example of interprocess file locking
Multiple processes access locked files:
# include # include void sys_err (char * str) {perror (str); exit (1);} int main (int argc, char * argv []) {int fd; struct flock flocklock; if (argc < 2) {printf (". / a.out filename\ n"); exit (1) } if ((fd = open (argv [1], O_RDWR) < 0) sys_err ("open"); f_lock.l_type = FairWRLCK; / * choose to write * / f_lock.l_type = Flying RDLCK; / * choose to read * / f_lock.l_whence = SEEK_SET; f_lock.l_start = 0; f_lock.l_len = 0 / * 0 means the entire file is locked * / fcntl (fd, F_SETLKW, & f_lock); printf ("get flock\ n"); sleep (10); f_lock.l_type = Fairchild UNLCK; fcntl (fd, F_SETLKW, & f_lock); printf ("un flock\ n"); close (fd); return 0;}
File locks are similar to read-write locks and still follow the "read-sharing, write-exclusive" characteristics. However, if the process manipulates the file without locking, it can still be accessed successfully, but the data is bound to be confused.
Now that file locks can be used in processes, can file locks be used in multithreading?
The answer is no. Because file descriptors are shared among multithreads, locking files is achieved by modifying member variables in the file structure that the file descriptor points to. Therefore, file locks cannot be used in multithreading.
The above is all the contents of the article "how to achieve inter-process synchronization in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.