水果v5吧 关注:3贴子:26
  • 2回复贴,共1


1楼2013-01-19 18:35回复
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <stdint.h>
    #include <pthread.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <errno.h>
    typedef struct {
    off_t file_size;
    ssize_t read_size;
    } copy_ctx_t;
    void * display_thread(void *args)
    {
    int done;
    copy_ctx_t *ctx = (copy_ctx_t *)args;
    while(ctx->read_size != ctx->file_size) {
    done = (int)((double)ctx->read_size /
    (double)ctx->file_size * 100);
    printf("%d%% Done.", done);
    fflush(stdout);
    printf("\r");
    usleep(1000 * 100);
    }
    done = (int)((double)ctx->read_size /
    (double)ctx->file_size * 100);
    printf("%d%% Done.", done);
    fflush(stdout);
    printf("\r\n");
    return NULL;
    }
    int main(int argc, char *argv[])
    {
    int ret;
    int src_fd;
    int dst_fd;
    ssize_t rn;
    uint8_t buffer;
    pthread_t ptid;
    struct stat st;
    copy_ctx_t copy_ctx;
    if (argc != 3) {
    printf("Usage: app src dst.\n");
    return 0;
    }
    if (stat(argv[1], &st) == -1) {
    //FIXME
    }
    copy_ctx.file_size = st.st_size;
    copy_ctx.read_size = 0;
    ret = pthread_create(&ptid, NULL,
    display_thread, &copy_ctx);
    if (ret != 0) {
    printf("create thread failed.\n");
    //FIXME
    }
    src_fd = open(argv[1], O_RDONLY);
    if (src_fd == -1) {
    //FIXME
    }
    dst_fd = open(argv[2],
    O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (dst_fd == -1) {
    //FIXME
    close(src_fd);
    }
    for ( ; (rn = read(src_fd, &buffer, 1)) > 0 ;) {
    write(dst_fd, &buffer, 1);
    copy_ctx.read_size += 1;
    usleep(1000 * 500);
    }
    pthread_join(ptid, NULL);
    close(src_fd);
    close(dst_fd);
    return 0;
    }
    学习pthread的函数 读取文件 打印百分比状态


    2楼2013-01-19 18:36
    回复
      #include<stdio.h>
      #include<stdlib.h>
      #include<string.h>
      #include<unistd.h>
      #include<pthread.h>
      #include<errno.h>
      #include<sys/stat.h>
      #include<sys/types.h>
      #include<stdint.h>
      #include<fcntl.h>
      typedef struct {
      off_t file_size;
      ssize_t read_size;
      }copy_ctx_t;//第一步:先确定文件的大小参数,参数的类型根据要获得方式,man 2 stat以及man read 得到
      void *threads(void *arg)
      {
      int done;
      copy_ctx_t *ctx = (copy_ctx_t *)arg;
      while(ctx->read_size != ctx->file_size)
      {
      done = (int)((double)ctx->read_size/(double)ctx->file_size *100);//先转换双精度浮点型,再转换int
      printf("%d%% Done.",done);//打印百分比
      fflush(stdout);//fflush(stdout)刷新标准输出缓冲区,把输出缓冲区里的东西打印到标准输出设备上
      printf("\r");//\r是将当前位置移到本行的开头
      usleep(1000*1000);
      }
      done = (int)((double)ctx->read_size/(double)ctx->file_size *100);
      printf("%d%% Done",done);
      fflush(stdout);
      printf("\r\n");
      return NULL;
      }//对读取过程的状态进行百分比计数并打印
      int main(int argc, char*argv[])
      {
      int ret;
      pthread_t ptid;
      ret = pthread_create(&ptid,NULL,threads,&copy_ctx);
      if(ret != 0)
      {
      fprintf(stderr,"cannt create %s\n",strerror(errno));
      exit(1);
      }
      pthread_join(ptid,NULL);//第二步:创建子线程,并确定创建子线程的函数名字
      int src_fd;
      int dst_fd;
      src_fd = open(argv[1],O_RDONLY);
      if(src_fd == -1)
      {
      perror("src open failed\n");
      exit(1);
      }
      dst_fd = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0644);
      if(dst_fd == -1)
      {
      perror("dst open failed\n");
      exit(1);
      }
      close(src_fd);
      close(dst_fd);//第三步:获得两个文件的文字描述符,若文件存在,则长度被截为0,属性不变O_TRUNC │若文件存在,则长度被截为0,属性不变 ,0644中的0表示8进制
      struct stat st;
      if(stat(argv[1],&st) == -1)
      {
      fprintf(stderr,"fsize:cannt access %s\n",argv[1]);
      exit(1);
      }
      copy_ctx_t copy_ctx;
      copy_ctx.file_size = st.st_size;
      copy_ctx.read_size = 0;//第四步:用stat函数得到源文件的大小,设置目标文件大小为0;
      ssize_t rn;
      uint8_t buffer;
      for(;(rn = read(src_fd,&buffer,1)) > 0;)
      {
      write(dst_fd,&buffer,1);
      copy_ctx.read_size += 1;
      usleep(1000 * 500);
      }//第五步:从源文件中每次读取1字节到目的文件,uint8_t 8位无符号整型数(int)(内存地址) ,定义是这样的: typedef unsigned int uint8_t; usleep功能把进程挂起一段时间, 单位是微秒(百万分之一秒)
      return 0;
      }//最后整理


      3楼2013-01-20 09:11
      回复