linux多线程实现单链表linux,【链表多线程面试题】面试问题:linux 多… - 看准网...

linux则只允许单进程拥有一个定时器,因此在linux下的单进程中要使用多个定时器,则需要自己维护管理。

方法一:

进程中只有一个定时器,通过标志位,可以在不同时间执行不同的任务。

方法二:

Linux进程多个定时器  实现数据更新 数据上传

#include

#include

#include

#include "sqlite3.h"

#include "uhdt.h"

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include "read_line_conf.h"

#include "sqlite.h"

#include

#define TIME_WHEEL_SIZE 8

#define SN_CONF "./sn.conf"

#define _DEBUG_

#define TIME_WHEEL_SIZE 8

#define NetWorkCard "eth0"

typedef void (*func)(int data);

struct timer_node {

struct timer_node *next;

int rotation;

func proc;

int data;

};

struct timer_wheel {

struct timer_node *slot[TIME_WHEEL_SIZE];

int current;

};

struct timer_wheel timer = {{0}, 0};

void tick(int signo)

{

// 使用二级指针删进行单链表的删除

struct timer_node **cur = &timer.slot[timer.current];

while (*cur) {

struct timer_node *curr = *cur;

if (curr->rotation > 0) {

curr->rotation--;

cur = &curr->next;

} else {

curr->proc(curr->data);

*cur = curr->next;

free(curr);

}

}

timer.current = (timer.current + 1) % TIME_WHEEL_SIZE;

alarm(1);

}

void add_timer(int len, func action)

{

int pos = (len + timer.current) % TIME_WHEEL_SIZE;

struct timer_node *node = (struct timer_node *)malloc(sizeof(struct timer_node));

// 插入到对应格子的链表头部即可, O(1)复杂度

node->next = timer.slot[pos];

timer.slot[pos] = node;

node->rotation = len / TIME_WHEEL_SIZE;

node->data = 0;

node->proc = action;

}

int InformationCount=0;

int InformationUploadCount=0;

void do_Information(int data)

{

if(InformationCount%1==0&&InformationCount>0){

#ifdef _DEBUG_

printf("Information Information\n");

#endif

}

InformationCount++;

add_timer(5, do_Information);

}

// test case3: 5s循环定时器

void do_InformationUpload(int data)

{

if(InformationUploadCount%5==0&&InformationUploadCount>0){

#ifdef _DEBUG_

printf("InformationUpload InformationUpload\n");

#endif

}

InformationUploadCount++;

add_timer(3, do_InformationUpload);

}

// 线程Information 方法

void * Information(void *a){

printf("void * Information(void *a)\n");

//time begin

add_timer(0, do_Information);

// time end

while(1);

return NULL;

}

// 线程InformationUpload 方法

void * InformationUpload(void *b){

printf("void * InformationUpload(void *b)\n");

//time begin

add_timer(0, do_InformationUpload);

// time end

while(1);

return NULL;

}

/*********timer end*********/

void threadInit(){

pthread_t t0;

pthread_t t1;

// 创建线程Information

if(pthread_create(&t0, NULL, Information, NULL) == -1){

puts("fail to create pthread t0");

exit(1);

}

//InformationUpload

if(pthread_create(&t1, NULL, InformationUpload, NULL) == -1){

puts("fail to create pthread t1");

exit(1);

}

// 等待线程结束

void * result;

if(pthread_join(t0, &result) == -1){

puts("fail to recollect t0");

exit(1);

}

if(pthread_join(t1, &result) == -1){

puts("fail to recollect t1");

exit(1);

}

#ifdef _DEBUG_

printf("t0--------:%d\n",pthread_join(t0, &result));

printf("t1---------%d\n",pthread_join(t1, &result));

#endif

}

int main( void )

{

signal(SIGALRM, tick);

alarm(1); // 1s的周期心跳

threadInit();//thread init

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值