int main(int argc, char** argv){ int pid; int parent_pid = getpid(); int pages_count; printf(1,"enableSwapping\n"); enableSwapping(); pid = fork(); if(pid == 0){ sleep(300); pages_count = num_of_pages(parent_pid); printf(1,"parent nummber of pages while swaping enabled: %d\n",pages_count); printf(1,"child %d do ls- we expect to see %d.swap (parent_pid)\n",getpid(),parent_pid); exec("ls",argv); } printf(1,"parent is wating and will be swaped out\n"); while (wait() != -1){} //main thread should not cross that line (program never finishes) ////////////////////////part 2 after disabling printf(1,"disbleSwapping\n"); disableSwapping(); pid = fork(); if(pid == 0){ sleep(800); pages_count = num_of_pages(parent_pid); printf(1,"parent nummber of pages while swaping disabled: %d\n",pages_count); printf(1,"child %d do ls- we expect NOT to see %d.swap (the parent_pid.swap)\n",getpid(),parent_pid); exec("ls",argv); } printf(1,"parent is wating and will not be swaped out\n"); while (wait() != -1){} //main thread should not cross that line (program never finishes) exit(); return 1; }
int main(int argc,char** argv) { int pid; printf(1,"\n**************** PART 1 *****************\n\n"); printf(1,"Enabling swapping now...\n"); enableSwapping(); if((pid = fork())==0) { pid = getpid(); printf(1,"Child process Pid = %d has %d memory pages allocated, going to sleep and will be swapped out\n",pid,getAllocatedPages(pid)); sleep2(); printf(1,"Child process pid = %d has woken up and was swapped in - has %d memory pages allocated.\n",pid,getAllocatedPages(pid)); exit(); } else { int j,k; for(j=0;j<10000000;j++) { k = j; j = j+1; j = k; } if(fork()==0) { printf(1,"we will now run the 'ls' command and see that the %d.swap file is created\n",pid); exec("ls",argv); } else { for(j=0;j<10000000;j++) { k = j; j = j+1; j = k; } printf(1,"\nChild process Pid = %d is sleeping and swapped out - has %d memory pages allocated.\n",pid,getAllocatedPages(pid)); wakeup2(); while(wait()>0); printf(1,"\n\n**************** PART 2 *****************\n\n"); printf(1,"Disabling swapping now...\n"); disableSwapping(); if((pid = fork())==0) { pid = getpid(); printf(1,"Child process Pid = %d has %d memory pages allocated, going to sleep and WILL NOT be swapped out\n",pid,getAllocatedPages(pid)); sleep2(); printf(1,"Child process pid = %d has woken up and was swapped in - has %d memory pages allocated.\n",pid,getAllocatedPages(pid)); exit(); } else { if(fork()==0) { printf(1,"we will now run the 'ls' command and see that the %d.swap file has not been created\n",pid); exec("ls",argv); } else { sleep(100); printf(1,"\nChild process Pid = %d is sleeping and NOT swapped out - has %d memory pages allocated.\n",pid,getAllocatedPages(pid)); wakeup2(); } } } } while(wait()>0); exit(); }