Well-known semaphore Semphore
If the signal is a mechanism for the association of external events and processes, then semaphores are the mechanism of communication between processes and threads. Depending on whether the communication between different processes is supported or a different thread within the same process, the semaphore can be divided into known semaphores and nameless semaphores. As the name implies, a well-known semaphore needs to explicitly name a semaphore when it is built, and in Linux the name corresponds to/dev/shm-semphorename, the shared memory mapping file, because it can be accessed simultaneously by different processes, so that communication can be achieved between different processes , and nameless signals are used only between different threads within the same process, because different threads share the resources of the process that created them, so there is no need to specify any names or files that can be collectively accessed. The common denominator is that both support the classic p/v operation, when requesting a semaphore, if the application is not able to be dispatched to hibernation until the V Operation wakes up.
The following is a description of the known semaphore and the nameless semaphore based on the Centoslinux system respectively.
Well-known signal volume
Dependent header File
#include <fcntl.h>/* for o_* Constants */
#include <sys/stat.h>/* for Mode constants */
#include <semaphore.h>
Common functions
Create a named semaphore
Sem_t*sem_open (const char *name, int oflag);
Sem_t*sem_open (const char *name, int oflag, mode_t mode, unsigned intvalue);
P operation
Intsem_wait (sem_t *sem); If the semaphore is not greater than 0, the current thread or process is blocked until the semaphore is greater than 0
Intsem_trywait (sem_t *sem)///If the semaphore is not greater than 0, returning an error value does not block the current thread or process
Intsem_timedwait (sem_t *sem, const struct timespec*abs_timeout);//If the semaphore is not greater than 0, the current thread or process is blocked abs_timeout the specified time, and then continues into the run queue
V Operation
Intsem_post (sem_t *sem);
Turn off the signal
Intsem_close (sem_t *sem); Allows any resources requested for this semaphore to be released
Intsem_unlink (sem_t *sem);//Delete the known semaphore that the SEM points to
Query the value of the current semaphore
Intsem_getvalue (sem_t *sem, int *sval);//Read the value of the current semaphore into the sval.
Connecting dependent libraries
Depending on the line libraries, you need to add-lpthread when linking.
code example: SEM_SVC.C
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <fcntl.h>/* for o_* Constants */
#include <sys/stat.h>/* for Mode constants */
#include <semaphore.h>
Constchar * fname = "tstmy";
Voidmain (void)
{
Pthread_tftids;
Sem_t *svc_sem;
Svc_sem =sem_open (fname, O_creat | O_EXCL, 0644, 0);
printf ("Willpost semp soon ... \ n");
Sleep (2);
Sem_post (Svc_sem);
printf ("Willpost semp again ... \ n");
Sleep (2);
Sem_post (Svc_sem);
printf ("Postsemp done!\n");
Sem_close (Svc_sem);
Sem_unlink (fname);
}
SEM_CLNT.C:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <fcntl.h>/* for o_* Constants */
#include <sys/stat.h>/* for Mode constants */
#include <semaphore.h>
Constchar * fname = "tstmy";
sem_t* Clnt_sem;
Voidstate_thread ()
{
while (1) {
printf ("waitsemp....\n");
Sem_wait (Clnt_sem);
printf ("Waitsemp done:semp comes\n");
}
}
Voidmain (void)
{
Pthread_tftids;
Clnt_sem =sem_open (FNAME,O_EXCL);
Pthread_create (&ftids,null, (void *) State_thread, NULL);
while (1) {
printf ("Inclnt main process\n");
Sleep (1);
}
}
The compile link is as follows:
[Email protected]testcases]$ gcc-o sem_svc Sem_svc.c-lpthread
[Email protected]testcases]$ gcc-o sem_clnt Sem_clnt.c-lpthread
Typical application scenarios:
1. Based on its implementation of multiple processes between the read/write lock/or producer consumer model;
2. The need to implement a communication synchronization relationship between multiple threads belonging to different processes;
Note: The above instance code SEM_SVC.C in Sem_close () and sem_unlink can not be ignored, or the next run to Sem_open () error, it is important to pay attention to, especially in the multi-threaded, process environment, These two functions are suggested to be performed in a system or module cleanup function (De-init or destruct).
This article is from the "Store Chef" blog, so be sure to keep this source http://xiamachao.blog.51cto.com/10580956/1784665
Signal, semaphore, lock, condition variable, message communication, shared memory, RPC (ii)