pthread_mutex_t mutex = Pthread_mutex_initializer, ... pthread_mutex_lock (&mutex);//Locking .../*share Memory handle*/... pthread_mutex_unlock (&mutex);//unlock ....
Now we are using a similar approach:
sem_t *sem_mutex = NULL; Sln_mutex_shm_lock (Sem_mutex_file, Sem_mutex);//locking .../*share memory handle*/... Sln_mutex_shm_unlock (Sem_mutex);//unlock ...
which Sem_mutex_file is the name of the well-known semaphore required for the Sem_open function.
two of these lock unlock implementations are:
#define Sln_mutex_shm_lock (Shmfile, Sem_mutex) do { Sem_mutex = Sem_open (shmfile, O_RDWR | O_creat, 0666, 1); if (sem_failed = = Sem_mutex) { printf ("Sem_open (%d):%s\n", __line__, Strerror (errno)); } Sem_wait (Sem_mutex);} while (0) #define SLN_MUTEX_SHM_UNLOCK (Sem_mutex) does {sem_post (Sem_mutex);} while (0)
is to initialize a binary semaphore, its initial value is 1, and perform a wait operation, so that the value of the semaphore becomes 0, when other processes want to operate shared memory also need to perform a wait operation, but at this time the value of the semaphore is 0, so start waiting for the semaphore value into 1. When the current process has finished operating the shared memory, it begins to unlock, and the post operation adds a value of the semaphore to the other process, where wait can be returned. The following is an example of a mutually exclusive access to shared memory, and a POSIX shared memory implementation, see the previous IPC article series. Ser process:
int Nms_shm_get (char *shm_file, void **shm, int mem_len) { int fd; FD = Shm_open (shm_file, O_RDWR | O_creat, 0666); if (FD < 0) { printf ("Shm_pen <%s> failed:%s\n", Shm_file, Strerror (errno)); return-1; } Ftruncate (FD, Mem_len); *SHM = Mmap (NULL, Mem_len, Prot_read | Prot_write, map_shared, FD, 0); if (map_failed = = *shm) { printf ("Mmap:%s\n", Strerror (errno)); return-1; } return 0;} int main (int argc, const char *argv[]) { sem_t *sem_mutex = NULL; char *str = NULL; Sln_mutex_shm_lock (Sem_mutex_file, Sem_mutex); Locking Nms_shm_get (Shm_file, (void * *) &str, Shm_max_len);//The following three lines mutually exclusive access to the shared memory sleep (6); snprintf (str, Shm_max_len, "POSIX semphore server!"); Sln_mutex_shm_unlock (Sem_mutex); Unlock sleep (6); Shm_unlink (shm_file); return 0;}
Client process:
int main (int argc, const char *argv[]) { sem_t *sem_mutex; char *str = NULL; Sln_mutex_shm_lock (Sem_mutex_file, Sem_mutex); Nms_shm_get (Shm_file, (void * *) &str, shm_max_len); printf ("Client Get:%s\n", str); Sln_mutex_shm_unlock (Sem_mutex); return 0;}
Start the service process first with a lock, create shared memory and manipulate it, and lock in sleep for 6 seconds to test whether the client process is waiting while the service process is not releasing the lock. The client process starts immediately after the service process starts, is in a wait state, and the client process obtains shared memory information when the service process is unlocked after 6 seconds. After 6 seconds, the service process deletes the shared memory and the client process fails to acquire shared memory again.
#./server & [1] 21690 #./client Client Get:posix Semphore server! #./clientshm_open <share_memory_file> failed:no Such file or directoryclient get: (null) [1]+ done./server
POSIX-known semaphores create semaphore files and shared memory files in the/dev/shm/directory:
# ls/dev/shm/ Sem.sem_mutex Share_memory_file #
When two processes share data, when a process writes data to the shared memory, it needs to notify the other process, which requires synchronization between the two processes, where we add synchronous operations to the above program on a mutually exclusive basis. Synchronization is also accomplished using POSIX semaphores. Server process:
int main (int argc, const char *argv[]) {sem_t *sem_mutex = NULL; sem_t *sem_consumer = null, *sem_productor = NULL; int semval; char *sharememory = NULL; Sem_consumer = Sem_open (Sem_consumer_file, O_creat, 0666, 0); Initialize the semaphore Sem_consumer and set the initial value to 0 if (sem_failed = = Sem_consumer) {printf ("Sem_open <%s>:%s\n", Sem_consume R_file, Strerror (errno)); return-1; } sem_productor = Sem_open (Sem_productor_file, O_creat, 0666, 0);//Initialize the semaphore Sem_productor and set the initial value to 0 if (sem_failed = = s Em_productor) {printf ("Sem_open <%s>:%s\n", Sem_productor_file, Strerror (errno)); return-1; } for (;;) {//The service process has been circulating processing client process requests Sem_getvalue (Sem_consumer, &semval); printf ("%d waiting...\n", semval); if (sem_wait (Sem_consumer) < 0) {//If the Sem_consumer is 0, then block here, wait for the customer process post operation to make Sem_consumer greater than 0, here and the customer process synchronizes printf ("Sem _wait:%s\n ", Strerror (errno)); return-1; } printf ("Get request...\n"); SlN_mutex_shm_lock (Sem_mutex, Sem_mutex);//here begins mutually exclusive access to shared memory Nms_shm_get (Shm_file, (void * *) &sharememory, Shm_max_len) ; Sleep (6); snprintf (Sharememory, Shm_max_len, "Hello, this is server ' s message!"); Sln_mutex_shm_unlock (Sem_mutex); Sem_post (sem_productor);//Make the semaphore sem_productor plus one so that the blocked client process continues to execute printf ("Response request...\n"); } sem_close (Sem_consumer); Sem_close (Sem_productor); return 0;}Client process:
int main (int argc, const char *argv[]) {sem_t *sem_consumer = null, *sem_productor = NULL; struct TIMESPEC timeout; int ret; char *sharememory = NULL; Sem_t *sem_mutex; Sem_consumer = Sem_open (Sem_consumer_file, O_RDWR);//Gets the value of the semaphore Sem_consumer if (sem_failed = = Sem_consumer) {print F ("Sem_open <%s>:%s\n", Sem_consumer_file, Strerror (errno)); return-1; } sem_productor = Sem_open (Sem_productor_file, O_RDWR);//Gets the value of the semaphore sem_productor if (sem_failed = = Sem_productor) { printf ("Sem_open <%s>:%s\n", Sem_productor_file, Strerror (errno)); return-1; }//clear_exist_sem (Sem_productor); Sln_mutex_shm_lock (Sem_mutex, Sem_mutex);//mutually exclusive access to shared memory Nms_shm_get (Shm_file, (void * *) &sharememory, Shm_max_len); printf ("Sharememory:%s\n", sharememory); Sln_mutex_shm_unlock (Sem_mutex); Sem_post (Sem_consumer);///Semaphore Sem_consumer plus one, wake-up is a service process that blocks on that semaphore printf ("post...\n"); Sem_wait (sem_productor);//wait for the service process to respond/* Timeout. tv_sec = Time (NULL) + sem_timeout_sec; timeout.tv_nsec = 0; ret = sem_timedwait (Sem_productor, &timeout); if (Ret < 0) {printf ("sem_timedwait:%s\n", Strerror (errno)); } */printf ("Get response...\n"); Sem_close (Sem_consumer); Sem_close (Sem_productor); return 0;}Sample source download for this section: Click the Open link
Linux Mutex and synchronization application (iv): Mutual exclusion and synchronization of POSIX semaphores