⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clockid.c

📁 一个测试程序
💻 C
字号:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/neutrino.h>
#include <sys/dispatch.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <inttypes.h>
#include<pthread.h>
#include<string.h>

#define TIMER_PULSE_EVENT (_PULSE_CODE_MINAVAIL + 3)
long function(int n);
void *computer(void *x);
void *computer1(void *x);

//typedef union {
typedef struct {
    //struct _pulse pulse;
    uint64_t runtime;
    int tid;
} message_t;

//int clockid;
//uint64_t  start,stop; 
pthread_t tids[5];
int data[4];
int chid,coid;

int main()
{
	int               rcvid,i;
    //struct sigevent   event;
   // int               pcoid;
    message_t         msg;
	//timer_t			 timeID;
	//struct itimerspec itime;
    setvbuf (stdout, NULL, _IOLBF, 0);
    
    chid = ChannelCreate( 0 );
    if (chid == -1)
	{
        fprintf( stderr, "ChannelCreate failed: %s\n",strerror(errno) );
        exit( EXIT_FAILURE );
    }
    
//    pcoid = ConnectAttach( 0, 0, chid, _NTO_SIDE_CHANNEL, 0 );
//    if (coid == -1) 
//    {
//        fprintf( stderr, "ConnectAttach failed: %s\n", strerror(errno) );
//        exit( EXIT_FAILURE );
//    }
//   
//   	SIGEV_PULSE_INIT( &event, pcoid, 10, TIMER_PULSE_EVENT, 0 );
//    
//    timer_create(CLOCK_REALTIME,&event,&timeID);
//	itime.it_value.tv_sec=10;
//	itime.it_value.tv_nsec=0;
//	itime.it_interval.tv_sec=0;
//	itime.it_interval.tv_nsec=5000000;
//	timer_settime(timeID,0,&itime,NULL);
	 
    for(i=0;i<4;i++)
	{  
		pthread_create (&tids[i], NULL, &computer, NULL);
		//pthread_create (&tids[0], NULL, &computer, NULL);
	}
	pthread_create (&tids[5], NULL, &computer1, NULL);
    
	while (1) 
	{
       
        rcvid = MsgReceive( chid, &msg, sizeof(msg), NULL );
        if (rcvid == -1) 
        {
            fprintf( stderr, "MsgReceive failed: %s\n", strerror(errno) );
            continue;
        }
//        if (rcvid == 0) 
//        {
//          
//            switch (msg.pulse.code) 
//            {
//            	case TIMER_PULSE_EVENT:
//                	//printf( "got our pulse, the timer must have expired\n");                	
//                	break;
//            	default:
//                	printf( "unexpect pulse code: %d\n",msg.pulse.code );
//                	break;
//         	}
//     	}
     	if(rcvid)
     	{
     		printf("%d runtime :%llu\n",msg.tid,msg.runtime);
  		
     		MsgReply(rcvid,0, NULL,NULL);  		
     	}
    }
    
		for (i = 0; i < 5; i++) 
		{
			pthread_join (tids[i], NULL);
		}
	
	return 0;
}




void *computer(void *x)
{
		int j ,a=1234,count=123000,found=1,k=0,run_mask;
	    pthread_t mytid;
	    uint64_t runtime1;
		message_t msg;
		int clockid;
		uint64_t  start,stop; 
		 
		coid = ConnectAttach( 0, 0, chid, _NTO_SIDE_CHANNEL, 0 );
		mytid=pthread_self();
		clockid=ClockId(0,mytid);
		
		ClockTime(clockid, NULL, &start);
		printf("started a thread\n ");
		 
//		for(j=1;j<=count;j++)
//		{
//			function(a);
//		}
//	     //sleep(1); 
		while(found)
		{
		 if(pthread_equal(mytid,tids[k])==0)
		 	k++;
		 	else
		 	{
		 		found=0;
	 			run_mask=data[k];
	  			ThreadCtl(_NTO_TCTL_RUNMASK,(int *)&run_mask);
	       for(j=1;j<=count;j++)
	       {
	        function(a);
	       } 
		 ClockTime(clockid, NULL, &stop);
		 runtime1=(stop-start);
		 msg.runtime=runtime1;
		 msg.tid=mytid;
		
		if (MsgSend( coid, &msg, sizeof( msg ), NULL, 0 ))
    	{
        	printf("Msgsend err!");
        	exit( EXIT_FAILURE );
    	}
		 //printf("runtime :%llu\n",runtime);
		 
		 	}
		 }
		 return 0;	
}

void *computer1(void *x)
{
		//int j ,a=1234,count=123000;
	    pthread_t mytid;
	    uint64_t runtime1;
		message_t msg;
		int clockid;
		uint64_t  start,stop; 
		 
		coid = ConnectAttach( 0, 0, chid, _NTO_SIDE_CHANNEL, 0 );
		mytid=pthread_self();
		clockid=ClockId(0,mytid);
		
		ClockTime(clockid, NULL, &start);
		printf("started a thread\n ");
		 
//		for(j=1;j<=count;j++)
//		{
//			function(a);
//		}
	     sleep(1);  
		 ClockTime(clockid, NULL, &stop);
		 runtime1=(stop-start);
		 msg.runtime=runtime1;
		 msg.tid=mytid;
		
		if (MsgSend( coid, &msg, sizeof( msg ), NULL, 0 ))
    	{
        	printf("Msgsend err!");
        	exit( EXIT_FAILURE );
    	}
		 //printf("runtime :%llu\n",runtime);
		 
		 return 0;	
}


long function(int n)
{
	 long i;
	 if(n>1)
	 i=n+function(n-1);
	 else
	 i=1;
	 return i;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -