I have made an OS simulator for a project, for the next part I have to modify the simulator to include 4 CPUs in place of only 1 CPU from the previous part, so that all the processes get done in a faster time. 开发者_StackOverflow
So I have to add concurrency but I am not sure what the right design pattern is for this kind of thing. I need to know if the following will actually give me a speed up.
CPU extends Thread
//in main
get process1
get process 2
get process 3
get process 4
cpu1.run(process1)
cpu2.run(process2)
cpu3.run(process3)
cpu4.run(process4)
am I right in assuming that because cpus are extending thread they will all run concurrently for finish the 4 processes or will it be just like running the 4 processes on a single CPU?
By the nature of the question, this is a class project and that your representation of a cpu is relatively simple. For example, just runs a series of instructions like thread class. If however, you are trying to emulate real world CPUs and microprocessors, we need to know more about the cpu features: scheduling, event handling and other low level aspects that are normally hidden.
But, in the simple case, the answer is generally yes.
Note, depending on the tasks in those processes and the CPU you run this code on, you may see different behaviors because of how the CPU and JVM are actually implementing threads. But, I think in your case it isn't relevant.
It depends mainly on 3 things:
- The kind of operations involved in threads. Do they share variables? Do they need synchronization between themselves or are they completely indipendent?
- The environment in which they are executed. You inserted Java tag but it's not clear if you want to give your simulator the ability to schedule processes on multi (real) cpus or just use more than one core. By the way if you plan to use more real CPUs you have to avoid green threads (wikipedia).
- If you want to use multiple real cores you have to care about the structure of the CPU too. Which kind of cache do they share? And so on..
Do you have anykind of simulated scheduler inside your OS?
Time slicing says that you will have to divide the single CPU among the N threads that you decide to start. There won't be any parallelism.
In you example each CPU is able to run a process concurrently, so if you just have 4 processes you're doing good.
If you want your program to work also for the case where there are more processes than CPUs you need something more complex. In that case I would recommend you take a look at the Java concurrency framework. For the simplest solution when you have more than 4 processes that you want to run I would use ExecutorService.newFixedThreadPool(4), and add each process (as a Callable) to the resulting thread-pool using either invokeAll() or submit().
BUT this does not give you concurrency between all running processes (it will only pick up the 5th process when one of the first 4 processes has completed). If you want your program to act as a real multi-threaded OS (where more processes can be active than CPUs available) you'll have to add some sort of scheduler that can assign a part of a process on one of the available CPUs, then (before the first is done) let another process use that same CPU for a part of its work, etc.. So it will have to allow for part of processes to be done, then do part of one or more other processes then let the first do a bit more of its work, etc until all processes are done. Your simulator then also needs some way to decide when a process can be 'paused' (i.e. put aside by the scheduler to be picked up later)...
精彩评论