Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1826551
  • 博文数量: 286
  • 博客积分: 3713
  • 博客等级: 少校
  • 技术积分: 2275
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-11 09:47
个人简介

http://blog.chinaunix.net/uid/16979052.html

文章分类

全部博文(286)

文章存档

2018年(1)

2017年(16)

2016年(9)

2015年(17)

2014年(15)

2013年(112)

2012年(116)

分类: C/C++

2012-07-03 07:56:37

#include "new_dic.h"
#include
#include

int init_dic(dic_t *dic, int minlen, int maxlen, const char *charset)
{
if(dic == NULL)
return EXIT_FAILURE;
if((strlen(charset) >= STR_MAX_LEN) || (maxlen >= STR_MAX_LEN) || (minlen < 0) || (maxlen < minlen))
return -1;
bzero(dic, sizeof(dic_t));
strncpy(dic->CharSet, charset, STR_MAX_LEN);
dic->CharSetSize=strlen(charset);
dic->MaxPos = maxlen - 1;
int i,j;
for(i=0; i
dic->StrValue[i] = -1;
for(j=1; j
dic->StrValue[j] = 0;
if(minlen == 0)
dic->StrValue[0] = -2;
return EXIT_SUCCESS;
}
void free_dic(dic_t *dic)
{
}
inline int Add(dic_t *dic, int pos)
{
if (pos > dic->MaxPos)
{
if(dic->StrValue[0] == -2)
{
(dic->StrValue[0])++;
return 0;
}
return -1;
}
if (++(dic->StrValue)[pos]>(dic->CharSetSize-1))
{
dic->StrValue[pos]=0;
return Add(dic, ++pos);
}
return 0;
}

int getseq(dic_t *dic, char *str, u_int64_t *count)
{
int result = Add(dic, 0);
(*count)++;
int i;
for(i = dic->MaxPos; (i>=0) && (result == 0); i--)
{
if (dic->StrValue[i] != -1)
*str++ = *(dic->CharSet + (dic->StrValue)[i]);
}
*str=0;
return result;
}
#if 0
#include
#include
#include

#define IPCKEY 0x1234
#define IPCFILE "/tmp/shmdic-count.key" 
#define CHARSET "1234567890"
#define NTHREAD 1
void *test_thread(void *vparm)
{
key_t cntkey = ftok(IPCFILE, IPCKEY);
int cntid = shmget(cntkey, sizeof(u_int64_t), SHM_R|SHM_W);
u_int64_t *counter = (u_int64_t *)shmat(cntid, 0, 0);
dic_t hidic;
if(0 != init_dic(&hidic, 0, 1, CHARSET, 1))
{
printf("init dic error\n");
return NULL;
}
char histr[20];
while(0 == getseq(&hidic, histr, counter))
{
printf("[%s]\n", histr);
}
free_dic(&hidic);

shmdt(counter);
printf("cpu [%d] free!!\n", sched_getcpu());
return NULL;
}
#include
int main()
{
key_t cntkey = ftok(IPCFILE, IPCKEY);
int cntid = shmget(cntkey, sizeof(u_int64_t), IPC_CREAT|SHM_R|SHM_W);
if(cntid == -1)
{
printf("%s\n", strerror(errno));
}
u_int64_t *counter = (u_int64_t *)shmat(cntid, 0, 0);
if(counter == (void*)-1)
{
printf("counter shmat %s\n", strerror(errno));
}
*counter = 0;
long t0 = gettickcount();


int i;
for(i=0;i
{
if(fork()==0)
{
/*子进程*/
test_thread(NULL);
exit(0);
}
}

while(waitpid(-1, &i, WNOHANG)>=0)
{
sleep(2);
long t1 = t0;
t0 = gettickcount();
u_int64_t speed = ((*counter) * 1000)/((t0 - t1) ? (t0 - t1) : 1);
printf("speed = %jd\n", speed);
*counter = 0;
if(speed == 0)
break;
}
shmdt(counter);
shmctl(cntid, IPC_RMID, NULL);
printf("Exit\n");
return 0;
}
#endif

阅读(867) | 评论(0) | 转发(0) |
0

上一篇:new_dic.h

下一篇:report_hw.sh

给主人留下些什么吧!~~