Lab 2: Processes

These questions were written by Jerry Cain and Ryan Eberhardt.

Before the end of lab, be sure to fill out the lab checkoff sheet here!

Problem 1: Virtual Memory

Assume the OS allocates virtual memory to physical memory in 4096-byte pages.

For fun, optional reading, read these two documents (though you needn’t do this reading, since it goes beyond the scope of my lecture discussion of virtual memory):

Problem 2: File descriptors

  1. Consider the following code:
    int main() {
        int fd = open("/cplayground/code.cpp", O_RDONLY);
        printf("Using file descriptor %d\n", fd);
       
        // Try reading some bytes:
        char buf[16];
        ssize_t num_read = read(fd, buf, sizeof(buf));
        printf("Read %ld bytes\n", num_read);
       
        // Close file descriptor
        close(fd);
    }
    
    1. What does open do to the file descriptor, open file, and vnode tables? What about read? What about close?

      Open this Cplayground and press “Debug” to start the program. Navigate to the “Open Files” tab to see a visualization of the three tables. Try stepping through the code line-by-line to confirm your intuition.

    2. What happens to the file descriptor, open file, and vnode tables if you add an extra open call?

      int fd = open("/cplayground/code.cpp", O_RDONLY);
      int fd2 = open("/cplayground/code.cpp", O_RDONLY);
      

      Use Cplayground to confirm your intuition.

  2. The dup system call accepts a valid file descriptor, claims a new, previously unused file descriptor, configures that new descriptor to alias the same file session as the incoming one, and then returns it. Briefly outline what happens to the relevant open file table and vnode table entries as a result of dup being called. (Read man dup if you’d like, though don’t worry about error scenarios).

Problem 3: Creating processes