A producer consumer model for POSIX-based semaphores

Source: Internet
Author: User

Semaphores are similar to mutexes, indicating the number of available resources, and unlike mutexes, this number can be greater than 1, that is, if the semaphore describes the number of resources is 1 o'clock, then the semaphore and the mutex are the same.

Let's look at the POSIX Semaphore library function, which can be used for synchronization between threads of the same process, or for synchronization between different processes.

1. int Sem_init (sem_t *sem,int pshared,unsigned int value)

We can use this function to create an unnamed semaphore, the pshared parameter indicates whether the semaphore is used in more than one process, and if it is set to a value other than 0, the value parameter sets the initial value of the semaphore.

2.int Sem_destroy (sem_t *sem)

When we have finished using the unnamed semaphore, we can call the Sem_destroy function to discard it. After calling Sem_destroy, you can no longer use any semaphore function with SEM unless you reinitialize it by calling Sem_init.

3.int sem_wait (sem_t *sem)

int sem_trywait (sem_t *sem)

We can use the sem_wait or sem_trywait function to achieve a minus 1 of the semaphore. When using the Sem_wait function, blocking occurs if the semaphore count is 0. It is not returned until the signal has been successfully reduced by 1 or interrupted by a signal. You can use sem_trywait to avoid blocking. When Sem_trywait is called, if the semaphore is 0, it is not blocked, but returns-1, and the errno is set to Eagain.

4.int sem_post (sem_t *sem)

We can call it the semaphore increases by 1.


Let's take a look at a producer consumer model based on the semaphore:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

#define _SIZE_ 20
int Buf[_size_];
sem_t Blank;
sem_t data;

void *product (void *arg)
{
int index=0;
int count=0;
while (1)
{
Sem_wait (&blank);//p
buf[index]=count++;
Sleep (2);
Sem_post (&data);//v
index++;
Index%= _size_;
}
}

void *consumer (void *arg)
{
int index=0;
int count=0;
while (1)
{
Sem_wait (&data);
Count=buf[index];
printf ("Consumer data:%d\n", count);
Sem_post (&blank);
index++;

Index%= _size_;
}
}

int main ()
{
Sem_init (&blank,0,_size_);
Sem_init (&data,0,0);

pthread_t Tid1,tid2;
Pthread_create (&tid1,null,product,null);
Pthread_create (&tid2,null,consumer,null);

Pthread_join (Tid1,null);
Pthread_join (Tid2,null);

Sem_destroy (&blank);
Sem_destroy (&data);
return 0;
}

The results of the operation are as follows:

650) this.width=650; "title=" K1QCT~J4IPKF (' 8]}6faf]d.png ' src= ' http://s2.51cto.com/wyfs02/M00/7F/63/ Wkiom1ccse6dljxzaaan7y_wbvk937.png "alt=" Wkiom1ccse6dljxzaaan7y_wbvk937.png "/>

We can see that consumers are producing data in a non-stop consumer producer 、、、、、








A producer consumer model for POSIX-based semaphores

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.