#include #include #include

70
#include <pthread.h> #include <semaphore.h> #include <stdlib.h> pthread_mutex_t sem_mut = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t cond_mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_t tip ; pthread_t tip1 ; int rander ; void *sender(void *) ; void *receiver(void *) ; sem_t sema_dude ; main() { rander = rand() % 10 ; //returns random num between 0 - 9 printf("Rander is %d\n", rander) ; sleep(rander) ; sem_init(&sema_dude, 0, 0); pthread_create(&tip, NULL, sender, NULL) ; pthread_create(&tip1, NULL, receiver, NULL) ; pthread_join(tip, NULL); pthread_join(tip1, NULL) ; }

Upload: presencia-queiro

Post on 31-Dec-2015

86 views

Category:

Documents


3 download

DESCRIPTION

#include #include #include pthread_mutex_t sem_mut = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t cond_mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_t tip ; pthread_t tip1 ; int rander ; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: #include  #include  #include

#include <pthread.h>#include <semaphore.h>#include <stdlib.h>

pthread_mutex_t sem_mut = PTHREAD_MUTEX_INITIALIZER;pthread_mutex_t cond_mut = PTHREAD_MUTEX_INITIALIZER;pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

pthread_t tip ;pthread_t tip1 ;

int rander ;void *sender(void *) ;void *receiver(void *) ;

sem_t sema_dude ;

main()

{ rander = rand() % 10 ; //returns random num between 0 - 9 printf("Rander is %d\n", rander) ; sleep(rander) ; sem_init(&sema_dude, 0, 0); pthread_create(&tip, NULL, sender, NULL) ; pthread_create(&tip1, NULL, receiver, NULL) ; pthread_join(tip, NULL); pthread_join(tip1, NULL) ;

}

Page 2: #include  #include  #include

void *sender (void *param){printf("Hello world!!\n") ; sem_wait(&sema_dude) ; printf("Im back\n") ;}

void *receiver (void *param){printf("Hello from ME!\n") ; sleep(2) ; sem_post(&sema_dude) ; }

Page 3: #include  #include  #include

gcc try_sem.c -lpthread -lposix4 //on gandalf

(g)cc try_sem.c –lpthread //most Linux systems

Page 4: #include  #include  #include

Random Number Generator

#include <stdlib.h> int rand() //returns a random integer, not double int my_rand = rand() % 20 ; //returns a random int

between 0 and 19

Page 5: #include  #include  #include

Page 240: sem_t sem mutex incorrect. sem_t mutex ;

while (true) { sleep(….) ; rand = rand() ;…… }

while (true){sleep_time = rand() % 10 ;

sleep(sleep_time) ; ……………..}

Page 6: #include  #include  #include

Background Virtual memory – separation of user logical memory

from physical memory. Only part of the program needs to be in memory for

execution. Logical address space can therefore be much larger than

physical address space. Allows address spaces to be shared by several processes. Allows for more efficient process creation. Virtual memory can be implemented via:

Demand paging Demand segmentation

Page 7: #include  #include  #include

Shared Library Using Virtual Memory

Page 8: #include  #include  #include

Demand Paging Bring a page into memory only when it

is needed. Less I/O needed Less memory needed Faster response More users

(higher level of multiprogramming) Page is needed reference to it

invalid reference abort not-in-memory bring to memory

Page 9: #include  #include  #include

Page Table When Some Pages Are Not in Main Memory

Page 10: #include  #include  #include

Page Fault

If there is ever a reference to a page, first reference will trap to OS page fault

OS decides: Invalid reference abort. Just not in memory.

Get empty frame.

Page 11: #include  #include  #include

Page Fault Get empty frame. Swap page into frame. Reset tables, validation bit = 1. Restart instruction

Page 12: #include  #include  #include

Steps in Handling a Page Fault

Page 13: #include  #include  #include

What happens if there is no free frame?

Page replacement – find some page in memory, but not really in use, swap it out.

algorithm performance – want an algorithm which will result in

minimum number of page faults.

Same page may be brought into memory several times.

Page 14: #include  #include  #include

Page Replacement

Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk.

Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory.

Page 15: #include  #include  #include

Basic Page Replacement1. Find the location of the desired page on disk.

2. Find a free frame:- If there is a free frame, use it.- If there is no free frame, use a page

replacement algorithm to select a victim frame.

3. Read the desired page into the (newly) free frame. Update the page and frame tables.

4. Restart the process.

Page 16: #include  #include  #include

Page Replacement Algorithms

Want lowest page-fault rate. Evaluate algorithm by running it on a particular string of

memory references (reference string) and computing the number of page faults on that string.

In examples, the reference string is: 7,0,1,2,0, 3,0,4,2,3, 0,3,2,1,2,0,1,7,0,1

Page 17: #include  #include  #include

Optimal Algorithm

Replace page that will not be used for longest period of time.

Used for measuring how well your algorithm performs.

Page 18: #include  #include  #include

Optimal Page Replacement

Page 19: #include  #include  #include

Optimal Page Replacement

Page 20: #include  #include  #include

Optimal Page Replacement

Page 21: #include  #include  #include

Optimal Page Replacement

Page 22: #include  #include  #include

Optimal Page Replacement

Page 23: #include  #include  #include

First-In-First-Out

Throw out the page that has been in memory the longest.

Good when talking about a set of pages for initialization.

Bad when talking about heavily used variable.

Page 24: #include  #include  #include

FIFO Page Replacement

Page 25: #include  #include  #include

Least Recently Used (LRU) Algorithm

Based on principal of “Locality of Reference”. A page that has been used in the near past is likely

to be used in the near future. LRU: Determine the least recently used page

in memory and evict it. Can be done but difficult to implement

efficiently.

Page 26: #include  #include  #include

LRU Page Replacement

Page 27: #include  #include  #include

LRU Page Replacement

Page 28: #include  #include  #include

LRU Page Replacement

Page 29: #include  #include  #include

Second Chance Approximations

Try to approximate LRU. Add a “reference” bit to page table. The

hardware sets this bit to 1 when the page is accessed.

The reference bit is maintained in the page table.

Set of “second chance” algorithms that use the reference bit in page table entry to determine if it has been recently used.

Example: Clock Page Replacement Algorithm.

Page 30: #include  #include  #include

Cur. Pos

Evict

Page Fault: Evict Current Page and increment pointer.

Page 31: #include  #include  #include

Cur. Pos

Second Page Fault: This guy looks good, evict him and increment pointer

Evict

Page 32: #include  #include  #include

Cur. Pos

Third Page Fault: Can’t evict this guy. He has been referenced. Reset reference flag to 0 and

check next potential victim.

Page 33: #include  #include  #include

Cur. Pos

0

Can’t evict this guy either. Reset Reference bit to 0 and move on to next

potential victim.

Page 34: #include  #include  #include

Cur. Pos

0

0

I can evict this page because it has not been referenced since last time I checked.

Victim

Page 35: #include  #include  #include

Cur. Pos

0

0

This page is referenced and reference bit is set to 1. Which page will be evicted next assuming no

other reference bits are set?

Victim

1

Page 36: #include  #include  #include

Other Software Approximations to LRU

Not Frequently Used (NFU). Associate a software counter with each page. On timer interrupt, OS scans all pages in memory. For each page, the R bit (Referenced bit)is added to the

counter. Page with lowest count is evicted.

Page 37: #include  #include  #include

Problem with NFU

It never forgets. A page referenced often in earlier phases of the

program may not be evicted long after it has been used.

Would like to have an algorithm that “ages” the count. That is, the latest references should be the most important.

Page 38: #include  #include  #include

Aging Algorithm for Simulating LRU

Now maintain an 8-bit counter for each page in memory.

On each timer interrupt the OS scans the pages to determine which pages have been referenced since last time it checked.

Shift right one bit of the counter. Place the R bit in the leftmost bit of the

counter. Choose the page to evict that has the lowest

count.

Page 39: #include  #include  #include

Example

Assume all counters are currently 0.

Consider the case when pages 0,2,4, and 5 are referenced between last interrupt.

Page 40: #include  #include  #include

Simulating LRU in Software

The aging algorithm simulates LRU in software

Page 41: #include  #include  #include

Simulating LRU in Software

The aging algorithm simulates LRU in software

Assume pages 0,1, and 4 are referenced since last interrupt.

Page 42: #include  #include  #include

Simulating LRU in Software

The aging algorithm simulates LRU in software

Page 43: #include  #include  #include

Simulating LRU in Software

The aging algorithm simulates LRU in software

Assume 0,1,3,5 are referenced since last interrupt.

Page 44: #include  #include  #include

Simulating LRU in Software

The aging algorithm simulates LRU in software

Page 45: #include  #include  #include

Allocation of Frames

Each process needs minimum number of pages. Two major allocation schemes.

Equal allocation Proportional allocation.

Replacement Scope can be: Local. Global.

Page 46: #include  #include  #include

Local Scope

Number of pages per process is fixed based on some criteria.

Can use equal allocation or proportional allocation. Equal allocation – e.g., if 100 frames and 5

processes, give each 20 pages. What are the drawbacks of equal allocation?

Page 47: #include  #include  #include

Proportional Allocation, Local Scope

Proportional Allocation Allocate number of pages based on the size of the

process. Problem?

Page 48: #include  #include  #include

Equal allocation – e.g., if 100 frames and 5 processes, give each 20 pages.

What are the drawbacks of this approach? Allocation may be too small causing significant page

faulting. Allocation may too large reducing number of

processes in memory and wasting memory that could be used by other processes.

Page 49: #include  #include  #include

Proportional Allocation Allocate pages based on the size of the process.

Problem? Process needs will vary over its execution leading to

the same problems as equal-size pages.

Page 50: #include  #include  #include

Global Replacement When a page fault occurs, new page frame allocated to

the process. Page replacement based on previous approaches: e.g.,

LRU, FIFO, etc. No consideration of which process should (or can best

afford) to lose a page. Can lead to high page-fault rates.

Page 51: #include  #include  #include

Thrashing

If a process does not have “enough” pages, the page-fault rate is very high. This leads to:

low CPU utilization. operating system thinks that it needs to increase the

degree of multiprogramming. another process added to the system.

Thrashing a process is busy swapping pages in and out.

Page 52: #include  #include  #include

Thrashing

Page 53: #include  #include  #include

Locality In A Memory-Reference Pattern

Page 54: #include  #include  #include

Working-Set Model

working-set window a fixed number of page references Example: 10,000 instruction

WSSi (working set of Process Pi) =total number of pages referenced in the most recent (varies in time)

if too small will not encompass entire locality. if too large will encompass several localities. if = will encompass entire program.

D = WSSi total demand frames if D > m (memory) Thrashing

Policy if D > m, then suspend one of the processes.

Page 55: #include  #include  #include

Working-set model

Page 56: #include  #include  #include

• The page-fault rate will peak when transitioning between localities.

• Once the pages of the current working set have been loaded, page faults will decrease.

Page 57: #include  #include  #include

Page-Fault Frequency Scheme

Establish “acceptable” page-fault rate. If actual rate too low, process loses frame. If actual rate too high, process gains frame.

Page 58: #include  #include  #include

Memory-Mapped Files

File system calls are expensive (e.g., read(), write(), seek).

An alternative is to map disk blocks into main memory.

Will initially get mapped through page faults. Subsequent operations handled as routine memory

accesses. Create shared memory by allowing multiple

processes to map the same file concurrently.

Page 59: #include  #include  #include

Homework:

9.2, 9.3, 9.4, 9.6, 9.15.

Page 60: #include  #include  #include

Shared Memory Through Memory Mapping Same File

Page 61: #include  #include  #include

Other Considerations

Prepaging: Predicting future page requests. In pure-demand paging, lots of page faults when

process started and whenever it is swapped out (and later resumed).

Example: Remember working set when process is swapped out– load entire working set when reloaded.

Page 62: #include  #include  #include

Page Size

Trade-offs between larger/smaller page sizes. Advantage of smaller page sizes?

Page 63: #include  #include  #include

Page Size

Trade-offs between larger/smaller page sizes. Advantage of smaller page sizes?

Less internal fragmentation. Disadvantages of smaller page size?

Page 64: #include  #include  #include

Page Size

Trade-offs between larger/smaller page sizes. Advantage of smaller page sizes?

Less internal fragmentation. Disadvantages of smaller page size?

Larger page tables More I/O operations More page faults

Page 65: #include  #include  #include

Advantages of larger page size?

Page 66: #include  #include  #include

Advantages of larger page size? Smaller page tables More efficient I/O Fewer page faults

Page 67: #include  #include  #include

Other Considerations

TLB Reach - The amount of memory accessible from the TLB.

TLB Reach = (TLB Size) X (Page Size)

Ideally, the working set of each process is stored in the TLB.

Page 68: #include  #include  #include

Increasing the Size of the TLB

Increase the Page Size. This may lead to an increase in fragmentation as not

all applications require a large page size.

Provide Multiple Page Sizes. This allows applications that require larger page

sizes the opportunity to use them without an increase in fragmentation.

Requires OS to manage the TLB in software. The current trend is to manage the TLB in software.

Page 69: #include  #include  #include

Windows NT Uses demand paging with clustering.

Clustering brings in pages surrounding the faulting page.

Processes are assigned working set minimum and working set maximum.

Working set minimum is the minimum number of pages the process is guaranteed to have in memory.

Page 70: #include  #include  #include

Windows NT A process may be assigned as many pages up to its

working set maximum. When the amount of free memory in the system falls

below a threshold, automatic working set trimming is performed to restore the amount of free memory.

Working set trimming removes pages from processes that have pages in excess of their working set minimum.

Uses variation of the clock or FIFO algorithms.