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

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值