zoukankan      html  css  js  c++  java
  • 62.多线程多文件检索,每个线程检索一个文件,用于内存不够的情况下

      1 #define _CRT_SECURE_NO_WARNINGS
      2 #include<stdio.h>
      3 #include<stdlib.h>
      4 #include <string.h>
      5 #include <memory.h>
      6 #include <process.h>
      7 #include <Windows.h>
      8 //多线程多文件,实现线程的调度
      9 //释放内存
     10 //内存不够的情况,排队完成任务
     11 
     12 //创建多线程结构体
     13 struct infos
     14 {
     15     //路径
     16     char path[256];
     17     //线程id
     18     int id;
     19     //把文件的内容载入
     20     char **g_pp;
     21     //文件的长度
     22     int length;
     23     //要查询的数据
     24     char findstr[100];
     25 }myinfo[22] = {0};//22个结构体保存了22个文件的信息
     26 HANDLE inithd[22] = {0};//22个初始化线程地址
     27 HANDLE findhd[22] = { 0 };//22个查找线程地址
     28 
     29 //根据路径初始化线程地址,把数据写入到infos结构体中
     30 void runthreadinit(void *p)
     31 {
     32     struct infos *pinfo = p;
     33     FILE *pf = fopen(pinfo->path, "r");
     34     if (pf!=NULL)
     35     {
     36         //测试多少行
     37         int i = 0;
     38         while (!feof(pf))
     39         {
     40             char str[256] = { 0 };
     41             fgets(str, 256, pf);//读取
     42             i++;
     43         }
     44         //i记录行数
     45         rewind(pf);//回到开头 fseek(pf,0,SEEK_SET);
     46         pinfo->g_pp = calloc(i, sizeof(char*));//分配内存初始化
     47         pinfo->length = i;//记录长度
     48 
     49         //读取并把数据拷贝到g_pp[i]中
     50         for (int j = 0; j < i;j++)
     51         {
     52             char str[256] = { 0 };
     53             //读取
     54             fgets(str, 256, pf);
     55             //获取长度
     56             int length = strlen(str);
     57             //分配内存
     58             pinfo->g_pp[j] = calloc(length + 1, sizeof(char));
     59             //拷贝到**g_pp中
     60             if (pinfo->g_pp[j]!=NULL)
     61             {
     62                 strcpy(pinfo->g_pp[j], str);
     63             }
     64         }
     65     } 
     66     //关闭文件
     67     fclose(pf);
     68     printf("线程%d init  over
    ", pinfo->id);
     69 }
     70 
     71 //调用线程函数
     72 void runthreadsearch(void *p)
     73 {
     74     //指针类型转换
     75     struct infos *pinfo = p;
     76     //每个线程都进行相应的检索
     77     for (int i = 0; i < pinfo->length;i++)
     78     {
     79         if (pinfo->g_pp[i]!=NULL)
     80         {
     81             char *px = strstr(pinfo->g_pp[i], pinfo->findstr);
     82             if (px!=NULL)
     83             {
     84                 printf("
    %s", pinfo->g_pp[i]);
     85             }
     86         }
     87     }
     88 
     89     printf("线程%d find over
    ", pinfo->id);
     90 }
     91 
     92 //释放内存
     93 void freeall(struct infos *pinfo)
     94 {
     95     printf("freeall   start");
     96     //释放指针数组每一个指针对于的内存
     97     for (int i = 0; i < pinfo->length;i++)
     98     {
     99         free(pinfo->g_pp[i]);
    100     }
    101     free(pinfo->g_pp);//释放g_pp
    102 
    103     printf("freeall   end;");
    104 
    105 }
    106 
    107 void main()
    108 {
    109     //给线程结构体初始化地址和要查询的字符串
    110     for (int i = 0; i < 15;i++)
    111     {
    112         myinfo[i].id = i+1;
    113         sprintf(myinfo[i].path, "dangdangwang%d.txt", i + 1);
    114         strcpy(myinfo[i].findstr, "小王");
    115     }
    116 
    117     //根据地址初始化结构体
    118     for (int i = 0; i < 15;i++)
    119     {
    120          inithd[i] = _beginthread(runthreadinit, 0, &myinfo[i]);
    121     }
    122     //等待
    123     WaitForMultipleObjects(15, inithd, TRUE, INFINITE);
    124     system("pause");
    125 
    126     //执行查找线程
    127     for (int i = 0; i < 15;i++)
    128     {
    129         findhd[i] = _beginthread(runthreadsearch, 0, &myinfo[i]);
    130     }    
    131     //等待
    132     WaitForMultipleObjects(15, findhd, TRUE, INFINITE);
    133     system("pause");
    134 
    135     //释放所有内存
    136     printf("开始释放");
    137     for (int i = 0; i < 15;i++)
    138     {
    139         freeall(&myinfo[i]);
    140     }
    141     printf("结束释放");
    142 
    143     system("pause");
    144 }
  • 相关阅读:
    抽象工厂模式
    工厂模式
    简单工厂模式
    查询功能测试的测试点汇总
    网卡设置 网卡的高级设置说明
    30道python真实面试题(搜集到的,看看其实都是基础)
    测试用例设计方法--正交试验法
    小游戏:乌龟吃鱼
    Tkinter 的三大布局管理器 pack、grid 和 place用法汇总
    Python中输出字体的颜色设置
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8437232.html
Copyright © 2011-2022 走看看