Csi 3310 Midterm Samples Solution
Csi 3310 Midterm Samples Solution
Professor:
Stefan Dobrev
Date:
Hour:
Room:
February 26 2007
14:30-16:00 (1.5 hours)
Montpetit 202
Midterm Exam
Solution
Part 1
Part 2
Part 3
Total
Multiple Choice
Short Answers
Problem Solving
8 points
10 points
10 points
25+3 points
The exam is worth 28 points, 3 of them are essentially bonus (25 points represent
25% of the total mark).
2. d
3. b,e
4. c
5. d
6. a, d 7. a
8. c
Question 5 :
No, it is not valid. It does note satisfy the progress requirement, since each process must wait for
the other process to pas through its CS, even if the other process is in its remainder section.
Question 6 :
wait(S):
S.value --;
if S.value < 0
{ // Semaphore is busy if value < 0
Add the process/thread to S.L;
block() // the process/thread is placed in the wait state
}
Wait
Ready
P1(12)
P0(3)
P1(7)
P0(12)
P0(7)
P2(12), P0(5)
P1(12), P2(10), P0(3)
P1(10), P2(8), P0(1)
P1(9), P2(7)
P3(12), P1(5), P2(3)
P3(9), P1(2)
P1(2)
P0(9)
P2(4)
CPU
P0(8), 5
P0(6), 3
P1(12), 5
P0(3), 5
P1(7), 5
P2(2), 5
P1(2), 5
P3(6), 5
P3(5), 4
P0(9), 5
P0(6), 2
Question 2:
int main(int argc, char *argv[])
{
int i,j;
char *pgrm1;
char *pgrm2;
int pipe1to2[2];
int pipe2to1[2];
int pid;
if(argc != 3)
{
printf("Usage: stdout2stdin <pgrm1> <pgrm2> \n");
exit(1);
}
/* get programs */
pgrm1 = argv[1];
pgrm2 = argv[2];
/* create the pipes */
pipe(pipe1to2);
pipe(pipe2to1);
/* create process 1 */
pid = fork();
if(pid == 0)
{
dup2(pipe1to2[0], 0);
dup2(pipe2to1[1], 1);
close(pipe1to2[0]);
close(pipe1to2[1]);
close(pipe2to1[0]);
close(pipe2to1[1]);
execlp(pgrm1, pgrm1, NULL);
}
/* create process 2 */
pid = fork();
if(pid == 0)
{
dup2(pipe2to1[0], 0);
dup2(pipe1to2[1], 1);
close(pipe1to2[0]);
close(pipe1to2[1]);
close(pipe2to1[0]);
close(pipe2to1[1]);
execlp(pgrm2, pgrm2, NULL);
}
}