第三次课后总结

在这节课的学习中我们主要了解了exec函数族,进程同步以及进程间通信的相关知识(主要是套接字编程)

1.exec函数族

exec:一个进程调用exec类函数,它本身就"死亡"了,系统把代码段替换成新的程序代码,废弃原有数据段和堆栈段,并为新程序分配新数据段与堆栈段

参数说明:
(1)当参数是path,传入的为路径名;当参数是file,传入的可执行文件名;
(2)可以将exec函数族分为execl和execv两类:

    execl类:函数将以列举的形式传入参数,由于参数列表的长度不定,所以要用哨兵NULL表示列举结束;
    execv类:函数将以参数向量表传递参数,char * argv[]的形式传递文件执行时使用的参数,数组中最后一个参数为NULL;
 

test_exec.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
    pid_t tempPid;
    tempPid=fork();
    if(tempPid == -1){   
        perror("fork error");
        exit(1);
    } else if(tempPid > 0) {   
        printf("parent process:pid=%d\n", getpid());
    } else {   
        printf("child process:pid=%d\n", getpid());
        //execl("/bin/ls","-a","-l","test_exec.c",NULL);	//①
        //execlp("ls","-a","-l","test_exec.c",NULL);	//②
        char *arg[]={"-a","-l","test_exec.c", NULL};	//③
        execvp("ls", arg);
        perror("error exec\n");
        printf("child process:pid=%d\n", getpid());
    } //of if  
    return 0;
} //of main

2.特殊进程

    孤儿进程:父进程负责回收子进程,如果父进程在子进程退出之前退出,子进程就会变成孤儿进程,此时init进程将代替父进程完成子进程的回收工作;
    僵尸进程:调用exit函数后,该进程不会马上消失,而是留下一个称为僵尸进程的数据结构。它几乎放弃进程退出前占用的所有内存,既没有可执行代码也不能被调度,只是在进程列表中保留一个位置,记载进程的退出状态等信息供父进程回收。若父进程没有回收子进程的代码,子进程将会一直处于僵尸态。
3.wait函数

挂起进程,进程进入阻塞状态,直到子进程变为僵尸态,如果捕获到子进程的退出信息就会转为运行态,然后回收子进程资源并返回;若没有变为僵尸态的子进程,wait函数就会让进程一直阻塞。若当前进程有多个子进程,只要捕获到一个变为僵尸态的子进程,wait函数就会恢复执行态。

test_wait.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(){
	pid_t tempPid, tempW;
	tempPid = fork();
	if(tempPid == -1){
		perror("fork error");
		exit(1);
	}else if(tempPid == 0){//child
		sleep(3);
		printf("Child process, pid = %d, ppid = %d\n", getpid(), getppid());
	}else{//parent 
		tempW = wait(NULL);
		printf("Catched a child process, pid = %d, ppid = %d\n", tempW, getppid());
	}//of if
	printf("......finish......");
	return 0;
}//of main

第2行结果打印出来前有3 秒钟的等待时间,这就是我们设定的让子进程睡眠的时间,只有子进程从睡眠中苏醒过来,它才能正常退出,也就才能被父进程捕捉到。其实这里我们不管设定子进程睡眠的时间有多长,父进程都会一直等待下去.

test_wait2.c
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(){
    int tempStatus;
    pid_t tempPid, tempW;
    tempPid = fork();
    if(tempPid == -1){
        perror("fork error");
        exit(1);
    } else if(tempPid == 0){//子
        sleep(3);
        printf("Child process: pid=%d\n",getpid());
        exit(5);
 	}  else{//父
        tempW = wait(&tempStatus);
        if(WIFEXITED(tempStatus)){
            printf("Child process pid=%d exit normally.\n", tempW );
            printf("Return Code:%d\n",WEXITSTATUS(tempStatus));
        } else {
            printf("Child process pid=%d exit abnormally.\n", tempW);
        }//of if
    }//of if
    return 0;
}//of main

父进程可以准确地捕捉到子进程的返回值并打印出来。

test_waitpid.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(){
	pid_t tempPid, tempP, tempW;
	tempPid= fork();							//创建第一个子进程
	if (tempPid == -1){							
		perror("fork1 error");
		exit(1);
	} else if (tempPid == 0){						//子进程沉睡
		sleep(5);
		printf("First child process:pid=%d\n", getpid());
	} else {						//父进程继续创建进程
		int i;
		tempP = tempPid;
		for (i = 0; i < 3; i++){					//由父进程创建3个子进程
			if ((tempPid = fork()) == 0){
				break;
			}//of if
		}//of for i
		if (tempPid == -1){						//出错
			perror("fork error");
			exit(2);
		} else if (tempPid == 0){					//子进程
			printf("Child process:pid=%d\n", getpid());
			exit(0);
		} else {					//父进程
			tempW = waitpid(tempP, NULL, 0);			//等待第一个子进程执行
			if (tempW == tempP){
				printf("Catch a child Process: pid=%d\n", tempW);
			}else{
				printf("waitpid error\n");
			}//of if
		}//of if
	}//of if
	return 0;
}//of main

当fork调用成功后,父子进程各做各的事情,但当父进程的工作告一段落,需要用到子进程的结果时,它就停下来调用wait,一直等到子进程运行结束,然后利用子进程的结果继续执行

test_waitpid2.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main() {
	pid_t tempPid, tempW;
	tempPid = fork();
	if (tempPid == -1){
		perror("fork error");
		exit(1);
	} else if (tempPid == 0){
		sleep(3);
		printf("Child process:pid=%d\n", getpid());
		exit(0);
	} else {
		do{
			tempW = waitpid(tempPid, NULL, WNOHANG);
			if (tempW == 0){
				printf("No child exited\n");
				sleep(1);
			}//of if
		} while (tempW == 0);
		if (tempW == tempPid){
			printf("Catch a Child process:pid=%d\n", w);
		}else{
			printf("waitpid error\n");
		}//of if
	}//of if
	return 0;
}//of main

父进程经过几次失败的尝试之后,终于收集到了退出的子进程。

4.进程间通信

管道类似于队列。

管道其实质是由内核管理的一个缓冲区
形象地认为管道的两端连接着两个进程:

  • 一个进程进行信息输出,将数据写入管道;
  • 另一个进程进行信息输入,从管道中读取信息。

管道不能同时进行读写操作,读的时候关闭写的功能,写的时候关闭读的功能,但是读写的内容可以共享。

管道i读数据是一次性操作,数据一旦被读出,他就从管道中丢弃,释放内存

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(){
    int tempFd[2];//定义文件描述符数组
    int tempRet=pipe(tempFd);//创建管道
    if(tempRet == -1){   
        perror("pipe");
        exit(1);
    }   
    pid_t tempPid=fork();
    if(tempPid > 0){//父进程—读
        close(tempFd[1]);//关闭写端
        char tempBuf[64]={0};
        tempRet = read(tempFd[0], tempBuf, sizeof(tempBuf));//读数据
        close(tempFd[0]);
        write(STDOUT_FILENO, tempBuf, tempRet);//将读到的数据写到标准输出
        wait(NULL);
    } else if(tempPid == 0){//子进程—写
        close(tempFd[0]);//关闭读端
        char *tempStr="hello,pipe\n";
        write(tempFd[1], tempStr, strlen(tempStr)+1);//写数据
        close(tempFd[1]);
   }//of if
   return 0;
}//of main

通过pipe函数创建的这两个文件描述符fd[0]和fd[1]分别构成管道的两端,往fd[1]写入的数据可以从fd[0]读出,并且fd[1]一端只能进行写操作,fd[0]一端只能进行读操作,不能反过来使用。要实现双向数据传输,可以使用两个管道。

经过这节课的学习,我们可以更加清晰的认识到进程同步以及进程间通信的方法,了解到了pipe函数,对我们对数据的读写有比较大的帮助。

操作系统概念背诵 一、进程管理 1. 进程管理的功能 ① 进程控制 ② 进程同步 ③ 进程通信 ④ 进程(线程)调度 2. 程序顺序执行时的特征:顺序性、封闭性、可再现性。 3. 程序并发执行时的特征:间断性、失去封闭性、不可再现性。 4. 进程由程序段、数据段和进程控制块(PCB)组成。 5. 进程的定义 ① 进程是程序的一次执行。 ② 进程是一个程序及其数据在处理机上顺序执行时所发生的活动。 ③ 进程进程实体的运行过程,是系统进行资源分配和调度的一个独立单位。 ④ 进程是程序在一个数据集合上的运行过程,是系统进行资源分配和调度的一个独立单位。 6. 进程的基本特征:动态性、并发性、独立性、异步性、结构特征(程序+数据+PCB) 7. 进程的状态 三态:就绪状态、运行状态、阻塞状态。 五态:活动就绪、静止就绪、活动阻塞、静止阻塞、运行。 8. 进程控制块(PCB)的组成:进程标识符、处理机状态、进程调度信息、进程控制信息。 9. 临界区:进程中访问临界资源的那段代码叫做临界区。 10. 同步机制必须遵循的原则:空闲让进、忙则等待、有限等待、让权等待。 11. P, V 操作的定义 P(S):S = S − 1; 若S≥0,则当前进程继续运行; 若S<0,则将当前进程插入到S 的等待队列中去。 V(S):S = S + 1; 若S>0,则当前进程继续运行; 若S≤0,则从S 的等待队列中移出一个进程放到就绪队列中去。 12. 信号量的物理意义 S = −n 时,表示有n 个等待进入临界区的进程,当前已有进程在临界区中访问临界资源; S = 0 时,表示不允许任何进程进入临界区,当前已有进程在临界区中访问临界资源; S = n 时,表示临界区是空闲的,该类资源的可用数目为n,可以有n 个进程访问该类资源。 13. 高级通信机制有:共享存储器系统、消息传递系统、管道通信系统。 14. 线程的定义:线程是进程内的一个实体,是处理机调度的基本单位,是程序内部一个单一的顺序控 制流。 15. 引入进程的目的:是为了使多个程序并发执行,提高资源利用率和系统吞吐量。 16. 引入线程的目的:是为了减少程序并发执行时的时空开销,使操作系统具有更好的并发性。
(1)进程的软中断通信 #include #include #include #include int wait_flag; void stop(); main( ) { int pid1, pid2; // 定义两个进程号变量 signal(2,stop); // 或者 signal (14,stop); while((pid1 = fork( )) == -1); // 若创建子进程1不成功,则空循环 if(pid1 > 0) { // 子进程创建成功,pid1为进程号 while((pid2 = fork( )) == -1); // 创建子进程2 if(pid2 > 0) { wait_flag = 1; //sleep(1); // 父进程等待5秒 kill(pid1,SIGUSR1); // 杀死进程1 kill(pid2,SIGUSR2); // 杀死进程2 wait(0); wait(0); printf("\n Parent process is killed !!\n"); exit(0); // 父进程结束 } else { wait_flag = 1; signal(SIGUSR2,stop); // 等待进程2被杀死的中断号17 printf("\n Child process 2 is killed by parent !!\n"); exit(0); } } else { wait_flag = 1; signal(SIGUSR1,stop); // 等待进程1被杀死的中断号16 printf("\n Child process 1 is killed by parent !!\n"); exit(0); } } void stop() { wait_flag = 0; } (2)进程的管道通信 #include #include #include int pid1,pid2; // 定义两个进程变量 main( ) { int fd[2]; char OutPipe[100],InPipe[100]; // 定义两个字符数组 pipe(fd); // 创建管道 while((pid1 = fork( )) == -1); // 如果进程1创建不成功,则空循环 if(pid1 == 0) { lockf(fd[1],1,0); // 锁定管道 sprintf(OutPipe,"\n Child process 1 is sending message!\n"); write(fd[1],OutPipe,50); // 向管道写入数据 sleep(5); // 等待读进程读出数据 lockf(fd[1],0,0); // 解除管道的锁定 exit(0); // 结束进程1 } else { while((pid2 = fork()) == -1); // 若进程2创建不成功,则空循环 if(pid2 == 0) { lockf(fd[1],1,0); sprintf(OutPipe,"\n Child process 2 is sending message!\n"); write(fd[1],OutPipe,50); sleep(5); lockf(fd[1],0,0); exit(0); } else { wait(0); // 等待子进程1 结束 read(fd[0],InPipe,50); // 从管道中读出数据 printf("%s\n",InPipe); // 显示读出的数据 wait(0); // 等待子进程2 结束 read(fd[0],InPipe,50); printf("%s\n",InPipe); exit(0); // 父进程结束 } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值