创建独立线程来输出小于或等于用户输入数的所有素数

本文介绍了一个在Linux环境下利用独立线程实现的程序,该程序能够找出所有小于或等于用户指定数值的素数,并输出这些素数。通过使用pthread库创建线程,程序实现了高效的素数筛选。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在linux下 创建独立线程来输出小于或等于用户输入数的所有素数

#include <pthread.h>
#include <stdio.h>


#define MAX_SIZE 256
int primes[MAX_SIZE];
void *runner(void *param);
int main(int argc, char *argv[])
{
  int i;
  pthread_t tid;
  pthread_attr_t attr;

  if (argc != 2) {
    fprintf(stderr,"usage: a.out <integer value>\n");
    return -1;
  }

  if (atoi(argv[1]) < 2) {
    fprintf(stderr,"Argument %d must be >= 2 \n",atoi(argv[1]));
    return -1;
  }

 
  pthread_attr_init(&attr);
 
  pthread_create(&tid,&attr,runner,argv[1]);
  pthread_join(tid,NULL);
  for (i = 1; i <= atoi(argv[1]); i++)
    if (primes[i] > 0)
     printf("%d\n", i);
}

void *runner(void *param)
{
  int i, j;
  int upper = atoi(param);
 primes[1] = 0;
 for (i = 2; i <= upper; i++)
  primes[i] = 1;
 for (i = 2; i <= upper; i++)
  if(primes[i])
  {
   for (j = i; j*i <= upper; j++)
   {
    
    primes[i*j] = 0;
   }
  }
 pthread_exit(0);
}

 

/* Copyright(c) 2025 Shenzhen TP-LINK Technologies Co.Ltd. * * file main.c * brief This is the samples * * author Panj Jianhui * version 1.0.0 * date 25Jul31 * * history */ #include <stdio.h> #include <pthread.h> // 线程支持 #include <string.h> // 字符串操作 #include <stdlib.h> // 标准库函 #include "min_heap.h" // 常量定义 #define MAX_NUMBERS 1000 // 最大字存储量 #define HASH_SIZE 10009 // 质减少哈希码冲突 // 函声明 int is_number(char *str); // 检查字符串是否为纯字 int numbers[MAX_NUMBERS]; // 存储字的组 // 线程处理函声明 int process_number(); // 字处理线程 int process_word(); // 单词处理线程 unsigned int word_hash(const char *str); // 哈希函 // 单词条目结构 typedef struct { char *word; // 单词指针 int used; // 使用标记(0=未用,1=已用) } WordEntry; // 主函 int main() { int res = 0; pthread_t t_num, t_word; // 线程标识符 // 创建字处理线程 res = pthread_create(&t_num, NULL, (void *)process_number, NULL); if (res != 0) { perror("线程创建失败!"); exit(-1); } // 创建单词处理线程 res = pthread_create(&t_word, NULL, (void *)process_word, NULL); if (res != 0) { perror("线程创建失败!"); exit(-1); } // 等待线程结束 pthread_join(t_num, NULL); pthread_join(t_word, NULL); return 0; } // 检查字符串是否为纯字 int is_number(char *str) { int ret = 0; for (size_t i = 0; i < strlen(str); i++) { // 混合字符串检测(实际逻辑需优化) if ((i > 0) && (str[i] - '0' >= 0 && str[i] - '0' <= 9) && (ret == 0)) { perror("错误!文本中存在混合字符串!"); exit(-1); } else if ((i > 0) && !(str[i] - '0' >= 0 && str[i] - '0' <= 9) && (ret == 1)) { perror("错误!文本中存在混合字符串!"); exit(-1); } if (str[i] >= '0' && str[i] <= '9') { // 当前字符是字 ret = 1; // 标记为字 } else { ret = 0; // 标记为非字 } } return ret; // 返回检测结果 } // FNV-1a哈希函实现 unsigned int word_hash(const char *str) { unsigned int hash = 2166136261u; // FNV偏移基础值 while (*str) { hash ^= (unsigned char)*str++; // 异当前字符 hash *= 16777619; // FNV质 } return hash % HASH_SIZE; // 返回哈希表范围内的值 } // 字处理线程 int process_number() { min_heap *my_heap = create_min_heap(MAX_NUMBERS); char buff[32] = {0}; // 读取缓冲区 FILE *f_text = fopen("text.txt", "r"); // 输入文件 if (!f_text) { perror("无法打开text.txt!"); exit(-1); } FILE *f_numbers = fopen("numbers.txt", "w"); // 输出文件 if (!f_numbers) { perror("无法打开numbers.txt!"); exit(-1); } // 读取并处理文本 while (fscanf(f_text, "%31s", buff) == 1) { if (is_number(buff)) { // 如果是字 int num_tmp = atoi(buff); // 转换为整 if (my_heap->current_size < 1000 || num_tmp > extract_min(my_heap)) { insert_element(my_heap, num_tmp); // 插入最小堆 } } memset(buff, 0, 32); // 清空缓冲区 } // 将字写入文件 for (size_t i = 0; i < my_heap->current_size; i++) { fprintf(f_numbers, "%d ", my_heap->heap_data[i]); } free_min_heap(my_heap); // 关闭文件 fclose(f_text); fclose(f_numbers); return 0; } // 单词处理线程 int process_word() { FILE *f_text = fopen("text.txt", "r"); // 输入文件 if (!f_text) { perror("无法打开text.txt!"); exit(-1); } FILE *f_words = fopen("words.txt", "w+"); // 输出文件 if (!f_words) { perror("无法打开words.txt!"); exit(-1); } char buff_text[32] = {0}; // 读取缓冲区 WordEntry words[HASH_SIZE]; for (int i = 0; i < HASH_SIZE; i++) { words[i].used = 0; } // 读取并处理文本 while (fscanf(f_text, "%31s", buff_text) == 1) { if (is_number(buff_text)) continue; // 跳过字 // 计算哈希值 unsigned int hash = word_hash(buff_text); int isexsit = 0; // 处理哈希冲突(线性探测) while (words[hash].used) { if (strcmp(buff_text, words[hash].word) == 0) { isexsit = 1; // 单词已存在 break; } hash = (hash + 1) % HASH_SIZE; // 探测下一个位置 } if (isexsit) continue; // 跳过重复单词 // 存储新单词(未分配终止符空间) words[hash].word = malloc(sizeof(buff_text)); // 应+1给'\0' memcpy(words[hash].word, buff_text, sizeof(buff_text)); words[hash].used = 1; // 写入单词文件 fprintf(f_words, "%s ", buff_text); memset(buff_text, 0, 32); } // 释放单词内存 for (size_t i = 0; i < HASH_SIZE; i++) { if (words[i].used) { free(words[i].word); } } // 关闭文件 fclose(f_text); fclose(f_words); return 0; } 技术要点分析
最新发布
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hebastast

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值