Skip to content

Latest commit

 

History

History
83 lines (68 loc) · 3.78 KB

7_Implementing philosopher_get_access_both_spoons( ).md

File metadata and controls

83 lines (68 loc) · 3.78 KB

🍽️ Dining Philosophers Problem: Accessing Both Spoons

📜 Flowchart Summary:

  1. Check if the left spoon is available.
    • If not, the philosopher waits for the left spoon's condition variable.
    • If yes, the philosopher's thread updates the state to show the left spoon is taken.
  2. After acquiring the left spoon, check if the right spoon is available.
    • If not available, release the left spoon.
    • If available, the philosopher has access to both spoons.
  3. The function returns true if the philosopher gets both spoons. Otherwise, it returns false.

🛠️ Implementation:

bool philosopher_get_access_both_spoons(phil_t *phil) {
	spoon_t *left_spoon  = phil_get_left_spoon(phil);
	spoon_t *right_spoon = phil_get_right_spoon(phil);

	// Try to acquire left spoon
	pthread_mutex_lock(&left_spoon->mutex);
	// Wait if left spoon is used by another philosopher
	while(left_spoon->is_used && left_spoon->phil != phil) {
		pthread_cond_wait(&left_spoon->cv, &left_spoon->mutex);
	}
	left_spoon->is_used = true;
	left_spoon->phil = phil;
	pthread_mutex_unlock(&left_spoon->mutex);

	// Try to acquire right spoon
	pthread_mutex_lock(&right_spoon->mutex);
	if (right_spoon->is_used == false) {
		right_spoon->is_used = true;
		right_spoon->phil = phil;
		pthread_mutex_unlock(&right_spoon->mutex);
		return true;
	}
	// If failed to get right spoon, release the left spoon
	else {
		if (right_spoon->phil != phil) {
			pthread_mutex_lock(&left_spoon->mutex);
			left_spoon->is_used = false;
			left_spoon->phil = NULL;
			pthread_mutex_unlock(&left_spoon->mutex);
			pthread_mutex_unlock(&right_spoon->mutex);
			return false;
		}
		else {
			pthread_mutex_unlock(&right_spoon->mutex);
			return true;
		}
	}
}
  • Start by checking the left spoon's availability.
  • If available, lock its mutex and update its state to reflect its taken.
  • If not available, the philosopher thread waits using a condition variable.
  • After acquiring the left spoon, check the right spoon.
  • If the right spoon is unavailable, release the left spoon.
  • If it's available, lock its mutex and update its state.
  • The function should return true if both spoons are taken, otherwise false.

📌 Note: Mutex ensures that no other thread changes the state of a resource while another thread is checking it.

❓ Interview Questions:

  1. How does the philosopher try to access both spoons based on the flowchart?
    🅰️ The philosopher first tries to access the left spoon. If successful, they proceed to try for the right one. If either spoon is not available when required, they will release any acquired spoon and wait.

  2. Why is a mutex used in this implementation?
    🅰️ Mutex ensures that while a thread is inspecting or updating the state of a resource, no other thread can modify that resource. It ensures safe access in a multi-threaded environment.

  3. What happens if a philosopher acquires the left spoon but not the right spoon?
    🅰️ If a philosopher successfully acquires the left spoon but fails to acquire the right one, they will release the left spoon and wait.

  4. Is there a scenario where a philosopher might already have the right spoon? If so, what does the code do in that case?
    🅰️ There's an edge case handled where if the philosopher already has the right spoon, the function simply unlocks its mutex and returns true. However, this scenario is unlikely but was included for completeness.

  5. What's the significance of the condition variables in this problem?
    🅰️ Condition variables allow threads to wait until a particular condition is met. In this scenario, it's used to make a philosopher wait if a required spoon is not available.

I hope this helps with your revision for interviews! If you need more information or have other questions, let me know.