Thread synchronization, semaphores, System V IPC

Source: Internet
Author: User
Tags mutex semaphore

One, thread synchronization condition variable what is a condition variable? Thread A waits for a condition to be established, and thread a continues to execute downward. Thread B executes the condition and wakes up thread A after the condition is established to continue execution. This condition is the condition variable. The pthread_cond_t type is the type of the condition variable that encapsulates the type as follows: #include<pthread.h>//static initialization of conditional variablespthread_cond_t cond =Pthread_cond_initializer;intPthread_cond_init (pthread_cond_t *cond,\ pthread_condattr_t *cond_attr); function: Initializes a condition variable parameter: Cond: Specifies the condition variable to initialize Cond_attr:null the default property return value:0Success not 0 errorintPthread_cond_signal (pthread_cond_t *cond); function: Starts a thread parameter that waits for the condition variable to become true: Cond: Specifies the condition variable return value:0Success not 0 errorintPthread_cond_broadcast (pthread_cond_t *cond); function: Start all wait condition variables to true thread parameter: Cond: Specifies the condition variable return value:0Success not 0 errorintPthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex); function: Wait for the condition variable to be true: Cond: Specifies the conditional variable to wait mutex: The lock return value that needs to be undone before waiting :0successful non-0 error operation steps:1, the first atom to unlock the mutex lock. 2, let the thread wait for the condition variable to become true. 3, when the condition variable is true, lockintPthread_cond_timedwait (pthread_cond_t *Cond, pthread_mutex_t*Mutex,Const structTimespec *abstime); function: Timeout wait, timeout return error parameter: Cond: Specify conditional variable to wait mutex: waits for a lock that needs to be undone before Abstime: Specifies the time to wait for the return value:0Success not 0 errorintPthread_cond_destroy (pthread_cond_t *cond); function: Destroy condition variable parameter: Cond: Specifies the return value of the condition variable to destroy :0successful non-0 error producers and consumers of examples of chain list implementation producers produce objects placed into the list of the head of the chain of consumers from the head of the list to take out consumption. First thoughts: How two threads synchronize access to the head of a linked list second thought: If the list is empty, the consumer waits for the producer thread to produce the object. Third thinking: Producer threads, producing objects to inform consumers. Code See: COND.C semaphore When multiple threads are using multiple resources, use semaphores to achieve thread synchronization. The operation of the sem_t type type is as follows: Sem_init (3) #include<semaphore.h>intSem_init (sem_t *sem,intpshared, unsignedintvalue); function: Initialize semaphore parameter: SEM: Specifies the semaphore pshared to initialize:0applied to multi-threaded non-0 process value: Specifies the initial value of the semaphore return value:0Success-1failure errno is set Sem_destroy (3) #include<semaphore.h>intSem_destroy (sem_t *sem); function: Destroy semaphore parameter: SEM: Specifies the semaphore return value to destroy:0Success-1error errno is set Sem_post (3) #include<semaphore.h>intSem_post (sem_t *sem) function: The value of the semaphore plus 1 operation parameters: SEM: The specified semaphore is the value of this semaphore plus 1. return value:0Success-1error errno is set sem_wait (3) #include<semaphore.h>intSem_wait (sem_t *sem) function: The value of the semaphore is reduced by 1. If the semaphore has a value of 0. Blocking wait parameters: Sem: Specifies the semaphore return value to be manipulated:0Success-1error errno is setintSem_trywait (sem_t *sem);intSem_timedwait (sem_t *SEM,Const structTimespec *abs_timeout); Use semaphores to implement examples of producers and consumers using ring queues. code See SEM.C thread end two, the System V IPC Semaphore set is an array, and each element in the array is a semaphore type. 1, first get the key value Ftok (3)2, using the key value to get the idsemget of the semaphore set (2) #include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>intSemget (key_t Key,intNsems,intsemflg); function: Gets the ID parameter of the semaphore set: Key:ftok (3The return value of Nsems: Specifies the number of elements in the semaphore set Semflg:IPC_CREAT:IPC_EXCL:mode: Specifies the permission return value for the semaphore set: A nonnegative integer that is the ID of the semaphore set. -1error errno is set3, sets the initial value of the semaphore in the semaphore set, or gets the amount of the semaphore. Semctl (2) #include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>intSemctl (intSemid,intSemnum,intcmd, ...);  Function: Semaphore control operation parameters: Semid: Specifies the semaphore set Semnum: Specifies a semaphore in the array subscript cmd: Specifies the control operation command for the Semaphore Getval: Gets the Semval value to the first number of signals. Semval value Setval: Sets the Semval value of the number of semaphores to Arg.val. 0... : Variable parameters. Does this have, what type? Depends on CMD. return value:-1error errno is set to custom union Semun {intVal/*Value for Setval*/    structSemid_ds *buf;/*Buffer for Ipc_stat,ipc_set*/unsigned Short*array;/*Array for GETALL, SETALL*/    structSeminfo *__buf;/*Buffer for Ipc_info (linux-specific)*/};4, the addition or subtraction operation of the specified semaphore Semop (2) #include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>intSemopintSemid,structSEMBUF *SOPs, unsigned nsops); function: semaphore operation parameter: Semid: Specified semaphore set SOPs: Specifies the specific operation of a semaphore Nsops: Specifies the number of semaphores to be manipulated. return value:0Success-1error errno is defined in this struct by the set operationstructsembuf{unsigned ShortSem_num;/*Semaphore Number*/         ShortSem_op;/*Semaphore Operation*/         ShortSEM_FLG;/*Operation Flags*/};sem_flg:ipc_nowait ipc_undone Automatic undo Operation Sem_num: Specifies the semaphore to be manipulated in the subscript of the array Sem_op: Specifies the type of operation of the operation type is divided into 3 kinds>0semval+Sem_op Semval do the addition=0<0: Semval>The absolute value of the sem_op, and this operation is performed immediately. Semval<Sem_op The absolute number of resources requested>number of available resources ipc_nowait non-blocking SEM_FLG=0A blocking wait resource can be used to illustrate the use of semaphore sets for interprocess communication. Two processes PA.C sets the value of the semaphore, every 3 seconds, the value of Semval minus 1 pb.c every 1 seconds, gets the value of the semaphore,

Thread synchronization, semaphores, System V IPC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.