0% found this document useful (0 votes)
18 views6 pages

Print

Dev Connect

Uploaded by

toklesreeja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Print

Dev Connect

Uploaded by

toklesreeja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 6
Roll No: 160122733303 Exp. No: 07 Date: 22/10/24 WEEK-7 Ps cement Algorithms 4)Aim: Write a program to demonstrate FIFO page replacement algorithm. Description The FIFO (First1n-First-Out) page replacement algorithm replaces the oldest page in memory when a new page needs to be loaded and the frame is full. It maintains a queue, ensuring that the first loaded page is the first to be removed. If the requested page is not in the frame, a page fault occurs, and the page is added to memory. The goal is to minimize page faults while simulating memory management. Code: def fifo_page_replacement(pages, frame_size): frame = [] page faults = 0 print("Page\tFrames") for page in pages: if page not in frame: if len(frame) < frame_size: frame.append(page) else: frame. pop(0) frame.append(page) page_faults += 1 print({pagel\ t{frame}") print(f"\nTotal Page Faults: {page_faults}") pages = 7,0, 1,2, 0,3,0, 4, 2, 3,0,3, 2) frame_size fifo_page_replacement(pages, frame_size) Page No. ..c.ceseccseseeseeeeeeene Signature of the Factlty........00-csseeeeooe Roll No: 160122733303 Exp. No: 07 Date: 22/10/24 rrr 8 Bede Seas Page No. ..c.ceseccseseeseeeeeeene Signature of the Faculty.....cccsssseeeeseseeee Roll No: 160122733303 Exp. No: 07 Date: 22/10/24 2)Aim: Write a program to demonstrate LRU page replacement algorithm. Description :LRU (Least Recently Used) replaces the page not accessed for the longest time when the frame is full. It minimizes page faults by retaining frequently used pages but requires extra tracking overhead. Code: def Iru_page_replacement(pages, frame_size): frame =[] ‘page_faults = 0 print("Page\Frames") for page in pages: if page not in frame: if len(frame) < frame_size: frame.append(page) else: Itu_page = min(frame, key=lambda p: pages|.pages. index(page)].count{p)) frame.remove(hu_page) frame.append(page) page_faults += 1 print({page}\t{frame}") print(f"nTotal Page Faults: (page_fautts}") pages =[7, 0, 1, 2, 0, 3, 0 4,2, 3.0, 3,2] frame_size = 3 Iru_page_replecement(pages, frame_size) Output: ore age P: 7 A rl a ry 3 ry 4 3 ° = Page No. ..c.ceseccseseeseeeeeeene Signature of the Factlty........00-csseeeeooe Roll No: 160122733303 Exp. No: 07 Date: 22/10/24 3)Aim: Write a program to demonstrate the optimal page replacement algorithm. Description: The Optimal algorithm replaces the page that won't be needed for the longest time in the future. It minimizes page faults but requires future knowledge of page references, making it ideal only in theoretical or predictive scenarios. Code: det optimal_page_replacement(pages, frame_size): frame = [] page_faults=0 print("Page\'Frames") for , page in enumerate(pages): if page not in frame: if len(trame) < frame_size: frame.append(page) else: future_indices = [(pages[i+1:].index(p) if p in pages|i+1:] else float('inf)) for p in frame] frame. pop(future_indices.index(max(future_indices))) frame.append(page) page_faults += 1 print(F{page}\t{frame}") print(f"vnTotal Page Faults: {page_fautts}") pages =[7, 0, 1, 2, 0, 3, 0, 4, 2, 30,3, 2] frame_size = 3 optimal_page_replacement(pages, frame_size) Output: rey bapers seeps < es ey ieee Page No. ..c.ceseccseseeseeeeeeene Signature of the Factlty........00-csseeeeooe Roll No: 160122733303 Exp. No: 07 Date: 22/10/24 4)Aim: Write a program to illustrate threads. Description: This program demonstrates multithreading in C using the pthread library, where three threads run concurrently. One thread takes user Input from the keyboard, the second displays the input multiple times, and the third saves the input to a file. A shared buffer is used to store input, ensuring that threads can access it. Synchronization is achieved using pthread_join to make sure input is completed before the other threads run. This showcases parallel processing with shared data and thread management Code: #include #include #include // For sleep and getpid void* takeinputfromkeyboard(void *arg) { print{("Thread ID: %ilu, Process ID: %d - Taking input from keyboard\n", pthread_self(), getpid(); for (int i= 0;i <5; i++) { printf("Thread ID: %lu - Waiting for user input \n", pthread_self()); sleep(1); J return NULL: 1 void* displayContent(void *arg) { printf("Thread ID: %slu, Process ID: %d - Display Content on the screen \n", pthread_self(), getpid(); for (int i= 0;1 <5; i#+) { printf("Thread ID: %lu - Display Content \n", pthread_self()); sleep(1); } return NULL; ) void* savingcontentHardisk(void *arg) { printi("Thread ID: %lu, Process ID: %d - Saving the content\n", pthread _self(), getpid()); for (inti=0;i <5; i++) { printf("Thread ID: %lu - Saving the content into file \n", pthread_self()); sleep(l); } return NULL; int main() { pthread_t t, 2, 3; Page No. ..c.ceseccseseeseeeeeeene Signature of the Factlty........00-csseeeeooe Roll No: 160122733303 Exp. No: 07 Date: 22/10/24 pthread _create(éet1, NULL, takeinputfromkeyboard, NULL); pthread_create(ét2, NULL, displayContent, NULL): pthread_create(ét3, NULL, savingcontentHardisk, NULL); pthread _join(t1, NULL); pthread _join(t2, NULL); pthread _join(t3, NULL); return 0; ) ‘Output: Smt Page No. ..c.ceseccseseeseeeeeeene Signature of the Factlty........00-csseeeeooe

You might also like