ANSI C中带有PTHREADS的执行器池
我正在用ANSIC(1972)编写一个程序,我必须使用固定数量的线程。我基本上是在读一个大文件,里面有纬度和经度数据的记录,比如ANSI C中带有PTHREADS的执行器池,c,multithreading,visual-c++,pthreads,C,Multithreading,Visual C++,Pthreads,我正在用ANSIC(1972)编写一个程序,我必须使用固定数量的线程。我基本上是在读一个大文件,里面有纬度和经度数据的记录,比如.csv,我必须对它们进行处理。问题是我不能在一个100.000.000行的文件上等待2周,我需要使用线程或分叉 基本上我是这样读.txt文件的 FILE *file2 = fopen ( lat_long_file, "r" ); if (file2 != NULL) { char line2 [128]; while (fgets(line2, s
.csv
,我必须对它们进行处理。问题是我不能在一个100.000.000行的文件上等待2周,我需要使用线程
或分叉
基本上我是这样读.txt
文件的
FILE *file2 = fopen ( lat_long_file, "r" );
if (file2 != NULL)
{
char line2 [128];
while (fgets(line2, sizeof line2, file2) != NULL)
{
//fputs(line2, stdout);
char *this_record = trimqq(line2);
// .....
// ..... STUFF TO DO (here i must send data to thread function like in JAVA)
// Thread temp_thread = new Thread(new ThreadClass(arguments ....));
// temp_thread.start(); <- this is how i would do if i was programming in JAVA
// .....
}
}
我将获得此id的输出
Sleeping in thread
Sleeping in thread
Thread with id 5 ...exiting
Sleeping in thread
Thread with id 0 ...exiting
Sleeping in thread
Sleeping in thread
Sleeping in thread
Thread with id 9 ...exiting
Thread with id 1 ...exiting
Sleeping in thread
Sleeping in thread
Thread with id 7 ...exiting
Thread with id 3 ...exiting
Thread with id 2 ...exiting
Thread with id 6 ...exiting
Sleeping in thread
Thread with id 4 ...exiting
Sleeping in thread
Thread with id 8 ...exiting
Main: completed thread id : 0 exiting with status : (nil)
Main: completed thread id : 1 exiting with status : (nil)
Main: completed thread id : 2 exiting with status : (nil)
Main: completed thread id : 3 exiting with status : (nil)
Main: completed thread id : 4 exiting with status : (nil)
Main: completed thread id : 5 exiting with status : (nil)
Main: completed thread id : 6 exiting with status : (nil)
Main: completed thread id : 7 exiting with status : (nil)
Main: completed thread id : 8 exiting with status : (nil)
Main: completed thread id : 9 exiting with status : (nil)
Main: program exiting.
执行时间为3秒
如果我更改系统(“sleep3;date;”)代码>到系统(“睡眠10;日期;”)代码>,执行时间将为10秒,而我希望每次调用void*wait(void*t)
函数时都能睡着
main_2_fork(我也试过fork,但没用)
而且执行不是平行的。。。相反,它是一个接一个地执行,尽管我认为fork()
函数正在ps ax | grep'fork2.exe'
中创建子进程
下面是一个我想要的例子:
假设5是一次最多的线程数
问题
为什么void*wait(void*t)
函数睡眠不正常?为什么pthread
一个接一个地执行它们,而不是并行执行
在C
中,如何使线程池具有固定的最大线程数
非常感谢。我还不能发表评论,所以我会在这里回复。您的线程示例正好占用一个线程(wait()函数)的睡眠时间。也就是说,如果你这样写的话会更清楚:
void *some_running_task(void *t)
{
int i;
long tid = (long)t;
printf("Sleeping in thread #%lu ...\n", tid);
system("sleep 3; date;");
printf("Thread with #%lu ... exiting\n", tid);
pthread_exit(NULL);
}
正如@fuzzxl所说,标准线程库中有一个wait(),所以您不应该使用它
所有线程都是在同一瞬间启动的,可能只有几十微秒。它们都在同一时刻开始,因此都在3秒钟后结束。将睡眠指令更改为10秒,程序将持续10秒
您可能需要的是一个线程池,该线程池在整个工作完成之前始终保持相同数量的线程处于忙碌状态:只要有数据要处理,就触发一个线程,直到达到最大池数。不过,同步线程池容易出现死锁。您还可以让每个线程处理文件中自己的部分。。。除非你想要的是把一条线放在一条线上
我在这里看到的并行性的一个问题是顺序。如果您关心序列顺序,那么线程不一定会以与您读取行相同的顺序生成数据。因此,除非将处理后的数据与行号一起放入数据库中,否则将丢失序列顺序
我看到的另一个问题是输出处理过的数据。它需要适当的同步,以避免一个线程的输出干扰另一个线程的输出(当然,iif线程应该打印出它们的数据)
我有点不清楚,除了加快全局处理时间之外,您对并行性的期望是什么。如果你想让一堆线程处理一堆行,你无论如何都会想出一些类似的方法,就像拆分源数据文件一样简单。。。当然,如果能做到的话。但至少您可以在读取每一行时控制数据的顺序,然后您可以重新启动长时间运行的单线程进程,而不是长时间运行的多线程应用程序。单线程应用程序比多线程应用程序更容易编程
C语言的使用是否也是强制性的,比如说,Python或其他语言都不能使用?最大的优点是省去了线程同步的麻烦
无论如何,加速线性数据处理的方法不止一种。例如,可以使用UNIXsed
将一定数量的行传输到处理应用程序。根据需要运行尽可能多的sed |
。或者,您可以通过管道将数据文件的部分拆分到用C或Python编写的处理应用程序中
只是给出标题。没有1972 ANSI C。你是说1989年标准化的C89吗?我不确定,我是一个年轻的程序员,我能告诉你的是我做了一个start.sh
,其中包含以下内容:clear;rmpthreads.exe;gcc-m64-o pthreads.exe pthreads.c-lpthread/pthreads.exe$1$2;回音“\n”;ls-al
你在使用Cygwin吗?你确实意识到Linux上的程序没有.exe
后缀?@fuzzxl->是的,我喜欢在Linux中使用.exe
前缀,即使它不是必需的@Kerrek SB->我不受i/O的限制,我只是想在同一时间进行多个操作,我在JAVA
中做了这件事,工作非常完美,我可以放置10个、100个、1000个固定线程,我想要多少个,但在C
中我不知道如何操作首先,非常感谢您的回答,我非常感谢1
->触发一个线程,直到达到最大池数,只要有数据要处理
是的,这正是我所需要的,基本上函数void*一些正在运行的任务(void*t)
运行一个卷曲和打印(“http://somesite.com/test.php");代码>带有post参数,它必须类似于后台,我一次最多需要10个后台函数,不再需要了<代码>2->需要适当的同步
一旦线程开始或结束,printf
状态或write()
在同一内存区域3->使用Python或Cython?
相信我,如果由我决定,我会使用JAVA
,但项目必须在C
中完成。。。。,4->
sed |
你有教程还是什么的?我只在字符串解析或转换中使用sed…5->
这是我得到的输出:这是我想要的输出,我想你最好使用一些shell脚本,而不是普通的C。看看。希望它能给你一个起点。信息技术
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#define MAX_COUNT 200
#define BUF_SIZE 100
int random_number(int min_num, int max_num);
void main(void)
{
int numforks = 0;
int maxf = 5;
int status;
char buf[BUF_SIZE];
pid_t PID;
int job = 0;
for(job; job <= 10; job++)
{
// fork() = make a copy of this program from this line to the bottom
PID = fork();
int fork_id = random_number(1000000,9999999);
if (PID < 0)
{
// if -1 then couldn't fork ....
fprintf(stderr, "[!] Couldn't fork!\n");
exit(1);
}
if (( PID == 0 ))
{
// 0 = has created a child process
exit(0);
}
else
{
// means that PID is 1 2 3 .... 30000 44534 534634 .... whatever
// increment the fork count
numforks++;
sprintf(buf, "FORK[#%d] BEGIN pid=%d num_forks=%d\n",fork_id,PID,numforks);
write(1, buf, strlen(buf));
// sleep(random_number(1,2));
char str[300];
sprintf(str,"sleep %d; ps ax | wc -l",random_number(1,4));
puts(str);
// OUTPUT COMMAND BEGIN
FILE *command_execute = popen(str, "r");
char buf[256];
int increment = 0;
while (fgets(buf, sizeof(buf), command_execute) != 0)
{
printf("LINE[%d]:%s",increment,buf);
increment++;
break;
}
pclose(command_execute);
// OUTPUT COMMAND END
// block to not do extra forks
if (numforks > maxf)
{
for (numforks; numforks > maxf; numforks--)
{
PID = wait(&status);
}
}
sprintf(buf, "FORK[#%d] END pid=%d num_forks=%d\n",fork_id,PID,numforks);
write(1, buf, strlen(buf));
}
// sleep(1);
}
}
int random_number(int min_num, int max_num)
{
int result=0,low_num=0,hi_num=0;
if(min_num<max_num)
{
low_num=min_num;
hi_num=max_num+1; // this is done to include max_num in output.
}
else
{
low_num=max_num+1;// this is done to include max_num in output.
hi_num=min_num;
}
srand(time(NULL));
result = (rand()%(hi_num-low_num))+low_num;
return result;
}
FORK[#7495656] BEGIN pid=29291 num_forks=1
sleep 1; ps ax | wc -l
LINE[0]:312
FORK[#7495656] END pid=29291 num_forks=1
FORK[#9071759] BEGIN pid=29296 num_forks=2
sleep 4; ps ax | wc -l
LINE[0]:319
FORK[#9071759] END pid=29296 num_forks=2
FORK[#2236079] BEGIN pid=29330 num_forks=3
sleep 4; ps ax | wc -l
......
void *some_running_task(void *t)
{
int i;
long tid = (long)t;
printf("Sleeping in thread #%lu ...\n", tid);
system("sleep 3; date;");
printf("Thread with #%lu ... exiting\n", tid);
pthread_exit(NULL);
}