当前位置:   article > 正文

Linux实验----进程管理 实验三_父进程从终端读取要执行的命令,并交给子进程执行。父进程等待子进程结束,并打印子

父进程从终端读取要执行的命令,并交给子进程执行。父进程等待子进程结束,并打印子

一 实验题目:

进程管理实验

二 实验目的

加深对进程概念的理解,明确进程与程序的区别;进一步认识并发执行的实质掌握进程管理、进程通信

三 实验内容

1、进程创建
2、进程控制
3、进程的管道通信
4、父进程从终端读取要执行的命令,并交给子进程执行。父进程等待子进程结束,并打印子进程的返回值。

四 实验步骤

1、进程创建

编写一段程序,使用系统调用fork()创建两个子进程。当此程序运行时,在系统中有一个父进程和两个子进程活动。让每一个进程在屏幕上显示一个字符:父进程显示“a“;子进程分别显示字符”b“和字符“c”。观察记录屏幕上的显示结果,并分析原因。

2、进程控制

修改已编写的程序,将每一个进程输出一个字符改为每一个进程输出一句话,再观察程序执行时屏幕上出现的现象,并分析原因。

3、进程的管道通信

编写程序实现进程的管道通信。使用系统调用pipe()建立一个管道,二个子进程P1和P2分别向管道各写一句话:
Child 1 is sending a message!
Child 2 is sending a message!
父进程从管道中读出二个来自子进程的信息并显示(要求先接收P1,再接收P2)。
管道是进程间通信中最古老的方式,它包括无名管道和有名管道两种,前者用于父进程和子进程间的通信,后者用于运行于同一台机器上的任意两个进程间的通信。
无名管道由pipe()函数创建:

 #include <unistd.h>
 int pipe(int filedis[2])
  • 1
  • 2

参数filedis返回两个文件描述符:filedes[0]为读而打开,filedes[1]为写而打开。filedes[1]的输出是filedes[0]的输入。下面的例子示范了如何在
父进程和子进程间实现通信。

4、父进程从终端读取要执行的命令,并交给子进程执行。父进程等待子进程结束,并打印子进程的返回值。
提示:从终端读取要执行的命令可用fgets()实现。

五、实验结果

1在这里插入图片描述
2
在这里插入图片描述
3
在这里插入图片描述
4
在这里插入图片描述

代码实现

fork.c

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
int main(int argc, char *argv[])
{
	pid_t pid1,pid2,ppid;
	printf("before fork\n");
	pid1=fork();
		if(pid1==0)
		{
			printf(" b\n");
		}
		if(pid1<0)
		{
			printf("fork\n");
		}
		else
		{
			printf("  a\n");
		}
	pid2=fork();

		if(pid2==0)
		{
			printf(" c\n");
		}
		else
		{
			printf("fork\n");
		}
return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

fork1.c

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
int main(int argc, char *argv[])
{
	pid_t pid1,pid2,ppid;
	printf("bofore fork\n");
	pid1=fork();
		if(pid1==0)
		{
			printf("successd this is a son process b\n");
		}
		if(pid1<0)
		{
			printf("fork\n");
		}
		else
		{
			printf("this is father process  a\n");
		}
	pid2=fork();
		if(pid2==0)
		{
			printf("successd this is a son process c\n");
		}
		else
		{
			printf("fork\n");
		}
return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

3_write.c

#include<unistd.h>
#include<sys/wait.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
int main(int argc,char *argv[])
{
        int fd_pipe[2];
        if(pipe(fd_pipe)<0)
        {
                printf("pipe\n");

        }
        else
        {
                printf("pipe is  success\n");
        }

        pid_t pid1;
        pid1=fork();
       	char buf[50]="Child 1 is sending a message!\n";
	printf("before fork\n");
         if(pid1==0)
	{
                printf(" this is son process b\n");
         write(fd_pipe[1],buf,strlen(buf));
	}
                wait(NULL);
                memset(buf,0,strlen(buf));
                read(fd_pipe[0],buf,sizeof(buf));
                printf("father process  a buf=%s\n",buf);
        

	 pid_t pid2;
        pid2=fork();
        char buf2[50]=" Child 2 is sending a message ! \n";
       printf("before fork\n");
        
                        
		if(pid2==0)
		{		
			printf(" this is son process c\n");
        	 write(fd_pipe[1],buf2,strlen(buf2));
        
        
        
                wait(NULL);
                memset(buf2,0,strlen(buf2));
                read(fd_pipe[0],buf2,sizeof(buf2));
                printf("father process  a  buf= %s \n",buf2);
        
	
        return 0;
}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

六 总结

1.Fork函数创建进程,还要知道进程的分类,及两个等待函数wait waitpid
2.Exit 退出函数
3.知道管道的特点

  • 半双工,同一时刻只能住在一个方向流动
  • 管道只存于内存中
  • 管道没有名字 只能用在具有公共祖先的进程间
  • 管道所传送的数据是无格式的
  • 管道只能从一段写入,另一端读出
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/968315
推荐阅读
相关标签
  

闽ICP备14008679号