麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統(tǒng) > Ubuntu > 正文

ubuntu8.04如何修改默認文件關聯(lián)打開程序

2024-06-28 13:02:49
字體:
供稿:網(wǎng)友
 

線程的創(chuàng)建和使用

線程的創(chuàng)建是用下面的幾個函數(shù)來實現(xiàn)的.

#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創(chuàng)建一個線程,thread是用來表明創(chuàng)建線程的ID,attr指出線程創(chuàng)建時候的屬性,我們用NULL來表明使用缺省屬性.start_routine函數(shù)指針是線程創(chuàng)建成功后開始執(zhí)行的函數(shù),arg是這個函數(shù)的唯一一個參數(shù).表明傳遞給start_routine的參數(shù). pthread_exit函數(shù)和exit函數(shù)類似用來退出線程.這個函數(shù)結束線程,釋放函數(shù)的資源,并在最后阻塞,直到其他線程使用pthread_join函數(shù)等待它.然后將*retval的值傳遞給**thread_return.由于這個函數(shù)釋放所以的函數(shù)資源,所以retval不能夠指向函數(shù)的局部變量. pthread_join和wait調(diào)用一樣用來等待指定的線程. 下面我們使用一個實例來解釋一下使用方法.在實踐中,我們經(jīng)常要備份一些文件.下面這個程序可以實現(xiàn)當前目錄下的所有文件備份.備份后的后綴名為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;    /* 因為線程退出時,所有的變量空間都要被釋放,所以我們只好自己分配內(nèi)存了 */    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;    /* 得到當前路徑下面所有的文件(包含目錄)的個數(shù) */    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;    }    /* 創(chuàng)建線程,進行文件拷貝 */    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函數(shù)里面創(chuàng)建的內(nèi)存 */    free(byte_copy_p);    }    }    printf("Total Copy Bytes %d/n/a",byte_copy);    free(thread);    free(file);    exit(0);    }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产91久久久久久 | 国产男女爽爽爽爽爽免费视频 | 国产精品一区免费在线观看 | 中文字幕免费播放 | 欧美 日韩 三区 | 欧美成人精品一区二区 | 深夜视频福利 | 国产精品嘿咻嘿咻在线播放 | 成人在线视频免费播放 | 亚洲一区在线免费视频 | 综合国产在线 | 国产精品一品二区三区四区18 | wwwxxx国产| 伦一区二区三区中文字幕v亚洲 | 日本在线一区二区 | 国产免费视频在线 | 欧美一级黄视频 | 91九色福利| 中文字幕精品在线观看 | 久久久久国产精品久久久久 | 日本视频在线免费观看 | 欧美巨乳在线观看 | 午夜视频在线免费观看 | 巨根插入 | 精品久久中文字幕 | 国产妞干网 | 欧美成人性色 | 免费观看9x视频网站在线观看 | 蜜桃精品视频 | 黄网站免费观看视频 | 麻豆传传媒久久久爱 | 午夜精品久久久久久久久久久久久蜜桃 | 性aaa| 少妇一级淫片免费放播放 | 国产不卡av在线 | 91麻豆精品国产91久久久点播时间 | 久久精品亚洲精品国产欧美kt∨ | avlululu| 亚洲射逼| 中午日产幕无线码1区 | 久久精品国产清自在天天线 |