School of Computer Science
Main Summer Examinations 2021
代写Systems Programming There is a serious problemwith this Explain what is wrong, why it is a problem, and how it can be fixed.
Systems Programming in C and C++
Note
Answer ALL questions. Each question will be marked out of 20. The paper will be marked out of 60, which will be rescaled to a mark out of 100.
Question 1
The question is about pointers and memory management in C.
(a)Willthere be any memory leakage in the following program? Explain your answer.
1 int main() 2{ 3 int *A = (int *) malloc(sizeof(int)); 4 scanf(”%d”, A); 5 int *B; 6 B = A; 7 free(B); 8 return 0; 9}
[6 marks]
(b)A programmer has written the following function with the aim to return a pointer to an array of 10 random integers (int) to a caller function. There is a serious problemwith this Explain what is wrong, why it is a problem, and how it can be fixed. Use this to write a correct version of the function without changing the function-signature. Assume that the caller function of randomArray is responsible for freeing any memory occupied by the array.
1 int* randomArray(void) 2 – 3 int array[10], i; 4 for (i = 0; i ¡ 10; i++) 5array[i] =rand(); 6return&array[0]; 7 }
(c)Consider the following two C functions sum2Darray1 and sum2Darray2. Both of themcompute the sum of all the elements of an input 2-dimensional Which one of them will be able to exploit memory hierarchy and thus achieve faster com- putation time? Explain your answer. 代写Systems Programming
1 int sum2Darray1(int a[N][M]) 2 { 3 int i, j, sum=0; 4 for(i=0;i<M;i++) 5 for(j=0;j<N;j++) 6sum =sum +a[j][i]; 7returnsum; 8}
1 int sum2Darray2(int a[N][M]) 2 { 3 int i, j, sum=0; 4 for(i=0;i<N;i++) 5 for(j=0;j<M;j++) 6sum =sum +a[i][j]; 7returnsum; 8 }
[7 marks]
Question 2 代写Systems Programming
The question is about concurrent programming.
(a)Consider a concurrent system with three processes P1, P2 and P3.
Provide a (semaphore based) solution to synchronize P1, P2 and P3 such that the following constraints on execution order is satisfied:
- StatementB before Statement A,
- Statement A before StatementC
Provide a possible (deadlock free) trace of your solution. 代写Systems Programming
(b)In the following program, the main thread creates four peer threads and passes a pointer to the loop variable to each one. Each peer thread prints a message containing the loop variable.
1#include¡stdio.h> 2#include¡pthread.h> 3 4void *foo(void*arg){ 5int *myid = (int *)arg; 6printf(”Hello from thread % d\n”,*myid); 7returnNULL; 8} 9 10intmain(){ 11pthread˙ttid[4]; 12 inti; 13 14 for(i=0; i<4; i++) 15 pthread˙create(&tid[i], NULL, foo, &i); 16 17 for(i=0; i<4; i++) 18 pthread˙join(tid[i], NULL); 19 20 return 0; 21 }
We might expect that the program will print all the four values of i, but when the program is executed, we see the following incorrect result containing repetitions:
Hello from thread 1
Hello from thread 3
Hello from thread 3
Hello from thread 3
What causes this behavior? Explain your answer. [7 marks]
(c)[Continuation of Question 2b above] Rectify only the main() function such thatthe concurrent peer threads print unique values, e., the first thread prints 0, the second thread prints 1, the third thread prints 2 and the final thread prints 3. We don’t expect the threads will print ”in order” (we expect that they just print the correct value per thread). Explain your answer. [6 marks]
Question 3 代写Systems Programming
This question is about Processes, Memory management & Scheduling.
(a)TheC program shown below is compiled and run on a UNIX Predict all possible outputs that this program will print to the console and explain your answer.
1#include<sys/types.h> 2#include<sys/wait.h> 3#include<stdio.h> 4 #include<unistd.h> 5int main(){ 6int x =1; 7pid˙t pid = fork(); 8 if (pid == 0)– 9 x = x * 2; 10}else if (pid > 0)– wait(NULL); 12 x = 3; 13 } 14 "%d\n”,x); 15 }
[4 marks]
(b)Schedulethe processes (given in Table 1) using round robin scheduling with quantum 10. Also, calculate the totalturnaroundtime. [5 marks] 代写Systems Programming
Table 1: Process table
(c)Whatare the physical memory addresses for each of the following logical addresses for the segment table (Table 2)? Note any that are The logical addresses are:
(i) 3 , 15
(ii) 0 , 512
(iii)1 , 4096
(iv) 0 , 1
[4 marks]
Table 2: Segment table
Segment | Base Address | Length |
0 | 16384 | 400 |
1 | 4096 | 4096 |
2 | 32768 | 810 |
3 | 20480 | 1024 |
(d)Considera system with four frames of memory, and the following sequence of page accesses: 0,1,2,3,4,2,3,0,1,4,2. When do page faults occur using FIFO and LRU replacement algorithms? Briefly justify your [4 marks] 代写Systems Programming
(e)Consider a demand-paged computer system which was recently measured to deter- minethe utilisation of CPU and the paging disk to decide the degree of multipro- gramming. The results are shown in the figure Explain what is happening in each scenario and what actions an operating system can take.
[3 marks]
更多代写:Canvas代做 多邻国代考 assignment范文 Book Report 代写 article代写summary 报告代写