You are given a piece of code below. Try to briefly answer the following questio
ID: 3738456 • Letter: Y
Question
You are given a piece of code below. Try to briefly answer the following questions. You don’t need to give too much detail of the questions. Some high level information which we covered in class suffices.
#include <sys/stat.h (Links to an external site.)Links to an external site.>
#include <fcntl.h (Links to an external site.)Links to an external site.>
#include <stdio.h>
int main()
{
char buffer [100];
int fd, num;
printf(“address of buffer: %x ”, buffer);
fd = open(“/home/readme”, O_RDONLY);
num = read(fd, buffer, 100*sizeof(int));
if (num <= 0) {
printf(“read data from readme failed ”);
} else {
printf(“data from readme: %s ”, buffer);
}
close(fd);
}
1. When you (root user) login to computer and run this program, will the program itself run in user mode or kernel mode?
2. The statement of “printf(“address of buffer: %x ”, buffer)” will print a virtual address or physical address?
3. When accessing the variables “buffer”, “fd” and “num”, if their virtual addresses are 0xbfdc4084, 0xbfdc4020, 0xbfdc401c, how many times does the OS kernel need to refer to page tables to do virtual-to-physical address translation? Why? (hint: we assume the size of one page is 4KB, and there is no context switch when the process accessing the three variables.)
4. When does OS allocate physical memory for variable “buffer”?
5. What does OS kernel need to do when the program “close(fd)”?
Explanation / Answer
1. user mode
2. physical memory
3.2^32/2^12 times
4. at the time of compilation,
5. the reference of the fd is dereferenced.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.