课堂笔记七之Linux进程控制编程

本文介绍了Linux进程控制编程,涵盖进程定义、生命周期、状态、进程ID、进程互斥与同步、调度算法等内容,并讨论了fork、vfork和exec函数族在进程创建与执行中的应用。

1、fprintf(格式化输出数据至文件)
表头文件:#include <stdio.h>
定义函数:
int fprintf(FILE * stream,const char *format,…);
函数说明:
fprintf()会根据参数format字符串来转换并格式化数据,然后将结果输出到参数steam指定的文件中,直到出现字符串结束(‘、0’)为止。
返回值:成功则返回实际输出的字符串,失败则返回-1,错误原因存于errno中。

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#incude <fcntl.h>

int main(int argc,char* argv[]){
if(agrc != 2){
printf("argument is invalid!");
exit(1);
}
FILE * fp=fopen(argv[1],"r+");
if(fp==NULL){
perror("fopen error!");
exit(1);
}
int a[]={1,2,3,4,5,6,7,8,9,10},i;
for(i=0;i<10;i++){
fprintf(fp,"index=%d,value=%d\n",i,a[i]);
}
if(fclose(fp)==0){
printf("close successful!\n");
}else{
perror("close error!");
}
}

2、fcanf(格式化字符串输入)
表头文件:#include <stdio.h>
定义函数:
In t fscanf(FILE * stream,const char *format,…)
函数说明:
fscanf()会从参数stream的文件流中读取字符串,再根据参数format字符串来转换并格式化数据。
返回值:
成功则返回参数数目,
失败则返回-1,错误原因存于errno中。

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#incude <fcntl.h>

int main(int argc,char* argv[]){
if(agrc != 2){
printf("argument is invalid!");
exit(1);
}
FILE * fp=fopen(argv[1],"r+");
if(fp==NULL){
perror("fopen error!");
exit(1);
}
int i,index,value,pos;
for(i=0;i<10;i++){
fscanf(fp,"index=%d,value=%d\n",&index,&value);
pos=ftell(fp);
printf("pos:read:%d,%d\n",pos,index,value);
}
if(fclose(fp)==0){
printf("close successful!\n");
}else{
perror("close error!");
}
}
一、进程控制编程

①进程控制理论基础
②进程控制编程

1、进程的定义
进程是一个具有一定独立功能的程序的一次运行活动,同时也是资源分配的最小单元;
2、进程与程序
(1)进程是动态的,程序是静态的:程序是有序代码的集合;进程是程序的执行。通常进程不可在计算机之间迁移;而程序通常对应着文件、静态和可以复制;
(2)进程是暂时的,程序是长久的:进程是一个状态变化的过程,程序可长久保存;
(3)进程与程序组成不同:进程的组成包括程序、数据和进程控制块(即进程状态信息);
(4)进程与程序的对应关系:通过多次执行,一个程序可对应多个进程;通过调用关系,一个进程可包括多个程序。

#include <stdio.h>

int main(){
int i=0;
while(1){
printf("pid:%d,i=%d\n",getpid(),i++);
sleep(1);
}
}

3、进程的生命周期
(1)创建:每个进程都是由其父进程创建,进程可以创建子进程,子进程又可以创建子进程的子进程;
(2)运行:多个进程可以同时存在,进程间可以通信;
(3)撤销:进程可以被撤销,从而结束一个进程的运行。
4、进程的状态
(1)执行状态:进程正在占用CPU
(2)就绪状态:进程已具备一切条件,正在等待分配CPU的处理时间片;
(3)等待状态:进程不能使用CPU,若等待事件发生则可将其唤醒。
在这里插入图片描述
5、Linux进程
(1)Linux系统是一个多进程的系统,它的进程之间具有并行性、互不干扰等特点;
(2)也就是说,每个进程都是一个独立的运行单位,拥有各自的权利和责任。其中,各个进程都运行在独立的虚拟地址空间,因此,即使一个进程发生异常,它也不会影响到系统中的其他进程。
6、Linux下进程地址空间
(1)Linux中的进程包含3个段,分别为“数据段”、“代码段”和“堆栈段”;
(2)“数据段”存放的是全局变量、常数以及动态数据分配的数据空间;
(3)“代码段”存放的是程序代码的数据;
(4)“堆栈段”存放的是子程序的返回地址、子程序的参数以及程序的局部变量等。
在这里插入图片描述
7、进程ID
(1)进程ID(PID):标识进程的唯一数字;
(2)父进程的ID(PPID);
(3)启动进程的用户ID(UID)。
8、进程互斥
(1)进程互斥是指当有若干进程都要使用某一共享资源时,任何时刻最多允许一个进程使用,其他要使用该资源的进程必须等,直到占用该资源者释放了该资源为止。
9、临界资源
(1)操作系统中将一次只允许一个进程访问的资源称为临界资源。

#include <stdio.h>

int main(){
int i=0;
while(1){
printf("pid:%d,i=%d\n",getpid(),i++);
usleep(100000);
}
int balance =100;
while(balance>0){
balance --;
}
printf("%d",balance);
}

10、临界区
(1)进程中访问临界资源的那段程序代码称为临界区,为实现对临界资源的互斥访问,应保证进程互斥地进入各自的临界区。
11、进程同步
一组并发进程按一定的顺序执行的过程称为进程间的同步;具有同步关系一组并发进程称为合作进程;合作进程间互相发送的信号称为消息或事件。
12、进程调度
(1)概念:
按一定算法,从一组待运行的进程中选出一个来占有CPU运行。
(2)调度方式:
1)抢占式
2)非抢占式
13、调度算法
(1)先来先服务调度算法
(2)短进程优先调度算法
(3)高优先级优先调度算法
(4)时间片轮转法
14、死锁
(1)多个进程因竞争资源而形成一种僵局,若无外力作用,这些进程都将永远不能再向前推进。
15、获取ID
#include <sys/types.h>
#include <unistd.h>
pid_t getpid(viod) 获取本进程ID
pid_t getppid(vid) 获取父进程ID

#include <stdio.h>

int main(){
int i=0;
while(1){
printf("pid:%d,ppid=%d,i=%d\n",getpid(),getppid(),i++);
sleep(1);
}
}
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(viod)
{
printf("PID=%d\n",getpid());
printf("PPID=%d\n",getppid());
return 0;
}

16、进程创建
(1)#include <unistd.h>
pid_t fork(viod)
1)功能:创建子进程
fork的奇妙之处在于它被调用一次,却返回两次,它可能有三种不同的返回值。
2)返回值:
0:子进程
子进程ID(大于0):父进程
-1:出错

#include <stdio.h>
#include <unistd.h>

int main(){
int i=0;
pid_t pid=fork();
printf("······%d······\n",pid);
}
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> 

int main(){
int i=0;
pid_t pid=fork();
if(pid==-1){
perror("fork error!");
exit(1);
}
if(pid==0){//sub process
for(;i<10;i++){
sleep(1);
printf("chiid:%d\n,i");
}
}else if(pid>0){//parent process
for(;i<10;i++){
sleep(1);
printf("parent:%d\n,i");
}
}
printf("······%d······\n",pid);
}
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid;
/*此时仅有一个进程*/
pid=fork();
/*此时已经有两个进程在同时运行*/
if(pid<0)
printf("error in fork!");
else if(pid==0)
printf("I am the child process,ID is %d\n",getpid());
else
printf("I am the parent process,ID is %d\n",getpid);
}

3)在pid=fork(),只有一个进程在执行,但在这条语句执行之后,就变成两个进程在执行了,这两个进程的共享代码段,将要执行的下一条语句都是if(pid==0)。
4)两个进程中,原来就存在的那个进程被称作“父进程”,新出现的那个进程被称作“子进程”,父子进程的区别在于进程标识符(PID)不同。

int main(void)
{
pid_t pid;
int count=0;
pid=fork();
if(0==pid)
{
count++;
printf("count=%d\n",count);
}
else if(pid>0)
{
count++;printf("count=%d\n",count);
}
return 0;
}

5)子进程的数据空间、堆栈空间都会从父进程得到一个拷贝,而不是共享。
6)在子进程中对count进行加1的操作,并没有影响到父进程中的count值,父进程中的count值仍然为0.
(2)进程创建——vfork
1)
#include <sys/types.h>
#include <unistd.h>
pid_t vfork(viod)
功能:创建子进程

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> 

int main(){
int i=0;
pid_t pid;
if(pid==-1){
perror("fork error!");
exit(1);
}
for(;i<5;i++){
pid=fork();
if(pid==0){
printf("chiid:pid=%d,ppid=%d\n,"getpid(),getppid());
}else if(pid>0){
printf("chiid:pid=%d,ppid=%d\n,"getpid(),getppid());
}
}
while(1){
sleep(1);
}
printf("······%d······\n",pid);
}

2)表头文件:#include <unistd.h>
定义函数:pid_t vfork(void);
函数说明:
vfork()会产生一个新的子进程,其子进程会复制父进程的数据与堆栈空间,并继承父进程的用户代码,组代码,环境变量、已打开的文件代码、工作目录和资源限制等。
子进程不会继承父进程的文件锁定和未处理的信号。
注意,Linux中的fork不保证子进程会比父进程先执行或晚执行,因此编写程序时要留意死锁或竞争条件的发生。
vfork保证子进程先运行,共享内存。
(3)fork PK vfork
区别:
1)fork:子进程拷贝父进程的数据
vfork:子进程与父进程共享数据
2)fork:父、子进程的执行次序不确定
vfork:子进程先执行,父进程后运行
(4)exec函数族
1)exec用被执行的程序替换调用它的程序
区别:
fork创建一个新的进程,产生一个新的PID。
exec启动一个新程序,替换原有的进程,因此进程的PID不会改变。
2)#include <unistd.h>
int execl(const char *path,const char *arg1,…)
参数:
path:被执行程序名(含完整路径)。
arg1-argn:被执行程序所需的命令行参数,含程序名。以空指针(NULL)结束。
3)#include <unistd.h>
int exec(const char *path,char *const argv[])
参数:
path:被执行程序名(含完整路径)。
argv[]:被执行程序所需的命令行参数数组。
17、进程等待
#include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid(pid_t pid,int *status,int options)
功能:
会暂时停止目前进程的执行,直到有信号来到或子进程结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值