COMP3511 Homework 1 Solved

35.00 $

Category:

Description

5/5 - (1 vote)

 

Please read the following instructions carefully before answering the questions:
• You should finish the homework assignment individually.
• This homework assignment contains three parts:
1) Multiple choices 2) Short Answer 3) Simple C programs on fork()
• Homework Submission: submit to Homework #1 on Canvas.

1. (30 points) Multiple choices

Write your answers in the boxes below:

MC1 MC2 MC3 MC4 MC5 MC6 MC7 MC8 MC9 MC10

B 1) Which of the following statements is correct?
A) Operating systems include compilers for × higher-level languages.
B) Mobile operating systems often include middleware to provide additional services.
C) I/O devices are essential components of the CPU in Von Neumann Architecture.× D) CPU is heavily involved in ×the entire process of DMA data transfer to improve transmission efficiency.

D 2) Which of the following statements is correct?
A) Main memory is categorized into non-volatile secondary storage in the storage system, which the CPU can directly accessx .
B) Larger cache hit ratio usually leads to a longer average access time.×
C) In a symmetric × multiprocessing system, the master processor often assigns specific tasks to the slave processors, and the master handles I/O.
D) Different operating systems can run concurrently on a single physical machine with virtual machines.
D 3) When the block method is used to pass the parameters of system calls to the operating system, ________.
A) the detailed contents of all parameters are stored in registers×
B) parameters are pushed onto a stack by the program×
C) the number of registers limits the number and length of parameters×
D) the address of the block where parameters are stored is passed via the register

c 4) Before running a program on a CPU core, the executable file is brought into memory by ________.

A) assembler
B) linker → exeutable file
C) loader
D) compiler -→ objeefile

c 5) Which of the following statements about operating system structure is correct? hybrid
A) Windows XP is designed with the layered approach.t
B) In the loadable kernel module approach, each layer invokes operations on lower-level layers, simplifying the construction and debugging.θ
C) It is easy to add new services to the microkernel-based operating system without modifying the kernel, greatly improving the performance of microkernels.
D) Microkernel provides communication between the client program and the various λ services running in user space through message passing.

A 6dashed box) Considering the memory layout of the following C program, the pointer ) is stored in _____. *x (in the red
#include<stdio.h> int *x;
int main(int argc, char *argv[])
{ x = (int *)malloc(sizeof(int)*5); free(x); return 0;
}

A) uninitialized data section
B) initialized data section
C) stack
D) heap
7) After the completion of one I/O operation requested by the process, the process state will be changed _______.

A) from waiting to ready
B) from running to ready
C) from running to waiting
D) from ready to running

8) In which of the following scenarios does the system switch from user mode to kernel mode?
I. Divide an integer by zero.
II. Access the data in a file with the read() system call.

A) Only I.
B) Only II.
C) Both I and II.
D) Neither of the two scenarios.

9) Cooperating processes cannot use ______ to communicate.

A) shared memory
B) message passing
C) global variables in the C program
D) pipes

10) Which of the following statements about the parent process and child process is correct?

A) In UNIX, fork() creates a new process, which duplicates the same pid of the parent process.
B) Parent process and children processes have different process control blocks.
C) Parent process can create children processes but cannot terminate its children process.
D) If the parent process is terminated without invoking wait(), the child process becomes a zombie process.

2. (30 points) Short answer

(1) (6 points) What are the main goals of the operating system? Please answer from the user view and the system view, respectively.

,

(2) (6 points) Please illustrate two advantages of using APIs rather than invoking system calls directly.

(3) (6 points) Please illustrate the advantages of the multiprocessor systems (4 points). What is the possible reason for the problem that, compared to a computer with a single processor, the speed-up ratio of a computer with eight identical processors is less than eight (2 points)?
increase thronghput that gives more Computig Capability ,
The multipocessor system auld
An diehasbenefitshaveofeononyofscalewhichontd otherdevies
Such as I 10devie
more aer , itouldEncnelsethereliabilitythaehasgracefulgradation or fault toleraud ,

Jince multiprolesshastheproblonoverheud suchasoutention forShaned resoures

的 cladns bus and mewy, thisIssuewill lead to a phenoulnon tlhctthesPeedup

ratioof Npossorthanesss

(4) (6 points) Please summarize the function of the process scheduler in brief (2 points). A common representation of process scheduling is a queueing diagram, including two different types of scheduling queues of processes. Please describe the state of the processes placed in these two types of queues, respectively (4 points).

(5) (6 points) Please illustrate the main advantage of dual-mode operation (i.e., why should the system distinguish between user mode and kernel mode) (2 points) and describe the workflow of dual-mode operation after a user application requests a service via a system call (4 points).
.

3. (40 points) Simple C programs on fork()
For all the C programs below, you can assume that necessary header files are included and fork() always creates a new process successfully.

1) (10 points) Consider the following code segments:

int main(){ pid_t pid, id;
for (int i = 0; i < 5; i++){ 品 id = fork();
if(id == 0) break; child
} 6b Fabe
pid = getpid(); child rocess if (pid == id) printf(“true “);
else printf(“false√ “); p Δ
fflush(stdout); //force the output to be displayed on the console before the program exits return 0;
} T
How many “true” and “false” will be printed, respectively (4 points)? Please elaborate (6 points).
falserne ; 0 time , : times6,
From the for loop , thePaventprocess will execute 5 tine forkC ) systen cals
Once the child pooss escreated , itwilldirectlybreakthechildplocess sincetheforky s 4 sten call hill return Zero if t ‘ s dnchildpoess,
And there Us no process that have pid = 0 , hence the fie child pocesses will outht
fiul tin nes false ,
rhen after the Parent Poless finishedtheexention of for 100 p, it will also

give the false output because the forinwill reurnthe child process ‘s pid
which is differetfromthepaventpocess’s pid .

2) (10 points) Consider the following code segments:
int main(){ if (fork() || fork()){ printf(“true “);
}
printf(“process “);
fflush(stdout);
return 0;
}

How many “true” will be printed (3 points)? How many “process” will be printed (2 points)? Please elaborate (5 points). (Hint: In C language, if State1 is true, “State1 || State2” will directly return true without checking State2.)

Aud G hill rearsively call the forlhionwithiteration = 1 , thesinilarPocess hill be done liketheaboe elaboration . the child pocess will first Priie the
aud valal heuce It will ortPat 1 , 2 . the valuce is eaual values of iteration ,
to 2 because theorijualvalne of4 gotdedcted tbiynfortweo s lastly , the Parere puiess will Priht 1 , 3 . Even thosh e will rearsively call the fua ( tion ,theItearationalradyreached 2thathillretunthefuuction ,

4) (10 points) In this question, we introduce the macro WEXITSTATUS(int status) defined in <sys/wait.h> header. This macro evaluates to the least significant 8 bits of the exit status value that the child process passed to exit(). You can take the following code as an example.
int main(){ int status; pid_t pid = fork(); // Create a child process.
if(pid == 0) exit(50); // The child process terminates and passes 50 as the exit status value.
wait(&status); // The parent process waits and receives the exit status value of the child process.
int value = WEXITSTATUS(status); // value = 50 return 0;
}

In this question, we want to use fork() to implement a dot product between two vectors with multiple processes. For example, given two vectors ! = [3, 2, 4] and ) = [1, 5, 2], the dot product between ! and ) is ! ⋅ ) = 3 ∗ 1 + 2 ∗ 5 + 4 ∗ 2 = 21. The input of our program includes two vectors. The output is the dot product between them.
The input data satisfies the following conditions.
1. The elements of the vectors are all non-negative integers.
2. The number of elements in each vector is fixed to 5.
3. The result of the dot product is less than 256.
Please fill in the five blanks to complete the following program. You can write at most 20 characters in each blank (10 points, 2 points for each blank).
10
#define size 5 int main() {
int i, status, product, value;
int a[size], b[size];
pid_t pid;
for (i = 0; i < size; i++){
scanf(“%d”, &a[i]); // Input vector a
}
for (i = 0; i < size; i++){
scanf(“%d”, &b[i]); // Input vector b
}
for (i = 0; i < size – 1; i++){
pid = fork(); // Create processes
if(!pid){
Blank 1 brenk;
}
} aci] * b τi]
product = Blank 2 ; // Calculate the product if(!pid) exit(product); for (i = 0; i < Blank 3 ; i++){ Blank 4 ; value = WEXITSTATUS(status);
product = Blank 5 ; // Calculate the dot product
}
printf(“The result of the dot product is %d. “, product); return 0;
}
Sample Input:
3 4 3 1 5
2 1 4 5 2
Sample Output:
The result of the dot product is 37
Write your answer in the following blanks.
Blank 1: break
Blank 2: a[i ] * bi ⻔
Blank 3: size – 1
Blank 4: wait ( &Status )
Blank 5: produle t ralue
11

  • HW1-giaqqi.zip