OS Onsite interview Questions

1) When to use volatile?
specially in cases where value of variable can be modified by hardware

2) How do functions return value to the function that has called it?
using EAX register.
Case of context switch in case of functions

3) Write code for reader-writer problem?

4)

Advertisements

Thread Concepts

1) What is a daemon thread?

Sol: A daemon thread is a thread whose execution state is not evaluated when the JVM decides if it should stop or not. The JVM stops when all user threads (in contrast to the daemon threads) are terminated. Hence daemon threads can be used to implement for example monitoring functionality as the thread is stopped by the JVM as soon as all user threads have stopped:

public class Example {

	private static class MyDaemonThread extends Thread {

		public MyDaemonThread() {
			setDaemon(true);
		}

		@Override
		public void run() {
			while (true) {
				try {
					Thread.sleep(1);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) throws InterruptedException {
		Thread thread = new MyDaemonThread();
		thread.start();
	}
}

The example application above terminates even though the daemon thread is still running in its endless while loop.