`
genier200
  • 浏览: 22803 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类

linux c thread

 
阅读更多
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/time.h>
#include <limits.h>
//编译命令:g++ -g -o multithread multithread-2.c -lpthread
const char *FILE_PATH="/root/test";
const int file_size[]={512,1024,46900};

struct args
{
	int max;
	int min;
};

void* work( void*);

int main()
{
	//printf("%s\n",FILE_PATH);
	printf("starting.........\n");
	
	int array_length=sizeof(file_size)/sizeof(file_size[0]);
	
	pthread_t* ids=(pthread_t*)malloc(sizeof(pthread_t)*array_length);
	
	int ret,i;
	
	struct args* arg;

	for( i=0;i<array_length;i++)
	{

		 arg=(struct args*)malloc(sizeof(struct args));
		
		if(i==0)
		{
			arg->max =file_size[0];
			arg->min = 0;
		}
		else
		{
			arg->max=file_size[i];
			arg->min=file_size[i-1];
		}
		

		ret=pthread_create(&ids[i],NULL,work,arg);
		
		if(ret!=0)
		{   
			printf ("Create pthread error!\n");
	                exit (1);
	        } 
	}
	
	for(int i=0;i<array_length;i++)
	{
		pthread_join(ids[i],0);
	}

	free(ids);
	free(arg);

	printf("ending.........\n");
	return 0;
}

void* work(void* parameter)
{
	args *arg=(args*)parameter;
	
	printf("this is a thread:[min-max]=%d-%d,id=%d\n",arg->min,arg->max,pthread_self());
	struct timeval* tv=(struct timeval*)malloc(sizeof(struct timeval));
	struct timezone* tz=(struct timezone*)malloc(sizeof(struct timezone));
	gettimeofday(tv,tz);
	
	printf("%d\n",tv->tv_sec);
	printf("%d\n",tv->tv_usec);
	
	char* char1=(char*)malloc(1000);
	sprintf(char1,"%d%d",tv->tv_sec,tv->tv_usec);
	printf("time:%s\n",char1);
	
	srand((unsigned)atoi(char1));
	
	int i;
	
	for(i=0;i<10;i++)
	{
		long int _rand =rand();
		printf("seed:%d\n",_rand);
		int bs=_rand%(arg->max-arg->min)+arg->min+1;
		printf("random:%d\n",bs);
		sprintf(char1,"dd if=/dev/zero of=%s/test.%d.%d.bin count=10 bs=%d\n",FILE_PATH,i,bs,bs);
		printf("%s\n",char1);
		//system(char1);
	}

	free(tv);
	free(tz);
	free(char1);

}
分享到:
评论
1 楼 genier200 2011-08-09  
update,修复bug

相关推荐

    嵌入式Linux C语言应用程序设计

    其中args_cmd.h是解析命令相关的头文件,ctrl.c和ctrl.h是控制命令的源代码,proc_cmd.c是具体操作的源代码,thread.c和thread.h是线程相关的源代码,types.h是类型相关的源代码,net_send.h是网络相关的源代码。...

    线程池Linux C语言简单版本

    本线程池采用C语言实现。包括以下内容 &gt; - thread_pool_create:创建线程池所需要的资源,包含不限于任务队列,子线程的创建。 &gt; - thread_pool_post:用于任务的发布,将执行任务存在任务队列中。 &gt; - thread_pool_...

    Linux下多线程计算圆周率 C语言

    Linux下多线程计算圆周率 C语言 //实现一个用多线程计算 π(圆周率)的程序。 #include #include #include #include #define N 50000//设定计算次数 #define NUM 2//设定线程数,经过反复实验,在装有1个双核...

    Linux Multiply thread programming(多线程编程初步)

    Linux Multiply thread programming(多线程编程初步)

    linux_下多线程编程 C语言

    linux下C语言多线程编程实例 在单线程的程序里,有两种基本的数据:全局变量和局部变量。但在多线程程序里,还有第三种数据类型:线程数据(TSD: Thread-Specific Data)

    linuxC语言线程池实现CP命令

    linux系统下C语言 利用线程池技术实现CP命令 压缩包包含:源代码+开发说明PPT 线程池头文件: //任务 struct task { void *(*task)(void *arg); void *arg; struct task *next; }; //线程池 typedef struct ...

    GeoIP C API (Linux下使用)

    This API should work on most UNIX and GNU/Linux platforms and is thread-safe. ./configure make make check make install For installing on Windows, see the READMEwin32.txt file supplied with the ...

    Linuxc 信号的使用

    1.编程实现以下功能: 主线程实现以下功能: ① 定义全局变量key; ② 创建两个线程; ③ 如果线程正常结束,得到线程的结束...④ 结束的时候打印字符串“thread1 ,exited!:key值”; 用多线程实现生产者消费者:

    arm-linux-gcc-4.3.2(交叉编译器)

    1. tar zxvf arm-linux-gcc-3.4.1.tgz -C/ 2. 编辑 ~/.bashrc ,加入 export PATH=$PATH:/usr/local/arm/3.4.1/bin 3. 验证安装 [root@xxxxx root]# arm-linux-gcc -v Rea ding specs from /usr/local/arm/3.4.1/bin...

    arm-linux-gcc交叉编译器 3.4.1

    1. tar zxvf arm-linux-gcc-3.4.1.tgz -C/ 2. 编辑 ~/.bashrc ,加入 export PATH=$PATH:/usr/local/arm/3.4.1/bin 3. 验证安装 [root@xxxxx root]# arm-linux-gcc -v Reading specs from /usr/local/arm/3.4.1/bin/...

    arm-linux-gcc-3.4.1(交叉编译器)

    1. tar zxvf arm-linux-gcc-3.4.1.tgz -C/ 2. 编辑 ~/.bashrc ,加入 export PATH=$PATH:/usr/local/arm/3.4.1/bin 3. 验证安装 [root@xxxxx root]# arm-linux-gcc -v Rea ding specs from /usr/local/arm/3.4.1/bin...

    thread kill deom

    linux kill thread demo,use signal to thread for kill

    arm-linux-gcc交叉编译器 4.3.2 part2

    1. tar zxvf arm-linux-gcc-4.3.2.tgz -C/ 2. 编辑 ~/.bashrc ,加入 export PATH=$PATH:/usr/local/arm/4.3.2/bin 3. 验证安装 [root@xxxxx root]# arm-linux-gcc -v Using built-in specs. Target: arm-none-linux...

    Linux System Programming, 2nd edition.pdf

    A Linux kernel, C library, and C compiler overview Basic I/O operations, such as reading from and writing to files Advanced I/O interfaces, memory mappings, and optimization techniques The family of ...

    Linux C/C++发起http请求示例

    Linux C/C++发起http请求, 多线程示例

    linux 系统编程 网络编程 源码

    client1.c client2.c client.c server1.c server2.c server.c ./tcp_dos: tcp_dos tcp_dos.c ./term: getpass getpass.c gets gets.c translate ./term/translate: ansi_code.c ansi_code.o chin.c dict dict.o ...

    史上最强的嵌入式底层驱动开发课程 Linux系统开发+Linux高级程序+主板开发+ARM等

    ├&lt;1 Linux操作系统基础&gt; │ ├01 - 说在前面的话1.mp4 │ ├02 - 说在前面的话2.mp4 │ ├03 - 说在前面的话3.mp4 │ ├04 - 说在前面的话4.mp4 │ ├05 - 计算机组成原理概述1 .mp4 │ ├06 - 计算机组成原理概述2...

    一个linux下C实现的线程池源码

    一个linux下的c实现的线程池,其中包括线程池的创建、销毁、线程状态等操作。

    socket linux c++ 多线程3

    最近几个月总结和网上收集资料第三套:c语言Socket编程多线程编程入门指导Socket编程Thread_多线程Linux下的多线程编程等10多个资料....

    线程池 threadPool c语言实现

    保存的一份Linux下的线程池代码,使用C语言编写,另外添加了较详细的中文注释,附带测试代码,可以加深一些对线程池的认识,居家必备

Global site tag (gtag.js) - Google Analytics