27.10.2014 Views

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Solutions</strong> to Chapter 18 | Threads <strong>and</strong> Locks<br />

18.2 How can you measure <strong>the</strong> time spent in a context switch?<br />

SOLUTION<br />

pg 86<br />

This is a tricky question, but let’s start with a possible solution.<br />

A context switch is <strong>the</strong> time spent switching between two processes (e.g., bringing a waiting<br />

process into execution <strong>and</strong> sending an executing process into waiting/terminated state).<br />

This happens in multitasking. The operating system must bring <strong>the</strong> state information of<br />

waiting processes into memory <strong>and</strong> save <strong>the</strong> state information of <strong>the</strong> running process.<br />

In order to solve this problem, we would like to record timestamps of <strong>the</strong> last <strong>and</strong> first instruction<br />

of <strong>the</strong> swapping processes. The context switching time would be <strong>the</strong> difference in<br />

<strong>the</strong> timestamps between <strong>the</strong> two processes.<br />

Let’s take an easy example: Assume <strong>the</strong>re are only two processes, P1 <strong>and</strong> P2.<br />

P1 is executing <strong>and</strong> P2 is waiting for execution. At some point, <strong>the</strong> OS must swap P1 <strong>and</strong><br />

P2—let’s assume it happens at <strong>the</strong> Nth instruction of P1. So, <strong>the</strong> context switch time for this<br />

would be Time_Stamp(P2_1) – Time_Stamp(P2_N)<br />

Easy enough. The tricky part is this: how do we know when this swapping occurs? Swapping<br />

is governed by <strong>the</strong> scheduling algorithm of <strong>the</strong> OS. We can not, of course, record <strong>the</strong><br />

timestamp of every instruction in <strong>the</strong> process.<br />

Ano<strong>the</strong>r issue: <strong>the</strong>re are many kernel level threads which are also doing context switches,<br />

<strong>and</strong> <strong>the</strong> user does not have any control over <strong>the</strong>m.<br />

Overall, we can say that this is mostly an approximate calculation which depends on <strong>the</strong><br />

underlying OS. One approximation could be to record <strong>the</strong> end instruction timestamp of a<br />

process <strong>and</strong> start timestamp of a process <strong>and</strong> waiting time in queue.<br />

If <strong>the</strong> total timeof execution of all <strong>the</strong> processes was T, <strong>the</strong>n <strong>the</strong> context switch time = T –<br />

(SUM for all processes (waiting time + execution time)).<br />

CareerCup.com<br />

2 5 8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!