PM
CMPE320 Final Exam questions and answers with complete
solutions verified graded a++ latest update 2025/2026
Terms in this set (53)
Select the parts that are shared among threads
(in the same process)
1, 3 1. code segment
2. register values
3. heap data
4. call stack
True T/F In the simulation homework of
chapter 26, program loop.s will never
have data race/race condition.
int *a;
*a = 3;
TestAndSet(a, 2);
3, 2
What is the return value
of this TestAndSet call?
What is the value of *a
1/41
,7/7/25, 5:34
PM
after this TestAndSet call?
int *a;
*a = 3;
CompareAndSwap(a, 2, 1);
3, 3
What is the return value of this
CompareAndSwap?
What is the value of *a after this
CompareAndSwap operation?
2/41
,7/7/25, 5:34
PM
Insert the C statements related to mutex
to some of the marked locations so the
program below can run properly.
#include <stdio.h>#include
<assert.h>#include <pthread.h>#include
"common.h"#include
"common_threads.h"#include <math.h>
static volatile int counter = 0;
// Location A
void
mythread(void
arg) {
printf("%s:
begin\n", (char
*) arg); int i;
// Location B
for (i = 0; i < 1e7; i++) {
A
// Location
C counter
= counter
3/41
, 7/7/25, 5:34
PM
+ 1;
// Location D
} printf("%s: done\
n", (char *) arg);
return NULL;}
int main(int argc, char *argv[]) {
// Location E
pthread_t p1, p2; printf("main: begin
(counter = %d)\n", counter);
Pthread_create(&p1, NULL,
mythread, "A");
Pthread_create(&p2, NULL, mythread, "B"); // join
waits for the threads to finish
// Location F
Pthread_join(p1, NULL); Pthread_join(p2, NULL);
// Location G
printf("main: done with both (counter = %d)\n",
counter); return 0;}
Where does pthread_mutex_t lock; go?
4/41