MT1 Sample Qs and Solutions

I have jumps in question numbers because I've removed the linker questions.

Q1: What is a real-time system (RTS)? What is a hard RTS? What is a soft RTS? What is different between the two? Give an example of both hard and soft RTS.

Answer: A real-time system is a system where there are timing requirements on the system's response. That is functionality (ie. correct output values) is not enough unless it is output at the right time. Sometimes a system like that will not exhibit the correct behavior because it outputs the right value at the wrong time. That is because the system's output is an input to the environment around it, and wrong timing of the outputs will result in a different environment (eg. a control system).

Hard real-time systems are systems where, when the timimg requirements are not met, either the system does not function or it catastrophically fails. An ABS (Automatic Braking System in cars) system, most of aviation (ie. aeroplane) electronics, most weapon systems are hard real-time systems. Also, in hard RTSs, timing requirements are stringent (ie. tighter).

A soft real-time system, on the other hand, does not have stringent timing requirements. They have average timing requirements such as a multi-media system (eg. MP3 player). Timing requirements have to be met over a period of time, and when they are not met there is no catastroph. Also, soft RTSs can tolerate insertion of an extra latency such as shifting a whole by a second in time.


Q2: Which one of the below is correct about a RAMDISK?
(a) RAMDISK is a hard disk with a RAM cache.
(b) RAMDISK is another name for USB sticks.
(c) RAMDISK provides many times faster file access than a regular disk drive.
(d) RAMDISKs are not available on UNIX systems.
(e) RAMDISK is a feature of some HDs that needs to be turned on in the OS.

Answer: (c) RAMDISK provides many times faster file access than a regular disk drive.


Q3: What does PCB stand for?

Answer: Process Control Block.


Q4: What is a RAMDISK? What is it useful for?

Answer: We can use part of our RAM as a disk drive, and such a drive is called RAMDISK. The advantage of a RAMDISK is file access speed. The downside is the drive volatile. However, a special program can be installed to save the contents on a true disk drive at power-down and reload at power-up. See Wikipedia for more info.


Q5: What is interleaving?

Answer: Interleaving comes up in different contexts. Interleaving usually means using multiple copies of the same resource and spawning newer requests to extra copies of the resource while that resource is busy. This method increases throughput of storage devices such as RAM and HD.


Q6: What is the distinguishing property of hard real-time systems? Give an example of a hard real-time system.

Answer: Hard real-time systems are systems where, when the timimg requirements are not met, either the system does not funtion ro it catastrophically fails. An ABS (Automatic Braking System in cars) system, most of aviation (ie. aeroplane) electronics, most weapon systems are hard real-time systems.


Q7: What is different between hard and soft real-time systems? Give an example of a soft real-time system.

Answer: A hard real-time system has to obey timing requiremets. However, in a soft real-time system it is desirable that timing requirements are met. If they are nit met, user experience gets worse but the result is not catastroph. Simply put, if a soft real-time system does not meet desired timing, nobody dies. A multimedia system such as an MP3 player or DivX player is a soft real-time system. If timing is not met, the audioor video is not smooth and gets interrupted at times. Take a look at Wikipedia for more info.


Q8: We have 100 processes. Pi arrives at t=2i. P0's CPU burst is 200ms. Other processes all have a burst of 20ms. The OS is doing a RR (round robin) scheduling with a time quantum of tq. What is the average completion time (a) when tq=10 (b) when tq=100.

Answer: (a) P1 will finish first. Between t=10-20 its first 10ms will be executed. After the OS goes thru all 100 processes and does a 10ms of P0 again, it will execute and finish the burst of P1 (t=1010-1020). After that, every 10ms, a new process will finish. At t=2000, P1 thru P99 will have finished. At that point we executed 20 out of 200ms of P0. P0 will finish at t=2180. Completion time is finish time minus arrival time (ie. start time). Average completion time is sum of completion times divided by the number of processes (=100 in our case). Sum of completion times can be computed by summing finish times and subtracting sum of arrival times. Sum of finish times is equal to (Sum for i=1 to 99 of (1010 +10i)) +2180. That is equal to (1010x99 +10x(99x100)/2) +2180 = 151,670. Sum of arrival times is Sum for i=1 to 100 of (2i) = 2x(99x100)/2 = 99x100 = 9900. Hence sum of completion times = 151,670 -9,900 = 141,770. Divide that by 100 for average and you get approx. 1,517ms. This is really a large number. Most of the processes have a burst time of 20ms and they take almost 75 times that to complete.

(b) When tq=100, we do a 100 of P0 first, then processes finish their burst one by one before they get contexted out. Sum of completion times is (Sum for i=1 to 99 of (100 +20i)) +2180 = 111,080. Sum of arrival times was computed above: 9,900. Sum of completion times is 101,180. Average that over 100 processes and you get 1,012ms. That is a savings of 505ms per process. This also shows that tq should be picked such that an ordinary process' CPU burst should not be interrupted due to tq (ie. timer interrupt).


Q10 (16 pts): Write a C code that creates exactly 769 child and grandchild processes. Write it so that it creates them the quickest way (ie. the most parallel). No shared memory or semaphores are allowed.

Answer: The idea is we write code that spawns child processes in parallel. This is supposed to be faster compared to a for-loop in which the first parent process spawns all processes. Since we need 769 child processes, we will have 770 processes in total including the parent process. If we spawn processes in a binary tree fashion (ie. each process spawning 1 child), then in 9 forks we will have a total of 512 processes. Hence, in the 10th step we need 770-512=258 of them to spawn a child process. If we assign an id number to each process, then id=0..257 will fork(), and id=258..511 will not. Here is the code that does this:

main () {
  int id=0;
  for (i=0; i<9; i++)
    forkAndAdjustId(&id);
  if (id < 258)
    fork();
}

void forkAndAdjustId(int *idPtr) {
  pid_t pid = fork();
  *idPtr = ((*idPtr)<<1) +(pid==0);
}

Q11: Which one below is not a UNIX shell?
(a) csh
(b) tcsh
(c) zsh
(d) bash
(e) powsh

Answer: (e) powsh: There is no such thing but there is Power Shell for Windows.


Q12: Timer interrupt preempts cooperative multitasking processes. True or false?

Answer: False: That is the whole point. Unlike true (peemptive) multitasking, cooperative multitasking waits until a process voluntarily releases control (ie. CPU).


Q13: In Windows let's say I just wrote and compiled a program in C:\myprogs\mynewprog\bindir directory. I want to be able to run this program from a cmd window while in other directories without typing the path to the executable. How do I do this?

Answer: When you type a command in a Windows or UNIX shell, the shell first checks to see if it is a built-in shell command. If not, it assumes it is an executable (ie. program) and searches for it in the directories listed in the environment variable named PATH. In Windows cmd shell, it also checks pwd in addition to what is in PATH. SO the answer is, we need to append C:\myprogs\mynewprog\bindir to variable PATH. And that is done by:

  SET PATH=%PATH%;C:\myprogs\mynewprog\bindir

Q15: Which one below is not a UNIX shell?
(a) csh
(b) tcsh
(c) zsh
(d) cmdsh
(e) bash

Answer: (d) cmdsh


Q17: When a process is not currently executing, there are certain things that the operating system must maintain on behalf of the process, so that when the process starts executing again, it does so correctly. On the list below identify what does not need to be maintained?
(a) Information about file access status.
(b) Values of registers.
(c) Context switch time.
(d) Stack pointer.
(e) Program counter.

Answer: (c) Context switch time.


Q18: DMA means Direct Memory Access. What is it that accesses memory directly?
(a) CPU
(b) Cache
(c) Hard disk
(d) Peripherals
(e) Graphics adapter

Answer: (d) Peripherals


Q19: Which one is not true in the case of a cooperative multitasking OS?
(a) Processes cannot be preempted.
(b) There is no timer interrupt.
(c) A process can be halted when doing stdio.
(d) Processes are cooperative so yield to each other.
(e) The kernel does not trigger regularly.

Answer: (d) Processes are cooperative so yield to each other.


Q20: Which of the following is not a plus of using dynamic libraries as opposed to static libraries?
(a) Smaller executables
(b) Quicker load times
(c) Quicker run times
(d) Quicker compile times
(e) They are all pluses

Answer: (c) Quicker run times: Because resolving a function's address in a dynamic library has to be done by the OS and hence takes longer.


Q21: Which of the following is false?
(a) Protected mode is a superset of dual mode.
(b) Dual mode is a subset of protected mode.
(c) Kernel mode is a subset of dual mode.
(d) Kernel mode is a subset of protected mode.
(e) Protected mode is a subset of dual mode.

Answer: (e) Protected mode is a subset of dual mode.


Q23: When a process is not currently executing, there are certain things that the operating system must maintain on behalf of the process, so that when the process starts executing again, it does so correctly. On the list below identify what does not need to be maintained?
(a) Interrupt history.
(b) Information about file access status.
(c) Values of registers.
(d) Program counter.
(e) Stack pointer.

Answer: ((a) Interrupt history.


Q25: When a process is not currently executing, there are certain things that the operating system must maintain on behalf of the process, so that when the process starts executing again, it does so correctly. On the list below identify the things that the operating system must maintain on behalf of a non-executing process.

a.Interrupt vector.
b.Information about open files.
c.Register state.
d.Timer interrupt.
e.Control status register.
f.Console interrupt.
g.Memory management information (e.g. main_memory pointer).
h.Kernel/User mode bit.
i.Context switches.
j.Information about CPU speed.
k.The process' cache.
l.The PC location of the process' next instruction.
m.Turnaround time.
n.Disk controller information.

Answer:


Q26: Consider the following C program which spawns a child process. Tell me what the parent process prints; then tell me what the child process prints.

int main(int argc, char **argv)
{
  int pid, x=5;

  pid = fork();
  x = x+1;

  if (pid == 0)
  { /* child */
    printf("x1=%d\n", x);
    printf("x2=%d\n", x);
    printf("x3=%d\n", x);
    sleep(1);
    printf("x4=%d\n", x);
  } else {
    /* parent */
    printf("x5=%d\n", x);
    x = 3;
    sleep(1);
    printf("x6=%d\n", x);
  }

  printf("x7=%d\n", x);
}

Answer: Here is what they print. Remember that each process has its own memory space.

Parent:
x5=6
x6=3
x7=3

Child:
x1=6
x2=6
x3=6
x4=6
x7=6

Q27: Protecting the OS is crucial to ensuring that the computer system operates correctly. Provision of this protection is the reason for dual-mode operation, memory protection, and the timer. To allow max flexibility, however, you should also place minimal constraints on the user. The following is a list of instructions that are normally protected. What is the minimal set of instructions that must be protected?

a. Change to user mode.
b. Change to monitor mode.
c. Read from monitor memory.
d. Write into monitor memory.
e. Fetch an instruction from monitor memory.
f. Turn on timer interrupt.
g. Turn off timer interrupt.

Answer: The minimal set of instructions that must be protected are:

a. Change to monitor mode.
b. Read from monitor memory.
c. Write into monitor memory.
d. Turn off timer interrupt.

Q28: What is BIOS?

Answer: See Wikipedia.


Q29: What is DMA?

Answer: See Wikipedia.


Q30: Write C code that creates 5 child processes including grandchildren if any.

Answer:

for (i=0; i<5; i++) {
  if (fork () == 0)
    break;
}

Speaking of fork(), it is a very useful concept and also a function in UNIX. Windows does not exactly have the same thing. Read this to find out how a fork can be done in Windows.


Q31: How many child processes does the below code spawn?

  fork();
  fork();
  fork();
  fork();
  fork();

Answer: There are 5 forks and hence after the last one we will have a total of 25=32 processes. Since one of them is the parent, we have happened to create 32-1=31 child processes.


Q32: What is cooperative multitasking? And how is it different from preemptive (ie. regular) multitasking?

Answer: Cooperative multitasking means the OS cannot premept processes. The OS can switch to another process only when the process in possession of the CPU makes a system call (hence switches to the OS).


Q33: Give me the memory hierarchy starting from near the CPU and going to periherals in today's computers.

Answer: Registers → Cache (SRAM) → DRAM (main memory) → HD → secondary non-volatile storage such as optical disk, magnetic tapes, portable flash disks.


Q34: Compare compiling with static libraries and dynamic libraries in terms of their pros and cons.

Answer: You figure it out.


Q35: What is the equivalent of .DLL in Linux?

Answer: .so meaning "shared object".


Q36: Where do Windows and Linux look for shared libraries?

Answer: You figure it out.


Q37: What does the "make" program do?

Answer: You figure it out.


Q38: What are the two steps a compiler performs? And what is the advantage of separating the two steps?

Answer: Compiling and linking. When we separate the two steps, we compile only the files that changed and then link all of them together.


Q39: What is .o file? What is .a file in UNIX?

Answer: You figure it out.


Q40: How do I control whether I run a process in the background when issued from a command window in UNIX? Do I have a similar control mechanism in Windows?

Answer: If I put & sign at the end of the command-line, then the process is run in the background, otherwise is run in the foreground and we cannot issue subsequent commands to the command shell. There is no equivalent control mechanism in Windows.


Q41: What is the name of the first PC-UNIX? Which company developed it and which company funded the development?

Answer: Name is XENIX. SCO (Santa Cruz Operation) developed it for Microsoft.


Q42: For what reasons early mainframes used magnetic tapes as main memory?

Answer: In those days, RAM was expensive and big. Hence, mainframes had limited RAM. Even in those days, many applications required significant amount of memory. Therefore, the mainframes had no choice but use magnetic tapes as main memory (not just virtual memory). Even one magnetic tape was not enough sometimes. These main frames used magnetic tapes also as what we use HD these days for.


Q44: How does a cooperative multitasking OS switch from a process to another?

Answer: When the process makes a system call.


Q45: What is BIOS? Give me a short description.

Answer: When a PC boots up, that is when the PC is loading the OS from the hard disk (HD), it cannot talk to the HD because all those system functions come with the OS and the OS has not been loaded yet. Hence, we need a basic set of system functions especially those to read the master boot record (MBR) of the HD. These functions need to be stored in an easily readable device. This device is a ROM (read-only memory). Think of it like a non-volatile version of RAM. This ROM contains some basic OS functions. BIOS stands for Basic Input Output System. BIOS is mistakenly known as Basic Integrated Operating System by some people.


Q46: What are the two steps a compiler performs? And what is the advantage of separating the two steps?

Answer: Compile and Link. This way, we only link files that have not been modified and hence save a lot of compile time.


Q48: Which one below is a UNIX shell?
(a) Powersh
(b) Csh
(c) tcsh
(d) zshell
(e) besh

Answer: (c) tcsh

Here is what the other answers are: (a) needs to be PowerShell but it is a Windows shell anyway. (b) Csh is misspelled. It is csh. UNIX is case-sensitive. (d) zshell is a typo. It is zsh. UNIX shell names all end in "sh". (e) besh is a typo. It is bash. Born Again SHell.


Q50: If progA is a program that prints on the screen, how do I make it print into a file called fileA instead?

Answer: progA > fileA


Q51: If progB is a program that reads from the screen, how do I make it read from fileB instead?

Answer: progB < fileB


Q52: Let's say I have thousands of files in a dir. I want to list them on the screen page by page. How?

Answer: ls put_dir_path_here | more
ls puts the files in put_dir_path_here on the screen. | (pipe) redirects it to the next command instead of printing them on the screen. ANd more prints its stdin on the the screen (stdout) page by page.


Q53: What is the main difference between a process and a thread?

Answer: We have a slogan: "Her process kendi coplugunde oter." That is to say that every process runs in its own memory space. However, threads of a program run all in the same memory space.


Q54: What is "dual mode"?

Answer: Dual mode is a feature in most modern processors. Dual mode means the processor has two modes, namely, user mode and kernel mode. User programs run in user mode and OS kernel runs in kernel mode. Code running in user mode only has access to its won memory space. Code running in kernel mode has access to all parts of the memory space. In addition, code running in user mode does not have access to certain instructions, including the instruction that changes the mode to kernel mode. Kernel mode is also called other names such as privileged mode and monitor mode.


Q55: There are 3 processors connected in a pipeline that are cooperating to process incoming data and produce an output. Processor 1 (P1) takes 1 min to do its part and passes intermediate results to P2, P2 takes 3 min and passes results to P3, and P3 takes 5 min. What are the throughput and latency?

Answer: cycleTime equals the max of the station processing times. Hence Cycle time = max(1,3,5) = 5 min/result. Throughput = 1 / cycleTime = 12 results/hour. Latency has two answers. One answer is (n=#stations) * cycleTime = 3*5 min = 15 min. However, if optimizing latency is very important (which usually is not since cycleTime is what needs to be optimized) then the first station picks up inputs as late as possible, middle stations have to take cycleTime long, and the last station picks up inputs immediately at the beginning of a cycle. Then latency = firstStationTime +(n-2)*cycleTime +lastStationTime. hence, latency = 1 +(3-1)5 +5 = 11 min. Also, note that if latency of individual results is important the first result can be obtained faster. firsResultTime = Sum of all station times = 9 min.


Q56: What is the difference between hup and nohup?

Answer: In UNIX if a program is run with the word hup at the beginning of the command line, then when its parent process dies (the process from which it is spawned, hence the shell process where the command was typed), the process for that command is also killed. If a nohup is typed at the beginning of the command line, when the parent process dies, the process still goes non. This way you may be continuing to run processes even when you log off. If you do not type either hup or nohup, the common default these days is nohup. In the old days, hup was the default. hup and nohup is a shell keyword and may not exist in some UNIX shells. Although these are UNIX keywords, hup and nohup are rather concepts and can be implemented even in Windows.


Q57: What is Protected Mode?

Answer: Protected Mode is a Pentium (and Pentium clone) related buzzword. Protected Mode means the processor has the capability of going into protected mode (ie. dual mode) or just stay in Real Mode (where there is only mode where all programs can do all things -- access any memory and any instruction). Old MSDOS programs do not run even in kernel mode. Therefore, a modern Intel processor used to be put into Real Mode (a bacwards compatibility mode) to run old MSDOS programs. However, that used to sometimes hang (ie. lock) PCs, since there is no way OS can preempt a program in Real Mode unless the program makes a system (OS) call. Nowadays, I beileve older programs are run within a Virtual MSDOS machine (hence emulated) so that they cannot hang the computer.


Q58: Same type of question as Q55 but assume 4 processors with 4, 5, 2, 2 minutes. What are the throughput and latency.

Answer: CycleTime = max(4,5,2,2) = 5 min. Hence throughput is 12 results/hour. Latency is n*CycleTime = 20 min -- simple answer. latency = 4 +2*5 +2 = 16 min is the more involved answer. And it is possible that the first result is produced in only 4+5+2+2 = 13 minutes.


Q59: Give two reasons why caches are useful. What problems do they solve? What problems do they cause? If a cache can be made as large as the device for which it is caching (for instance, a cache as large as a disk), why not make it that large and eliminate the device?

Answer: Answer: Caches are useful when two or more components need to exchange data, and the components perform transfers at differing speeds. Cahces solve the transfer problem by providing a buffer of intermediate speed between the components. If the fast device finds the data it needs in the cache, it need not wait for the slower device. The data in the cache must be kept consistent with the data in the components. If a component has a data value change, and the datum is also in the cache, the cache must also be updated. This is especially a problem on multiprocessor systems where more than one process may be accessing a datum. A component may be eliminated by an equal-sized cache, but only if: (a) the cache and the component have equivalent state-saving capacity (that is, if the component retains its data when electricity is removed, the cache must retain data as well), and (b) the cache is affordable, because faster storage tends to be more expensive.


Q60: Writing an operating system that can operate without interference from malicious or undebugged user programs requires some hardware assistance. Name three hardware aids for writing an operating system, and describe how they could be used together to protect the operating system.

Answer:

a. Monitor/user mode
b. Privileged instructions
c. Timer
d. Memory protection

Q61: Some CPUs provide for more than two modes of operation. What are is a possible use of this?

Answer: In processors that explicity support VMs (ie. virtual machines), the lowest OS layer is the VM layer (also called hypervisor), and the individual OSes of the virtual machines sit on the VM layer. In processors like that, there are 3 HW modes: VM mode, OS mode, and the user mode. VM mode is the most powerful mode and can do everything.


Q62: What are the five major activities of an operating systemin regard to processmanagement?

Answer:

 The creation and deletion of both user and system processes
 The suspension and resumption of processes
 The provision of mechanisms for process synchronization
 The provision of mechanisms for process communication
 The provision of mechanisms for deadlock handling

Q63: What are the three major activities of an operating system in regard to memory management?

Answer: 1. Keep track of which parts of memory are currently being used and by whom. 2. Decide which processes are to be loaded into memory when memory space becomes available. 3. Allocate and deallocate memory space as needed.


Q64: What are the five major activities of an operating system in regard to file management?

Answer:

 The creation and deletion of files
 The creation and deletion of directories
 The support of primitives for manipulating files and directories
 The mapping of files onto secondary storage
 The backup of files on stable (nonvolatile) storage media

Q65: What is the purpose of the command interpreter? Why is it usually separate from the kernel?

Answer: It reads commands from the user or from a file of commands and executes them, usually by turning them into one or more system calls. It is usually not part of the kernel since the command interpreter is subject to changes.


Q66: List five services provided by an operating system. Explain how each provides convenience to the users. Explain also in which cases it would be impossible for user-level programs to provide these services.

Answer:

Program execution. The operating system loads the contents (or sections) of a file into memory and begins its execution. A user-level program could not be trusted to properly allocate CPU time.

I/O operations. Disks, tapes, serial lines, and other devices must be communicated with at a very low level. The user need only specify the device and the operation to perform on it, while the system converts that request into device- or controller-specific commands. User-level programs cannot be trusted to only access devices they should have access to and to only access them when they are otherwise unused.

File-system manipulation. There aremany details in file creation, deletion, allocation, and naming that users should not have to perform. Blocks of disk space are used by files and must be tracked. Deleting a file requires removing the name file information and freeing the allocated blocks. Protections must also be checked to assure proper file access. User programs could neither ensure adherence to protection methods nor be trusted to allocate only free blocks and deallocate blocks on file deletion.

Communications. Message passing between systems requires messages be turned into packets of information, sent to the network controller, transmitted across a communications medium, and reassembled by the destination system. Packet ordering and data correction must take place. Again, user programs might not coordinate access to the network device, or they might receive packets destined for other processes.

Error detection. Error detection occurs at both the hardware and software levels. At the hardware level, all data transfers must be inspected to ensure that data have not been corrupted in transit. All data on media must be checked to be sure they have not changed since they were written to the media. At the software level, media must be checked for data consistency; for instance, do the number of allocated and unallocated blocks of storage match the total number on the device. There, errors are frequently process-independent (for instance, the corruption of data on a disk), so theremust be a global program(the operating system) that handles all types of errors. Also, by having errors processed by the operating system, processes need not contain code to catch and correct all the errors possible on a system.


Q67: What is the purpose of system calls?

Answer: System calls allow user-level processes to request services of the operating system.


Q68: We have a true multi-tasking OS on our computer. It has a tq of 3ms. No tq for I/O tasks. Ignore the context swiching time. Assume we have only the following two processes running on the OS. P1 is started at t=0 and P2 at t=2ms. Below (P1: C3I5C4...) means P1 has a CPU curst of 3ms first then an I/O burst of 5ms, then a CPU burst of 4ms and so on. Give me the Gantt chart of how the OS schedules the two processes on the CPU and I/O as a tabel running vertically. Show the CPUwait and I/Owait queues.

  P1: C3I5C4I2C3
  P2: C6I3C2I6C5

Answer:

  P1: P1C1=3 P1I1=5 P1C2=4 P1I2=2 P1C3=3
  P2: q2C1=6 q2I1=3 q2C2=2 q2I2=6 q2C3=5

t   CPU  CPUwait | I/O  I/Owait
--- ---- ------- | ---- -------
1   P1C1  P1C1
2   "
3   "     q2C1
4   q2C1  "        P1I1  P1I1
5   "              "
6   "              "
7   q2C1  q2C1     "
8   "              "
9   "     P1C2
10  P1C2           q2I1  q2I1
11  "              "
12  "              "
13  q2C2  q2C2,P1C2
14  "     P1C2
15  P1C2  "        q2I2  q2I2
16                 "     P1I2
17                 "     "
18                 "     "
19                 "     "
20                 "     "
21 q2C3  q2C3      P1I2  "
22 "               "
23 "     P1C3
24 P1C3  P1C3,q2C3
25 "     q2C3
26 "     "
27 q2C3  "
28 "