第九章 Linux下C语言编程--线程操作_第1页
第九章 Linux下C语言编程--线程操作_第2页
第九章 Linux下C语言编程--线程操作_第3页
第九章 Linux下C语言编程--线程操作_第4页
第九章 Linux下C语言编程--线程操作_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Linux 下 C 语言编程 线程操作 前言 Linux 下线程的创建 介绍在 Linux 下线程的创建和基本的使用 Linux 下的线程是一个非常复杂的问题 由于 我对线程的学习不时很好 我在这里只是简单的介绍线程的创建和基本的使用 关于线程的高 级使用 如线程的属性 线程的互斥 线程的同步等等问题 可以参考我后面给出的资料 现在 关于线程的资料在网络上可以找到许多英文资料 后面我罗列了许多链接 对线程的高级属性 感兴趣的话可以参考一下 等到我对线程的了解比较深刻的时候 我回来完成这篇文章 如果 您对线程了解的详尽我也非常高兴能够由您来完善 先介绍什么是线程 我们编写的程序大多数可以看成是单线程的 就是程序是按照一定的顺序 来执行 如果我们使用线程的话 程序就会在我们创建线成的地方分叉 变成两个 程序 在执 行 粗略的看来好象和子进程差不多的 其实不然 子进程是通过拷贝父进程的地址空间来执 行的 而线程是通过共享程序代码来执行的 讲的通俗一点就是线程的相同的代码会被执行几 次 使用线程的好处是可以节省资源 由于线程是通过共享代码的 所以没有进程调度那么复 杂 线程的创建和使用 线程的创建是用下面的几个函数来实现的 include 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 include include include include include include include include include include 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 else if bytes read 0 buffer p buffer while bytes write write outfile buffer p bytes read 0 if bytes write 1 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 if stat filename 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 j byte copy 0 for i 0 i 等待线程结束 if pthread join thread i void 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 Co

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论