BEST代写-线上编程学术专家

Best代写-最专业靠谱代写IT | CS | 留学生作业 | 编程代写Java | Python |C/C++ | PHP | Matlab | Assignment Project Homework代写

C语言代写 | CSC 112: Midterm (Sample)

C语言代写 | CSC 112: Midterm (Sample)

这个作业是用C语言完成一系列编程习题

CSC 112: Midterm (Sample)

Problem 1: A program’s main function is as follows:
int main(int argc, char *argv[]) {
char *str = argv[1];
while (1)
printf(“%s”, str);
return 0;
}
Two processes, both running instances of this program, are currently
running (you can assume nothing else of relevance is, except perhaps the
shell itself). The programs were invoked as follows, assuming a “parallel
command”:
Shell> main a && main b
Below are possible (or impossible?) screen captures of some of the output
from the beginning of the run of the programs. Which of the following are
possible?
To answer: Fill in A for possible, B for not possible.
1. abababab …
2. aaaaaaaa …
3. bbbbbbbb …
4. aaaabbbb …
5. bbbbaaaa …
Problem 2: Here is source code for another program, called increment.c:
int value = 0;
int main(int argc, char *argv[]) {
while (1) {
printf(“%d”, value);
value++;
}
return 0;
}
3
While increment.c is running, another program, reset.c, is run once as a
separate process. Here is the source code of reset.c:
int value;
int main(int argc, char *argv[]) {
value = 0;
return 0;
}
Which of the following are possible outputs of the increment process?
To answer: Fill in A for possible, B for not possible.
6. 012345678 …
7. 012301234 …
8. 012345670123 …
9. 01234567891011 …
10. 123456789 …
Problem 3:Which of the following are more like policies, and which are
more like mechanisms?
To answer: Fill in A for policy, B for mechanism.
11. The timer interrupt
12. How long a time quantum should be
13. Saving the register state of a process
14. Continuing to run the current process when a disk I/O interrupt occurs
15. The address space
Problem 4: A concurrent program (with multiple threads) looks like this:
volatile int counter = 1000;
void *worker(void *arg) {
Counter–;
return NULL;
}
int main(int argc, char *argv[]) {
pthread_t p1, p2;
pthread_create(&p1, NULL, worker, NULL);
4
pthread_create(&p2, NULL, worker, NULL);
pthread_join(p1, NULL);
pthread_join(p2, NULL);
printf(“%d\n”, counter); return 0;
}
Assuming pthread create() and pthread join() all work as expected (i.e.,
they don’t return an error), which outputs are possible?
To answer: Fill in A for possible, B for not possible.
16. 0
17. 1000
18. 999
19. 998
20. 1002
Problem 5: Processes exist in a number of different states. We’ve focused
upon a few (Running, Ready, and Blocked) but real systems have slightly
more. For example, xv6 also has an Embryo state (used when the process is
being created), and a Zombie state (used when the process has exited but
its parent hasn’t yet called wait() on it).
Assuming you start observing the states of a given process at some point in
time (not necessarily from its creation, but perhaps including that), which
process states could you possibly observe?
To answer: Fill in A for possible, B for not possible.
Note: once you start observing the process, you will see ALL states it is in,
until you stop sampling.
21. Running, Running, Running, Ready, Running, Running, Running, Ready
22. Embryo, Ready, Ready, Ready, Ready, Ready
23. Running, Running, Blocked, Blocked, Blocked, Running
24. Running, Running, Blocked, Blocked, Blocked, Ready, Running
25. Embryo, Running, Blocked, Running, Zombie, Running

bestdaixie