1 / 70

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

#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 ;

Télécharger la présentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. #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) ; }

  2. 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) ; }

  3. gcc try_sem.c -lpthread -lposix4 //on gandalf (g)cc try_sem.c –lpthread //most Linux systems

  4. 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

  5. Page 240: sem_t sem mutex incorrect. • sem_t mutex ; while (true) { sleep(….) ; rand = rand() ; …… } while (true) {sleep_time = rand() % 10 ; sleep(sleep_time) ; ……………..}

  6. 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

  7. Shared Library Using Virtual Memory

  8. 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

  9. Page Table When Some Pages Are Not in Main Memory

  10. 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.

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

  12. Steps in Handling a Page Fault

  13. 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.

  14. 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.

  15. Basic Page Replacement • Find the location of the desired page on disk. • 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. • Read the desired page into the (newly) free frame. Update the page and frame tables. • Restart the process.

  16. 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

  17. Optimal Algorithm • Replace page that will not be used for longest period of time. • Used for measuring how well your algorithm performs.

  18. Optimal Page Replacement

  19. Optimal Page Replacement

  20. Optimal Page Replacement

  21. Optimal Page Replacement

  22. Optimal Page Replacement

  23. 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.

  24. FIFO Page Replacement

  25. 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.

  26. LRU Page Replacement

  27. LRU Page Replacement

  28. LRU Page Replacement

  29. 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.

  30. Page Fault: Evict Current Page and increment pointer. Cur. Pos Evict

  31. Second Page Fault: This guy looks good, evict him and increment pointer Cur. Pos Evict

  32. Third Page Fault: Can’t evict this guy. He has been referenced. Reset reference flag to 0 and check next potential victim. Cur. Pos

  33. Can’t evict this guy either. Reset Reference bit to 0 and move on to next potential victim. 0 Cur. Pos

  34. I can evict this page because it has not been referenced since last time I checked. 0 0 Cur. Pos Victim

  35. This page is referenced and reference bit is set to 1. Which page will be evicted next assuming no other reference bits are set? 1 0 0 Cur. Pos Victim

  36. 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.

  37. 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.

  38. 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.

  39. Example Assume all counters are currently 0. Consider the case when pages 0,2,4, and 5 are referenced between last interrupt.

  40. Simulating LRU in Software • The aging algorithm simulates LRU in software

  41. Simulating LRU in Software • The aging algorithm simulates LRU in software Assume pages 0,1, and 4 are referenced since last interrupt.

  42. Simulating LRU in Software • The aging algorithm simulates LRU in software

  43. Simulating LRU in Software Assume 0,1,3,5 are referenced since last interrupt. • The aging algorithm simulates LRU in software

  44. Simulating LRU in Software • The aging algorithm simulates LRU in software

  45. Allocation of Frames • Each process needs minimum number of pages. • Two major allocation schemes. • Equal allocation • Proportional allocation. • Replacement Scope can be: • Local. • Global.

  46. 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?

  47. Proportional Allocation, Local Scope • Proportional Allocation • Allocate number of pages based on the size of the process. • Problem?

  48. 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.

  49. 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.

  50. 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.

More Related