FINAL EXAM
What would be printed as a result of running following code: void *child(void *arg) { printf("3"); done = 1; return NULL; } int main(int argc, char *argv[]) { pthread_t p; printf("1"); Pthread_create(&p, NULL, child, NULL); while (done == 0); printf("2"); return 0; }
132
If the input is 12, what is the final value for numItems? int x; int numItems = 12; scanf("%d", &x); if (x == 12) { numItems = 100; } else { numItems = 200; } numItems = numItems + 1;
200 , Not Selected 201 , Not Selected Correct answer: 101 100
What will be the output of LINE A in the following program? #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 5; int main() { pid_t pid; pid = fork(); if(pid == 0){ /*child process*/ value += 15; return 0; } else if(pid > 0) {/*parent process*/ wait(NULL); printf("PARENT: value = %d", value); /* LINE A */ return 0; } }
5
Which is true regarding binary numbers?
A bit can be represented by a 1 or a 2 , Not Selected The binary number system is base 1 , Not Selected Correct answer: The compiler translates decimal numbers to binary to store the value Base 10 numbers do not need to be converted to binary to be stored in memory
Which of the following did we assume to be the data structure that keeps track of free memory?
A free hash table , Not Selected A free heap , Not Selected Correct answer: A free list A free tree
Which of the following can cause a segmentation fault?
A process accessing a different logical segment e.g. accessing the stack segment instead of a heap segment , Not Selected The OS losing track of all segment bases , Not Selected A process accessing an illegal address , Not Selected Incorrect answer: A process requesting an address not available in the entire memory space
Which process state best matches the following description?
A process has performed some kind of operation that makes it not ready to run until some other event takes place. Waiting , Not Selected Correct answer: Blocked Zombie , Not Selected Not Ready
Which is true regarding variables?
A programmer declaring a variable just needs to provide the name , Not Selected Every time a new value is assigned to a variable, the compiler designates a new memory location , Not Selected Variables must be declared before being assigned a value or read , Not Selected Incorrect answer: The compiler designates a new memory location for a variable the first time a value is assigned
What is programmed to raise an interrupt in every certain interval?
A scheduler , Not Selected A boot sequence , Not Selected Correct answer: A Timer A process
What is the benefit of having separate locks for enqueue and dequeue operations over a single lock in concurrent queues? Select all that apply.
Answer, Correctness guarantee Correct answer: Concurrency of enqueue and dequeue operations Correct answer: Performance improvement Answer, Fairness guarantee
[Select all the apply] Which of the following statement(s) is/are not true?
Answer, Instruction pointer provides answer to the question on which instruction of a program is currently being executed Correct answer: Policy provides answer to the question on how does an OS perform a context switch Answer, The heap memory is required for interesting data structures including linked list, hash table, and trees Correct answer: Low-level mechanism provides answer to the question on which process should the OS execute now
Semaphore can be used as a substitute for: (Select all that apply)
Answer, Locked counter Correct answer: Condition variables Correct answer: Locks Answer, Signal variables
Which of the following statements is/are true about the reader-writer problem? Select all that apply.
Answer, Multiple writers can write at the same time Answer, One reader and One writer can write at the same time Answer, Multiple readers and One writer can write at the same time Correct answer: Multiple readers can read at the same time
Which of the following are reasons one might use threads?
Answer, Multiprocessing Answer, Data Sharing Correct answer: Avoiding Blocks Correct answer: Parallelism
Select all the default open file descriptors of a process in UNIX systems:
Answer, Standard Error Answer, Standard Output Correct answer: Standard Input
How do you get out of a while loop while it is running? Select all that apply.
Answer, The loop condition must return true Correct answer: Use a break command Correct answer: The loop condition must fail Answer, You can't get out of a while loop while it is running
Consider this implementation of producer/consumer problem. In what scenario(s) does this implementation work correctly? int loops; // must initialize somewhere...cond_t cond;mutex_t mutex;void *producer(void *arg) { int i; for (i = 0; i < loops; i++){ Pthread_mutex_lock(&mutex); // p1 if (count == 1) // p2 Pthread_cond_wait(&cond, &mutex); // p3 put(i); // p4 Pthread_cond_signal(&cond); // p5 Pthread_mutex_unlock(&mutex); // p6 }} void *consumer(void *arg) { int i; for(i = 0; i < loops; i++) { Pthread_mutex_lock(&mutex); // c1 if (count == 0) // c2 Pthread_cond_wait(&cond, &mutex); // c3 int tmp = get(); // c4 Pthread_cond_signal(&cond); // c5 Pthread_mutex_unlock(&mutex); // c6 printf("%d\n", tmp); }}
Answer, multiple consumers, 1 producer Correct answer: 1 consumer, 1 producer Answer, Never Answer, 1 consumer, multiple producers
Select all the components in which the virtual address generated by a process is initially broken into:
Answer, physical frame number (PFN) Correct answer: the offset within the page Correct answer: the virtual page number (VPN) Answer, page table entry (PTE)
Which of the following is not a metric of evaluation of a lock?
Complexity , Not Selected Efficiency , Not Selected Incorrect answer: Fairness Mutual exclusion
What is the ending value of y if x is 50? y and x are integers. y = (x < 21) ? 100 : x; Correct answer: 50 'x' , Not Selected 100 , Not Selected 21
Correct answer: 50 'x' , Not Selected 100 , Not Selected 21
Select all the steps taken by the switch() routine when switching context from Process A to Process B:
Correct answer: Changes the stack pointer to use B's kernel stack (and not A's) Answer, Switches to kernel mode to handle the timer interrupt Correct answer: Saves current register values (into the process structure of A) Correct answer: Restores the registers of Process B (from its process structure entry) Answer, Returns-from-trap to start running Process B
Select all the steps taken by the switch() routine when switching context from Process A to Process B:
Correct answer: Changes the stack pointer to use B's kernel stack (and not A's) Correct answer: Restores the registers of Process B (from its process structure entry) Answer, Returns-from-trap to start running Process B
Select all components which may constitute a TLB entry:
Correct answer: Dirty bit Correct answer: Protection bit Correct answer: PFN Correct answer: VPN
Which of the following best describes the purpose of the program counter (PC)?
Correct answer: It tells us the instruction of the program which will execute next.
What is the benefit of having separate locks for enqueue and dequeue operations over a single lock in concurrent queues? Select all that apply.
Correct answer: Performance imporvement Answer, No benefit in using separate locks Answer, Correctness guarantee Correct answer: Concurrency of enqueue and dequeue operations
Which of the following refers to a few bits per segment that indicate whether or not a program can read or write a segment, or perhaps execute code that lies within the segment?
Correct answer: Protection bits Reading bits , Not Selected Permission bits , Not Selected Read-write bits
Select all the default open file descriptors of a process in UNIX systems:
Correct answer: Standard Input Correct answer: Standard Error Answer, Standard Warning Correct answer: Standard Output
Which is the first thing the compiler does when a function is called?
Correct answer: Stores the address of the function call Converts the function's code to assembly , Not Selected Creates a jump instruction to the function , Not Selected Creates instructions to jump to the function
Which of the following techniques ensure fairness?
Correct answer: Ticket lock using fetch-and-add Lock using load-linked and store-conditional , Not Selected Spin lock using compare-and-swap , Not Selected Spin lock using test-and-set
[Select all that apply] Consider a process P that executes the fork system call twice. That is, it runs code like this: int ret1 = fork(); int ret2 = fork(); How many direct children of P (i.e., processes whose parent is P) and how many other descendants of P (i.e., processes who are not direct children of P, but whose grandparent or great grandparent or some such ancestor is P) are created by the above lines of code? You may assume that all fork system calls succeed.
Correct answer: Two direct children of P are created Answer, Four direct children of P are created Correct answer: One other descendant of P is created Answer, No other descendant of P is created
Which of the following approaches work well in context of spinning? Select all that apply.
Correct answer: Using yield Answer, Compare-and-swap Correct answer: Using queues Answer, Fetch-and-add
Which of the following is inherently non-atomic operation:
Correct answer: addadd a number at a specific memory address movmov address to a register , Not Selected addadd a number to a register , Not Selected movmov from register to a specific memory address
Which statement XXX outputs each element of array customerName to a file called CustomerList.txt using a file pointer? char* customerName[5] = {"Bob", "Sally", "Jim", "Joe", "Denise"}; FILE *inputFile = NULL; int numberPlayers; inputFile = fopen ("CustomerList.txt", "w"); for (int i = 0; i < (5); ++i) { XXX }
Correct answer: fprintf (inputFile, "%s\n", customerName[i]); fprintf(inputFile, "%s\n", customerName); , Not Selected fprintf(stdout, "%s\n", customerName[i]); , Not Selected fprintf("CustomerList.txt", "%s\n", customerName[i]);
Fill in the blanks: To fix the problem of __________________, if a job gives up the CPU before its time slice ends but it has used its time allotment, then its priority ___________________ .
Correct answer: gaming the scheduler, reduces
Fill in the blank: The time from when a job arrives in a system to the first time it is scheduled is called ____________.
Execution Time , Not Selected Turnaround Time , Not Selected Processing Time , Not Selected Correct answer: Response Time
The free space consists of variable-sized units such that there is not enough contiguous space to satisfy a request or to allocate to a process. Which of the following best describes the statement above?
External Fragmentation , Not Selected Incorrect answer: Internal Fragmentation Fragmentation , Not Selected Segmentation
Convoy effect deteriorates the performance of which scheduler Correct answer:
FCFS (First-come first served) PSJF (Preemptive shortest job first) , Not Selected SJF (Shortest job first) , Not Selected STCF (Shortest time to completion first)
Which of the following CPU schedulers matches the following description? "Any time a new job enters the system, the scheduler determines which of the remaining jobs (including the new job) has the least time left, and schedules that one."
FIFO , Not Selected Correct answer: STCF Round Robin , Not Selected SJF
What is one benefit and one loss of write-through caching in hard drives?
Faster but unreliable , Not Selected Correct answer: Reliable but slower Reliable yet faster , Not Selected Faster but lower capacity
Which of the following does not result in errors when you use the free() function?
Freeing memory before you are done with it , Not Selected Calling free() incorrectly , Not Selected Correct answer: Freeing memory only once Forgetting to free memory
0 / 2 points What is the output of the following code block? int n; for (n = 0; n < 10; n = n + 3) {printf("%d ", n);}
Incorrect answer: 0 3 6 9 12 1 4 7 10 , Not Selected 0 3 6 9 , Not Selected 0 1 2 3 4 5 6 7 8 9
Which of the following did we assume to be the data structure that keeps track of free memory?
Incorrect answer: A free heap A free hash table , Not Selected A free list , Not Selected A free tree
Which of the following best describes the purpose of the program counter (PC)?
Incorrect answer: It tells us the program which will execute next. It manages the stack for function parameters, local variables, and return addresses. , Not Selected It manages the memory allocated for a program. , Not Selected It tells us the instruction of the program which will execute next.
What could be a potential issue if the following two threads run simultaneously? pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; Thread 1:: pthread_mutex_lock(&lock); if (ptr) { ptr -> val = 1; } pthread_mutex_unlock(&lock); Thread 2:: pthread_mutex_lock(&lock); ptr = NULL; pthread_mutex_unlock(&lock);
Incorrect answer: Order violation Atomicity violation , Not Selected None of the other choices is correct. , Not Selected Potential of a deadlock
Which of the following refers to a few bits per segment that indicate whether or not a program can read or write a segment, or perhaps execute code that lies within the segment?
Incorrect answer: Permission bits Read-write bits , Not Selected Protection bits , Not Selected Reading bits
The idea of the approximate counter technique is to have a separate local counter for each _________
Incorrect answer: program OS , Not Selected thread , Not Selected CPU
Rule 5: After some time period S, move all the jobs in the system to the topmost queue. If S is set too low, which of the following will happen?
Interactive jobs may not get a proper share of the CPU. , Not Selected The value of S has no impact on the jobs in the system. , Not Selected New jobs will take longer to execute. , Not Selected Incorrect answer: Long-running jobs could starve.
Which of the following is the reason, interrupts cannot be used for building locks?
No support of single CPU , Not Selected Incorrect answer: All of the above The approach tends to be complex , Not Selected Requires privileged action
Consider a process executing on a CPU. Give an example scenario that can cause the process to undergo an involuntary context switch.
OS policy , Not Selected Performance optimization , Not Selected A blocking system call , Not Selected Correct answer: Timer interrupt
Fill in the blank: A multilevel feedback queue scheduler (MLFQ) generally assigns a long quantum to _______________
Old processes , Not Selected High priority processes , Not Selected New processes , Not Selected Correct answer: Low priority processes
Fill in the blank: A multilevel feedback queue scheduler (MLFQ) generally assigns a long quantum to _______________
Old processes , Not Selected Incorrect answer: New processes Low priority processes , Not Selected High priority processes
What could be a potential issue if the following two threads run simultaneously? Thread 1:: if (ptr) { ptr -> val = 1; } Thread 2:: ptr = NULL;
Order violation , Not Selected None of the other choices is correct , Not Selected Potential of a deadlock , Not Selected Correct answer: Atomicity violation
What seems to be the issue with following usage of conditional variable cc? void thr_exit() { done = 1; Pthread_cond_signal(&c);}void thr_join() { if (done == 0) Pthread_cond_wait(&c);}
Performance issues , Not Selected Correct answer: Potential of a race condition There is no issue , Not Selected Potential of infinite blocking
Which of the following refers to a few bits per segment that indicate whether or not a program can read or write a segment, or perhaps execute code that lies within the segment?
Permission bits , Not Selected Read-write bits , Not Selected Correct answer: Protection bits None of the other choices
What could be a potential issue if the following two threads run simultaneously? Thread 1::if (ptr) { ptr -> val = 1;}Thread 2::ptr = NULL;
Potential of a deadlock , Not Selected Circular wait , Not Selected Order violation , Not Selected Correct answer: Atomicity violation
What does the base of a segment contain?
Starting virtual address of a process , Not Selected Segment length , Not Selected Correct answer: Starting physical address of a process Offset that you have to add to the address you want to access
What does a CPU do when a process tries to access memory outside its bounds?
The CPU modifies its bounds to access the memory that the process wants. , Not Selected The CPU omits that particular instruction in the program. , Not Selected The CPU stops the process. , Not Selected Correct answer: The CPU raises an exception.
What could be a potential issue if the following two threads run simultaneously? pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER; Thread 1::pthread_mutex_lock(&lock);if (ptr) { ptr -> val = 1; pthread_mutex_lock(&lock2); ptr2 = NULL; pthread_mutex_unlock(&lock2);}pthread_mutex_unlock(&lock);Thread 2::pthread_mutex_lock(&lock2);pthread_mutex_lock(&lock);ptr2 = ptr;ptr = NULL;pthread_mutex_unlock(&lock);pthread_mutex_unlock(&lock2);
There is no issue with the code , Not Selected Correct answer: Potential of a deadlock Order violation , Not Selected Atomicity violation
The free space consists of variable-sized units such that there is not enough contiguous space to satisfy a request or to allocate to a process.
Which of the following best describes the statement above Segmentation , Not Selected Correct answer: External fragmentation Fragmentation , Not Selected Internal fragmentation
The free space consists of variable-sized units such that there is not enough contiguous space to satisfy a request or to allocate to a process.
Which of the following best describes the statement above? Internal Fragmentation , Not Selected Correct answer: External Fragmentation Fragmentation , Not Selected Segmentation
Fill in the blank: In MIPS TLB, _____________ bits determine how a page is cached by the hardware.
cache , Not Selected global , Not Selected valid , Not Selected Correct answer: coherence
What will be the output of the following command?
echo '$HOME' Correct answer: $HOME HOME , Not Selected /home , Not Selected It will show an error
Which of combination below is a powerful way to create and manipulate process?
fork/wait , Not Selected exec/wait , Not Selected Correct answer: fork/exec fork/exec/wait
Fill in the blank: If you write one byte too far past the end of the allocated space, then it can result in ____________. In some cases this is harmless, for example, overwriting a variable that isn't used anymore.
heap overflow , Not Selected Correct answer: buffer overflow memory overflow , Not Selected stack overflow
Which statement causes an integer overflow?
int x = 3000;int y = 1000;int z; z = (x * y) + (x * y) + (x * y) , Not Selected Correct answer: z = x * y * y; z = (x * x) + (y * y) + (x * y) , Not Selected z = (x * x) + (y * y)
Fill in the blank: If you write one byte too far past the end of the allocated space, then it can result in ____________. In some cases this is harmless, for example, overwriting a variable that isn't used anymore.
memory overflow , Not Selected buffer overflow , Not Selected heap overflow , Not Selected Incorrect answer: stack overflow
Which is not a valid identifier?
name1 , Not Selected Correct answer: 1stName FirstName , Not Selected first_name
Fill in the blanks A common error when allocating a string is forgetting to account for the _____.
null character , Not Selected Incorrect answer: space between characters 0-th index , Not Selected bytes in a string
Fill in the blank: A __________ stores virtual-to-physical address translations, thus letting the system know where each page of an address space actually resides in physical memory.
page table entry (PTE) , Not Selected reference bit , Not Selected Correct answer: page table page frame
What is the problem with the code below to ensure mutual exclusion for the variable x?
pthread_mutex_t lock;pthread_mutex_lock(&lock);x = x + 1; // critical section pthread_mutex_unlock(&lock); Incorrect call to pthread\_mutex\_unlockpthread_mutex_unlock , Not Selected Correct answer: Faulty initialization of locklock Nothing , Not Selected Incorrect call to pthread\_mutex\_lockpthread_mutex_lock
You want to access a lock if it is not already held, which call do you make?
pthread_mutex_timedlock() , Not Selected pthread_mutex_lock() , Not Selected Correct answer: pthread_mutex_trylock()
Consider following code to initialize a semaphore. In L2L2 what does value 11 represent?
sem_t s;sem_init(&s, 0, 1); //L2 Represents whether ss is shared between different threads , Not Selected State of the lock , Not Selected The initial value of the semaphore , Not Selected Incorrect answer: Max value of the semaphore
Which of the following is/are region(s) of the address space that may grow (and shrink) while the program runs?
stack, heap
Fill in the blanks: To fix the problem of __________________, if a job gives up the CPU before its time slice ends but it has used its time allotment, then its priority ___________________ .
starvation, reduces , Not Selected Correct answer: gaming the scheduler, reduces starvation, stays the same , Not Selected gaming the scheduler, stays the same
Fill in the blanks Heap is a region in program memory where _____.
the "malloc" function allocates memory , Not Selected Incorrect answer: a function's local variables are allocated global and static local variables are allocated , Not Selected the program instructions are stored
How do you prevent the problem of gaming the scheduler?
via accounting , Not Selected via voodoo constants , Not Selected via boosting , Not Selected Correct answer: via multiple priority queues
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(int*), void *arg); The function pointer start _routine has what return type?
void , Not Selected int , Not Selected Correct answer: void* int*
What is wrong with the following code?
void *mythread(void *arg) { myarg_t *args = (myarg_t *) arg; printf("%d %d\n", args->a, args->b); myret_t var; var.x = 1; var.y = 2; return (void *) &var; } Correct answer: Stack pointer returned by a function Race condition , Not Selected Null pointer accessed , Not Selected Memory leak
The _______________ system call does not return control to the calling point when it succeeds.
wait( ) , Not Selected Incorrect answer: close( ) fork( ) , Not Selected exec( )
Both the following conditions must be true for a person to ride: At least 5 years old, and Taller than 36 inches. Which expression evaluates to true if a person can ride?
(age <= 5) && (height >= 36) , Not Selected (age >= 5) || (height > 36) , Not Selected (age > 5) && (height <= 36) , Not Selected Correct answer: (age >= 5) && (height > 36)
Which of the following statements is true about a memory leak? A memory leak occurs when a program allocates memory but loses the ability to access the allocated memory.
, Not Selected Incorrect answer: A memory leak is a memory location that has been dynamically allocated but no longer used by a program. A memory leak is a process used to determine if the memory is usable or unusable. , Not Selected A memory leak is an automatic process of finding and freeing unreachable allocated memory locations.
Which of the following is achieved by using a base register? It ensures that such addresses are within the confines of the address space.
, Not Selected Incorrect answer: It is used to store all the physical addresses that form the address space of a program. It stores the contents in the virtual address space so that they can be transferred to their corresponding physical address space. , Not Selected It transforms virtual addresses (generated by the program) into physical addresses.
Thread throttling is the process of: Locking and unlocking threads
, Not Selected Killing some threads to allow smooth functioning , Not Selected Correct answer: Limiting the number of running threads Bogging down of OS due to excess threads
Which of the following can cause a segmentation fault? A process accessing a different logical segment e.g. accessing the stack segment instead of a heap segment
, Not Selected The OS losing track of all segment bases , Not Selected A process requesting an address not available in the entire memory space , Not Selected Correct answer: A process accessing an illegal address
Which of the following can cause a segmentation fault? A process accessing a different logical segment e.g. accessing the stack segment instead of a heap segment
, Not Selected The OS losing track of all segment bases , Not Selected Correct answer: A process accessing an illegal address A process requesting an address not available in the entire memory space
Consider this implementation of producer/consumer problem. In what scenario(s) does this implementation work correctly? int loops; // must initialize somewhere... cond_t cond; mutex_t mutex; void *producer(void *arg) { int i; for (i = 0; i < loops; i++){ Pthread_mutex_lock(&mutex); // p1 if (count == 1) // p2 Pthread_cond_wait(&cond, &mutex); // p3 put(i); // p4 Pthread_cond_signal(&cond); // p5 Pthread_mutex_unlock(&mutex); // p6 } } void *consumer(void *arg) { int i; for(i = 0; i < loops; i++) { Pthread_mutex_lock(&mutex); // c1 if (count == 0) // c2 Pthread_cond_wait(&cond, &mutex); // c3 int tmp = get(); // c4 Pthread_cond_signal(&cond); // c5 Pthread_mutex_unlock(&mutex); // c6 printf("%d\n", tmp); } }
1 consumer, 1 producer
