IMX6:semaphore测试

2024-01-25 14:28
文章标签 测试 semaphore imx6

本文主要是介绍IMX6:semaphore测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一 相关函数

1 初始化函数sem_init

NAME
       sem_init - initialize an unnamed semaphore

SYNOPSIS
       #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 semaphore

SYNOPSIS
       #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 semaphore

SYNOPSIS
       #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 semaphore

SYNOPSIS
       #include <semaphore.h>

       int sem_getvalue(sem_t *sem, int *sval);

       Link with -pthread.

5 销毁一个信号量

NAME
       sem_destroy - destroy an unnamed semaphore

SYNOPSIS
       #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测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/643551

相关文章

将一维机械振动信号构造为训练集和测试集(Python)

从如下链接中下载轴承数据集。 https://www.sciencedirect.com/science/article/pii/S2352340918314124 import numpy as npimport scipy.io as sioimport matplotlib.pyplot as pltimport statistics as statsimport pandas

编译测试后出现“发现不明确的匹配”错误

原文链接:http://blog.163.com/zhaoyanping_1125/blog/static/201329153201204218533/ 错误提示: 【“/”应用程序中的服务器错误。  分析器错误 说明: 在分析向此请求提供服务所需资源时出错。请检查下列特定分析错误详细信息并适当地修改源文件。  分析器错误信息: 发现不明确的匹配。】   这个问题发生原因一般情况是

RODNet安装测试

项⽬地址: GitHub - yizhou-wang/RODNet: RODNet: Radar object detection network 搭建环境并配置RODNet 1. 参考README.md搭建并配置环境 准备数据集 1. 本实验使⽤ ROD2021 dataset. 百度⽹盘链接:百度网盘 请输入提取码 密码:slxy 2. 使⽤这个script来重新组织文件。 具体形

Mockito测试

Mockito 一 mockito基本概念 Mock测试是单元测试的重要方法之一,而Mockito作为一个流行的Mock框架,简单易学,且有非常简洁的API,测试代码的可读性很高。 Mock测试就是在测试过程中,对于一些不容易构造(如HttpServletRequest必须在Servlet容器中才能构造出来)或者说获取比较复杂的对象(如JDBC中的ResultSet对象)

jmeter测试https请求

公司最近在搞全站HTTPS改造,进一步提高网站的安全性,防止运营商劫持。那么,改造完成后,所有前后端的URL将全部为https。 So ,研究下怎么用Jmeter访问https请求呢。 其实很简单, 第一步在jmeter中创建HTTP请求,如下图进行配置,https端口为443; 第二步,在本机浏览器,如Chrome中导入该域名证书,在更多工具-设置-管理证书的地方,找到该证书,导出到本地。然后在

pytest测试框架flaky插件重试失败用例

Pytest提供了丰富的插件来扩展其功能,本章介绍下插件flaky ,用于在测试用例失败时自动重新运行这些测试用例。与前面文章介绍的插件pytest-rerunfailures功能有些类似,但是功能上不如pytest-rerunfailures插件丰富。 flaky官方并没有明确python和pytest版本限制。 flaky安装 使用pip命令安装: pip install flaky

Selenium进行Web自动化测试

Selenium进行Web自动化测试 Selenium+Python实现Web自动化测试一、环境配置 Selenium+Python实现Web自动化测试 一、环境配置 环境基于win10(X64) 安装Python;安装PyCham安装chomedriver chomedriver下载地址 可以查看本地chrome软件版本下载对应的chomedriver,如果没有则下载最新

pytorch国内镜像源安装及测试

一、安装命令:  pip install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple  二、测试: import torchx = torch.rand(5, 3)print(x)

测试测量-DMM直流精度

测试测量-DMM直流精度 最近去面试,发现了自己许多不足,比如我从未考虑过万用表准或者不准,或者万用表有多准? 在过去的实验室中,常用的DMM有KEYSIGHT 34401A以及 KEITHLEY THD2015,就以这两台为例,我们去看看他们能测试的边界在哪里? 图1展示了34401A的测试精度说明,图2展示了THD2016的精度说明 图1:34401A 精度说明 图2:THD

JMeter对博客园进行性能测试

原文转自:http://www.cnblogs.com/yjlch1016/p/8320546.html 现在有这个一个场景: 普通用户在未登录的状态下, 先打开博客园的网站, 然后搜索JMeter的相关文章; 那么我们要对博客园进行性能测试, 分别模拟在100个、200个和300个并发的情况下, 博客园服务器的性能怎么样; 需要注意的是, 本次的场景是: 用户第一步同时打开博