linux 线程编程
本章介绍 POSIX 线程的基本线程编程例程。本章介绍缺省线程(即,具有缺省属性值的线程),这是多线程编程中最常用的线程。本章还介绍如何创建和使用具有非缺省属性的线程。
本章介绍的 POSIX 例程具有与最初的 Solaris 多线程库相似的编程接口。
线程库
下面简要论述了特定任务及其相关手册页。
创建缺省线程
如果未指定属性对象,则该对象为 NULL,系统会创建具有以下属性的缺省线程:
进程范围
非分离
缺省栈和缺省栈大小
零优先级
还可以用 pthread_attr_init() 创建缺省属性对象,然后使用该属性对象来创建缺省线程。有关详细信息,请参见初始化属性一节。
pthread_create 语法
使用 pthread_create(3C) 可以向当前进程中添加新的受控线程。
int pthread_create(pthread_t *tid, const pthread_attr_t *tattr,
void*(*start_routine)(void *), void *arg);
#include <pthread.h>
pthread_attr_t() tattr;
pthread_t tid;
extern void *start_routine(void *arg);
void *arg;
int ret;
/* default behavior*/
ret = pthread_create(&tid, NULL, start_routine, arg);
/* initialized with default attributes */
ret = pthread_attr_init(&tattr);
/* default behavior specified*/
ret = pthread_create(&tid, &tattr, start_routine, arg);
使用具有必要状态行为的 attr 调用 pthread_create() 函数。 start_routine 是新线程最先执行的函数。当 start_routine 返回时,该线程将退出,其退出状态设置为由 start_routine 返回的值。请参见pthread_create 语法。
当 pthread_create() 成功时,所创建线程的 ID 被存储在由 tid 指向的位置中。
使用 NULL 属性参数或缺省属性调用 pthread_create() 时,pthread_create() 会创建一个缺省线程。在对 tattr 进行初始化之后,该线程将获得缺省行为。
pthread_create 返回值
pthread_create() 在调用成功完成之后返回零。其他任何返回值都表示出现了错误。如果检测到以下任一情况,pthread_create() 将失败并返回相应的值。
EAGAIN
描述:
超出了系统限制,如创建的线程太多。
EINVAL
描述:
tattr 的值无效。
等待线程终止
pthread_join() 函数会一直阻塞调用线程,直到指定的线程终止。
pthread_join 语法
使用 pthread_join(3C) 等待线程终止。
int pthread_join(thread_t tid, void **status);
#include <pthread
相关文档:
linux—select详解
select系统调用时用来让我们的程序监视多个文件句柄的状态变化的。程序会停在select这里等待,直到被监视的文件句柄有一个或多个发生了状态改变。
关于文件句柄,其实就是一个整数,通过socket函数的声明就明白了:
int socket(int domain, int type, int protocol);
我们最熟悉的句柄是0、1、2 ......
[转]linux启动时挂载rootfs的几种方式
一直对linux启动时挂载根文件系统的过程存在着很多疑问,今天在水木精华区找到了有用的资料,摘录如下:
1。linux启动时,经过一系列初始化之后,需要mount 根文件系统,为最后运行init进程等做准备,mount
根文件系统有这么几种方式:
1)文件系统已经存在于硬盘(或者类似的设备 ......
创建时间:2003-08-22
文章提交:raodan (raod_at_30san.com)
==Phrack Inc.==
卷标 0x0b, 期刊号 0x3d, Phile #0x0d of 0x0f
|=---------------------=[ 深入Linux网络核心堆栈 ]=-----------------------= ......
mail+uuencode
[root@room i386]# uuencode openvpn-2.0.5-1.i386.rpm openvpn-2.0.5-1.i386.rpm | mail -s youname@domain.com openvpn-2.0.5-1.i386.rpm
如果没有找到 uuencode 命令,则需要安装sharutils
[root@room i386]# yum install sharutils
未做测试,不知道是否可行,暂做保存 ......
Linux基本指令
alias
替指令取别名
◎ alias 列出目前系统所使用的所有指令别名
◎ alias 别名=Linux 指令名称 此时若输入「别名」则功能会跟输入「Linux 指令名称」相同
◎ 若想要每次开机都使用此别名,在 bash 中,一定要在 .bashrc 中指定,如果是 tcsh 中,则要在 .cshrc 中指定
ar
将许多档案备存成一个或多个 ......