121 lines
		
	
	
	
		
			3.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			121 lines
		
	
	
	
		
			3.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #include <stdio.h>
 | |
| #include <pthread.h>
 | |
| #include <limits.h>
 | |
| #define MAX_PROC 10
 | |
| #define MAX_REF 100
 | |
| #define MAX_FRAMES 10
 | |
| 
 | |
| typedef struct {
 | |
|     int id;               // Process ID
 | |
|     int frames;           // Number of frames for this process
 | |
|     int n;                // Number of page references
 | |
|     int ref[MAX_REF];     // Array of page references
 | |
|     int faults;           // Page faults counter
 | |
| } Process;
 | |
| 
 | |
| Process procs[MAX_PROC];
 | |
| int proc_count = 0;
 | |
| 
 | |
| void *simulateOptimal(void *arg) {
 | |
|     Process *p = (Process *)arg;
 | |
|     int frameArr[MAX_FRAMES];
 | |
|     int i, j;
 | |
| 
 | |
|     // Initialize frames as empty (-1)
 | |
|     for (i = 0; i < p->frames; i++) {
 | |
|         frameArr[i] = -1;
 | |
|     }
 | |
|     p->faults = 0;
 | |
| 
 | |
|     // Process each page reference
 | |
|     for (i = 0; i < p->n; i++) {
 | |
|         int page = p->ref[i];
 | |
|         int found = 0;
 | |
|         // Check if page is already in a frame
 | |
|         for (j = 0; j < p->frames; j++) {
 | |
|             if (frameArr[j] == page) {
 | |
|                 found = 1;
 | |
|                 break;
 | |
|             }
 | |
|         }
 | |
|         if (found)
 | |
|             continue;
 | |
|         // Page fault occurs
 | |
|         p->faults++;
 | |
| 
 | |
|         // Look for an empty frame (-1)
 | |
|         int empty = -1;
 | |
|         for (j = 0; j < p->frames; j++) {
 | |
|             if (frameArr[j] == -1) {
 | |
|                 empty = j;
 | |
|                 break;
 | |
|             }
 | |
|         }
 | |
|         if (empty != -1) {
 | |
|             frameArr[empty] = page;
 | |
|             continue;
 | |
|         }
 | |
| 
 | |
|         // No empty frame; choose a victim using Optimal algorithm:
 | |
|         int replace = 0, farthest = -1;
 | |
|         for (j = 0; j < p->frames; j++) {
 | |
|             int k, nextUse = INT_MAX;
 | |
|             for (k = i + 1; k < p->n; k++) {
 | |
|                 if (frameArr[j] == p->ref[k]) {
 | |
|                     nextUse = k;
 | |
|                     break;
 | |
|                 }
 | |
|             }
 | |
|             if (nextUse > farthest) {
 | |
|                 farthest = nextUse;
 | |
|                 replace = j;
 | |
|             }
 | |
|         }
 | |
|         frameArr[replace] = page;
 | |
|     }
 | |
| 
 | |
|     printf("Process %d: Faults = %d\n", p->id, p->faults);
 | |
|     return NULL;
 | |
| }
 | |
| 
 | |
| int main() {
 | |
|     int i, j;
 | |
|     pthread_t threads[MAX_PROC];
 | |
| 
 | |
|     // Input the number of processes
 | |
|     printf("Enter number of processes (max %d): ", MAX_PROC);
 | |
|     scanf("%d", &proc_count);
 | |
|     if (proc_count > MAX_PROC) proc_count = MAX_PROC;
 | |
| 
 | |
|     // Input process details
 | |
|     for (i = 0; i < proc_count; i++) {
 | |
|         procs[i].id = i + 1;
 | |
|         printf("\nProcess %d:\n", procs[i].id);
 | |
|         printf("Enter number of frames (max %d): ", MAX_FRAMES);
 | |
|         scanf("%d", &procs[i].frames);
 | |
|         if (procs[i].frames > MAX_FRAMES) procs[i].frames = MAX_FRAMES;
 | |
|         printf("Enter number of page references (max %d): ", MAX_REF);
 | |
|         scanf("%d", &procs[i].n);
 | |
|         if (procs[i].n > MAX_REF) procs[i].n = MAX_REF;
 | |
|         printf("Enter %d page references (space separated): ", procs[i].n);
 | |
|         for (j = 0; j < procs[i].n; j++) {
 | |
|             scanf("%d", &procs[i].ref[j]);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     // Create a thread for each process simulation
 | |
|     for (i = 0; i < proc_count; i++) {
 | |
|         pthread_create(&threads[i], NULL, simulateOptimal, &procs[i]);
 | |
|     }
 | |
| 
 | |
|     int totalFaults = 0;
 | |
|     // Wait for all threads to complete
 | |
|     for (i = 0; i < proc_count; i++) {
 | |
|         pthread_join(threads[i], NULL);
 | |
|         totalFaults += procs[i].faults;
 | |
|     }
 | |
| 
 | |
|     // Calculate and display average faults
 | |
|     printf("\nAverage Page Faults: %.2f\n", (float)totalFaults / proc_count);
 | |
|     return 0;
 | |
| }
 |