Riverbed Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
2
of 2 vote

class Semaphore()
{
	private:
		innerValue;
		Mutex mutex;

	public:
		Semaphore(int _v)
		{
			innerValue=_v;
		};

		void post()
		{
			mutex.lock();
			++innerValue;
			mutex.unlock();
		};

		void wait()
		{
			mutex.lock();
			while(innerValue<=0)
			{
				mutex.unlock;
				sleep();
				mutex.lock();
			}

			mutex.unlock();
		};
}

- Anonymous February 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

in wait method, you must do "--innerValue;"

- siva.sai.2020 November 06, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A semaphore can be used for managing the use of a limited resource. Its initialized to the count of available resources. Now a mutex is a binary semaphore that takes only 2 values. To implement a semaphore using a mutex have an integer used as a semaphore initialized to some count. You increment and decrement this semaphore int every time resource is used and consumed. you have to make sure that each update on semaphore is gaurded by the mutex.

any thoughts ?

- Anonymous April 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a mutex and a count variable encapsulated in a class. Implement a semLock function which inturn checks for the count and returns a bool status whether to lock or not. If it returns true, then call pthread_mutex_lock, else just reduce the count and let the processing continue. When the call exits the critical section, increment the count.

- Kiran April 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Also as the previous answer, the update on semaphore needs to be atomic. This calls for use of another mutex.

- Anonymous April 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Does this make sense?

lock_t mutex;   
sem_t sem;
sem_init(sem, 5);  //lets say there are 5 instances of this resource available

//to access resource:
lock(mutex);
while(sem == 0)
  wait();
//take resource
sem--;
unlock(mutex);

//to release resource
lock(mutex);
sem++;
unlock(mutex);
signal();   //wake up any thread waiting on the semaphore

- Anonymous May 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

When taking the the resource:
if(sem == 0)
{...

use conditional variables so that you're not holding on to the mutex. Do a conditional wait to release the mutex, wait till the condition sem>0, and then get the lock and proceed.

- Anonymous May 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Sem_Mutex
{
public:
Sem_Mutex(maxReaders)
{
this.maxReaders = maxReaders
}

void lockRead() { semaphore++; }
void unlockRead() { semaphore--; }
void lockWrite() {
lock(mutex);
for (int i = 0; i < maxReaders(); ++i)
semaphore++;
unlock(mutex);
}
void unlockWrite() { semaphore = 0; }
int maxReaders() const { return semaphore; }


private:
int semaphore;
int maxReaders;
lock_t mutex;
};

- Sal June 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Sal: Shouldn't LockRead also be protected by a mutex? what if 2nd thread tries a lockRead() when the first thread is doin a lockWrite?

- CodeBoyS August 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* If conditional variable is available to used together with mutex 
 */
typedef semaphore_s {
    pthread_mutex_t mx;
    pthread_cond_t   cv;
    int ctr;
} sem_t;

void sem_init(sem_t *sem, int count) {
   pthread_mutex_init(&sem->mx);
   pthread_cond_init(&sem->cv);
   sem->ctr=count;
}

void sem_wait(sem_t *sem) {
    pthread_mutex_lock(&sem->mx);
    while(sem->ctr==0) pthread_cond_wait(&sem->cv, &sem->mx);
    sem->ctr--;
    pthread_mutex_unlock(&sem->mx);
}

void sem_post(sem_t *sem) {
   pthread_mutex_lock(&sem->mx);
   if(sem->ctr++==0) pthread_cond_broadcast(&sem->cv);
   pthread_mutex_unlock(&sem->mx);
}

- jzhu May 20, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More