操作系统——实验壹——熟悉Linux基础命令及进程管理

一、 实验目的

  • 加深对进程概念的理解,明确进程和程序的区别。
  • 进一步认识并发执行的实质。
  • 分析进程争用资源的现象,学习解决进程互斥的方法。

二、 实验内容

  • 运行程序,查看自己运行的结果,并进行分析。
  • 编写程序,要求见附录部分

三、 代码及运行结果分析

程序一:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>  
#include <sys/wait.h>
#include <stdlib.h>
#include <error.h>
int main()
{
int i,j,k;
if (i=fork())
{
j=wait(NULL);
printf("Parent Process!\n");
printf("i=%d,j=%d,k=%d\n",i,j,k);
}
else 
{
k=getpid();
printf("Child Process!\n");
printf("i=%d,j=%d,k=%d\n",i,j,k);
}
}

在这里插入图片描述
wait阻塞了父进程,所以先执行fork返回0,进入子进程板块,打印child process!此时i=j=fork()=0,k就是当前子进程的进程id。而后子进程退出,fork()返回子进程id,即i=fork()=95496,wait()不阻塞,j为父进程捕获子进程的id,也为95496,此时k为默认值0。

在这里插入图片描述

程序二:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
#include <error.h>
int main()
{
int p1,p2;
while((p1=fork())==-1);    
if(p1==0)
{    	
printf("b.My process ID is %d\n",getpid());
}
else
{
while((p2=fork())==-1);    
if(p2==0){    			
printf("c.My process ID is %d\n",getpid());
}
else{
printf("a.My process ID is %d\n",getpid());		
}
}
}

在这里插入图片描述
fork()调用后,产生父进程和子进程。子进程打印a.My process ID is……,父进程进入else语句块执行fork(),由此父进程再复制出一个子进程,父进程打印b.My process ID is……,子进程打印c.My process ID is……。
在这里插入图片描述

程序三:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
#include <error.h>
void main()
{int m,n,k;
m=fork();
printf("PID:%d\t",getpid());
printf("The return value of fork():%d\t\t",m);
printf("he\n");
n=fork();
printf("PID:%d\t",getpid());
printf("The return value of fork():%d\t\t",n);
printf("ha\n");
k=fork();
printf("PID:%d\t",getpid());
printf("The return value of fork():%d\t\t",k);
printf("ho\n");
}

在这里插入图片描述
以he为结尾的是最开始fork()所产生的父子两个进程。一个id为96870,一个为96871,96871是96870的子进程,此时96871没有子进程,fork()返回的是0。以ha为结尾的是以上述两个进程为父进程,各自产生一个子进程。最后以ho为结尾的是以上述四个进程各自产生一个子进程所形成的。
在这里插入图片描述

程序四:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
#include <error.h>
void main()
{
int p1,p2,i;
while((p1=fork())==-1);
if(p1==0){
for(i=0;i<50000;i++){
printf("son %d\n",i);}
}
else
{
while((p2=fork())==-1);
if(p2==0){
for(i=0;i<50000;i++){
printf("daughter %d\n",i);}
}
else{
for(i=0;i<50000;i++){
printf("parent %d\n",i);}
}
}
}

在这里插入图片描述
乱序执行
在这里插入图片描述

程序五:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
#include <error.h>
void main()
{int m,n,k,j;
m=fork();
printf("PID:%d\t",getpid());
printf("The return value of fork():%d\t\t",m);
printf("he\n");
if(m>0){
k=fork();
printf("PID:%d\t",getpid());
printf("The return value of fork():%d\t\t",k);
printf("ha\n");
if(k>0){
j=fork();
printf("PID:%d\t",getpid());
printf("The return value of fork():%d\t\t",j);
printf("ho\n");
}
}
else{
n=fork();
printf("PID:%d\t",getpid());
printf("The return value of fork():%d\t\t",n);
printf("hi\n"); 
}}

在这里插入图片描述
通过条件判断对进程进行fork()函数的调用。
在这里插入图片描述
四、 实验心得

  1. 对linux操作系统有了进一步了解与熟悉
  2. 熟悉了虚拟机操作系统一些常用指令。
  3. 对进程的执行和fork函数有了更好的了解。
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 实验目的 1) 加深进程概念理解,明确进程和程序的区别。 2) 进一步认识并发执行实质。 3) 分析进程争用资源的现象,学习解决进程互斥的方法。 4) 学习解决进程同步的方法。 5) 了解Linux系统中进程通信的基本原理。   进程操作系统中最重要的概念,贯穿始终,也是学习现代操作系统的关键。通过本次实验,要求理解进程实质进程管理的机制。在Linux系统下实现进程从创建到终止的全过程,从中体会进程的创建过程、父进程和子进程之间的关系、进程状态的变化、进程之间的互斥、同步机制、进程调度的原理和以管道为代表的进程间的通信方式的实现。 2. 内容及要求:   这是一个设计型实验,要求自行编制程序。   使用系统调用pipe()建立一条管道,两个子进程分别向管道写一句话:   Child process1 is sending a message!   Child process2 is sending a message!   父进程从管道读出来自两个子进程的信息,显示在屏幕上。   要求: 1) 父进程先接收子进程1发来的消息,然后再接收子进程2发来的消息。 2) 实现管道的互斥使用,当一个子进程正在对管道进行写操作时,另一子进程必须等待。使用系统调用lockf(fd[1],1,0)实现对管道的加锁操作,用lockf(fd[1],0,0)解除对管道的锁定。 3) 实现父子进程的同步,当子进程把数据写入管道后,便去睡眠等待;当父进程试图从一空管道中读取数据时,也应等待,直到子进程将数据写入管道后,才将其唤醒。 3.相关的系统调用 1) fork() 用于创一个子进程。 格式:int fork(); 返回值:在子进程中返回0;在父进程中返回所创建的子进程的ID值;当返回-1时,创建失败。 2) wait() 常用来控制父进程与子进程的同步。 在父进程中调用wait(),则父进程被阻塞,进入等待队列,等待子进程结束。当子进程结束时,父进程从wait()返回继续执行原来的程序。 返回值:大于0时,为子进程的ID值;等于-1时,调用失败。 3) exit() 是进程结束时最常调用的。 格式:void exit( int status); 其中,status为进程结束状态。 4) pipe() 用于创建一个管道 格式:pipe(int fd); 其中fd是一个由两个数组元素fd[0]和fd[1]组成的整型数组,fd[0]是管道的读端口,用于从管道读出数据,fd[1] 是管道的写端口,用于向管道写入数据。 返回值:0 调用成功;-1 调用失败。 5) sleep() 调用进程睡眠若干时间,之后唤醒。 格式:sleep(int t); 其中t为睡眠时间。 6) lockf() 用于对互斥资源加锁和解锁。在本实验中,该调用的格式为: lockf(fd[1],1,0);/* 表示对管道的写入端口加锁。 lockf(fd[1],0,0);/* 表示对管道的写入端口解锁。 7) write(fd[1],String,Length) 将字符串String的内容写入管道的写入口。 8) read(fd[0],String,Length) 从管道的读入口读出信息放入字符串String中。 4.程序流程 父进程: 1) 创建管道; 2) 创建子进程1; 3) 创建子进程2; 4) 等待从管道中读出子进程1写入的数据,并显示在屏幕上; 5) 等待从管道中读出子进程2写入的数据,并显示在屏幕上; 6) 退出。 子进程: 1) 将管道的写入口加锁; 2) 将信息“Child process n is sending message!”输入到变量OutPipe中,n=1,2; 3) 将OutPipe中信息写入管道; 4) 睡眠等待; 5) 将管道的写入口解锁; 6) 退出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值