CS 537 - Quiz #4 1. A spin lock acquire() can be implemented with the atomic exchange instruction: while (xchg(&lock, 1) == 1) ; // spin Recall that xchg() returns the old value at the address while atomically setting it to a new value. Here, we propose a new lock implementation: while (1) { while (lock > 0) ; // spin if (xchg(&lock, 1) == 0) return; } What is the difference with this new lock? Does it work? What is its new behavior? 2. Observe the following multi-thread safe list insertion code: typedef struct __node_t { int key; struct __node_t *next; } node_t; mutex_t m = MUTEX_INITIALIZER; // initialize lock node_t *head = NULL; int List_Insert(int key) { mutex_lock(&m); node_t *n = malloc(sizeof(node_t)); if (n == NULL) { return -1; } // failed to insert n->key = key; n->next = head; head = n; // insert at head mutex_unlock(&m); return 0; // success! } There is a problem here that can be solved by inserting one line of code. What is that line of code, and where should we put it?