Semaphores for communication between Linux threads (19)

1. Signal amount

Semaphore Overview Semaphore is widely used for synchronization and mutual exclusion between processes or threads. A semaphore is essentially a non-negative integer counter that is used to control access to public resources. When programming, you can judge whether you have access to public resources according to the result of operating the semaphore value. When the semaphore value is greater than 0, you can access it, otherwise it will be blocked. The PV primitive is an operation on the semaphore. A P operation decreases the semaphore by 1, and a V operation increases the semaphore by 1. Semaphores are mainly used in two typical situations: synchronization and mutual exclusion between processes or threads. The semaphore data type is: sem_t.

Semaphores are used for mutual exclusion:
Insert image description here
semaphores are used for synchronization:

Insert image description here

2.Initialize the semaphore function

	int sem_init(sem_t *sem, int pshared, unsigned int value);

Function:
Create a semaphore and initialize its value. An unnamed semaphore must be initialized before it can be used.

Parameters:
sem: the address of the semaphore.
pshared: equal to 0, the semaphore is shared between threads (commonly used); not equal to 0, the semaphore is shared between processes.
value: The initial value of the semaphore.

Return value:
Success: 0.
Failure: -1.

3. Semaphore minus one function

	int sem_wait(sem_t *sem);

Function:
Decrease the semaphore by one. If the value of the semaphore is 0, it will block. If it is greater than 0, it can be decremented by one.

Parameters:
sem: the address of the semaphore.

Return value:
Success: 0.
Failure: -1.

4. Try to subtract a function from the semaphore

	int sem_trywait(sem_t *sem);

Function:
Try to reduce the semaphore by one. If the value of the semaphore is 0, it will not block and return immediately. If it is greater than 0, it can be reduced by one.

Parameters:
sem: the address of the semaphore.

Return value:
Success: 0.
Failure: -1.

5. Semaphore plus one function

	int sem_post(sem_t *sem);

Function:
Increase the semaphore by one.

Parameters:
sem: the address of the semaphore.

Return value:
Success: 0.
Failure: -1.

6. Destroy the semaphore function

	int sem_destroy(sem_t *sem);

Function:
Destroy the semaphore.

Parameters:
sem: the address of the semaphore.

Return value:
Success: 0.
Failure: -1.

7. Reference code

//=============================================================================
// File Name    : thread_semaphore.c
// Author       : FengQQ
//
// Description  : 信号量
// Annotation   : 
//				  
// Created by FengQQ. 2020-10-05
//=============================================================================
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <semaphore.h>

sem_t sem1,sem2;
char ch='a';

//---------------线程1入口函数---------------
void *pthread1_callback(void *arg)
{
    
    
	while(1)
	{
    
    
		sem_wait(&sem2);		//sem2信号量减一
		ch++;
		sleep(1);
		sem_post(&sem1);		//sem1信号量加一
	}
}
//---------------线程2入口函数---------------
void *pthread2_callback(void *arg)
{
    
    
	while(1)
	{
    
    
		sem_wait(&sem1);		//sem1信号量减一
		putcharputchar(ch);		//输出字符
		fflush(stdout);			//清除读写缓冲区
		sem_post(&sem2);		//sem2信号量加一
	}
}

int main(int argc,char *argv[])
{
    
    
	int ret;
	pthread_t ptid1,ptid2;
	
	sem_init(&sem1,0,1);				//初始化信号量sem1
	sem_init(&sem2,0,0);				//初始化信号量sem2
	
	ret = pthread_create(&ptid1,NULL,pthread1_callback,NULL);		//创建线程1
	if(ret != 0)
	{
    
    
		printf("create new pthread1 failed...\r\n");
		return -1;
	}	
	ret = pthread_create(&ptid2,NULL,pthread2_callback,NULL);		//创建线程2
	if(ret != 0)
	{
    
    
		printf("create new pthread2 failed...\r\n");
		return -1;
	}
	
	ret = pthread_join(ptid1,NULL);									//回收线程1资源
	if(ret != 0)
	{
    
    
		printf("pthread1 join failed...\r\n");
	}	
	ret = pthread_join(ptid2,NULL);									//回收线程2资源
	if(ret != 0)
	{
    
    
		printf("pthread2 join failed...\r\n");
	}
	
	sem_destroy(&sem1);					//销毁信号量sem1
	sem_destroy(&sem2);					//销毁信号量sem2
	
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_39721016/article/details/120604654