Microsoft Interview Question for Software Engineer / Developers






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

http://doc.trolltech.com/qq/qq11-mutex.html

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

This is a nice solution ! Thanks for sharing...

- Ashish Kaila March 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi, what do you mean by upgrade/downgrade of locks?

Thanks!

- Anonymous November 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can you please elaborate the question ? in terms of exactly what they expected you to code.

- anon December 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can you please elaborate the question ? in terms of exactly what they expected you to code.

- anon December 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its about reader writer problem in a library .. it can be solved using mutex/semaphore

- intuidev January 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* pthread implementation of 1st reader/writer problem
 *  based on mutex and conditional variable
*/
typedef struct read_write_lock_s {
   pthread_mutex_t mutex;
   pthread_cond_t cv;
   int read_ctr;
   it  write_ctr;
}read_write_lock;

void rw_lock_init(read_write_lock *rw_lock)
{
  rw_lock->read_ctr = 0;
  rw_lock->write_ctr = 0;
  pthread_cond_init(&rw_lock->cv);
  pthread_mutex_init(&rw_lock->mutex);
}

void rw_lock_acquire_read(read_write_lock *rw_lock)
{
  pthread_mutex_lock(&rw_lock->mutex);
  while(rw_lock->write_ctr != 0) {  /* wait for writer */
    pthread_cond_wait(&rw_lock->cv, &rw_lock->mutex);
  }
  rw_lock->read_ctr++;
  pthread_mutex_unlock(&rw_lock->mutex);
}

void rw_lock_acquire_write(read_write_lock *rw_lock)
{
  pthread_mutex_lock(&rw_lock->mutex);
  while(rw_lock->write_ctr !=0 || rw_lock->read_ctr>0) {
    pthread_cond_wait(&rw_lock->cv, &rw_lock->mutex);
    rw_lock->write_ctr=1;
  }
  pthread_mutex_unlock(&rw_lock->mutex);
}

void rw_lock_release_read(read_write_lock *rw_lock)
{
  pthread_mutex_lock(&rw_lock->mutex);
  if(--rw_lock->read_ctr == 0) { /* no one is reading now; writer please hurry! */
    pthread_cond_broadcast(&rw_lock->cv);
  }
  pthread_mutex_unlock(&rw_lock->mutex);
}

void rw_lock_release_write(read_write_lock *rw_lock)
{
  pthread_mutex_lock(&rw_lock->mutex);
  rw_lock->write_ctr = 0;
  pthread_cond_broadcast(&rw_lock->cv); /* writer is done; readers in... */
  pthread_mutex_unlock(&rw_lock->mutex);
}

- jzhu May 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The idea is correct but the implementation has an issue:

pthread_mutex_lock(&rw_lock->mutex);
  while(rw_lock->write_ctr !=0 || rw_lock->read_ctr>;0) {
    pthread_cond_wait(&rw_lock->cv, &rw_lock->mutex);
    rw_lock->write_ctr=1;
  }

once u set rw_lock->write_ctr=1 u will never get of the while loop.

- kykydyk March 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* pthread implementation of 1st reader/writer problem
 *  based on mutex and conditional variable
*/
typedef struct read_write_lock_s {
   pthread_mutex_t mutex;
   pthread_cond_t cv;
   int read_ctr;
   it  write_ctr;
}read_write_lock;

void rw_lock_init(read_write_lock *rw_lock)
{
  rw_lock->read_ctr = 0;
  rw_lock->write_ctr = 0;
  pthread_cond_init(&rw_lock->cv);
  pthread_mutex_init(&rw_lock->mutex);
}

void rw_lock_acquire_read(read_write_lock *rw_lock)
{
  pthread_mutex_lock(&rw_lock->mutex);
  while(rw_lock->write_ctr != 0) {  /* wait for writer */
    pthread_cond_wait(&rw_lock->cv, &rw_lock->mutex);
  }
  rw_lock->read_ctr++;
  pthread_mutex_unlock(&rw_lock->mutex);
}

void rw_lock_acquire_write(read_write_lock *rw_lock)
{
  pthread_mutex_lock(&rw_lock->mutex);
  while(rw_lock->write_ctr !=0 || rw_lock->read_ctr>0) {
    pthread_cond_wait(&rw_lock->cv, &rw_lock->mutex);
    rw_lock->write_ctr=1;
  }
  pthread_mutex_unlock(&rw_lock->mutex);
}

void rw_lock_release_read(read_write_lock *rw_lock)
{
  pthread_mutex_lock(&rw_lock->mutex);
  if(--rw_lock->read_ctr == 0) { /* no one is reading now; writer please hurry! */
    pthread_cond_broadcast(&rw_lock->cv);
  }
  pthread_mutex_unlock(&rw_lock->mutex);
}

void rw_lock_release_write(read_write_lock *rw_lock)
{
  pthread_mutex_lock(&rw_lock->mutex);
  rw_lock->write_ctr = 0;
  pthread_cond_broadcast(&rw_lock->cv); /* writer is done; readers in... */
  pthread_mutex_unlock(&rw_lock->mutex);
}

- jzhu May 19, 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