1

It is a program that calculates the number of divisors of 10000 as the maximum number of threads created by 1000.

I thought realtime was larger and turned.

n_Threads is 1000

thread.c

struct tms mytms;
    clock_t t1, t2;

    if( (t1 = times( &mytms )) == -1 )
    {
        perror( "times 1" );
        exit( 1 );
    }
    for( int i = 0; i < n_Threads; i++ )
    {
        if( pthread_create( &tid[i], NULL, &thread_func, NULL ) != 0 ){
            fprintf( stderr, "pthread_create error!\n" );
            fprintf( stderr, "%s\n", strerror( errno ) );
            exit( 1 );
        }
    }
    for( int i = 0; i < n_Threads; i++ )
    {
        pthread_join( tid[i], NULL );
    }

    if( (t2 = times( &mytms )) == -1 )
    {
        perror( "times 2" );
        exit( 1 );
    }

    fprintf( stdout, "%d 의  약 수 의  개 수 : %d \n", MAX_NUMBER, shared_Value );

    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 );
void* thread_func( void* arg )
{
    while(loopValue<=MAX_NUMBER){

        pthread_mutex_lock( (&mutex) );
        if( MAX_NUMBER % loopValue == 0 ) {
            shared_Value++;
        }
        loopValue++;
        pthread_mutex_unlock( (&mutex) );
    }
    return NULL;
}

result

10000 number of proper divisor : 25
Real time : 0.40000 sec
User time : 0.09000 sec
System time : 0.59000 sec

why system time larger than real time?

gowoo
  • 11
  • 3
  • Please edit your question to show which is the exact command you have used to measure time and post also output of `uname -a`. – pifor Jun 12 '20 at 11:10
  • Maybe there is a rounding issue when converting to `double`: what is the output if you measure the number of tick clocks as `intmax_t` and convert to seconds using `sysconf(_SC_CLK_TCK)` ? – pifor Jun 12 '20 at 11:48
  • 1
    System time counts *total CPU time* spent inside the kernel. It can be higher than real time if you have more than one CPU or core. – Nate Eldredge Jun 12 '20 at 18:40
  • Did you read [time(7)](https://man7.org/linux/man-pages/man7/time.7.html) and [clock_gettime(2)](https://man7.org/linux/man-pages/man2/clock_gettime.2.html) and [times(2)](https://man7.org/linux/man-pages/man2/times.2.html)? – Basile Starynkevitch Jun 12 '20 at 19:15

1 Answers1

1

The system time is the amount of time your program spends in the operating system; as opposed to user time, which is the time your program spends running your direct code ( + libraries ).

You seem to have discovered that creating and deleting threads takes a lot of system time. You may also have discovered that incrementing a number 10000 times takes a lot less time than creating and deleting threads. These are good lessons.

Also, depending upon whether your mutex is properly initialized; that it takes very little time to execute mutex operations on an invalid mutex; or that you are spending a lot of time having 1000 threads thrashing on a single mutex. To figure out which, you could check the return values from these functions; often illuminating.

Maybe start with a smaller number of threads. For a program like yours, there is nothing to be gained by having more threads than CPUs. That isn't true of all programs; some threads periodically wait for I/O operations and the like; but there is seldom much justification for 1000s of threads.

mevets
  • 10,070
  • 1
  • 21
  • 33