实现了使用合并排序的具有1000000个节点的三个链表的多进程和多线程实现。我比较了已实现程序的实时性,但是多线程方法比较慢。为什么会这样?
process.c中的主要方法
/* Insert nodes */
Node* tmp = NULL;
int num;
for( int i = 0; i < MAX; i++ )
{
fscanf(fread,"%d",&num);
tmp = createNode(num , i );
insertNode( &list1.head, &list1.tail, tmp );
tmp = createNode(num , i );
insertNode( &list2.head, &list2.tail, tmp );
tmp = createNode(num , i );
insertNode( &list3.head, &list3.tail, tmp );
tmp = createNode(num , i );
}
free( tmp );
fclose(fread);
if ((t1 = times(&mytms)) == -1) {
perror("times 1");
exit(1);
}
pid1= fork();
if(pid1==0){
mergeSort( &list1.head );
file_output(&list1);
freeAll( list1.head );
exit(1);
}
pid2= fork();
if(pid2==0){
mergeSort( &list2.head );
file_output(&list2);
freeAll( list2.head );
exit(2);
}
pid3 = fork();
if(pid3==0){
mergeSort( &list3.head );
file_output(&list3);
freeAll( list3.head );
exit(3);
}
wait(&status);
wait(&status);
wait(&status);
if ((t2 = times(&mytms)) == -1) {
perror("times 2");
exit(1);
}
printf("Real time : %.5f sec\n", (double)(t2 - t1) / CLK_TCK);
printf("User time : %.5f sec\n", (double)mytms.tms_utime / CLK_TCK);
printf("System time : %.5f sec\n", (double)mytms.tms_stime / CLK_TCK);
结果实时:1.65
main in thread.c
/* Insert nodes */
Node* tmp = NULL;
int num;
for( int i = 0; i < MAX; i++ )
{
fscanf(fread,"%d",&num);
tmp = createNode(num , i );
insertNode( &list1.head, &list1.tail, tmp );
tmp = createNode(num , i );
insertNode( &list2.head, &list2.tail, tmp );
tmp = createNode(num , i );
insertNode( &list3.head, &list3.tail, tmp );
}
free( tmp );
fclose(fread);
if ((t1 = times(&mytms)) == -1) {
perror("times 1");
exit(1);
}
pthread_create( &t_id1, NULL, thread_func, &list1 );
pthread_create( &t_id2, NULL, thread_func, &list2 );
pthread_create( &t_id3, NULL, thread_func, &list3 );
pthread_join( t_id1, (void*)&status );
pthread_join( t_id2, (void*)&status );
pthread_join( t_id3, (void*)&status );
if ((t2 = times(&mytms)) == -1) {
perror("times 2");
exit(1);
}
printf("Real time : %.5f sec\n", (double)(t2 - t1) / CLK_TCK);
printf("User time : %.5f sec\n", (double)mytms.tms_utime / CLK_TCK);
printf("System time : %.5f sec\n", (double)mytms.tms_stime / CLK_TCK);
结果实时2.27
为什么多线程速度较慢?
它是特定于处理器的,并且与cores的编号,CPU caches的组织及其cache coherence相关。
同时也是compiler和optimization specific。阅读Dragon book。
它还取决于您的特定operating system和您的特定C standard library(例如GNU glibc与musl-libc不同),并且glibc 2.31
在[[相同]]上的性能可能与glibc 2.30
不同。 >计算机。您是否尝试过在最近的Linux上至少调用了最近的GCC 10 as?