10/24/2010 COP4600 Discussion 7 Assignment 3 Reminder Assignment3 is posted on Sakai TA: Huafeng Jin hj0@cise.ufl.edu Discussion 6 Recap Scheduling First-come First-served (FCFS) Round-robin Priority-based Semaphore vs. Monitor (Monitor: abstract data type) Virtual Memory Page Replacement Algorithms Question-1 Tau = 500, current time = 2204 Now, we would like to replace a page using WSClock algorithm, which page would be evicted? What is the state after replacement? Virtual-Physical Address Mapping (Inverted page table) WSClock (others: NRU, FIFO, LRU, Working Set…) Question-1 Page frame: (1502, R = 0, M = 0) will be replaced. Question-2 For the mapping given below, give virtual address = 22400, what is the page size? What is the offset? What is the physical address? 1 10/24/2010 Question-2 Page size = 4K = 2 12 = 4096 22400 belongs to page 5 (20K-24K) Page 5 maps to page frame 3 (12K-16K) Offset = 22400%4K = 22400%4096 = 1920 Physical Address = 12K + 1920 = 14208 Outline Assignment 3 C Version Description IPC in C Semaphore in C Java Version Description Page Replacement Algorithms Second Chance NRU Aging Outline Assignment 3 C Version Description IPC in C Semaphore in C Java Version Description Page Replacement Algorithms Second Chance NRU Aging IPC IPC (Interprocess Communication) ◦ Exchanging data among multiple processes. Remember: ◦ Threads communicate via shared data segment. ◦ Processes DON’T share variables. C Version Description Using IPC (interprocess communication) to implement a Producer-Consumer problem using semaphore ◦ One shared buffer with size 1 ◦ Two processes (NOT threads) One producer One consumer ◦ Three Semaphores mutex full empty Thread Example #include <stdio.h> #include <pthread.h> void *increment(); int count = 0; /*count is shared between two threads*/ main(){ pthread_t t1, t2; pthread_create(&t1, NULL, increment, NULL); pthread_create(&t2, NULL, increment, NULL); pthread_exit(NULL); } void *increment(){ count += 5; printf("%d\n", count); pthread_exit(NULL); } /*Prints out 5, 10 or 10, 5*/ 2 10/24/2010 Process Example #include <stdio.h> #include <pthread.h> int count = 0; main(){ int pid = fork(); if(pid == 0){ count += 5; printf("%d\n", count); } else{ count += 5; printf("%d\n", count); } } /*prints out 5, 5*/ shmget: ◦ int shmget (key_t key, size_t size, int flag) key: key to share size: size of the shared space flag: flags Libraries: ◦ sys/types.h ◦ sys/ipc.h ◦ sys/shm.h /*Each process has separate copy*/ shmget, shmat Shared Memory in C (shm) Functions: ◦ shmget ◦ shmat ◦ shmctl ◦ shmdt create/locate shared memory attach shared memory to process space set shared memory permissions detach shared memory to process space Mechanisms Processes use the same key to access the shared memory Only after shmat, process can access that shared memory shmat: ◦ void *shmat (int shmid, void *addr, int flag) shmid: shared memory id addr: attach point flag: flags Example #include <sys/types.h> Example (cont) /*Child process: prints out characters from the shared memory*/ #include <sys/ipc.h> #include <sys/shm.h> key_t key = 5678; else{ main(){ int pid = fork(); /*Locates the shared memory*/ if ((shmid = shmget(key, 27, 0666)) < 0) { perror("shmget"); exit(1); } int shmid; char *shm, *s; /*Parent process: produce 26 letters into the shared memory*/ if(pid != 0){ if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } char c; int shmid; char *shm, *s; /*Creates the shared memory*/ for (s = shm; *s != NULL; s++) putchar(*s); putchar('\n'); } if ((shmid = shmget(key, 27, IPC_CREAT | 0666)) < 0) { perror("shmget"); exit(1); } if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } } s = shm; ---- This example is not well-synchronized. for (c = 'a'; c <= 'z'; c++)*s++ = c; } 3 10/24/2010 C Semaphore C Semaphore: sem_init, sem_wait, sem_post sem semaphore variable pshared shared or not? (currently cannot be shared in Linux) value initial value of semaphore semaphore.h Initialize a semaphore semaphore down operation sem (P operation) semaphore variable sem_post (V operation) ◦ int sem_post(sem_t *sem) semaphore up operation How to share a semaphore sem_wait ◦ int sem_wait(sem_t *sem) ◦ Functions: sem_init sem_wait sem_post sem_init ◦ int sem_init(sem_t *sem, int pshared, int value) ◦ Do synchronizations among processes to the shared memory. ◦ Library: sem semaphore variable Example How to share a semaphore among multiple processes? One way is using shared memory: #include<sys/shm.h> ◦ Use shmget, shmat: int main(){ key_t key = 1000; int sm = shmget(IPC_PRIVATE,sizeof(sem_t),0666|IPC_CREAT|IPC_EXCL); #include<sys/ipc.h> #include<semaphore.h> /*create shared memory space*/ void *pm=shmat(sm,(void*)0,0); int sm = shmget(IPC_PRIVATE,sizeof(sem_ t),0 666|IPC _CRE AT|IPC _EX CL) ; sem_t* mutex=(sem_t*)pm; sem_init(mutex,1,1); int pid=fork(); if(pid>0){ /*Parent process*/ int shmid = shmget(key, 2, IPC_CREAT|0666); int *shm = shmat(shmid, NULL, 0); /*attach to process space*/ void *pm=shmat(sm,(void*)0,0); sem_wait(mutex); int *counter = shm; /*init semaphore*/ sem_t* mutex=(sem_t*)pm; Example (cont) else if(pid==0){ /*Child process*/ int shmid = shmget(key, 2, 0666); int *shm = shmat(shmid, NULL, 0); sem_wait(mutex); int *counter = shm; (*counter) ++; *counter = 10; printf("%d\n", *counter); sem_post(mutex); sem_init(mutex,1,1); } Note When compile the program contains semaphore, please add “-lrt” at the back: ◦ gcc test.c -lrt lrt is the real time library in C printf("%d\n", *counter); sem_post(mutex); } return 0; } 4 10/24/2010 Outline Java Version Description Assignment 3 C Version Description IPC in C Semaphore in C ◦ Second Chance ◦ NRU (Not Recently Used) ◦ Aging Java Version Description Page Replacement Algorithms Second Chance NRU Aging ◦ Parse the .txt file ◦ Computes page faults java mysimulator input.txt 50 Format: ◦ Clock period: java mysimulator input.txt 2 input.txt: 2 4 R3 R5 W3 R6 N (integer) ◦ Operation: Read: (R, 6) read page 6 Write: (W, 4) write page 4 Second Chance Maintains 2 fields for each page: ◦ R bit: recently referenced or not 0: cleared every clock period/put to the end of list 1: referenced ◦ Load Time: Initially records the load time into the physical memory 2 steps: Input file Clear the Reference bit (R bit) for all the pages after N page references. Input file Write a simulator that computes the page faults of different page replacement algorithm given a sequence of page operations. A linkedlist of pages from old to new. Example: Second Chance For each page fault, examine the page frames from linkedlist one by one: ◦ R = 0: Replace this page Set the load time to current time Put the new page to the end of the linkedlist ◦ R = 1: Set R = 0 Set the load time to current time Put it to the end of the linkedlist 5 10/24/2010 Example-1 Given: java mysimulator input.txt 2 1 4 2 R3 R3 R5 R5 W3 W3 R6 R6 Maintains 2 fields for each page: NRU (Not Recently Used) ◦ R bit: recently referenced or not ◦ M bit: modified or not 0: page doesn’t change 1: page write operation Example Given: java mysimulator input.txt 2 For each page fault, classify all the page frames into 4 categories: ◦ ◦ ◦ ◦ 0: cleared every clock period 1: referenced Given: java mysimulator input.txt 2 1 NRU (Not Recently Used) Example-2 Class 0: not referenced, not modified (R=0, M=0) Class 1: not referenced, modified (R=0, M=1) Class 2: referenced, not modified (R=1, M=0) Class 3: referenced, modified (R=1, M=1) Choose randomly from the lowestnumbered nonempty class. Aging 2 Maintains 2 fields for each page frame: ◦ R bit: recently referenced or not 4 0: cleared every clock period 1: referenced R3 R5 W3 ◦ Counter: Records the age R6 Shifts right each clock tick, and insert R bit to the left. Aging is an approximation of LRU 6 10/24/2010 Aging In each clock tick: ◦ For each page frames, right shift the counter, then insert R bit to the left. For each page fault, examine the counter field: Any Questions? ◦ Replace the page frame with the smallest counter field. ◦ Set R bit of that page to 1 7