Operating Systems Homework Questions
What is "file descriptor"? Does the number of open files equal the number of open "file descriptors"? Defend your answer.
- a handle used to access a file or other I/O resource in UNIX - No, not every open file has a file descriptor (Ex. text executables, working directories)
What is the Process Control Block (PCB) in OS? Please list the two components that are associated with PCB.
-Represents data needed for OS to control processes -Process ID number, Program counter
Please explain the system called "pipe". When is it used?
- FIFO queue of bytes treated as I/O device - When one process uses the output of another process as input
Assume you have a bank account, and the total amount is $1,000. If you withdraw $500 and your friend deposits $800, please explain the possible remainders due to race conditions. Also, how do Operating Systems solve the problem?
- $500, $1,300, $1,800 - Synchronize access to shared data
Briefly describe two differences and two similarities between Monitor and Semaphore.
- Both have signal() and wait(), both don't have busy waiting - Monitors are a lock and condition variables, Semaphores are a generalized lock. - Semaphores use locks for both mutual exclusion and synchronization, Monitors use locks for mutual exclusion and condition variables for synchronization
Please explain the following terms "busy waiting", "mutual exclusion", and "critical section".
- Busy waiting: Process waiting for an event to occur but still executing instructions, waste CPU resources. - Mutual exclusion: One thread or process does one activity at a time. - Critical section: Region in a program where shared resources are accessed.
What is a trap? Describe the differences between an interrupt and a trap.
- Exception in user process, synchronously generated by current running process - Interrupt: asynchronous, caused externally from hardware/software Trap: synchronous, generated by running process
What is a system call? Describe the mechanism of how an OS handles a system call.
- Interface between application and OS, allow processes to request services of the OS - 1) System calls provided by OS are requested by putting the parameters in registers/stacks, 2) Execute a trap instruction to switch from user mode to kernel mode, transfer control to OS, 3) OS examines the parameters of the call to determine which system call to be carried out, 4) OS invoke the system call, 5) System call finished and control is given back to user program
Please list two approaches for interprocess communication mechanisms for process and describe how they work.
- Message passing using send/receive like the producer/consumer problem - Shared Memory with semaphores, monitors, critical sections.
What is "context switching"? How can "context switching" be processed by the OS?
- Moving a process in or out of Running state then saving/restoring all processor-specific registers and other process information - OS saves state of process 1 into PCB1 and loads states of process 2 from PCB2
Please list three differences between multitasking OS and multiprocessing OS.
- Multitasking: Running multiple tasks simultaneously, Jobs/Tasks are managed by the OS and are kept in memory, Shares common resources between tasks such as a single processor/CPU - Multiprocessing: Running multiple processes simultaneously, Runs processes on multiple processors/CPU's
In the dining-philosopher problem, assume that the shared data are semaphore chopstick[4]; where all the elements of chopstick are initialized to 0. The structure of philosopher i is shown as follows: void philosopher (int i) { while (TRUE) { wait(chopstick[i]); wait(chopstick[i+1] % 5); ... eat(); ... signal(chopstick[i]); signal(chopstick[i+1] % 5); ... think(); ... } } 1) Can the above program codes solve the dining-philosophers problem? Why? 2) Please use Monitor to solve the problem (Only pseudo-code).
- No because of deadlock monitor DP { status state[5]; condition self[5]; Pickup(int i){ state[i] = hungry; test(i); if(state[i] != eating) self[i].wait; } Putdown(int i){ state[i] = thinking; test((i+1)%5); test((i-1)%5); } test(int i){ if(state[(i+1)%5] != eating && state[(i-1)%5] != eating && state[i]==hungry){ state[i] = eating; self[i].signal(); } } init() { for(i=0 to 4) state[i]=thinking; } }
Describe the basic process of interrupt handling.
- OS pause and save state of user program - Hardware checks interrupt vector to identify type of interrupt - Jump to associated address and invoke appropriate handler - OS resumes previous process
What is an Operating System? Descript a conceptual view of a computer system.
- Operating system is a program that controls execution of applications and acts as an interface between user and hardware - Operating system is in the middle of the hardware and software layers
Why are dual mode operations a necessity for resource protection? How do they work?
- Restrict access for user mode, unrestricted access for kernel mode. Pass control to kernel mode by processor exception if privileged instructions are executed on user mode. - Uses mode bits to determine user or kernel mode - System calls in user mode are used to request kernel for OS services
What's spooling? Please provide an example about spool.
- SPOOL (Simultaneous Peripheral Operations On Line) - Buffer that holds data temporarily until device is ready to process next job/data - Ex. Printer using spooling: print jobs sent to printer are saved in a queue in memory
What is interrupt? Describe the two differences between interrupt and exception.
- Signals sent to CPU from hardware or software to stop its current activities and execute the appropriate part of OS - Interrupt: asynchronous, handle external events Exception: synchronous to instructions, handle instruction faults
Given two processes, the structure of each process is as follows: PROCESS i do { flag[i] = true; turn = i; while (flag[j] && turn == j); //Critical Section flag[i] = false; //Remainder Section } while(1); PROCESS j do { flag[j] = true; turn = j; while (flag[i] && turn == i); //Critical Section flag[j] = false; //Remainder Section } while(1); Determine whether it is a correct solution to the critical-section problem. If yes, prove that the algorithm satisfies requirements for the critical-section problem. If not, explain your answer.
- Solution is not correct - Using Peterson's solution, Modify such as the following In process i: turn = i --> turn = j In process j: turn = j --> turn = i
What is a Makefile? What does it do?
- Special file that contains shell commands - Run the make file by typing 'make' in the terminal which compiles and links the files in a certain order
One way of achieving mutual exclusion is to allow a process to disable interrupts before it enters its critical section and then enable interrupts after it leaves its critical section. Please see the following code. Please explain why this code cannot work and also propose how to solve it. Lock::Acquire() { disable interrupts; } Lock::Release(){ enable interrupts; }
- Threads can't be stopped once interrupts are disabled, Critical section can be long, can't wait too long to respond to interrupts - The following Lock::Acquire() { disable interrupts; if(value==BUSY){ add thread to wait queue enable interrupts; thread->block() } else value = BUSY; enable interrupts; } Lock::Release(){ disable interrupts; if queue not empty{ take thread1 off wait queue put thread1 on ready queue } else value = FREE; enable interrupts; }
In a protected architecture, please list three different situations or machine level instructions that a user mode program uses to transfer control to the kernel.
- user program acts silly (division by zero) - attempting to perform privileged instruction - something interrupts the currently executing process
Please justify the difference between write() (a system call) and printf() (a standard library function).
- write(): Kernel level system call, only writes a sequence of bytes - printf(): User level library function, converts data into formatted sequence of bytes and invokes write() to output those bytes
What is a process?
A program in execution
Draw a process state transition diagram. Indicate in the diagram where "context switching" may happen and explain the reason.
New --> Ready Ready <--> Running Running --> Blocked Running --> Terminated Blocked --> Ready - Running --> Blocked: waiting for I/O to finish needs to save current process - Ready <--> Running: Interrupt lower priority processes to run higher priority ones
Assume that more than one producer and consumer, how to use semaphore to solve the producer-consumer problems.
PRODUCER WITH SEMAPHORES void producer (void) { int item; while (true) { producer_item(&item); down(&empty); //wait for empty down(&mutex); //wait for buffer use enter item(item); //only 1 up(&mutex); //release buffer up(&full); //increase full count } } CONSUMER WITH SEMAPHORES void consumer(void) { int item; while (true) { down(&full); //wait for full down(&mutex); //wait for buffer use remove item(item); up(&mutex); //release buffer up(&empty); //increase empty count consume_item(item); } }
Describe the implementations of create-a-new-process mechanism.
Parent continues executing --> Parent stops executing until children are finished --> Parents and children share all variables --> Children share only subset of parent's variables --> Parents and children share no common resources
Consider the following pseudocode, which you may assume compiles and runs: int c = 9 // global variable int main(){ pid_t pid = fork(); c++; if(pid == -1) exit(-1); else if(pid == 0) c += 4; printf("I am the child, c is %d\n", c); else c += 2; printf("I am the parent, c is %d\n", c); } a. What is the output? b. Is any other output possible? Defend your answer.
a. I am the child, c is 14 I am the parent, c is 12 b. I am the parent, c is 12 I am the parent, c is 14 - Possible because when fork() is called either parent or child can take over first
Operating systems provide thread and process facilities: a. Describe any two differences between threads and processes. b. Describe any two differences between kernel-level threads and user-level threads.
a. Process has own code/data/heap, and has expensive creation and context switching, Thread doesn't. b. User-level thread processes are blocked by system calls, Kernel-level thread processes aren't. User-level thread has customizable scheduler, Kernel-level thread has only one scheduler.
Name the Linux command user to: a. Print the directory contents b. Determine permissions for a file c. Determine the current directory d. Remove read permissions from group and other users for a particular file e. Compare two files
a. ls b. ls -l c. pwd d. chmod go-r <file> e diff <file1> <file2>
How do you compile a C program from the Linux command line?
gcc <filename>
What program can you use to debug a C program on the departmental Linux machines?
gdb a.out
In the following code use a lock to protect any data that might need protecting. Keep your critical sections as small as possible. int flag = 0; int a = 0; void thread_a() { a += 2; flag = 1; } void thread_b() { int b = 0; if (flag) { b++; } b += 3; a -= b; }
int flag = 0; int a = 0; void thread_a() { lock_acquire(); //****// a += 2; flag = 1; lock_release(); //****// } void thread_b() { int b = 0; lock_acquire(); //****// if (flag) { b++; } lock_release(); //****// b += 3; lock(); //****// a -= b; lock_release(); //****// }