Linux系统函数调用进程操作篇.pdf
文本预览下载声明
Untitled Document 页码,1/20
atexit(设置程序正常结束前调用的函数)
相关函数 _exit,exit,on_exit
表头文件 #includestdlib.h
定义函数 int atexit (void (*function)(void));
函数说明 atexit()用来设置一个程序正常结束前调用的函数。当程序通过调
用exit()或从main中返回时,参数function所指定的函数会先被调
用,然后才真正由exit()结束程序。
返回值 如果执行成功则返回0,否则返回-1,失败原因存于errno中。
范例 #includestdlib.h
void my_exit(void)
{
printf(“before exit () !\n”);
}
main()
{
atexit (my_exit);
exit(0);
}
执行 before exit()!
execl(执行文件)
相关函数 fork,execle,execlp,execv,execve,execvp
表头文件 #includeunistd.h
定义函数 int execl(const char * path,const char * arg,);
函数说明 execl()用来执行参数path字符串所代表的文件路径,接下来的参数
代表执行该文件时传递过去的argv(0)、argv[1]……,最后一个参
数必须用空指针(NULL)作结束。
返回值 如果执行成功则函数不会返回,执行失败则直接返回-1,失败原因
存于errno中。
范例 #includeunistd.h
main()
{
execl(“/bin/ls”,”ls”,”-al”,”/etc/passwd”,(char * )
0);
}
执行
/*执行/bin/ls -al /etc/passwd */
file://D:\linux_c\function\11.html 2004-1-9
Untitled Document 页码,2/20
-rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd
execlp(从PATH 环境变量中查找文件并执行)
相关函数 fork,execl,execle,execv,execve,execvp
表头文件 #includeunistd.h
定义函数 int execlp(const char * file,const char * arg,……);
函数说明 execlp()会从PATH 环境变量所指的目录中查找符合参数file的文件
名,找到后便执行该文件,然后将第二个以后的参数当做该文件的
argv[0]、argv[1]……,最后一个参数必须用空指针(NULL)作结
束。
返回值 如果执行成功则函数不会返回,执行失败则直接返回-1,失败原因
存于errno 中。
错误代码 参考execve()。
范例 /* 执行ls -al /etc/passwd execlp()会依PATH 变量中的/bin找
到/bin/ls */
显示全部