KLA Tencor Interview Question
Dev Leads Dev LeadsCountry: India
Interview Type: In-Person
static object bufferRWLock = new object();
static object bufferFullLock = new object();
static bool bufferFull = false;
static byte[] buffer = new byte[1024];
public class Writer
{
private string name;
public Writer(string name)
{
this.name = name;
}
public void Write()
{
lock (bufferFullLock)
{
if (bufferFull)
{
System.Console.WriteLine("{0} tried to write, but buffer was full", name);
return;
}
}
lock (bufferRWLock)
{
//
// Write to buffer here...
//
lock (bufferFullLock)
{
System.Console.WriteLine("{0} wrote to buffer", name);
bufferFull = true;
}
}
}
}
public class Reader
{
private string name;
public Reader(string name)
{
this.name = name;
}
public void Read()
{
lock (bufferFullLock)
{
if (bufferFull)
{
lock (bufferRWLock)
{
//
// Read the buffer and empty it here...
//
bufferFull = false;
System.Console.WriteLine("{0} has read the buffer", name);
}
}
else
{
System.Console.WriteLine("{0} tried to read, but no buffer", name);
}
}
}
}
static object bufferRWLock = new object();
static object bufferFullLock = new object();
static bool bufferFull = false;
static byte[] buffer = new byte[1024];
public class Writer
{
private string name;
public Writer(string name)
{
this.name = name;
}
public void Write()
{
lock (bufferFullLock)
{
if (bufferFull)
{
System.Console.WriteLine("{0} tried to write, but buffer was full", name);
return;
}
}
lock (bufferRWLock)
{
//
// Write to buffer here...
//
lock (bufferFullLock)
{
System.Console.WriteLine("{0} wrote to buffer", name);
bufferFull = true;
}
}
}
}
public class Reader
{
private string name;
public Reader(string name)
{
this.name = name;
}
public void Read()
{
lock (bufferFullLock)
{
if (bufferFull)
{
lock (bufferRWLock)
{
//
// Read the buffer and empty it here...
//
bufferFull = false;
System.Console.WriteLine("{0} has read the buffer", name);
}
}
else
{
System.Console.WriteLine("{0} tried to read, but no buffer", name);
}
}
}
}
We will implement writer class having a static property : int writerCount, and a private property writerId
and will implement reader class having a static property : int readerCount and a private property readerId
Each time a writer/readerId Object is created, the object will be assigned writerId = ++writerCount, and readerId = ++readerCount
Now writeToBuffer() method in writer class checks
if(getBuffer().isEmpty() && !getBuffer().writeLocked && getBuffer().writeCount()%Writer::writerCount == writerId) {
getBuffer().acquireWriteLock();
getBuffer().write(data);
getBuffer.incrementWriteCount();
getBuffer().setFull();
getBuffer.releaseWriteLock();
}
else wait();
For reader, we will do the following:
if(getBuffer().isFull() && getBuffer().readCount()%Reader::readerCount == readerId) {
data = getBuffer().read();
getBuffer.incrementReadCount();
getBuffer().setEmpty();
}
else wait();
We will implement writer class having a static property : int writerCount, and a private property writerId
- laxmi.udaseen November 18, 2014and will implement reader class having a static property : int readerCount and a private property readerId
Each time a writer/readerId Object is created, the object will be assigned writerId = ++writerCount, and readerId = ++readerCount
Now writeToBuffer() method in writer class checks
if(getBuffer().isEmpty() && !getBuffer().writeLocked && getBuffer().writeCount()%Writer::writerCount == writerId) {
getBuffer().acquireWriteLock();
getBuffer().write(data);
getBuffer.incrementWriteCount();
getBuffer().setFull();
getBuffer.releaseWriteLock();
}
else wait();
For reader, we will do the following:
if(getBuffer().isFull() && getBuffer().readCount()%Reader::readerCount == readerId) {
data = getBuffer().read();
getBuffer.incrementReadCount();
getBuffer().setEmpty();
}
else wait();