Unknown semaphore of process communication
Like a well-known semaphore, a nameless signal is also the primary means of communication between processes/threads, except that he cannot be used between different processes. Of course, if you put the nameless semaphore in shared memory that can be accessed by multiple processes, you can also implement communication between processes, but this is mainly due to the mechanism of shared memory. The following will be described based on the header files that use the nameless semaphore dependency, the library files that need to be linked, and the commonly used functions.
Dependent header files: #include <semaphore.h>
Link Dependent libraries: You need to link to libpthread.so, so you need to specify-lpthread in makefile
List of important functions:
Intsem_init (sem_t *sem, int pshared, unsigned int value);
The initialization function of the nameless semaphore, which embodies the main difference between the nameless semaphore and the well-known semaphore, differs from the Sem_open () of the initialized function of the known semaphore, which does not need to specify a file name, but simply passes in a process, and the thread can access the semaphore pointer. If you need to access between different threads of the same process, the pshared must be set to 0, and the first parameter must be guaranteed to be accessible to each thread, and if it needs to be accessed between different processes, then pshared must be set to 1, and the first parameter must be placed in the shared memory area. The three parameter of this function specifies the value that the semaphore initializes, which represents the number of system resources that the system has just begun to use.
When the above function returns to 0 o'clock, the function call succeeds; When returned to-1, the error code is placed inside the errno and can be printed by strerror (errno).
Intsem_post (sem_t *sem);
Unlock the nameless semaphore and adds 1 to the number of available resources, waking the thread or process waiting for the current semaphore, allowing the awakened process or thread to get the lock. The function return value is the same as above.
Intsem_wait (sem_t *sem);
Intsem_trywait (sem_t *sem);
Intsem_timedwait (sem_t *sem, const struct TIMESPEC *abs_timeout);
The above three functions can query whether there are currently available resources, and if so, grab the lock and reduce the current number of available resources by 1. If there are no available resources: The difference is that sem_wait () joins the current process or thread into the hibernation queue until it wakes up, and the sem_trywait () function returns an error immediately if the system does not have the resources available, and sets the error code to Eagain. The current process or thread is not blocked, and sem_timewait () will be blocked for a maximum length of abs_timeout without available resources, then return an error and set the error code to etimedout.
Intsem_getvalue (sem_t *sem, int *sval);
Get the current semaphore value (representing the number of available resources for the current system) and put it in the memory that Sval points to.
code example
#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>
#include <errno.h>
Sem_tclnt_sem;
Intcreate_done = 0;
Voidstate_thread1 ()
{
while (1) {
if (!create_done) continue;
printf ("%s:wait semp....\n", __function__);
Sem_wait (&clnt_sem);
printf ("%s:wait semp done:semp comes\n", __function__);
}
}
Voidstate_thread2 ()
{
while (1) {
printf ("%s:wait semp....\n", __function__);
Sem_wait (&clnt_sem);
printf ("%s:wait semp done:semp comes\n", __function__);
Sleep (10);
}
}
Voidmain (void)
{
Pthread_tftids;
intret;
Ret= Sem_init (&clnt_sem,0, 3);
if (Ret < 0) {
printf ("%s\n", Strerror (errno));
}
Pthread_create (&ftids,null, (void *) State_thread1, NULL);
Pthread_create (&ftids,null, (void *) state_thread2, NULL);
Create_done = 1;
Sem_post (&clnt_sem);
while (1) {
printf ("Inclnt main process\n");
Sleep (10);
}
Compile and run the results:
[Email protected]testcases]$ gcc-o sem_nameless Sem_nameless.c-lpthread
[Email protected]testcases]$./sem_nameless
INCLNT Main Process
State_thread2:wait semp ....
State_thread2:wait semp Done:semp comes
State_thread1:wait semp ....
State_thread1:wait semp Done:semp comes
State_thread1:wait semp ....
State_thread1:wait semp Done:semp comes
State_thread1:wait semp ....
State_thread1:wait semp Done:semp comes
State_thread1:wait semp ....
INCLNT Main Process
State_thread2:wait semp ....
This article is from the "Store Chef" blog, so be sure to keep this source http://xiamachao.blog.51cto.com/10580956/1791534
Unknown semaphore of process communication