当前位置: 首页 > wzjs >正文

德州做网站公司电话广州官方新闻

德州做网站公司电话,广州官方新闻,网站建设的市场策划,上海做兼职哪个网站目录 tasks.h type.h main.c tasks.c makefile 运行结果 调用多线程框架&#xff0c;在主函数写好环境监控文件的函数&#xff0c;使用结构体封装环境指标的参数 最后使用makefile管理工程文件 tasks.h #include<pthread.h>#ifndef __TASK_H__ #define __TASK_H_…

目录

tasks.h

type.h

main.c

tasks.c

makefile

运行结果


调用多线程框架,在主函数写好环境监控文件的函数,使用结构体封装环境指标的参数

最后使用makefile管理工程文件

tasks.h

#include<pthread.h>#ifndef __TASK_H__
#define __TASK_H__typedef void* (*Thread_fun_t) (void *);//函数指针类型,指向返回值为void*,参数为void*的函数
typedef struct task
{pthread_t tid;//线程的tidThread_fun_t pfun;//线程的函数}Task_t;extern int create_pthread_tasks(Task_t tasks[],int len);
extern void destroy_pthread_tasks(Task_t tasks[], int len);
#endif

type.h

#include<time.h>#ifndef __TYPE_H__
#define __TYPE_H__typedef struct env{  //用结构体封装环境指标参数int devid;//设备idfloat tmp;//温度float hum;//湿度float oxy;//氧气浓度struct tm tim;//时间结构体
}Env_data_t;#endif

main.c

#include<stdio.h>
#include <pthread.h>
#include<unistd.h>
#include<time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include"tasks.h"
#include"type.h"Env_data_t data_g;
void* get_data(void *arg)
{time_t sec;int cnt = 0;while(1){sec = time(NULL);struct tm * ptm = localtime(&sec);data_g.devid = cnt++;data_g.hum = 26.7;data_g.oxy = 66.6;data_g.tim = *ptm;data_g.tmp = 36.8;sleep(1);}
}
void* show_data(void *arg)
{while(1){printf("[%4d %2d-%2d %2d:%2d:%2d] 温度:%f, 湿度:%f, 氧气浓度:%f\n",data_g.tim.tm_year+1900, data_g.tim.tm_mon+1, data_g.tim.tm_mday,data_g.tim.tm_hour, data_g.tim.tm_min, data_g.tim.tm_sec,data_g.tmp,data_g.hum,data_g.oxy);sleep(1);}return NULL;
}
void* storage_data(void *arg)
{mkdir("../data",0777);FILE *fp = fopen("../data/data.txt", "a");if(NULL == fp){perror("fail open data.txt!");return NULL;}while(1){fprintf(fp, "[%4d %2d-%2d %2d:%2d:%2d] 温度:%f, 湿度:%f, 氧气浓度:%f\n",data_g.tim.tm_year+1900, data_g.tim.tm_mon+1, data_g.tim.tm_mday,data_g.tim.tm_hour, data_g.tim.tm_min, data_g.tim.tm_sec,data_g.tmp,data_g.hum,data_g.oxy);fflush(fp);sleep(1);}
}
void* send_data(void *arg)
{while(1){//网络通信sleep(1);}
}int main(int argc, char const *argv[])
{Task_t tasks[] = {{.pfun = get_data,},{.pfun = show_data,},{.pfun = storage_data,},{.pfun = send_data,},};create_pthread_tasks(tasks, sizeof(tasks) / sizeof(tasks[0]));destroy_pthread_tasks(tasks, sizeof(tasks) / sizeof(tasks[0]));return 0;
}

tasks.c

#include"tasks.h"
#include<pthread.h>
#include<stdio.h>
int create_pthread_tasks(Task_t tasks[],int len)
{int i;for(i = 0;i < len;++i){int ret = pthread_create(&(tasks[i].tid),NULL,tasks[i].pfun,NULL);if(ret != 0){printf("fail to pthread!\n");return -1;}}return 0;
}void destroy_pthread_tasks(Task_t tasks[], int len)
{int i;for(i = 0;i < len;++i){pthread_join(tasks[i].tid,NULL);}
}

makefile

DST=app
SRC=main.c tasks.c
CC=gcc
FLAGS=-lpthread
INC=../include$(DST):$(SRC)$(CC) $^ -o $@ $(FLAGS) -I$(INC)clean:rm $(DST)

运行结果

http://www.dtcms.com/wzjs/298476.html

相关文章:

  • 进入百度app查看360优化大师官方官网
  • 网站后台登录ip限制互联网服务平台
  • 宣传片制作公司前景seo外链软件
  • wordpress企业网站建设网站免费进入窗口软件有哪些
  • 怎么做新网站上线通稿石家庄网络关键词排名
  • 阿里巴巴网站域名注册新浪nba最新消息
  • 平面设计的范围北京seo网络优化招聘网
  • 北京成交型网站建设价格百度指数的主要功能有
  • 网站标题与关键词免费外链发布平台在线
  • 网站交互主要做什么的网店运营推广方案
  • wordpress获取菜单信息aso苹果关键词优化
  • 宝鸡手机网站开发广州推广服务
  • 个人婚礼网站模板一个网站推广
  • 福田做棋牌网站建设哪家好郑州网站seo优化
  • 北京建设制作网站淘宝指数官网
  • 做平台的网站友情链接出售
  • thinkphp 门户网站seo实战培训机构
  • 网站封面怎么做正规推广平台
  • 庭院设计效果图seo是一种利用搜索引擎
  • 网站做外部链接百度云盘登录入口
  • 西安本地十家做网站建设的公司业务推广方式有哪些
  • shafow网站是谁做的广告行业怎么找客户
  • wordpress社团网站软文网站推荐
  • 福建省政府seo优化易下拉排名
  • 合肥地区网站制作厦门人才网个人版
  • 大连最好的网站制作公司收录优美图片手机版
  • 网站建设预算费用子域名大全查询
  • 淄博网站建设电话咨询百度指数是什么意思
  • 西安网站建设公司排南昌seo推广
  • 寻找东莞微信网站建设广州最新疫情最新消息