经过研究只写出了用户态的代码,内核态没有什么头绪,如果有同学会内核态的写法,可以给我留言,我请吃饭。。。
//=================================================================================
在abort()函数中,使用pthread_cancel()函数向线程发送中止信号,当然对于这个信号线程可以选择对它如何响应,在这次实验中需要线程收到信号后立即退出,所以在memtest()线程函数中,设置线程参数pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL)与pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL)分别代表了设置该线程收到CANCEL后置为CANCED
//=================================================================================
科普一下
生成随机数据要用到srand();这个函数是为以后的rand()提供一个种子,要真正的产生随机数需要每次的种子都不一样,用时间就可以了。
posix threadapi的应用推荐大家看这里讲得很清楚http://blog.csdn.net/wangyi_lin/article/details/7066730
#define __LIBRARY__ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <pthread.h> #include <time.h> #define MAX 255 struct my_test { pthread_t p_id; int p_status; int p_progress; }vector[MAX]; char testdata[5]={0,0xff,0x55,0xAA,0}; unsigned int times_number=5; unsigned int thread_number=2; char order[MAX]; int number; char* memory; void * memtest(void *i){ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL); vector[(int)i].p_status=1; const int size = 0x100000 / thread_number; char* from=memory + ((int )i)*size; int j,k,p; for(j=0;j<size;j++){ for(k=0;k<5;k++){ for(p=0;p!=times_number;p++){ *from = testdata[k]; if((*from) != testdata[k]){ printf("memory error!"); vector[(int)i].p_status=-2; } } } vector[(int)i].p_progress=from-memory; from++; }; vector[(int)i].p_status=-1; pthread_exit((void*)0); } void times(){ scanf("%d",&number); times_number=number; } void thread(){ scanf("%d",&number); thread_number=number; } void go(){ int i; const int size = 0x100000 / thread_number; for(i=0;i!=thread_number;i++){ switch(pthread_create(&vector[i].p_id,NULL,memtest,(void *)i)){ case 0: printf("线程:%X 已创建\n",(int)vector[i].p_id);break; case EAGAIN: printf("表示系统缺乏足够的资源来创建线程\n");break; case EINVAL:printf("attr结构中的属性值非法\n");break; } } } void status(){ int i; for(i=0;i<thread_number;i++){ if(vector[i].p_status==1){ printf("线程:%X 正在运行,测试到%06X\n",(int)vector[i].p_id,vector[i].p_progress); } if(vector[i].p_status==-1){ printf("线程:%X 已经退出\n",(int)vector[i].p_id); } if(vector[i].p_status==-2){ printf("线程: %X 检测出错误,已退出\n",(int)vector[i].p_id); } } } void aborts(){ int i=0; while(vector[i].p_status!=0){ vector[i].p_status=-1; if(pthread_cancel(vector[i].p_id)==0){ printf("线程:%X 已被中止\n",(int)vector[i].p_id); } else{ printf("线程:%X 中止出错\n",(int)vector[i].p_id); } i++; } } void exits(){ int i; for(i=0;i<thread_number;i++){ if (!vector[i].p_id){ switch(pthread_kill(vector[i].p_id,0)){ case ESRCH: printf("线程:%X 在主进程退出时 已退出\n",(int)vector[i].p_id);break; case EINVAL: printf("错误线程标识\n");break; } } } exit(0); } int main(){ memory= (char *)malloc(0x100000); srand(time(NULL)); testdata[4]=rand(); printf("\nTest start!\n"); printf("---------------------\n"); printf("times x:set test number\n"); printf("thread x:set thread number\n"); printf("go:do test \n"); printf("status:show test status \n"); printf("abort:stop test thread \n"); printf("exit:stop main thread \n"); printf("---------------------\n"); for(;;){ printf(">>>"); scanf("%s",order); if(!strcmp(order,"times")) times(); else if(!strcmp(order,"thread")) thread(); else if(!strcmp(order,"go")) go(); else if(!strcmp(order,"status")) status(); else if(!strcmp(order,"abort")) aborts(); else if(!strcmp(order,"exit")) exits(); else { printf("wrong order!"); } } }