[14] Linux multithreading semaphore synchronization of threads

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/zztingfeng/article/details/90453491

What is a semaphore?

The concept of the amount of semaphores between threads and processes used in the communication signal is the same, it is a special variable, which can be increased or decreased, but the key to which access is guaranteed to be atomic. If a program has multiple threads try to change the value of a semaphore, the system will ensure that all operations are carried out in sequence.

The semaphore only two values ​​0 and 1 is called a binary semaphore, will focus on here. Usually the amount of the signal typically used to protect a piece of code, so that each time only one thread of execution run. We can use binary semaphores to finish the job.

Semaphore Functions

Semaphore function begins sem_, basic semaphore function threads used are four, they are declared in the header file in semaphore.h.

1, sem_init function is
the function used to create the semaphore, which prototype is as follows:
int sem_init (sem * sem_t, the pshared int, unsigned int value); 
signal This function initializes the object pointed to by the sem, set its sharing options and give it an initial integer value. pshared amount of control signal type, its value is 0, it means that the semaphore is a local semaphore of the current process, otherwise the amount of signals can be shared among multiple processes, value as an initial value of sem. Return 0 when you call success, failure to return -1.

2, sem_wait function
This function is used to atomically the semaphore value is decremented by one. Atomic operation is, if two threads attempt to simultaneously give a semaphore incremented or decremented by 1, without mutual interference between them. It has the following prototype:
int of sem_wait (sem_t * SEM); 
SEM semaphore object pointed to by the initialization call sem_init. Return 0 when you call success, failure to return -1.

3, sem_post function
This function is used to atomically increments the value of the semaphore 1. It has the following prototype:
int sem_post (sem_t * SEM);
and as sem_wait, sem semaphore object pointed to is called by sem_init initialized. Return 0 when you call success, failure to return -1.

4, sem_destroy function
This function is used to clean the spent amount signal. It has the following prototype:
int sem_destroy (sem_t * SEM);
return 0 on success, -1 on failure.

Code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<pthread.h>
#include<semaphore.h>
 
char buf[60];
sem_t sem;
void *thread_fun(void *arg)
{
    while(1)
    {
        sem_wait(&sem);
        printf("you enter %d characters\n", strlen(buf)-1);
    }
}
int main()
{
    pthread_t thread;
    void *thread_result;
    if(0 > sem_init(&sem, 0, 0)) /* 初始化信号量 */
    {
        perror("sem_init");
        exit(-1);
    }
    if (0 > pthread_create(&thread, NULL, thread_fun, NULL)) /* 创建线程 */
    {
        perror("pthread_create");
        exit(-1);
    }
    printf("input 'quit' to exit\n");
    while(0 != (strncmp(buf, "quit", 4)))
    {
        fgets(buf, 60, stdin);
        sem_post(&sem);
    }
    return 0;
}

gcc execution results

Guess you like

Origin blog.csdn.net/zztingfeng/article/details/90453491