Google Interview Question
1,230 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Senior Software Engineer at Google:
Helpful Question?
Yes |
No
Inappropriate?
2 of 2 people found this helpful
by Interview Candidate:
//input: A, B, C, D, E, F, G, ...,
static Semaphore BufferWrite;
// Data Structures
typedef my_spin_lock {
uint64 next_ticket;
uint64 cur_ticket;
}my_spin_lock_t;
my_spin_lock_t my_lock;
//Initialize
void initialize(my_spin_lock_t *my_lock)
{
my_lock->cur_ticket = 1;
my_lock->next_ticket = 0;
}
//spin lock
void spin_lock(....)
{
uint64 temp;
temp = atomic_increment(&my_lock->next_ticket);
while (temp != my_lock->cur_ticket) spin_waiting();
}
CRITICAL SECTION
void spin_unlock(....)
{
atomic_increment(&my_lock->cur_ticket);
}
//*****************************************//
void spin_lock(...){
while(&my_lock->next_ticket){
while(1){
wait(BufferWrite);
//Critical section
}
signal(BufferWrite);
}
}