Linux编程常用的函数
上一篇 / 下一篇 2007-12-15 18:04:49 / 个人分类:Linux
Q"Or"\}.PQ | K0呵呵~ 为了方便自己学习转载了http://sunwill.blog.ccidnet.com/blog-htm-do-showone-type-blog-itemid-138202-uid-34371.html上的文章IXPUB技术博客,`+S[Q*]-ec
W(K`z5P"B6y~0总共分为12部分分别是:进程、线程、消息队列、信号量集、共享内存、PGSQL编程、MYSQL编程、网络编程、文件访问、标准I/O、系统数据文件和信息、信号IXPUB技术博客
WUBaeD
(一) 进程IXPUB技术博客S5@qj2Abk/yN
1. 进程ID为0的进程通常是调度进程,常常被称为交换进程IXPUB技术博客#[u5L(p pC~A hR
进程ID为1的进程通常是init进程,在自举过程结束时由内核调用IXPUB技术博客[c"E R)`6@v}h[
进程ID为2的进程页守护进程,负责支持虚拟存储系统的分页操作
+peWQ(b\~02. pid_t getpid( void ); 返回值:调用进程的进程ID #include <unistd.h>IXPUB技术博客G3JE
y
z-m
3. pid_t getppid( void ); 返回值:调用进程的父进程ID
m#E
aq/m:Os04. uid_t getuid( void ); 返回值:调用进程的实际用户IDIXPUB技术博客:d(z;@|3nn!f4F
5. uid_t geteuid( void ); 返回值:调用进程的有效用户ID
a)W#GN-r?$x06. gid_t getgid( void ); 返回值:调用进程的实际组ID
UFt*a'@ ^&e07. gid_t getegid( void ); 返回值:调用进程的有效组IDIXPUB技术博客-DQ]R2[&Qk
8. pid_t fork( void );创建子进程,返回值:子进程返回0,父进程返回子进程ID,出错-1IXPUB技术博客T@H1BE7w w ]G*f
9. #include<sys/wait.h> pid_t wait(int *statloc);//statloc 保存进程终止状态的指针IXPUB技术博客w0qW kp5}9k$U]
10. #include<sys/wait.h>pid_t waitpid(pid_t pid,int *statloc,int options);IXPUB技术博客
ctx,ZC$M
pid ==-1 等待任一子进程
4^K*GB cxgrD"W0pid >0 等待其子进程ID与pid相等的子进程
"p-f2lt"pw2s6^0pid == 0 等待其组ID等于调用进程组ID的任一子进程IXPUB技术博客/ZHWO]2pG6t C{
pid <-1 等待其组ID等于pid绝对值的任一子进程IXPUB技术博客 s/lCt9nn
options:
$y(e R.z5DO1w0WCONTINUED 若实现支持作业控制,那么由pid指定的任一子进程在暂停后已经继续,但其状态尚未报告,则返回其状态IXPUB技术博客 u6cG
{9Dy wj
WNOHANG 若由pid指定的子进程并不是立即可用的,则waitpid阻塞,此时其返回0IXPUB技术博客QXb,I
BJ
WUNTRACED 若实现支持作业控制,而由pid指定的任一子进程已处于暂停状态,并且其状态自暂停以来还未报告过,则返回其状态
7V/^bxk;b
R011.#include<unistd.h> int setuid(uid_t uid); 设置实际实际用户ID和有效用户ID;
7W;i]'J QQ#o#Td)GW9]0int setgid(gid_t gid); 设置实际组ID和有效组ID;成功返回0,错误-1IXPUB技术博客'{2aiE#N9h#S
12.#include<stdlib.h>int system(const char *cmdstring)
-m$O$@p/\\3l1`rJAj0system返回值如下
1ZDUM4KBJ0-1出现错误
{%A$V-m5uP9I6I9jEj)a0 0调用成功但是没有出现子进程
)`$pSa(_Jc0 >0 成功退出的子进程的idIXPUB技术博客eOw4Slcs9F
(二)线程IXPUB技术博客a9PO4g$Y*Y+gox
1. #include<thread.h> int pthread_equal(pthread_t tid1, pthread_t tid2);
R"h9M3K8F
Q!] L{9c0//相等返回非0,否则返回0IXPUB技术博客p#W*y}$K*k
2. pthread_t pthread_self(void);返回调用线程的IDIXPUB技术博客dcP?lM
3. int pthread_create(pthread_t *restrict tidp,
a.y2ti/n*uj0 const pthread_attr_t *restrict attr, void *(*start_rtn)(void), void *restrict arg) ;IXPUB技术博客5Q*{fCn&b;tW
r
创建线程:成功返回0,否则返回错误编号
_
fIjT"r|k5P04. void pthread_exit(void *rval_ptr);//终止线程IXPUB技术博客g
t[[5|.U)~
5. int pthread_join(pthread_t thread, void **rval_ptr);IXPUB技术博客"N-IMt
qU5C#T)A
//自动线程置于分离状态,以恢复资源。成功返回0,否则返回错误编号
%DX#_V1w;]qF&Lu06. int pthread_cancel(pthread_t tid);
iE4a AsLm^)|Ai0//请求取消同一进程中的其他线程;成功返回0,否则返回错误编号IXPUB技术博客iM A g-q"R
7. void pthread_cleanup_push(void (*rtn)(void *), void *arg);IXPUB技术博客}6}*Km
^
u
//建立线程清理处理程序
'W'jQo^08. void pthread_cleanup_pop(int execute);//调用建立的清理处理程序IXPUB技术博客6L9@!NV
B[{D
9. int pthread_detach(pthread_t tid);//使线程进入分离状态,已分离也不出错IXPUB技术博客lA3y+X2^P3f
10.int pthread_mutex_init(pthread_mutex_t *restrict mutex,IXPUB技术博客K M,l |l(M)|
const pthread_nutexattr_t *restrict attr)//初始化互斥量;成功0,失败返回错误编号IXPUB技术博客1C9K%hcm0l
11.int pthread_mutex_destroy(pthread_mutex_t *mutex);
4{&K'? zm0//若有调用malloc动态分配内存则用该函数释放;成功0,失败返回错误编号