Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

cs439 Gheith Quiz 1115

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Quiz 11/15 Sample Answers

1. What are the advantages / disadvantages of: (Note: There are many possible answers, these are
only some of them.)

(a) using a tracing garbage collector

Answer:

Adv: Faster when working set is small due to never being invoked. No memory overhead. No
input from programmer needed. Handles cycles.

Disadv: huge performance hit when it is invoked due to "stopping the world". Doesn't work well
if allowed to manufacture pointers. Is non-deterministic since it cannot be very well predicted
when it is going to run (makes destructors not very useful).

(b) using a counting garbage collector

Answer:

Adv: More memory efficient since objects destructed as soon as safe to. Reliable destructor
invoking behavior.

Disadv: Can be slow when working set is small but objects created and destroyed rapidly, doesn't
handle cycles without programmer recognizing them ahead of time and using structures like
weak pointers. Memory overhead.

(c) letting programmers use malloc/free (or new/delete) directly


Answer:

Adv: Allows for very direct control of garbage collection, which can lead to very efficient
specialized methods. No overhead.

Disadv: Prone to programmer error. Sometimes can not hard code when to destruct due to issues
of multiple owners, especially in multithreaded code.

2. What is the difference between fork and exec? Why do we need both?

Answer: Fork creates a near exact copy of a process. Exec replaces the running program in an
existing process. We need both because without fork we can only ever have one process and
without exec we can only ever run one program (without doing complicated I/O management)

(3) Windows has a system call that combines fork and exec. Its simplified signature is:

int CreateProcess(char* programName, char* cwd, arg1, arg2, ..., 0)

Where:

- programName is the program to run

- cwd is that working directory in which to run the program

- arg1, arg2, ... are the arguments to pass to the new process

It creates a new child process running the given program in the given working directory and it
passes it the given arguments.

The actual CreateProcess takes more arguments and has a different signature from the one above
(a) Show how to implement CreateProcess using the system calls we discussed in class. Pseudo
code is fine.

Answer: Basic idea is to fork, then in the child process transfer the var args into exec call, and in
the parent process just return the child id.

(b) Why did Unix choose to break CreateProcess into 2 separate calls (fork and exec)?

Answer: Many reasons can be stated here. Examples include: Allows more flexibility on how to
run processes. Allows us to set up the child's fdt easier while maintaining the state of the parent's
fdt. Was easier to implement at the time and made more sense at the time. Because the Berkeley
timesharing system already had fork and they based a lot of early decisions on that system.

(c) CreateProcess can be more efficient to implement. Explain.

Answer: Biggest reason is it doesn't have to spend time copying the addressSpace just to throw it
away.

You might also like