您的位置:首页 > 运维架构 > Linux

LINUX学习笔记11——进程控制编程

2011-12-06 20:57 302 查看
1. 获取本进程ID: pid_t getpid(void),在程序中加入这个函数,就能在程序运行时获取本进程的ID号:printf(“pid=%d”,getpid()); 以整数形式输出
a) #include<unistd.h>
2. 获取父进程ID: pid_t getppid(void)
a) #include<unistd.h>
3. 创建子进程:pid_t fork(void)
a) 程序:创建的子进程运行父进程中fork函数以下的代码.共享代码段,数据段会重新拷贝一遍。
b) #include<unistd.h>
c) 作用:
d) 返回值:在父进程于子进程中都返回一个值
1. ID号:在父进程中,fork返回子进程ID号
2. 负值:出现错误,
3. 0:在子进程中,fork返回0,并不代表ID为0
4. 创建子进程:pid_t vfork(void)
a) #include<unistd.h>
b) 区别:
1. 共享数据段
2. 子进程先运行,父进程后运行;fork不确定
5. 替换进程:exec函数族
a) 作用:进程号不变,但是启动新程序替换原有的的程序
b) Int execl(const char *path, const char *arg1,…,NULL)
1. Cxecl(“/etc/ls”,”ls”,”-al”,” /etc/passwd”,NULL)
2. Path: 被执行的程序名,含完整路径
3. #include<unistd.h>
4. Arg1-argn: 被执行程序所需的命令行参数,第1个参数是程序名,以空指针结束
c) Int execlp(const char *path, const char *arg1,…,NULL)
1. Cxeclp(“ls”,”ls”,”-al”,” /etc/passwd”,NULL)
2. #include<unistd.h>
3. Path: 被执行的程序名,但是不包含完整路径,从LINUX系统的PATH指令中去找。
d) Int execv(const char *path, const char *argv[ ],NULL)
1. Path: 被执行的程序名,含完整路径
2. argv[ ]: 字符串数组
3. #include<unistd.h>
e) int system(const char* string)
1. 例:system(“ls –al /etc/passwd”)
2. #include<stdlib.h>
f) 功能:调用fork产生子进程,由子进程来调用/bin/sh –c string 来执行参数string 所代表的命令。相当于用shell来解析命令
6. 进程等待:pid_t wait(int* status)
a) Pid_t wait(NULL);
b) #include<sys/types.h>
c) #include<sys/wait.h>
d) 功能:阻塞该进程,直到某个子进程退出
e) 返回值:退出子进程的进程号
f) Status:返回进程结束状态值,如果不需要NULL
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: