本文主要是介绍IMX6:semaphore测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一 相关函数
1 初始化函数sem_init
NAME
sem_init - initialize an unnamed semaphoreSYNOPSIS
#include <semaphore.h>int sem_init(sem_t *sem, int pshared, unsigned int value);
Link with -pthread.
参数设置:
pshare:线程间通信设置为0,进程间通信设置为非0。
value:信号的初始值,默认设置为0
2 发送信号函数sem_post
NAME
sem_post - unlock a semaphoreSYNOPSIS
#include <semaphore.h>int sem_post(sem_t *sem);
Link with -pthread.
参数:已经初始化好的 sem_t变量指针。就是执行加1的操作
3 等待函数
NAME
sem_wait, sem_timedwait, sem_trywait - lock a semaphoreSYNOPSIS
#include <semaphore.h>int sem_wait(sem_t *sem);
int sem_trywait(sem_t *sem);
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
Link with -pthread.
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
sem_timedwait(): _POSIX_C_SOURCE >= 200112L
sem_wait:一直等待
sem_trywait:尝试获取信号量,不等待
sem_timedwait:带超时的等待:
struct timespec {
time_t tv_sec; /* Seconds */
long tv_nsec; /* Nanoseconds [0 .. 999999999] */
};
注意:他们都是可能被signal打断的,所以要判断EINTR。下面是返回值和相应的出错码:
RETURN VALUE
All of these functions return 0 on success; on error, the value of the semaphore is left unchanged, -1 is returned, and errno is set to indicate the error.ERRORS
EINTR The call was interrupted by a signal handler; see signal(7).EINVAL sem is not a valid semaphore.
The following additional error can occur for sem_trywait():
EAGAIN The operation could not be performed without blocking (i.e., the semaphore currently has the value zero).
The following additional errors can occur for sem_timedwait():
EINVAL The value of abs_timeout.tv_nsecs is less than 0, or greater than or equal to 1000 million.
ETIMEDOUT
The call timed out before the semaphore could be locked.
4 读信号量的值
NAME
sem_getvalue - get the value of a semaphoreSYNOPSIS
#include <semaphore.h>int sem_getvalue(sem_t *sem, int *sval);
Link with -pthread.
5 销毁一个信号量
NAME
sem_destroy - destroy an unnamed semaphoreSYNOPSIS
#include <semaphore.h>int sem_destroy(sem_t *sem);
Link with -pthread.
二 测试源码:
#include <sys/epoll.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <semaphore.h>#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%s:%d -- "format"\n" \
,__FILE__,__func__,__LINE__ \
, ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endifstruct my_struct {sem_t sem;int run;
};void * my_func1(void * arg){struct my_struct *p = (struct my_struct *)arg;int ret;sleep(2);ret = sem_wait(&p->sem);if(ret != 0){int eno = errno;perror("sem_wait");switch(eno){case EINTR:DEBUG_INFO("EINTR");break;case EINVAL:DEBUG_INFO("EINVAL");break;case EAGAIN:DEBUG_INFO("EAGAIN");break;// case EINVAL:// DEBUG_INFO("EINTR");// break;case ETIMEDOUT:DEBUG_INFO("ETIMEDOUT");break;default:break;}}else{DEBUG_INFO("sem_wait ok");} p->run = 0;return NULL;
}
void * my_func2(void * arg){int value;struct my_struct *p = (struct my_struct *)arg;sleep(1);int ret;ret = sem_post(&p->sem);sem_getvalue(&p->sem,&value);DEBUG_INFO("ret = %d,value = %d",ret,value);ret = sem_post(&p->sem);sem_getvalue(&p->sem,&value);DEBUG_INFO("ret = %dvalue = %d",ret,value);return NULL;
}
int main(int argc, char *argv[]){pthread_t p1;pthread_t p2;int value;struct my_struct *p = malloc(sizeof(struct my_struct));int ret;if(p == NULL){return NULL;}sem_init(&p->sem,0,0);p->run = 1;ret = pthread_create(&p1,NULL,my_func1,p);if(ret < 0){perror("pthread_create");DEBUG_INFO("pthread_create");return -1;}pthread_detach(p1);ret = pthread_create(&p2,NULL,my_func2,p);if(ret < 0){perror("pthread_create");DEBUG_INFO("pthread_create");return -1;}pthread_detach(p2);while(p->run){sleep(1);}ret = sem_getvalue(&p->sem,&value);DEBUG_INFO("ret = %d,value = %d",ret,value);DEBUG_INFO("bye bye");return 0;
}
三 CMakeLists.txt
project(libevent_project)
cmake_minimum_required(VERSION 3.8)
message(STATUS "lkmao:CMAKE_SOURCE_DIR -- ${CMAKE_SOURCE_DIR}")SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread -g -Wall")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -lstdc++")# add_executable(app epoll.c)
# add_executable(app epoll2.c)
add_executable(app sem.c)
四 编译脚本:
#!/bin/bash
set -e
rm -rf _build_
mkdir _build_ -p
cmake -S ./ -B _build_
make -C _build_
# ./_build_/main_01
# ./_build_/app
echo ""
file ./_build_/app
echo ""
./_build_/app hello.txt
五 测试结果
小结
这篇关于IMX6:semaphore测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!