CS FundamentalsModerateGATE CS · Amazon/Microsoft SDE MCQ
A program creates threads with `for (i = 0; i < 4; i++) pthread_create(&t[i], NULL, work, &i);` and each thread reads *arg as its index. What is the problem?
- A. pthread_create copies the value of i, so every thread just sees 0
- B. All threads get the same &i, so indices can repeat or even be 4
- C. The threads cannot start until the loop finishes, so all see 4 exactly
- D. Passing a pointer to a stack variable is rejected by the compiler
Correct answer: B. All threads get the same &i, so indices can repeat or even be 4
Every thread gets the same address, and the loop keeps changing i, so what a thread reads depends on timing: duplicates and the value 4 are possible. Pass a separate copy (an array element or heap value) per thread.
More Operating Systems questions
- In a directory, ln data h and ln -s data s are run, and then mv data data2 renames the original within the same file system. What happens…
- What does it mean that the read() system call on a terminal is 'blocking', and what state does the process enter while no input is…
- Which statement about thrashing and the degree of multiprogramming is correct?
- Two threads acquire mutexes: T1 locks A then B; T2 locks B then A. Which change prevents deadlock while still letting both threads hold…
- A system uses demand paging with a 4 KB page, and a process allocates a fresh 1 MB zeroed buffer with mmap but touches only its first 10…
- Which statement about a system call compared with an ordinary function call is correct?