linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > article > Linux开发区 > 软件开发 >
栏目导栏
资料搜索
热门文章
·Linux 下 C 语言编程
·Linux下的通用线程池创建
·C++字符串转换篇
·linux C 进程操作篇
·linux上的C/C++编译器gcc/egcs
·linux C 文件权限控制篇
·GCC使用手册
·linux C 接口处理篇
·在Redhat Linux上安装 GCC 编译
·fopen()函数的参数说明
·C语言运算符
·GCC使用指南
·Linux下C开发环境的构成和安装
·GCC使用手册与常用命令
·Linux常用C函数-日期时间篇
最新文章
·epoll入门
·在Linux下发布程序需要注意版本
·Suse Linux系统下JAVA AWT界面
·Vim编译器配合ctags实现函数原
·在Ubuntu Linux 8.04上构建GCC
·Linux操作系统下Socket编程地址
·将VC程序移植到Linux系统的几点
·Linux下malloc/free与new/dele
·Linux下用GTK和socket实现简单
·Linux操作系统下让Tomcat启动在
·Linux操作系统中如何编译C程序
·几种常被人们忽略的Linux系统下
·Eclipse编程工具 在Ubuntu下的
·Linux操作系统下的网络地址转换
·老手经验谈:Linux驱动程序开发
Google
 
Linux系统下C语言编程 线程操作入门常识
[ 作者:  加入时间:2008-04-21 09:52:40  来自:Linux联盟收集整理 ]
 

线程的创建和使用

线程的创建是用下面的几个函数来实现的.

#include <pthread.h>
int pthread_create(pthread_t *thread,pthread_attr_t *attr,
void *(*start_routine)(void *),void *arg);
void pthread_exit(void *retval);
int pthread_join(pthread *thread,void **thread_return);

pthread_create创建一个线程,thread是用来表明创建线程的ID,attr指出线程创建时候的属性,我们用NULL来表明使用缺省属性.start_routine函数指针是线程创建成功后开始执行的函数,arg是这个函数的唯一一个参数.表明传递给start_routine的参数. pthread_exit函数和exit函数类似用来退出线程.这个函数结束线程,释放函数的资源,并在最后阻塞,直到其他线程使用pthread_join函数等待它.然后将*retval的值传递给**thread_return.由于这个函数释放所以的函数资源,所以retval不能够指向函数的局部变量. pthread_join和wait调用一样用来等待指定的线程. 下面我们使用一个实例来解释一下使用方法.在实践中,我们经常要备份一些文件.下面这个程序可以实现当前目录下的所有文件备份.备份后的后缀名为bak

#include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #include <pthread.h>
    #include <dirent.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/time.h>

    #define BUFFER 512
    struct copy_file {
    int infile;
    int outfile;
    };
    void *copy(void *arg)
    {
    int infile,outfile;
    int bytes_read,bytes_write,*bytes_copy_p;
    char buffer[BUFFER],*buffer_p;
    struct copy_file *file=(struct copy_file *)arg;
    infile=file->infile;
    outfile=file->outfile;
    /* 因为线程退出时,所有的变量空间都要被释放,所以我们只好自己分配内存了 */
    if((bytes_copy_p=(int *)malloc(sizeof(int)))==NULL) pthread_exit(NULL);
    bytes_read=bytes_write=0;
    *bytes_copy_p=0;
    while((bytes_read=read(infile,buffer,BUFFER))!=0)
    {
    if((bytes_read==-1)&&(errno!=EINTR))break;
    else if(bytes_read>0)
    {
    buffer_p=buffer;
    while((bytes_write=write(outfile,buffer_p,bytes_read))!=0)
    {
    if((bytes_write==-1)&&(errno!=EINTR))break;
    else if(bytes_write==bytes_read)break;
    else if(bytes_write>0)
    {
    buffer_p+=bytes_write;
    bytes_read-=bytes_write;
    }
    }
    if(bytes_write==-1)break;
    *bytes_copy_p+=bytes_read;
    }
    }
    close(infile);
    close(outfile);
    pthread_exit(bytes_copy_p);
    }
    int main(int argc,char **argv)
    {
    pthread_t *thread;
    struct copy_file *file;
    int byte_copy,*byte_copy_p,num,i,j;
    char filename[BUFFER];
    struct dirent **namelist;
    struct stat filestat;
    /* 得到当前路径下面所有的文件(包含目录)的个数 */
    if((num=scandir(".",&namelist,0,alphasort))<0)
    {
    fprintf(stderr,"Get File Num Error:%s\n\a",strerror(errno));
    exit(1);
    }
    /* 给线程分配空间,其实没有必要这么多的 */
    if(((thread=(pthread_t *)malloc(sizeof(pthread_t)*num))==NULL)||
    ((file=(struct copy_file *)malloc(sizeof(struct copy_file)*num))==NULL)
    )
    {
    fprintf(stderr,"Out Of Memory!\n\a");
    exit(1);
    }

    for(i=0,j=0;i<num;i++)
    {
    memset(filename,'\0',BUFFER);
    strcpy(filename,namelist[i]->d_name);
    if(stat(filename,&filestat)==-1)
    {
    fprintf(stderr,"Get File Information:%s\n\a",strerror(errno));
    exit(1);
    }
    /* 我们忽略目录 */
    if(!S_ISREG(filestat.st_mode))continue;
    if((file[j].infile=open(filename,O_RDONLY))<0)
    {
    fprintf(stderr,"Open %s Error:%s\n\a",filename,strerror(errno));
    continue;
    }
    strcat(filename,".bak");
    if((file[j].outfile=open(filename,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))
    <0)
    {
    fprintf(stderr,"Creat %s Error:%s\n\a",filename,strerror(errno
    ));
    continue;
    }
    /* 创建线程,进行文件拷贝 */
    if(pthread_create(&thread[j],NULL,copy,(void *)&file[j])!=0)
    fprintf(stderr,"Create Thread[%d] Error:%s\n\a",i,strerror(errno));
    j++;
    }
    byte_copy=0;
    for(i=0;i<j;i++)
    {
    /* 等待线程结束 */
    if(pthread_join(thread[i],(void **)&byte_copy_p)!=0)
    fprintf(stderr,"Thread[%d] Join Error:%s\n\a",
    i,strerror(errno));
    else
    {
    if(bytes_copy_p==NULL)continue;
    printf("Thread[%d] Copy %d bytes\n\a",i,*byte_copy_p);
    byte_copy+=*byte_copy_p;
    /* 释放我们在copy函数里面创建的内存 */
    free(byte_copy_p);
    }
    }
    printf("Total Copy Bytes %d\n\a",byte_copy);
    free(thread);
    free(file);
    exit(0);
    }
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·Linux系统下精确到微秒级的时间操作函数  (2008-04-21 09:44:45)
 ·Linux系统下的PDF好帮手 LinuxPDFeditor  (2008-04-21 09:38:50)
 ·Linux内核2.6.25全新发布加入众多新特性  (2008-04-21 09:38:00)
 ·Debian Linux系统编译内核标准方式介绍  (2008-04-17 10:06:58)
 ·Linux系统中如何添加自己的库文件路径  (2008-04-17 10:00:13)
 ·关于Linux系统下对U盘不利的一些操作说明  (2008-04-17 09:59:24)
 ·Slackware Linux中开启内存共享提高FPS  (2008-04-17 09:54:23)
 ·学习园地:Linux下.bz2与.gz文件的区别  (2008-04-17 09:53:40)
 ·关于Linux操作系统“暴力破解”软件介绍  (2008-04-15 10:06:08)
 ·Debian Linux下配置heartbeat高可用集群  (2008-04-15 10:05:20)