操作系统程序题JAVA练习

1.某车站售票厅,任何时刻最多容纳20名购票者进入,当售票厅中少于20名购票者时,则厅外的购票者可立即进入,否则则需要在外面等待。若把一个购票者看做一个进程,请编程。


/**
 * 车站只能进20人
 */
public class Station {
    Semaphore people =new Semaphore(20);

    /**
     * 进站
     */
   void customIn() {
       new Thread() {
           @Override
           public void run() {
               while (true) {
                   try {
                       people.acquire();
                       System.out.println("俺进入车站了");
                   } catch (InterruptedException e) {
                       System.out.println("咋不让俺进去啊");
                       e.printStackTrace();
                   }
               }
           }
       }.start();
    }

    /**
     * 出站
     */
    void customOut() {
        new Thread() {
            @Override
            public void run() {
                while (true) {
                           people.release();
                        System.out.println("俺买完票了");
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                }
            }.start();
    }
    public static void main(String[] args) {
       Station station=new Station();
        station.customOut();
        station.customIn();
    }
}

2.请给出记录型信号量中对P、V操作的定义

伪代码

P(s){
value--;
if(value<0){
add this process to list
block
}
}
V(s){
value++;
if(value<=0){
remove a process P from list
wakeup(P);
}
}

3.开会签到,一次由一个人在一张签到表上签到,开会结束,参会人员在依次在签到表上签退。一个会场只能容纳20人;

public class SignUp {
    Semaphore mutext = new Semaphore(1);
    Semaphore people = new Semaphore(20);
    public void SignUp() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    try {
                        people.acquire();
                        mutext.acquire();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("签到签到");
                    mutext.release();
                }
            }
        }).start();

    }
    public void SignOut(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    try {
                        mutext.acquire();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("签退签退");
                    people.release();
                    mutext.release();
                }
            }
        }).start();
    }

    public static void main(String[] args) {
        SignUp sign=new SignUp();
            sign.SignUp();
            sign.SignOut();
    }
}

4.1、输入进程不断地从磁盘读入记录存入缓冲区(假设该缓冲区的大小正好等于两条磁盘记录),计算进程不断地从缓冲区取数据进行计算,要求输入进程和计算进程之间的合作必须保持同步:即输入进程不能向满的缓冲区内存记录,计算进程不能从空的缓冲区内取数据。用P、V原语描述输入进程Input和计算进程Calculate之间的合作。

public class Disk {
    Semaphore empty = new Semaphore(20);
    Semaphore full = new Semaphore(0);
    Semaphore mutext = new Semaphore(1);
    public void Input(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    try {
                        System.out.println("读磁盘记录");
                        empty.acquire();
                        mutext.acquire();
                        System.out.println("存入缓冲区");
                        full.release();
                        mutext.release();
                    } catch (Exception e) {

                    }
                }
            }
        }).start();
    }
    public void Output(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        full.acquire();
                        mutext.acquire();
                        System.out.println("取出磁盘记录");
                        System.out.println("进行计算");
                        empty.release();
                        mutext.release();
                    } catch (Exception e) {

                    }
                }
            }
        }).start();
    }

    public static void main(String[] args) {
        Disk disk =new Disk();
        disk.Input();
        disk.Output();
    }
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值