多线程多进程的简单演示

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
//multimode  0 : no multimode  1 : multithread 2 : multiprocess


#ifndef multimode
#define multimode 2
#endif




#define NLOOPS  0x10000000
#define NLOOPS  0x100000


//typedef struct targ__
typedef struct 
{
    int start;
    int jobid;
}targ, *ptarg;


typedef struct
{
    pid_t mypid;
    int jobid;
}pinfo, *ppinfo;




static const int nthread = 10;
static const int nprocs = 10;
volatile double x = 0.0;
volatile char managerproc = 1;


void *tfunc( void *arg )
{
    int i;
    int temp = NLOOPS/nthread;
    ptarg tp = ( ptarg )arg;


    printf( "My tid : %u jobid : %d\n", pthread_self( ), tp -> jobid );
    //printf( "My tid : %u jobid : %d\n",  tp -> jobid );


    
    if( tp -> jobid != ( nthread - 1) )
    {
        for( i= tp->start; i < tp -> start + temp; i++ )
        {
            x = sin(i);
        }
    }
    else
    {
        for( i= tp->start; i< NLOOPS; i++)
{
   x = sin(i);
}
    }


}


int main( int argc, char *argv[] )
{
    unsigned long i;
    int j;
    long sec, usec;
    long total;


    int waitret;


    struct timeval tstart, tend;
    pid_t pidtmp;
#if ( multimode == 2 )
    pinfo pp;
    memset( &pp, 0x0, sizeof(pinfo) );


#elif ( multimode == 1 )
    pthread_t *tid;
    ptarg pt;


    tid = (pthread_t *)malloc( nthread * sizeof(pthread_t) );
    pt = ( ptarg )malloc( nthread * sizeof( targ ) );
    
    for( j=0; j<nthread; j++ )
    {
        pt[j].start = (NLOOPS / nthread ) * j;
pt[j].jobid = j;
    }


#endif
    printf( "\n--------- simple parellel performance test ---------\n" );
    printf( "Source  File : %s\n", __FILE__ );
    printf( "Excute  file : %s\n", argv[0] );
    printf( "Compile Date : %s\n", __DATE__ );
    printf( "Compile Time : %s\n", __TIME__ );
    gettimeofday( &tstart, NULL );


#if ( multimode == 0 )
    // no parellel
    printf( "\nMultiMode    :  single process\n\n" );
    i = 0;
    while( i < NLOOPS )
    {
        x = sin(i);
i++;
    }


#elif ( multimode == 1 )
    //multithread
    printf( "\nMultiMode    :   multithread\n\n" );
    for( j=0; j<nthread; j++ )
    {
          pthread_create( &tid[j], NULL, tfunc, pt+j );  
    }


    for( j=0; j<nthread; j++ )
    {
        pthread_join( tid[j], NULL);
    }


#elif ( multimode == 2)
    //multi process
    printf( "\nMultiMode    :   multiprocess\n" );
    for( j=0; j<nprocs; j++ )
    {
        pidtmp = fork( );
if( pidtmp < 0) 
   goto err;
else 
   if( pidtmp > 0 )//the manage process
   {
pp.jobid = -1;
//break;
   }
   else
   {
       pp.mypid = pidtmp;
pp.jobid = j;
managerproc = 0;
break;
   }
       


    }
    if( 0 == managerproc )// job process
    {
printf( "My pid : %u jobid : %d\n", getpid( ), pp.jobid );
        for(i=pp.jobid; i<NLOOPS; i+=nprocs )
{
   x = sin(i);
}
exit(0);
    }
    else //manager process
    {
printf( "Manager process pid : %u\n", getpid( ));
        for( i=0; i<nprocs; i++)
   wait( &waitret );
    }
#endif


    
    gettimeofday( &tend, NULL );
    sec = tend.tv_sec - tstart.tv_sec;
    usec = tend.tv_usec - tstart.tv_usec;
    if( usec < 0 )
    {
        sec--;
usec += 1000000;
    }
    total = sec*1000 + usec/1000;
    printf( "\nOK done Total time : %ld ms\n\n", total );
    exit(0);
err:
    printf( "Multi process error exit\n" );
    exit( -1 );
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值