操作系统实践

这篇博客详细介绍了操作系统实践,包括线程的创建与启动、Java中的Thread和Runnable类、三种创建线程的方式。重点讨论了生产者消费者问题,描述了问题背景、实现思路,并给出了Java代码实现及测试结果。最后,作者总结了实践带来的收获,加深了对操作系统和线程同步的理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

操作系统实践总结报告

1 线程的创建与启动

1.1 进程与线程

主要描述进程线程概念和差别。

进程概念:一个具有一定独立功能的程序关于某个数据集合的一次运行活动,是系统进行资源分配和调度运行的基本单位

线程概念:一个进程内部可能包含了很多顺序执行流,每个顺序执行流就是一个线程。

线程与进程的区别:

1.一个程序至少有一个进程,一个进程至少有一个线程

2.线程的划分尺度小于进程,使得多线程程序的并发性高

3.进程在执行过程中拥有独立的内存单元,而多个线程共享内存,从而极大地提高了程序的运行效率

4.每个独立的线程有一个程序运行的入口、顺序执行序列和程序的出口。但是线程不能够独立执行,必须依存在应用程序中,由应用程序提供多个线程执行控制

5.多线程的意义在于一个应用程序中,有多个执行部分可以同时执行。但操作系统并没有将多个线程看做多个独立的应用,来实现进程的调度和管理以及资源分配

 

1.2 Java中的Thread和Runnable类

实现Runnable接口比继承Thread类所具有的优势:

1):适合多个相同的程序代码的线程去处理同一个资源

2):可以避免java中的单继承的限制

3):增加程序的健壮性,代码可以被多个线程共享,代码和数据独立

 

1.3 三种创建线程的办法

1)继承Thread类创建线程:

通过继承Thread类来创建并启动多线程的一般步骤如下

1.d定义Thread类的子类,并重写该类的run()方法,该方法的方法体就是线程需要完成的任务,run()方法也称为线程执行体。

2.创建Thread子类的实例,也就是创建了线程对象

3.启动线程,即调用线程的start()方法

 

2)实现Runnable接口创建线程:

通过实现Runnable接口创建并启动线程一般步骤如下:

1.定义Runnable接口的实现类,一样要重写run()方法,这个run()方法和Thread中的run()方法一样是线程的执行体

2.创建Runnable实现类的实例,并用这个实例作为Thread的target来创建Thread对象,这个Thread对象才是真正的线程对象

3.第三部依然是通过调用线程对象的start()方法来启动线程

 

3)使用Callable和Future创建线程:

1.创建Callable接口的实现类,并实现call()方法,然后创建该实现类的实例(从java8开始可以直接使用Lambda表达式创建Callable对象)。

2.使用FutureTask类来包装Callable对象,该FutureTask对象封装了Callable对象的call()方法的返回值

3.使用FutureTask对象作为Thread对象的target创建并启动线程(因为FutureTask实现了Runnable接口)

4.调用FutureTask对象的get()方法来获得子线程执行结束后的返回值

 

 

Runnable接口创建线程

package ly;

/**

 * Runnable的实现类,是线程执行的主体。

 * run函数是入口

 * @author Administrator

 *

 */

class MyR implementsRunnable{

   private String msg;

 

   public MyR(String msg) {

     this.msg=msg;

   }

 

   @Override

   publicvoid run() {

     while(true) {

        try {

           Thread.sleep(1000);

          System.out.println(msg);

        }catch(InterruptedException e) {

          e.printStackTrace();

          break;

        }

     }

   }

  

}

 

publicclassTestThread {

   publicstaticvoid main(String[] args) {

     Threadthread1=newThread(newMyR("hello"));

     thread1.start();

     Threadthread2=newThread(newMyR("wuwu"));

     thread2.start();

    

  

   }

 

}

 

 

 

 

Thread类创建线程:

 

package ly;

 

publicclassTestThread2 {

 

   publicstaticvoid main(String[] args) {

     TestThreadtestThread2=new TestThread();

     Runnablerunnable=new Runnable() {

        publicvoid run() {

          while(true) {

             try {

               Thread.sleep(1000);

               System.out.println("haha");

             }catch(InterruptedException e) {

               e.printStackTrace();

               break;

          }

         

        }

        }

     };

     Threadthread=newThread(runnable);

     thread.start();

     }

}

 

 

    

  

 

CallableFuture创建线程:

package ly;

 

publicclassTextThread3 {

 

   publicstaticvoid main(String[] args) {

     new Thread(new Runnable(){

        publicvoid run() {

         

       

         

            

             System.out.println("haha");

         

        }

         

       

        }).start();

       

    

  

     new Thread(()-> {

        System.out.println("haha");

     }).start();

   }

 

}

线程简单同步(同步块)

2.1 同步的概念和必要性

 为什么要同步,可以举例说明

概念:同步是一个操作系统级别的概念,是在多道程序的环境下,存在着不同的制约关系,为了协调这种互相制约的关系,实现资源共享和进程协作,从而避免进程之间的冲突,引入了同步。

必要性:如果不能采取有效的措施,对多个进程的运行进行妥善的管理,必然会因为这些进程对系统资源的无序争夺给系统造成混乱,此时就需要同步机制。

 

2.2 synchronize关键字和同步块

同步块

Java 同步块用来标记方法或者代码块是同步的。Java 同步块用来避免竞争。Java 中的同步块用 synchronized 标记。

synchronize关键字机制

synchronize修饰方法

1、使用synchronize关键字的方法性能比较低

2、每一个使用synchronize修饰的方法都是临界区

3、使用synchronize修饰的对象,那么同一时间只能有一个执行线程访问,如果其他线程试图访问这个对象的其他方法,都将被挂起

 

2.3 实例

线程的同步:

package ly;

 

import com.sun.media.jfxmedia.events.NewFrameEvent;

 

publicclasstestSync {

 

    staticintc;

    static Object lock=new Object();

   publicstaticvoid main(String[] args) {

     Thread[]threads=newThread[1000];

     for(inti=0;i<1000;i++) {

        finalintindex =i;

        threads[i]=new Thread(() ->{

          synchronized(lock) {

             System.out.println("thread"+index+"enter");

          inta= c;

          a++;

          try {

             Thread.sleep((long)(Math.random()*1000));

          }catch(InterruptedException e) {

             e.printStackTrace();

          }

          c=a;

          }

        });

        threads[i].start();

     }

     for(inti=0;i<1000;i++) {

        try {

          threads[i].join();

        }catch(InterruptedException e) {

          e.printStackTrace();

     }

     // TODO Auto-generated method stub

 

   }

     System.out.print("c="+c);

 

}

}

3 生产者消费者问题

3.1 问题表述

生产者消费者问题,也称有限缓冲问题,是一个多线程同步问题的经典案例。该问题描述了两个共享固定大小缓冲区线程——即所谓的生产者消费者”——在实际运行时会发生的问题。生产者的主要作用是生成一定量的数据放到缓冲区中,然后重复此过程。与此同时,消费者也在缓冲区消耗这些数据。该问题的关键就是要保证生产者不会在缓冲区满时加入数据,消费者也不会在缓冲区中空时消耗数据。

 

3.2 实现思路

用自然语言描述这个问题的解决思路

有一群生产者进程在生产产品,并将这些产品提供给消费者进程去消费。为使生产者进程与消费者进程能并发执行,在两者之间设置了一个具有n个缓冲区的缓冲池,生产者进程将其所生产的产品放入一个缓冲区中;消费者进程可从一个缓冲区中取走产品取消费。

3.3 Java实现该问题的代码

package ly;

 

import java.util.LinkedList;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

 

publicclassQueue {

   private Lock lock=new ReentrantLock();

   private Condition fullC;

   private Condition emptyC;

   privateintsize;

   public Queue(intsize) {

     this.size=size;

     fullC= lock.newCondition();

     emptyC= lock.newCondition();

   }

   LinkedList<Integer>list=newLinkedList<Integer>();

   /**

    * 入队

    */

   publicboolean EnQueue(intdata) {

     lock.lock();

     while(list.size()>=size) {

        try {

          fullC.await();

        }catch(InterruptedException e) {

          returnfalse;

        }

    

     }

     list.addLast(data);

     emptyC.signalAll();

     lock.unlock();

     returntrue;

   }

  

   /**

    * 出队

    * @param args

    */

   publicint DeQueue() {

     lock.lock();

     while(list.size()==0) {

        try {

          emptyC.await();

        }catch(InterruptedException e) {

          lock.unlock();

          return -1;

        }

     }

     intr=list.removeFirst();

     fullC.signalAll();

     lock.unlock();

     returnr;

   }

   publicboolean isFull() {

     returnlist.size()==0;

   }

   publicboolean isEmpty() {

     returnlist.size()==0;

   }

 

}

package ly;

 

import javax.net.ssl.SSLException;

 

publicclassTestPC {

   static Queue queue=new Queue(5);

 

   publicstaticvoid main(String[] args) {

     for(inti=0;i<3;i++) {

        finalintindex=i;

        new Thread(()-> {

          intdata=(int)(Math.random()*1000);

          System.out.printf("thread %d want to EnQueue %d\n"+data);

          queue.EnQueue(data);

          System.out.printf("thread %d EnQueue %d Success\n"+data);

          sleep();

        }).start();

     }

     for(inti=0;i<3;i++) {

          finalintindex=i;

          new Thread(()-> {

             while(true) {

             System.out.printf("customer thread %d want to DnQueue %d\n",index);

             intdata =queue.DeQueue();

             System.out.printf("customer thread %d DnQueue %d Success\n",index,data);

             sleep();

             }

          }).start();

        }

       

     }

    

  

   publicstaticvoid sleep() {

     intt=(int)(Math.random()*1000);

     try {

        Thread.sleep(t);

     }catch(InterruptedException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

     }

   }

}

 

3.4 测试

3.4.1 当生产能力超出消费能力时的表现

produce thread 0 want toEnQueue 189

produce thread 0 EnQueue 189Success

customer thread 1 want toEnQueue

customer thread 1 EnQueue 189Success

produce thread 2 want toEnQueue 148

produce thread 2 EnQueue 148Success

customer thread 2 want toEnQueue

customer thread 2 EnQueue 148Success

customer thread 0 want toEnQueue

produce thread 1 want toEnQueue 21

produce thread 1 EnQueue 21Success

customer thread 0 EnQueue 21Success

3.4.2 当生产能力弱于消费能力时的表现

produce thread 0 want toEnQueue 7

produce thread 0 EnQueue 7Success

customer thread 1 want toEnQueue

customer thread 1 EnQueue 7Success

produce thread 2 want toEnQueue 6

produce thread 2 EnQueue 6Success

customer thread 2 want toEnQueue

customer thread 2 EnQueue 6Success

customer thread 0 want toEnQueue

produce thread 1 want toEnQueue 7

produce thread 1 EnQueue 7Success

customer thread 0 EnQueue 7Success

4 总结

通过操作系统的实践,让我更加清楚了操作系统的运行,这次实践课我们对线程的创建做了一次实验,了解到了线程创建的三种方式和线程的同步,对生产者-消费者问题做了一次关键性的实验,是我对这个经典问题的理解不再停留在书面所写的知识上。这次实践课使我对操作系统这门课程的理解更加明白,使我动手与思考的能力加强了不少。

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值