CS 170

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

How do you implement a semaphore using pthreads:

#include < stdlib.h > #include < unistd.h > #include < stdio.h > #include < pthread.h > typedef struct { pthread_mutex_t lock; pthread_cond_t wait; int value; int waiters; } sema; sema *InitSem(int count) { sema *s; s = (sema *)malloc(sizeof(sema)); if(s == NULL) { return(NULL); } s->value = count; s->waiters = 0; pthread_cond_init(&(s->wait),NULL); pthread_mutex_init(&(s->lock),NULL); return(s); } void P(sema *s) { pthread_mutex_lock(&(s->lock)); s->value--; while(s->value < 0) { /* * maintain semaphore invariant */ if(s->waiters < (-1 * s->value)) { s->waiters++; pthread_cond_wait(&(s->wait),&(s->lock)); s->waiters--; } else { break; } } pthread_mutex_unlock(&(s->lock)); return; } void V(sema *s) { pthread_mutex_lock(&(s->lock)); s->value++; if(s->value <= 0) { pthread_cond_signal(&(s->wait)); } pthread_mutex_unlock(&(s->lock)); }

Disk Block Descriptor Table

------------------------------------------------- | swap device # | disk block # | swap file type | -------------------------------------------------

What are the entries of a frame table

-------------------------------------------------- | ref count | swap device # | disk block # | PTE | --------------------------------------------------

What are the entries in a page table

-------------------------------------------------------- | Physical Frame # | valid | modified | referenced | protection | --------------------------------------------------------

A Trap or Exception Occurs When

1) A User Attempts to Do Something Illegal 2) Attempts Something Impossible 3) Would Like the OS to Act on its Behalf

What Happens when an Exception is Thrown

1)When a program is running in the R3000 user space, it is running in non-privieldged mode. when the code makes a system call, it executes a trap instruction 2)the R3000 CPU records the trap (exception) type and the code for the specific system call and passes these to your OS in priviledged mode the C function void 3)exceptionHandler(ExceptionType which) the type of exception (SystemCall being one of them) is passed in the which variable the specific system call code is passed in registers[4] 4)you use the examine_registers() function to retrieve the registers associated with the user process at the time of the trap you execute your OS code to return to the process you 5)oad the integer return value in registers[2] load registers[PCReg] with registers[NextPCReg] call run_user_process(registers) 6)No code after the call to run_user_process() will be executed in your OS since that call transitions back into user space from kernel space and begins executing the user program again

A system uses FIFO policy for page replacement. It has 4 page frames with no pages loaded to begin with. The system first accesses 100 distinct pages in some order and then accesses the same 100 pages but now in the reverse order. How many page faults will occur? 196 192 197 195

196

What is the translation lookaside buffer

A cache of page table mappings to the CPU. The TLB is usually implemented as a fast associative memory in the CPU or MMU. When the OS successfully maps a page number to a frame number it puts this mapping the TLB. The TLB is checked each time an address translation is performed and if it contains the mapping, the table look up is not performed.

Consider a machine with 64 MB physical memory and a 32-bit virtual address space. If the page size is 4KB, what is the approximate size of the page table? 16mb 8mb 2mb 24mb

A page entry is used to get address of physical memory. Here we assume that single level of Paging is happening. So the resulting page table will contain entries for all the pages of the Virtual address space. Number of entries in page table = (virtual address space size)/(page size) Using above formula we can say that there will be 2^(32-12) = 2^20 entries in page table. No. of bits required to address the 64MB Physical memory = 26. So there will be 2^(26-12) = 2^14 page frames in the physical memory. And page table needs to store the address of all these 2^14 page frames. Therefore, each page table entry will contain 14 bits address of the page frame and 1 bit for valid-invalid bit. Since memory is byte addressable. So we take that each page table entry is 16 bits i.e. 2 bytes long. Size of page table = (total number of page table entries) *(size of a page table entry) = (2^20 *2) = 2MB

What is a process and process table? What are different states of process

A process is an instance of program in execution. For example a Web Browser is a process, a shell (or command prompt) is a process. The operating system is responsible for managing all the processes that are running on a computer and allocated each process a certain amount of time to use the processor. In addition, the operating system also allocates various other resources that processes will need such as computer memory or disks. To keep track of the state of all the processes, the operating system maintains a table known as the process table. Inside this table, every process is listed along with the resources the processes is using and the current state of the process. Processes can be in one of three states: running, ready, or waiting. The running state means that the process has all the resources it need for execution and it has been given permission by the operating system to use the processor. Only one process can be in the running state at any given time. The remaining processes are either in a waiting state (i.e., waiting for some external event to occur such as user input or a disk access) or a ready state (i.e., waiting for permission to use the processor). In a real operating system, the waiting and ready states are implemented as queues which hold the processes in these states. The animation below shows a simple representation of the life cycle of a process (Source: http://courses.cs.vt.edu/csonline/OS/Lessons/Processes/index.html)

What is a Thread? What are the differences between process and thread?

A thread is a single sequence stream within in a process. Because threads have some of the properties of processes, they are sometimes called lightweight processes. Threads are popular way to improve application through parallelism. For example, in a browser, multiple tabs can be different threads. MS word uses multiple threads, one thread to format the text, other thread to process inputs, etc. A thread has its own program counter (PC), a register set, and a stack space. Threads are not independent of one other like processes as a result threads shares with other threads their code section, data section and OS resources like open files and signals. See http://www.personal.kent.edu/~rmuhamma/OpSystems/Myos/threads.htm for more details.

Q2. Thrashing occurs when (a)When a page fault occurs (b) Processes on system frequently access pages not memory (c) Processes on system are in running state (d) Processes on system are in waiting state

Answer: (b) Explanation: Thrashing occurs when processes on system require more memory than it has. If processes do not have "enough" pages, the page fault rate is very high. This leads to: - low CPU utilization - operating system spends most of its time swapping to disk The above situation is called thrashing

Q3. A computer system supports 32-bit virtual addresses as well as 32-bit physical addresses. Since the virtual address space is of the same size as the physical address space, the operating system designers decide to get rid of the virtual memory entirely. Which one of the following is true? (a) Efficient implementation of multi-user support is no longer possible (b) The processor cache organization can be made more efficient now (c) Hardware support for memory management is no longer needed (d) CPU scheduling can be made more efficient now

Answer: (c) Explanation: For supporting virtual memory, special hardware support is needed from Memory Management Unit. Since operating system designers decide to get rid of the virtual memory entirely, hardware support for memory management is no longer needed.

Q1. Virtual memory is (a) Large secondary memory (b) Large main memory (c) Illusion of large main memory (d) None of the above

Answer: (c) Explanation: Virtual memory is illusion of large main memory.

What is deadlock?

Deadlock is a situation when two or more processes wait for each other to finish and none of them ever finish. Consider an example when two trains are coming toward each other on same track and there is only one track, none of the trains can move once they are in front of each other. Similar situation occurs in operating systems when there are two or more processes hold some resources and wait for resources held by other(s).

Open File Table

File Decsriptors, Open File Table, Inode Table -- each PCB has a table of file descriptor pointers -- each file descriptor pointer points to an open file table entry containing -- reference count -- offset -- inode table index -- each inode for an open file is stored in an "in-core" inode table entry with a reference count

Hard Links

Have to exist in the same file system. A directory contains a file that lists the name of all the files in the directory to a inode number. A hard link is when an inode number is given more than once, basically having two directories point to the same information. There is a reference count kept in the inode of all the hard links to the inode, only when the ref count is 0 can the inode be freed.

The Buffer Cache

In the cache data block chunks are allocated for reading in inodes and data blocks for easy storage. It has its own cache block free list. A Hash Table is used to lookup block numbers that may be in cache. This hashes to a doubly linked list of blocks. Accessing a block: a logical block is requested hash is checked first while a buffer is in use it is taken off the free list when it is done it is returned to the free list if the buffer has been modified it is scheduled for write back

Advantages of Virtual Memory

Large virtual memory. More efficient use of memory. Unconstrained multiprogramming. There is no limit on degree of multiprogramming.

A Computer system implements 8 kilobyte pages and a 32-bit physical address space. Each page table entry contains a valid bit, a dirty bit three permission bits, and the translation. If the maximum size of the page table of a process is 24 megabytes, the length of the virtual address supported by the system is _______________ bits

Max size of virtual address can be calculated by calculating maximum number of page table entries. Maximum Number of page table entries can be calculated using given maximum page table size and size of a page table entry. Given maximum page table size = 24 MB Let us calculate size of a page table entry. A page table entry has following number of bits. 1 (valid bit) + 1 (dirty bit) + 3 (permission bits) + x bits to store physical address space of a page. Value of x = (Total bits in physical address) - (Total bits for addressing within a page) Since size of a page is 8 kilobytes, total bits needed within a page is 13. So value of x = 32 - 13 = 19 Putting value of x, we get size of a page table entry = 1 + 1 + 3 + 19 = 24bits. Number of page table entries = (Page Table Size) / (An entry size) = (24 megabytes / 24 bits) = 223 Vrtual address Size = (Number of page table entries) * (Page Size) = 223 * 8 kilobits = 236 Therefore, length of virtual address space = 36

Deadlock Occurs When:

Mutual Exclusion Hold & Wait Circular Wait No Premption

What are the necessary conditions for deadlock?

Mutual Exclusion: There is s resource that cannot be shared. Hold and Wait: A process is holding at least one resource and waiting for another resource which is with some other process. No Preemption: The operating system is not allowed to take a resource back from a process until process gives it back. Circular Wait: A set of processes are waiting for each other in circular form.

Consider a system with byte-addressable memory, 32 bit logical addresses, 4 kilobyte page size and page table entries of 4 bytes each. The size of the page table in the system in megabytes is ___________

Number of entries in page table = 232 / 4Kbyte = 232 / 212 = 220 Size of page table = (No. page table entries)*(Size of an entry) = 220 * 4 bytes = 222 = 4 Megabytes

Disadvantages of Virtual Memory

Number of tables and amount of processor overhead for handling page interrupts are greater than in the case of the simple paged management techniques. Due to lack of an explicit constraint on a job's address space size.

Semaphores Work By:

P(Sem s) decrements s->value, and if this is less than zero, the thread is blocked, and will remain so until another thread unblocks it. This is all done atomically. V(Sem s) increments s->value, and if this is less than or equal to zero, then there is at least one other thread that is blocked because of s. Exactly one of these threads is chosen and unblocked.

In a particular Unix OS, each data block is of size 1024 bytes, each node has 10 direct data block addresses and three additional addresses: one for single indirect block, one for double indirect block and one for triple indirect block. Also, each block can contain addresses for 128 blocks. Which one of the following is approximately the maximum size of a file in the file system?

Question 52 Explanation: The diagram is taken from Operating System Concept book. Maximum size of the File System = Summation of size of all the data blocks whose addresses belongs to the file. Given: Size of 1 data block = 1024 Bytes No. of addresses which 1 data block can contain = 128 Now, Maximum File Size can be calculated as: 10 direct addresses of data blocks = 10*1024 1 single indirect data block = 128*1024 1 doubly indirect data block = 128*128*1024 1 triple indirect data block = 128*128*128*1024 Hence, Max File Size = 10*1024 + 128*1024 + 128*128*1024 + 128*128*128*1024 Bytes = 2113674*1024 Bytes = 2.0157 GB ~ 2GB

Open file table entry

Reference count of how many file descriptors pointers. Offset into file. Inode table index

A way to control Thrashing

Set the lower and upper bounds of page fault rate for each process. Using the above step, establish 'acceptable' page fault rate. If actual rate is lower than lower bound, decrease the number of frames If actual rate is larger than upper bound, increase the number of frames.

A CPU generates 32-bit virtual addresses. The page size is 4 KB. The processor has a translation look-aside buffer (TLB) which can hold a total of 128 page table entries and is 4-way set associative. The minimum size of the TLB tag is: 11 bits 13 bits 15 bits 20 bits

Size of a page = 4KB = 2^12 Total number of bits needed to address a page frame = 32 - 12 = 20 If there are 'n' cache lines in a set, the cache placement is called n-way set associative. Since TLB is 4 way set associative and can hold total 128 (2^7) page table entries, number of sets in cache = 2^7/4 = 2^5. So 5 bits are needed to address a set, and 15 (20 - 5) bits are needed for tag.

Inode Table

Stored in memory, and is a cached copy of the inodes for currently open files. Has a reference count of how many open files point to its Inode, and a filename.

err = pthread_join(thread_id,(void **)&result);

The first argument to this call is the identifier (filled in by the call to pthread_create() when the thread was created. The second argument is an out parameter of type (void **). That is, pthread_join() takes a pointer to a (void *) so that it can return the (void *) pointer passed back from the thread on exit.

err = pthread_create(&thread_id, NULL, SumThread, (void *)args);

The pthread_create() call takes four arguments and returns a single result. The arguments are a pointer to a variable of type pthread_t (as an out parameter) a pointer to a structure indicating how to schedule the thread (NULL means use the default scheduler) The name of the entry point function a single pointer to the arguments as a (void *)

Simlink

They do not need to be in the same file system. Uses the entire path of the file to reference.

What is Thrashing?

Thrashing is a situation when the performance of a computer degrades or collapses. Thrashing occurs when a system spends more time processing page faults than executing transactions. While processing page faults is necessary to in order to appreciate the benefits of virtual memory, thrashing has a negative affect on the system. As the page fault rate increases, more transactions need processing from the paging device. The queue at the paging device increases, resulting in increased service time for a page fault (Source: http://cs.gmu.edu/cne/modules/vm/blue/thrash.html)

In a virtual memory system, size of virtual address is 32-bit, size of physical address is 30-bit, page size is 4 Kbyte and size of each page table entry is 32-bit. The main memory is byte addressable. Which one of the following is the maximum number of bits that can be used for storing protection and other information in each page table entry?

Virtual memory = 232 bytes Physical memory = 230 bytes Page size = Frame size = 4 * 103 bytes = 22 * 210 bytes = 212 bytes Number of frames = Physical memory / Frame size = 230/212 = 218 Therefore, Numbers of bits for frame = 18 bits Page Table Entry Size = Number of bits for frame + Other information Other information = 32 - 18 = 14 bits Thus, option (D) is correct.

What is Virtual Memory? How is it implemented?

Virtual memory creates an illusion that each user has one or more contiguous address spaces, each beginning at address zero. The sizes of such virtual address spaces is generally very high. The idea of virtual memory is to use disk space to extend the RAM. Running processes don't need to care whether the memory is from RAM or disk. The illusion of such a large amount of memory is created by subdividing the virtual memory into smaller pieces, which can be loaded into physical memory whenever they are needed by a process.

Locks

a lock is initially in the state "unlocked" there is a primitive that allows a thread to attempt to "lock" the lock there is a primitive that allows a thread to "unlock" a lock that is locked any attempt to lock a lock that is already locked, blocks the thread and puts it on a list of threads waiting for the lock to be unlocked when a lock is unlocked by the thread that is holding the lock, if there are threads waiting (because they tried to lock before), one is selected by the system, given the lock, and allowed to proceed.

A thread is:

a sequential list of instructions that will be executed a set of local variables that "belong" to the thread (thread private) a set of shared global variables that all threads can read and write

Setting up your Address Space

allocate swap space allocate page table contiguous in memory since hardware indexes into it allocate disk block descriptor table copy the program into the swap space note the pages used in the disk block descriptor table set all valid bits to zero and then run the program the program's first instruction will page fault causing the code to get loaded into memory from backing store

Hierarchical Page Table

break the address space up hierarchically so that the high-order address bits encode an index into a lower-level page tables, an so on down to the offset. | 10 bits top level | 44 bits mid-level | 10 bits of offset |

Process of Loading File into Memory

each page in a program must be loaded into a memory frame before the CPU can access it each page in a program has a "shadow" copy on the swap device at some disk block number. when a page is loaded into a frame a free frame is allocated the page is fetched from the disk block location the page table entry is updated with the frame number the valid bit is set to indicate that the in-memory copy is valid with respect to the copy on disk the disk block address from the block descriptor table for the process is copied into the frame table entry for the frame a pointer to the page table entry is put in the frame table entry the reference count for the frame is incremented

int pthread_cond_init(pthread_cond_t *variable, pthread_cond_attr_t *attr):

initializes a condition variable (second argument NULL says to use pthreads defaults)

page stealer aka the page-out-daemon

maintains two "hands" -- one "hand" points to the last place the page stealer looked in the frame table when it ran last. The other "hand" points to the last place it started from. When the page stealer runs, it sweeps through the frame table between where it started last and where it ended last to see if any of the referenced bits are set. if the reference count is zero, or the valid bit is clear, the frame is already free and the page is skipped if the referenced bit is set and the valid bit is set, the page is part of a run set. The referenced bit it is cleared and the frame is skipped. otherwise, if the referenced bit is clear, valid bit is set, but the modified bit is clear, the page is clean and can be stolen. The page stealer sets the reference count to zero and puts the page on the free list, but it leaves the rest of the page intact. Notice that if the system is implementing page caching, and this page really is part of a run set (i.e. the page stealer was just a little too eager), a page fault will occur when the page is next referenced, and the page fault handler will most likely find the page on the free list. Thus, the cost of being "wrong" about a page being in a run set is most likely to be an extra page fault, but not an extra disk access. otherwise if the referenced bit is clear, valid bit is set, and the modified bit is set, the page is not part of a run set, but it is dirty. The page stealer schedules the frame for cleaning by putting it on the page out queue and waking the page out thread. It does not steal the page, though, but rather waits until the next pass to see if it is unreferenced and clean before it is stolen.

"producer-consumer" parallelism

occurs in operating systems frequently. In this example, thread 1 "produces" a value that thread 2 consumes (presumably to compute something else).

Clock Algorithm

page stealer is woken when free page count drops below low water mark OR by timer at a given interval maintain two 'hands' pointing in frame table to... left points where stealer started last right points where stealer ended last loops from left hand to right hand checking if referenced bits are set if valid bit clear the page is free if valid and referenced bits set then page is in use. Clears reference bit. if valid bit set, referenced bit clear, and modified clear... page stealer sets reference count to 0 puts the page on the free list but otherwise leaves page intact so that it may be resurrected from free list if valid bit set, referenced bit clear, and modified bit set... page is not in run set but it is dirty puts page on page out queue and wakes the page out thread note: does not immediately steal the page but rather waits for next pass to check requirements once the pass is finished the page stealer moves to next region of frame table called the 'clock' algorithm because it wraps around like a clock some variations time stamp page examinations and only steal the page after its been found to be stealable for some number of passes some systems have two low water marks introducing a sort of 'panic' mode where all stealable pages are annexed

int pthread_cond_signal(pthread_cond_t *variable):

signals at least one thread blocked on the condition variable passed as its first argument.

int pthread_cond_wait(pthread_cond_t *variable, pthread_mutex_t *lock):

sleep until a signal is sent to the condition variable passed in the first argument and will reacquire the lock (passed as the second variable) before the wait completes.

internal fragmentation

small processes often leave the middle portion of memory (between the stack and the heap) unused.

What happens when a Page Fault Occurs

the hardware masks off the page # and indexes into the page table for the process the valid bit in the page table entry for the page is checked (by hardware) if the valid bit is set, the frame number for the physical frame is retrieved, multiplied by the page size, and added to the offset in the address to form the physical address. No page fault is generated. if the valid bit is not set, the CPU throws page fault exception that the operating system must catch. the page fault exception handler must allocate a free frame fetch the page from the location specified in the disk block descriptor table and load it into the frame update the frame table entry (as described above) update the page table entry (as described above) restart the process so that it reissues the faulting instruction

How do you break down the logical address to give you the physical address?

the lower order bits give you the offset in the page the high order bits give you the page number, in linear order, among all frames Page Number | Offset 0000 0000 0000 0000 0000 001 | 0 1101 0000

external fragmentation

when the wasted space occurs outside of each assigned partition


संबंधित स्टडी सेट्स

PCP - Chapter 11: Corporate Reporting and Analysis

View Set

Animal Science Final Exam Part 1

View Set

Child Development (Eric Erikson)

View Set

Module 1 assessment practice qs

View Set

Network+ 8th Edition Chapter 12, Network+ 8th Edition Chapter 4, Network+ 8th Edition Chapter 6, Network+ 8th Edition Chapter 5, Network+ 8th Edition Chapter 3, Network+ 8th Edition Chapter 2, Network+ 8th Edition Chapter 1, Network+ 8th Edition Chap...

View Set

chapter 12 managing human resources

View Set

Child Abuse and Neglect: Module 3

View Set