A multi-process and multi-threaded implementation of three linked lists with 1000000 nodes using merge sort was implemented. I compared the real-time of the implemented program, but the multi-thread method is slower. Why is that?
main method in 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);
Result real-time : 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);
result real-time 2.27