Kakšno vrednost vrne izraz 3 + 4 * 5 / 8 * 2?
Kakšno vrednost vrne naslednji klic funkcije scanf, če se na standardnem vhodu pojavi niz podatkov "12 7 w 8 3.7 \n"?
int a, b, c, x;x = scanf("%d%d%d", &a, &b, &c);
In our working producer and consumer solutions with a single buffer slot using condition variables, we used two condition variables. Why was the second condition variable necessary?
When using a semaphore to ensure the child thread runs before the parent executes further as in the code snippet below, what value should the semaphore be initialized too (i.e., what should X be)?
sem_t s;void *child(void *arg) {
printf("child");
sem_post(&s); // signal here: child is done
return NULL;
}
int main(int argc, char *argv[]) {
sem_init(&s, 0, X); // what should X be?
printf("parent: begin");
pthread_t c;
pthread_create(c, NULL, child, NULL);
sem_wait(&s); // wait here for child
printf("parent: end");
return 0;
}
Recall the code below from class where we used a condition variable to make sure the child runs before the parent prints. Why did we include the int variable done if signalling and waiting is done automatically with condition variables?
int done = 0;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t c = PTHREAD_COND_INITIALIZER;
void thr_exit() {
Pthread_mutex_lock(&m);
done = 1;
Pthread_cond_signal(&c);
Pthread_mutex_unlock(&m);
}
void *child(void *arg) {
printf("child");
thr_exit();
return NULL;
}
void thr_join() {
Pthread_mutex_lock(&m);
while (done == 0)
Pthread_cond_wait(&c, &m);
Pthread_mutex_unlock(&m);
}
int main(int argc, char *argv[]) {
printf("parent: begin");
pthread_t p;
Pthread_create(&p, NULL, child, NULL);
thr_join();
printf("parent: end");
return 0;
}
Which of the following is true about the advantages and disadvantages of using pthread_cond_broadcast() over pthread_cond_signal()?
When the value of a semaphore is negative what does this number indicate?
When using a semaphore as a lock as in the code snippet below, what value should the semaphore be initialized too (i.e., what should X be)?
1 sem_t m;
2 sem_init(&m, 0, X);
3
4 sem_wait(&m);
5 //critical section here
6 sem_post(&m);