仓酷云

标题: 来讲讲:Linux下C言语编程 线程操纵进门知识 [打印本页]

作者: 愤怒的大鸟    时间: 2015-1-16 16:15
标题: 来讲讲:Linux下C言语编程 线程操纵进门知识
当你经过一段时间的学习后就应该扩充自己的知识,多学习linux命令,但是不要在初学阶段就系统的学习linux命令。
线程的创立和利用
线程的创立是用上面的几个函数来完成的。
#include<pthread.h>
intpthread_create(pthread_t*thread,pthread_attr_t*attr,
void*(*start_routine)(void*),void*arg);
voidpthread_exit(void*retval);
intpthread_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>
#defineBUFFER512
structcopy_file{
intinfile;
intoutfile;
};
void*copy(void*arg)
{
intinfile,outfile;
intbytes_read,bytes_write,*bytes_copy_p;
charbuffer[BUFFER],*buffer_p;
structcopy_file*file=(structcopy_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;
elseif(bytes_read>0)
{
buffer_p=buffer;
while((bytes_write=write(outfile,buffer_p,bytes_read))!=0)
{
if((bytes_write==-1)&&(errno!=EINTR))break;
elseif(bytes_write==bytes_read)break;
elseif(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);
}
intmain(intargc,char**argv)
{
pthread_t*thread;
structcopy_file*file;
intbyte_copy,*byte_copy_p,num,i,j;
charfilename[BUFFER];
structdirent**namelist;
structstatfilestat;
/*失掉以后路径上面一切的文件(包括目次)的个数*/
if((num=scandir(".",&namelist,0,alphasort))<0)
{
fprintf(stderr,"GetFileNumError:%s
a",strerror(errno));
exit(1);
}
/*给线程分派空间,实在没有需要这么多的*/
if(((thread=(pthread_t*)malloc(sizeof(pthread_t)*num))==NULL)||
((file=(structcopy_file*)malloc(sizeof(structcopy_file)*num))==NULL)
)
{
fprintf(stderr,"OutOfMemory!
a");
exit(1);
}
for(i=0,j=0;i<num;i++)
{
memset(filename,,BUFFER);
strcpy(filename,namelist[i]->d_name);
if(stat(filename,&filestat)==-1)
{
fprintf(stderr,"GetFileInformation:%s
a",strerror(errno));
exit(1);
}
/*我们疏忽目次*/
if(!S_ISREG(filestat.st_mode))continue;
if((file[j].infile=open(filename,O_RDONLY))<0)
{
fprintf(stderr,"Open%sError:%s
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%sError:%s
a",filename,strerror(errno
));
continue;
}
/*创立线程,举行文件拷贝*/
if(pthread_create(&thread[j],NULL,copy,(void*)&file[j])!=0)
fprintf(stderr,"CreateThread[%d]Error:%s
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]JoinError:%s
a",
i,strerror(errno));
else
{
if(bytes_copy_p==NULL)continue;
printf("Thread[%d]Copy%dbytes
a",i,*byte_copy_p);
byte_copy+=*byte_copy_p;
/*开释我们在copy函数内里创立的内存*/
free(byte_copy_p);
}
}
printf("TotalCopyBytes%d
a",byte_copy);
free(thread);
free(file);
exit(0);
}
</p>
要明白学好linux不是一件一蹴而就的事,一定要能坚持使用它,特别是在使用初期。
作者: 小女巫    时间: 2015-1-18 16:34
未来的学习之路将是以指数增加的方式增长的。从网管员来说,命令行实际上就是规则,它总是有效的,同时也是灵活的。
作者: 透明    时间: 2015-1-27 08:10
上课传授的不仅仅是知识,更重要的是一些道理,包括一些做人的道理,讲课时也抓住重点,循序渐进,让同学理解很快;更可贵的是不以你过去的成绩看问题.
作者: 兰色精灵    时间: 2015-2-5 07:07
安装一个新的软件时先看README,再看INSTALL然后看FAQ,最后才动手安装,这样遇到问题就知道为什么。如果Linux说明文档不看,结果出了问题再去论坛来找答案反而浪费时间。
作者: 飘飘悠悠    时间: 2015-2-11 08:11
随着实验课程的结束,理论课也该结束了,说实话教OS的这两位老师是我们遇到过的不错的老师(这话放这可能不太恰当).
作者: 只想知道    时间: 2015-3-2 01:49
其实当你安装了一个完整的Linux系统后其中已经包含了一个强大的帮助,只是可能你还没有发现和使用它们的技巧。
作者: 若天明    时间: 2015-3-11 02:41
得到到草率的回答或者根本得不到任何Linux答案。越表现出在寻求帮助前为解决问题付出的努力,你越能得到实质性的帮助。
作者: 变相怪杰    时间: 2015-3-17 19:10
Linux高手更具有鼓励新手的文化精神。如何在Linux社区获得帮助,需要说明的是你要有周全的思考,准备好你的问题,不要草率的发问。
作者: 金色的骷髅    时间: 2015-3-24 21:52
下面看看一个让人无法回答的问题:“救命各位高手,向你们请教一些问题:如何在Linux下配制HTTP、FTP、Samba、DNS、DHCP、Sendmail服务器,谢谢”这样的问题。




欢迎光临 仓酷云 (http://ckuyun.com/) Powered by Discuz! X3.2