Docsity

Operating Systems: Process Control Block (PCB) Analysis - Prof. Jonathan Walpole, Study Guides, Projects, Research of Operating Systems

Toggle sidebar
bg1
CS-333 Operating Systems
Page 1
Programming Project 4:
Kernel Resource Management
Due Date:
Monday, November 14, 2005, start of class
Duration:
Two weeks
Overview and Goal
In this project, you will implement three monitors that will be used in our Kernel. These are the
ThreadManager, the ProcessManager, and the FrameManager. The code you write will be similar to
other code from the previous projects in that these three monitors will orchestrate the allocation and
freeing of resources.
There is also an additional task—reimplement the Condition and Mutex classes to provide Hoare
Semantics—but that code will not be used in the Kernel.
Download New Files
Set up a new directory for this project called cs333/p4 and download all the files from
~walpole/public_html/class/cs333/fall2005/spank/p4
Even though some of the files have the same names, be sure to get new copies for each project. In
general some files may be modified.
Please keep your old files from previous projects separate and don’t modify them once you submit them.
This is a good rule for all programming projects in all classes. If there is ever any question about
whether code was completed on time, we can always go back and look at the Unix “file modification
date” information.
For this project, you should get the following files:
makefile
DISK
Runtime.s
Switch.s
System.h
System.c
List.h
List.c
BitMap.h
BitMap.c
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Operating Systems: Process Control Block (PCB) Analysis - Prof. Jonathan Walpole and more Study Guides, Projects, Research Operating Systems in PDF only on Docsity!

Programming Project 4:

Kernel Resource Management

Due Date: Monday, November 14, 2005, start of class

Duration: Two weeks

Overview and Goal

In this project, you will implement three monitors that will be used in our Kernel. These are the ThreadManager, the ProcessManager, and the FrameManager. The code you write will be similar to other code from the previous projects in that these three monitors will orchestrate the allocation and freeing of resources.

There is also an additional task—reimplement the Condition and Mutex classes to provide Hoare Semantics—but that code will not be used in the Kernel.

Download New Files

Set up a new directory for this project called cs333/p4 and download all the files from

~walpole/public_html/class/cs333/fall2005/spank/p

Even though some of the files have the same names, be sure to get new copies for each project. In general some files may be modified.

Please keep your old files from previous projects separate and don’t modify them once you submit them. This is a good rule for all programming projects in all classes. If there is ever any question about whether code was completed on time, we can always go back and look at the Unix “file modification date” information.

For this project, you should get the following files:

makefile DISK Runtime.s Switch.s System.h System.c List.h List.c BitMap.h BitMap.c

Main.h Main.c Kernel.h Kernel.c

The packages called Thread and Synch have been merged together into one package, now called Kernel. This package contains quite a bit of other material as well, which will be used for later projects. In this and the remaining projects, you will be modifying the Kernel.c and Kernel.h files. Don’t modify the code that is not used in this project; just leave it in the package.

The Kernel.c file contains the following stuff, in this order:

Thread scheduler functions Semaphore class Mutex class Condition class Thread class ThreadManager class ProcessControlBlock class ProcessManager class FrameManager class AddrSpace class TimerInterruptHandler other interrupt handlers SyscallTrapHandler Handle functions

In this project, you can ignore everything after TimerInterruptHandler. The classes called ThreadManager, ProcessManager, and FrameManager are provided in outline, but the bodies of the methods are unimplemented. You will add implementations. Some other methods are marked “unimplemented”; those will be implemented in later projects.

The BitMap package contains code you will use; look it over but do not modify it.

The makefile has been modified to compile the new code. As before, it produces an executable called os.

You may modify the file Main.c while testing, but when you do your final run, please use the Main.c file as it was distributed. In the final version of our kernel, the Main package will perform all initialization and will create the first thread. The current version performs initialization and then calls some testing functions.

Task 1: Threads and the ThreadManager

In this task, you will modify the ThreadManager class and provide implementations for the following methods:

following as its status: READY, RUNNING, BLOCKED, JUST_CREATED, and UNUSED.) Threads should have the status UNUSED iff they are on the freeList. You’ll also need to initialize the freeList and place all Threads in the threadTable array on the freeList.

You will need to turn the ThreadManager into a “monitor.” To do this, I recommend adding a Mutex lock (perhaps called threadManagerLock) and a condition variable (perhaps called aThreadBecameFree) to the ThreadManager class. The Init method will also need to initialize threadManagerLock and aThreadBecameFree.

The GetANewThread and FreeThread methods are both “entry methods,” so they must obtain the monitor lock in the first statement and release it in the last statement.

GetANewThread will remove and return a Thread from the freeList. If the freeList is empty, this method will need to wait on the condition of a thread becoming available. The FreeThread method will add a Thread back to the freeList and signal anyone waiting on the condition.

The GetANewThread method should also change the Thread status to JUST_CREATED and the FreeThread method should change it back to UNUSED.

I have provided code for the Print method to print out the entire table of Threads.

Note that the Print method disables interrupts. The Print method is used only while debugging and will not be called in a running OS so this is okay. Within the Print method, we want to get a clean picture of the system state—a “snapshot”—(without worrying about what other threads may be doing) so disabling interrupts seems acceptable. However, the other methods—Init, GetAThread and FreeThread—must NOT disable interrupts.

In Main.c I have provided a test routine called RunThreadManager, which creates 20 threads to simultaneously invoke GetAThread and FreeThread. Let’s call these the “testing threads” as opposed to the “resource threads,” which the ThreadManager will monitor. There are 20 testing threads and only 10 resource threads.

Every thread that terminates will be added back to the freeList (by Run, which calls FreeThread). Since the tester threads were never obtained by a call to GetANewThread, it would be wrong to add them back to the freeList. Therefore, each tester thread does not actually terminate. Instead it freezes up by waiting on a semaphore that is never signaled. By the way, the tester threads are allocated on the heap, in violation of the principle that the kernel must never allocate anything on the heap, but this is okay, since this is only debugging code, which will not become a part of the kernel.

In the kernel, we may have threads that are not part of the threadTable pool (such as the IdleThread), but these threads will never terminate, so there is no possibility that they will be put onto the freeList. Thus, the only things on the freeList should be Threads from threadTable.

You will also notice that the Thread class has been changed slightly to add the following fields:

class Thread ...

fields ... isUserThread: bool userRegs: array [15] of int -- Space for r1..r myProcess: ptr to ProcessControlBlock methods ... endClass

These fields will be used in a later project. The Thread methods are unchanged.

Task 2: Processes and the ProcessManager

In our kernel, each user-level process will contain only one thread. For each process, there will be a single ProcessContolBlock object containing the per-process information, such as information about open files and the process’s address space. Each ProcessControlBlock object will point to a Thread object and each Thread object will point back to the ProcessControlBlock.

There may be other threads, called “kernel threads,” which are not associated with any user-level process. There will only be a small, fixed number of kernel threads and these will be created at kernel start-up time.

For now, we will only have a modest number of ProcessControlBlocks, which will make our testing job a little easier, but in a real OS this constant would be larger.

const MAX_NUMBER_OF_PROCESSES = 10

All processes will be preallocated in an array called processTable, which will be managed by the ProcessManager object, much like the Thread objects are managed by the ThreadManager object.

Each process will be represented with an object of this class:

class ProcessControlBlock superclass Listable fields pid: int parentsPid: int status: int -- ACTIVE, ZOMBIE, or FREE myThread: ptr to Thread exitStatus: int addrSpace: AddrSpace fileDescriptor: array [MAX_FILES_PER_PROCESS] of ptr to OpenFile methods Init () Print () PrintShort () endClass

Each process will have a process ID (the field named pid). Each process ID will be a unique number, from 1 on up.

superclass Object fields processTable: array [MAX_NUM_OF_PROCESSES] of ProcessControlBlock processManagerLock: Mutex aProcessBecameFree: Condition freeList: List [ProcessControlBlock] aProcessDied: Condition methods Init () Print () PrintShort () GetANewProcess () returns ptr to ProcessControlBlock FreeProcess (p: ptr to ProcessControlBlock) TurnIntoZombie (p: ptr to ProcessControlBlock) WaitForZombie (proc: ptr to ProcessControlBlock) returns int endClass

There will be only one ProcessManager and this instance (initialized at start-up time) will be called processManager.

processManager = new ProcessManager processManager.Init ()

The Print() and PrintShort() methods for ProcessControlBlocks are provided for you. You are to implement the methods Init, GetANewProcess, and FreeProcess. The methods TurnIntoZombie and WaitForZombie will be implemented in a later project and can be ignored.

The freeList is a list of all ProcessControlBlocks that are FREE. The status of a ProcessControlBlock should be FREE if and only if it is on the freeList.

We assume that several threads may more-or-less simultaneously request a new ProcessControlBlock by calling GetANewProcess. The ProcessManager should be a “monitor,” in order to protect the freeList from concurrent access. The Mutex called processManagerLock is for that purpose. When a ProcessControlBlock is added to the freeList, the condition aProcessBecameFree can be Signaled to wake up any thread waiting for a ProcessControlBlock.

Initializing the ProcessControlManager should initialize

the processTable array all the ProcessControlBlocks in that array the processManagerLock the aProcessBecameFree and the aProcessDied condition variables the freeList

All ProcessControlBlocks should be initialized and placed on the freeList.

The condition called aProcessDied is signaled when a process goes from ACTIVE to ZOMBIE. It will not be used in this project, but should be initialized nonetheless.

The GetANewProcess method is similar to the GetANewThread method, except that it must also assign a process ID. In other words, it must set the pid. The ProcessManager will need to manage a single integer for this purpose. (Perhaps you might call it nextPid). Every time a ProcessControlBlock is allocated (i.e., everytime GetANewProcess is called), this integer must be incremented and used to set the process’s pid. GetANewProcess should also set the process’s status to ACTIVE.

The FreeProcess method must change the process’s status to FREE and add it to the free list.

Both GetANewProcess and FreeProcess are monitor entry methods.

Task 3: The Frame Manager

The lower portion of the physical memory of the SPANK computer, starting at location zero, will contain the kernel code. It is not clear exactly how big this will be, but we will allocate 1 MByte for the kernel code. After that will come a portion of memory (called the “frame region”) which will be allocated for various purposes. For example, the disk controller may need a little memory for buffers and each of the user-level processes will need memory for “virtual pages.”

The area of memory called the frame region will be viewed as a sequence of “frames”. Each frame will be the same size and we will have a fixed number of frames. For concreteness, here are some constants from Kernel.h.

PAGE_SIZE = 8192 -- in hex: 0x PHYSICAL_ADDRESS_OF_FIRST_PAGE_FRAME = 1048576 -- in hex: 0x NUMBER_OF_PHYSICAL_PAGE_FRAMES = 512 -- in hex: 0x

This results in a frame region of 4 MB, so our kernel would fit into a 5 MByte memory.

The frame size and the page size are the same, namely 8K. In later projects, each frame will hold a page of memory. For now, we can think of each frame as a resource that must be managed. We will not really do anything with the frames. This is similar to the dice in the gaming parlor and the forks for the philosophers... we were concerned with allocating them to threads, but didn’t really use them in any way.

Each frame is a resource, like the dice of the game parlor, or the philosophers’ forks. From time to time, a thread will request some frames; the frameManager will either be able to satisfy the request, or the requesting thread will have to wait until the request can be satisfied.

For the purposes of testing our code, we will work with a smaller frame region of only a few frames. This will cause more contention for resources and stress our concurrency control a little more. (For later projects, we can restore this constant to the larger value.)

NUMBER_OF_PHYSICAL_PAGE_FRAMES = 27 -- For testing only

Here is the definition of the FrameManager class:

class FrameManager superclass Object

(b) figure out the address of the free frame (c) execute the following aPageTable.SetFrameAddr (i, frameAddr) to store the address of the frame which has been allocated (d) adjust the number of free frames (e) set aPageTable.numberOfFrames to the number of frames allocated. (f) unlock the frame manager

The code in method

ReturnAllFrames (aPageTable: ptr to AddrSpace)

needs to do more or less the opposite. It can look at aPageTable.numberOfPages to see how many are being returned. It can then go through the page table and see which frames it possessed. For each, it can clear the bit.

for i = 0 to numFramesReturned- frameAddr = aPageTable.ExtractFrameAddr (i) bitNumber = ...frameAddr... framesInUse.ClearBit(bitNumber ) endFor

It will also need to adjust the number of free frames and “notify” any waiting threads that more frames have become available.

You’ll need to do a Broadcast, because a Signal will only wake up one thread. That thread that gets awakened may not have enough free frames to complete, but other waiting threads may be able to proceed. A broadcast should be adequate, but perhaps after carefully studying the Game Parlor problem, you will find a more elegant approach which wakes up only a single thread.

Also note that there is a possibility of starvation here. It is possible that one large process will be waiting for a lot of frames (e.g., 100 frames). Perhaps there are many small processes which free a few frames here and there, but there are always other small processes that grab those frames. Since there are never more than a few free frames at a time, the big process will get starved.

This particular scenario for starvation, where processes are competing for frames) is a very real danger in an OS and a “real” OS would need to ensure that starvation could not happen. However, in our situation, it is acceptable to provide a solution that risks starvation.

You should not modify the code for the AddrSpace class.

Task 4: Change Condition Variables to Hoare Semantics

The code I have given you for the Signal method in the Condition class uses MESA semantics. Change the implementation so that it uses Hoare semantics.

With MESA semantics, you tend to see code like this in monitors:

NewResourcesHaveBecomeAvail: Condition

In method A: ... numberAvail = numberAvail + 1 NewResourcesHaveBecomeAvail.Signal() ... In method B: ... while (numberAvail == 0) NewResourcesHaveBecomeAvail.Wait() endWhile ...

The code in method B contains a while loop because there is a possibility that some other thread has snuck in between the Signal and the Wait. When method A increments numberAvail, the condition (“resources are now available for some other thread to use”) has been made true. Method A invokes Signal to wake up some thread that is waiting for resources. The problem is that some other thread may have run between the Signal and the reawakening of the Wait in method B. Other threads may have come in and grabbed all the resources. So when the waiting thread is reawakened, it must always re- check for resources (or more generally, it must check to ensure the condition is still true.) Unfortunately, the condition may have changed back to false (i.e., no resources are available), so the thread will have to wait some more.

And with MESA semantics, starvation becomes a bigger problem. What if the thread waits, wakes up, and then goes back to sleep, over and over. Each time a Signal occurs, some other thread just happens to get in first and steal the resource. Then the unlucky thread keeps waking up, testing, and going back into a waiting sleep. This is a very real possibility if you have three threads and they are scheduled in round-robin fashion. Thread A runs and signals thread C. Thread B runs and takes the resource. Thread C runs, finds the condition is false and goes back to sleep. Then thread A runs again, and so on.

With Hoare semantics, the thread needing the resources can use code like this:

... if (numberAvail == 0) NewResourcesHaveBecomeAvail.Wait() endIf ...

Once the thread is reawakened, it can be sure that nothing has happened between the Signal and it; it can be sure that no other thread has gotten into the monitor.

Starvation is easier to ensure against, too. If a thread needs a resource, it waits. We assume the waiting queue is FIFO, so that the waiting thread will eventually be awakened. And when it wakes, it will

2 ProcessControlBlock pid=2, status=ACTIVE, parentsPid=0, exitStatus=0, name="----------", addrSpace= addr entry Logical Physical Undefined Bits Dirty Referenced Writeable Valid ========== ========== ========== ========== ============== ===== ========== ========= ===== 0x00093174: 0x00100003 0x00000000 0x00100000 YES YES

We prefer to see something more like this. Even though the long lines wrap around, at least things still line up properly.

2 ProcessControlBlock pid=2, status=ACTIVE, parentsPid=0, exitStatus=0, name="---------- ", addrSpace= addr entry Logical Physical Undefined Bits Dirty Referenced Writeable Valid ========== ========== ========== ========== ============== ===== ========== ========= ===== 0x00093174: 0x00100003 0x00000000 0x YES YES

In LARGE BLOCK LETTERS, write your full name, as it appears in the PSU registration system.

PLEASE STAPLE ALL PAGES THAT YOU HAND IN. This applies to homeworks, too.

Sample Output

Here is what I see when I run my solution code. You should see something similar, if your program works correctly:

==================== SPANKISH PROGRAM STARTING ==================== Initializing Thread Scheduler... Initializing Idle Process... Initializing Process Manager... Initializing Thread Manager... Initializing Frame Manager...

***** THREAD-MANAGER TEST *****

123.4..1562.7839.1041112.13141.15....16....3295.17.8184..76.192010.111315...14..12..161..9.173188..7.19. 2.20....14410.12.91113...616...15571..18320....122..17..10..9161114586157.......4.123.1.18217.13..20...9. 19.516.1411.2..8..110.15.64.12.1618.9.7.132019..11.3.15..14.812..61.9...2.2018..10165.15.78..9...4. .3..191815....714...17.11.151216.194...18.9.102076..11.12..15...14.84191013..93..2...1511518..8.714..9. 6.19..1..2015...813.42.1210.9......520.4.2.181116.14915..4.2.1.1117..5.8.1018.15.2.4.14.9.11..1.56.8.2. ..4.1410..93..18.2013.2.15..7.17...1210914.8.20.2.....17515123.14.9.20..16.819.12.5.3.15.1.16.14.20.8.3.. 513.12.15.18..1.8..209.193.5.6.18.7.8....20.14153.511.12.4.7.8.3.14.15.9.11.12...1518.16.3..14..1117.12.. 4.5..1710..14.26.1.7..123.16.8.19.17.6.20.2.10..12.165.3.19.1.17..4...6.8.1220216.19..7.13.13.6.11.19.16. .20.4.2.1.7..13.6.11.17.19..16.14.7..1....18....1013619.16.1711.4.7.20..13.18.16.19.10.6.17..411.7..13. .19...17.10.6..7.13..1918.17.10.6.....131817.19.106.13..17...10.13.17..13..

***** THREAD-MANAGER TEST COMPLETED SUCCESSFULLY *****

***** PROCESS-MANAGER TEST *****

123.4..1562783.9.104...1...11.738.12132149.156.5.1016.17..184...19..201.11138.714.12.3....5.26.17164.3. .19..1812..629.14..1015...17.137..16.84.1820.1912...116.15.14.....572.3.89.116....10417513..15.12.1814... 23..19.716.1204.1511...14..5..9.6131237..162....18.10.1718..5..1119.61620....9184..13.17..14108.... .159..2..11317....20.11..16..7146.119515.10.4.168...714..11.92.12.1..181720..14..6319...1219..7.168... 3...411.15..129.181.14..19.32.4..6.15.128.16..19.1410.5.20.9.7..46.16.11.3.13.17.14.9.8.7.1.16.2..5..

10.9.7..164.1.11...31317.5.9.7.6.12...19.13.11.20161.5.7.12.19.3.6..1110..113.5.12..719.11.6.2.3.20.16. .5.19.11.6..207.2..9.1416.5.19.15.6.11.20.2.14.9.12.4.5.17..1118.20.2.14.9...12.5154.11.20.6..152..18.16. 14.10.5.20..63.17..9.102.1.14.11.18.13.10...739.2.16.20.14.11.10.8.3..76.16..20.13...8.3...18....12194. .138.3...918..20.1310..124.19.3.1.15.17.13..4.14.10.12...20.8..1913.4.17.10..18.8...13.19.17.4.18.15.8. .17.13.19.18..15.8.18.17.10...15.8.18.17.15...18.17.15.18.17.15.18..15..15.

***** PROCESS-MANAGER TEST COMPLETED SUCCESSFULLY *****

***** FRAME-MANAGER TEST *****

1234.57.68.9110...32.5..74...8.69..101.32.45.7.....6.9.8.310215.4......10967138...2...5107.946...1.25.. .9..10.64..79.21..5..710.8.1.3.9.2..684.19..5..6310..48..9..65.1.7.53..2.8..93.10..12.47..1..4.1086..110. .48.6..35..9.710..9.85.3..4.78.6..310.4.7.3..58.10.6..94.1..37.8..6.92.5..74..12.4.2.3.1..5.26.10.3.1.7. .8.10.3.1.9.6.8.10.3.1.4.6.10.8.3..31...134...513..1.83...3101..43..81..8..93.8.4..1.8.34..17.8.3.2.1.4. .6.4.2.8.47...83.1.5.34..1.84.3.1..84..28..10..4107...4110..5.410..18.3.1.8.3..41..105..8..4510...7105.. 10..75..71...1085..7..410.75...175..9.75.10..58.7..4.52.7.6..7.6.5.4.10.85..10.6.7.47..5..106.7.510.7.. ..26..69..2..652..28.6.1.2..36..52..36..7.29...912..97..16...10.129..76...139..7.91..24..79..101..82.6.. 1.2.6.9..71..93..61.10.9.1..62.8...298..810..48.1..6.810..1.210..8.510.8..110..9.8.10..2810..98..710..79. 8..7.210.6...27.109.8...1017.2.10.7.8.10.7..47.10.8.1.10..48.7..4.17..84.5.4.1.7..8.45.4..58.4..7.5.104.. 56...56.4..765..10.5.67.4..9.5.6.7.5.6.45.8.6.4.7.6.1.5.9.6..26..105.4.6.10.2..23..3.1.23..6.5.23...623. 0.2.6..54.2..5.3.26.3.7.2.3.1.3.1.9.2...613..41.10...1410..10.6.1.104.1..9.10..219....31019.7..10.93..10. 1.910..32.1.3..109.2.5.1.2.3.10..97.1.10.5.9.4.9.1.2.10.3.9.2.9..110..93.9.1.7..71.7..27.5.7.4.7..42..7. 10.7..48...18.75..87.10..4.8.2.8.46.7...984.5.8..9.5..74.58..10.54..7.8.5.47.8..65.4.7..5.84.5.1.5.4.9.5. 3.5.8..95.6.6..10.65.8.7.6..26...926..72..6.2.54.2.3.6..102..56.10.2...7610...8610.1..2..110.4..1210.6.. .101.9..21.6..110.2..410..81.4.10..71..410.8.2.6.1.7..76.10.5.1..7.10.2.71..106.1..102.7.10.1.2.7..74.7.. 57.9..95.9..39..5..931..57..3.6..39.510.7..2.95.8.7...1039.2.9.8.9..2.5.310.5.2.5.7.9.1..73.5..54..5.1. .10.4.2...3.25.4.9..34.25.9..34.5..79..2.3.4.21.4.7.3.5..24.9..3.52.3.4.5.2..82.6.2.5.4.7.2..94..3.2.9.6. 1.6..69.6..56.7.6.4.2..46.9..46.9.6..3.69..49.6..92.6..39.5.9.3.9..9..79.8.8.1.9..86.2..89.6..29.6..89.8. 2.8..96.8..8.6.8..98.4.8.9.8.4.8.6.8.4.8.6..3..3.2.3..93.8.3..63..83.9.3..3.2.3.8.9...3..3.3.3.3.3.3.3.3.

Here is a histogram showing how many times each frame was used: 0:






1:






2:






3:






4:





5:





6:





7:





24:



25:



26:



***** FRAME-MANAGER TEST COMPLETED SUCCESSFULLY *****

==================== SPANKISH PROGRAM TERMINATION ====================

Regarding The Due Date and Time

Please bring your project submission to class. It will be due at the beginning of class. I will collect project submissions before lecture and I will not accept project submissions after I begin class.

Working Together

I encourage students to discuss the material in this class. Feel free to discuss the programming projects amongst yourselves.

However, this is not a group project. You must create the code on your own.

It is okay to look at someone else’s code for the purposes of helping that person or for comparing your different approaches, but each student must do the coding alone. You must type in every line of code you submit. You must not copy another student’s code. Do not look at some one else’s code, then turn around and type that code in. You must create your code on your own.

It will be considered cheating to copy code.