OneCompiler

osslip23

132

Q1
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
pid_t pid = fork();

if (pid == -1) {
    // Fork failed
    perror("Fork failed");
    return 1;
}

if (pid > 0) {
    // Parent process
    printf("Parent process (PID: %d) created a child process (PID: %d)\n", getpid(), pid);
    printf("Parent process is terminating and leaving the child as an orphan.\n");
} else if (pid == 0) {
    // Child process
    printf("Child process (PID: %d) with parent (PPID: %d)\n", getpid(), getppid());
    printf("Child process is doing some task...\n");
    // Simulating some task with sleep
    sleep(5);
    printf("Child process completed its task.\n");
}

// Both parent and child continue from here

return 0;

}

/*
Write the simulation program for demand paging and show the page
scheduling and total number of page faults according the Optimal page
replacement algorithm. Assume the memory of n frames.
Reference String : 7, 5, 4, 8, 5, 7, 2, 3, 1, 3, 5, 9, 4, 6,
*/

#include<stdio.h>

int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);

printf("Enter number of pages: ");
scanf("%d", &no_of_pages);

printf("Enter page reference string: ");

for(i = 0; i < no_of_pages; ++i){
    scanf("%d", &pages[i]);
}

for(i = 0; i < no_of_frames; ++i){
    frames[i] = -1;
}

for(i = 0; i < no_of_pages; ++i){
    flag1 = flag2 = 0;

    for(j = 0; j < no_of_frames; ++j){
        if(frames[j] == pages[i]){
               flag1 = flag2 = 1;
               break;
           }
    }

    if(flag1 == 0){
        for(j = 0; j < no_of_frames; ++j){
            if(frames[j] == -1){
                faults++;
                frames[j] = pages[i];
                flag2 = 1;
                break;
            }
        }    
    }

    if(flag2 == 0){
    	flag3 =0;

        for(j = 0; j < no_of_frames; ++j){
        	temp[j] = -1;

        	for(k = i + 1; k < no_of_pages; ++k){
        		if(frames[j] == pages[k]){
        			temp[j] = k;
        			break;
        		}
        	}
        }

        for(j = 0; j < no_of_frames; ++j){
        	if(temp[j] == -1){
        		pos = j;
        		flag3 = 1;
        		break;
        	}
        }

        if(flag3 ==0){
        	max = temp[0];
        	pos = 0;

        	for(j = 1; j < no_of_frames; ++j){
        		if(temp[j] > max){
        			max = temp[j];
        			pos = j;
        		}
        	}            	
        }

		frames[pos] = pages[i];
		faults++;
    }

    printf("\n");

    for(j = 0; j < no_of_frames; ++j){
        printf("%d\t", frames[j]);
    }
}

printf("\n\nTotal Page Faults = %d", faults);

return 0;

}