易截截图软件、单文件、免安装、纯绿色、仅160KB

Linux多线程编程基础

Linux系统下的多线程遵循POSIX线程接口,称为pthread。编写Linux下的多线程程序,需要使用头文件pthread.h,连接时
需要使用库libpthread.a。顺便说一下,Linux下pthread的实现是通过系统调用clone()来实现的。clone()是Linux
所特有的系统调用,它的使用方式类似fork,关于clone()的详细情况,有兴趣的读者可以去查看有关文档说明。下面我们展示一个最简单的多线程程序
example1.c。
/* example.c*/
#include <stdio.h>
#include <pthread.h>
void thread(void)
{
int i;
for(i=0;i<3;i++)
printf("This is a pthread.\n");
}
int main(void)
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,(void *) thread,NULL);
if(ret!=0)
{
   printf ("Create pthread error!\n");
   exit (1);
}
for(i=0;i<3;i++)
   printf("This is the main process.\n");
pthread_join(id,NULL);
return (0);
}
编译:gcc -o thread1 thread1.c -lpthread
运行结果
This is the main process.
This is the main process.
This is the main process.
This is a pthread.
This is a pthread.
This is a pthread.
pthread_t在头文件/usr/include/bits/pthreadtypes.h中定义:
typedef unsigned long int pthread_t;
它是一个线程的标识符。函数pthread_create用来创建一个线程,它的原型为:
extern
int pthread_create __P ((pthread_t *__thread, __const pthread_attr_t
*__attr,void *(*__start_routine) (void *), void *__arg));
第一个参数为指向线程标识符的指针,第二个参数用来设置线程属性,第三个参数是线程运行函数的起始地址,最后一个参数是运行函数的参数。这里,我们的
函数thread不需要参数,所以最后一个参数设为空指针。第二个参数我们也设为空指针,这样将生成默认属性的线程。对线程属性的设定和修改我们将在下一
节阐述。当创建线程成功时,函数返回0,若不为0则说明创建线程失败,常见的错误返回代码为EAGAIN和EINVAL。前者表示系统限制创建新的线程,
例如线程数目过多了;后者表示第二个参数代表的线程属性值非法。创建线程成功后,新创建的线程则运行参数三和参数四确定的函数,原来的线程则继续运行下一
行代码。
函数pthread_join用来等待一个线程的结束。函数原型为:
extern int


相关文档:

Linux 的多线程编程的高效开发经验

2009 年 4 月 23 日
本文中我们针对 Linux 上多线程编程的主要特性总结出 5 条经验,用以改善 Linux 多线程编程的习惯和避免其中的开发陷阱。在本文中,我们穿插一些 Windows 的编程用例用以对比 Linux 特性,以加深读者印象。
背景
Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多线程 API 有一些细微 ......

Linux主机监控工具munin monit ntop

来源:http://linux.chinaunix.net/ebook/doc/2009/09/17/1135830.shtml
本文介绍在centos 5.x环境下通过yum源的扩展使用munin、 monit、ntop工具来监管你的应用程序和服务器。题为懒人说说的是简便的安装方式而已,将强大的功能配置简单的应用起来是很重要的,可以节省时间并提高效率。
Monit:http:#www.tildeslas ......

Ubuntu 10.04 brings Linux closer to the mainstream

Ubuntu 10.04 brings Linux closer to the mainstream
No Windows viruses. Free. Any questions?
Of course. Start with this one: How can an operation system with those virtues, the open-source Linux, remain confined to a tiny minority of desktop and laptop computers at home?
Linux may run TiVo video r ......

Linux下Tomcat 6.0.26 安装

我用的是 fedora 12 自待的jdk 1.6 在 /usr/lib/jvm/java-1.6.0-openjdk
第一步:把apache-tomcat-6.0.26.tar.gz解压后放到/usr/local/下重命名为apache-tomcat-6.0.26
第二步:在/etc/profile文件中适当的位置添加如下环境变量
CATALINA_HOME=/usr/local/apache-tomcat-6.0.26
CATALINA_BASE=/usr/local/apache-tomcat ......

Linux JDK and ANT install

Linux JDK and ANT install
linux JDK的安装

安装JDK
从sun网站上直接下载JDK:http:
//java.sun.com/j2se/1.4.2
/download.html提供了两个下载:
1、RPM in self-extracting file
(j2sdk-1_4_2_04-linux-i586.bin,
32.77 MB) 这个是自解压的文件,在linux上安装如下:

chmod u+x
./j2sdk-1_4_2_04-l ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号