打印本文 打印本文 关闭窗口 关闭窗口
Linux C 函数参考(进程操作)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数3273  更新时间:2009/4/22 23:08:52  文章录入:mintao  责任编辑:mintao
#include<unistd.h>
main()
{
if(vfork() = =0)
{
printf(“This is the child process\n”);
}else{
printf(“This is the parent process\n”);
}
}
执行 this is the parent process
this is the child process
 


getpgid(取得进程组识别码)
相关函数 setpgid,setpgrp,getpgrp
表头文件 #include<unistd.h>
定义函数 pid_t getpgid( pid_t pid);
函数说明 getpgid()用来取得参数pid 指定进程所属的组识别码。如果参数pid为0,则会取得目前进程的组识别码。
返回值 执行成功则返回组识别码,如果有错误则返回-1,错误原因存于errno中。
错误代码 ESRCH 找不到符合参数pid 指定的进程。
范例 /*取得init 进程(pid=1)的组识别码*/
#include<unistd.h>
mian()
{
printf(“init gid = %d\n”,getpgid(1));
}
执行 init gid = 0
 


getpgrp(取得进程组识别码)
相关函数 setpgid,getpgid,getpgrp
表头文件 #include<unistd.h>
定义函数 pid_t getpgrp(void);
函数说明 getpgrp()用来取得目前进程所属的组识别码。此函数相当于调用getpgid(0);
返回值 返回目前进程所属的组识别码。
范例 #include<unistd.h>
main()
{
printf(“my gid =%d\n”,getpgrp());
}
执行 my gid =29546
 


getpid(取得进程识别码)
相关函数 fork,kill,getpid
表头文件 #include<unistd.h>
定义函数 pid_t getpid(void);
函数说明 getpid()用来取得目前进程的进程识别码,许多程序利用取到的此值来建立临时文件,以避免临时文件相同带来的问题。
返回值 目前进程的进程识别码
范例 #include<unistd.h>
main()
{
printf(“pid=%d\n”,getpid());
}
执行 pid=1494 /*每次执行结果都不一定相同*/
 


getppid(取得父进程的进程识别码)
相关函数 fork,kill,getpid
表头文件 #include<unistd.h>
定义函数 pid_t getppid(void);
函数说明 getppid()用来取得目前进程的父进程识别码。
返回值 目前进程的父进程识别码。
范例 #include<unistd.h>
main()
{
printf(“My parent ‘pid =%d\n”,getppid());
}
执行 My parent pid =463
 


getpriority(取得程序进程执行优先权)
相关函数 setpriority,nice
表头文件 #include<sys/time.h>
#include<sys/resource.h>
定义函数 int getpriority(int which,int who);
函数说明 getpriority()可用来取得进程、进程组和用户的进程执行优先权。
参数 which有三种数值,参数who 则依which值有不同定义
which who 代表的意义
PRIO_PROCESS who 为进程识别码
PRIO_PGRP who 为进程的组识别码
PRIO_USER who 为用户识别码
此函数返回的数值介于-20 至20之间,代表进程执行优先权,数值越低代表有较高的优先次序,执行会较频繁。
返回值 返回进程执行优先权,如有错误发生返回值则为-1 且错误原因存于errno。
附加说明 由于返回值有可能是-1,因此要同时检查errno是否存有错误原因。最好在调用次函数前先清除errno变量。
错误代码 ESRCH 参数which或who 可能有错,而找不到符合的进程。EINVAL 参数which 值错误。
 


nice(改变进程优先顺序)
相关函数 setpriority,getpriority
表头文件 #include<unistd.h>
定义函数 int nice(int inc);
函数说明 nice()用来改变进程的进程执行优先顺序。参数inc数值越大则优先顺序排在越后面,即表示进程执行会越慢。只有超级用户才能使用负的inc 值,代表优先顺序排在前面,进程执行会较快。
返回值 如果执行成功则返回0,否则返回-1,失败原因存于errno中。
错误代码 EPERM 一般用户企图转用负的参数inc值改变进程优先顺序。
 


on_exit(设置程序正常结束前调用的函数)
相关函数 _exit,atexit,exit
表头文件 #include<stdlib.h>
定义函数 int on_exit(void (* function)(int, void*),void *arg);
函数说明 on_exit()用来设置一个程序正常结束前调用的函数。当程序通过调用exit()或从main中返回时,参数function所指定的函数会先被调用,然后才真正由exit()结束程序。参数arg指针会传给参数function函数,详细情况请见范例。
返回值 如果执行成功则返回0,否则返回-1,失败原因存于errno中。
附加说明
范例 #include<stdlib.h>
void my_exit(int status,void *arg)
{
printf(“before exit()!\n”);
printf(“exit (%d)\n”,status);
printf(“arg = %s\n”,(char*)arg);
}
main()
{
char * str=”test”;
on_exit(my_exit,(void *)str);
exit(1234);
}
执行 before exit()!
exit (1234)
arg = test
 


setpgid(设置进程组识别码)
相关函数 getpgid,setpgrp,getpgrp
表头文件 #include<unistd.h>
定义函数 int setpgid(pid_t pid,pid_t pgid);
函数说明 setpgid()将参数pid 指定进程所属的组识别码设为参数pgid 指定的组识别码。如果参数pid 为0,则会用来设置目前进程的组识别码,如果参数pgid为0,则会以目前进程的进程识别码来取代。
返回值 执行成功则返回组识别码,如果有错误则返回-1,错误原因存于errno中。
错误代码 EINVAL 参数pgid小于0。
EPERM 进程权限不足,无法完成调用。
ESRCH 找不到符合参数pid指定的进程。
 


setpgrp(设置进程组识别码)
相关函数 getpgid,setpgid,getpgrp
表头文件 #include<unistd.h>
定义函数 int setpgrp(void);
函数说明 setpgrp()将目前进程所属的组识别码设为目前进程的进程识别码。此函数相当于调用setpgid(0,0)。
返回值 执行成功则返回组识别码,如果有错误则返回-1,错误原因存于errno中。
 


setpriority(设置程序进程执行优先权)
相关函数 getpriority,nice
表头文件 #include<sys/time.h>
#include<sys/resource.h>
定义函数 int setpriority(int which,int who, int prio);
函数说明 setpriority()可用来设置进程、进程组和用户的进程执行优先权。参数which有三种数值,参数who 则依which值有不同定义
which who 代表的意义
PRIO_PROCESS who为进程识别码
PRIO_PGRP who 为进程的组识别码
PRIO_USER who为用户识别码
参数prio介于-20 至20 之间。代表进程执行优先权,数值越低代表有较高的优先次序,执行会较频繁。此优先权默认是0,而只有超级用户(root)允许降低此值。
返回值 执行成功则返回0,如果有错误发生返回值则为-1,错误原因存于errno。
ESRCH 参数which或who 可能有错,而找不到符合的进程
EINVAL 参数which值错误。
EPERM 权限不够,无法完成设置
EACCES 一般用户无法降低优先权
 


system(执行shell 命令)
相关函数 fork,execve,waitpid,popen
表头文件 #include<stdlib.h>
定义函数 int system(const char * string);
函数说明 system()会调用fork()产生子进程,由子进程来调用/bin/sh-c string来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程。在调用system()期间SI

上一页  [1] [2] [3] [4]  下一页

打印本文 打印本文 关闭窗口 关闭窗口