#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>

#include <net/if.h>
#include <netpacket/packet.h>
#include <netinet/if_ether.h>
#include <netinet/tcp.h>
#include <netinet/in.h> 
#include <sys/socket.h> 
#include <sys/wait.h> 
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <sys/un.h>

#include <time.h>

#define MAX_THREAD 66
int count[MAX_THREAD];
void sig_handler(int signum)  
{  
    printf("catch SIGINT\n");  
}  
  
const char *socket_path = "/tmp/test.sock";

void *thread(void *para)
{
#if 0
	int i = (int)(unsigned long)para;
	struct timespec tv;
	tv.tv_nsec =1;
	tv.tv_sec =0;
	
	i = link_server("test");
	while (1)
	{
		//setup_signal();
		
		//close(i);
		char buf[1];
		write(i, buf, sizeof(buf));
	}
#else
	char str[32];
	sprintf(str, "./a.%d.text", para);
	int f = open(str, O_CREAT | O_TRUNC);
	int fds[1];
	int i;
	
	if (f  == -1 )
		printf("error\n");
	for (i = 0; i < sizeof(fds) / 4; i++)
			fds[i] = dup(f);
	printf("dup ok %d\n", para);		
	sleep(2);
	while (1)
	{		
		for (i = 0; i < sizeof(fds) / 4; i++)
		{
			//flock(fds[i], LOCK_SH);
			//flock(fds[i], LOCK_UN);
			int fd = fds[i];
			int val=fcntl(fd,F_GETFD);
			val|=FD_CLOEXEC;
			fcntl(fd,F_SETFD,val);
			count[(int)para] += 1;
		}		
	}	
#endif
}

void main()
{
	int i;
	pthread_t tid;

	for (i = 0; i < MAX_THREAD; i++)
	{
		pthread_create(&tid, NULL, thread, i);
	}
	
	while(1) {
		unsigned long num = 0;
		sleep(1);
		for (i = 0; i < MAX_THREAD; i++)
		{
		//	printf("thread %d 's performance counter is %d.\n", i, count[i]);
			num += count[i];
			count[i] = 0;
		}
		printf("\n %ld\n", num);
	}
}

