Chapter 3 Homework

docx

School

Jackson State University *

*We aren’t endorsed by this school

Course

OPERATING

Subject

Information Systems

Date

Feb 20, 2024

Type

docx

Pages

3

Uploaded by idrisatajuddin

Report
1. Explain the role of the init (or systemd) process on UNIX and Linux systems in regard to process termination When there is a process termination, the process moves to zombie state waiting for the parent to invoke a call to wait(); in this state, the process id and the entry in the process table are released. Once the parent process terminates, the init process becomes the new parent of this zombie. It does the task which were supposed to be done by the parent, that is releasing the pid and entry in the process table of the zombie process. 2. Including the initial parent process, how many processes are created by the program shown in Figure 3.32? #include <stdio.h> #include <unistd.h> int main() { int i; for(i=0; i < 4; i++) fork(); return 0; } Solution Number of processes created =2^4=16 3. Using the program shown in Figure 3.35, explain what the output will be at lines X and Y #include <sys/types.h> #include <stdio.h> #include <unistd.h> #define SIZE 5 int nums[SIZE] = {0,1,2,3,4}; int main() { int i; pid t pid; pid = fork(); if (pid == 0) { for (i = 0; i < SIZE; i++) { nums[i] *= -i; printf("CHILD: %d ",nums[i]); /   LINE X   / } } else if (pid > 0) {
wait(NULL); for (i = 0; i < SIZE; i++) printf("PARENT: %d ",nums[i]); /   LINE Y   / } return 0; } Solution For this program, the child is a copy of the parent; so, whenever there is change made by the child, it will occur in its copy of the data and won’t be reflected in the parent. Therefore, the outcome will be as follows: Line X Child: 0 Child: -1 Child: -4 Child: -9 Child: -16 Line Y Parent: 0 Parent: 1 Parent: 2 Parent: 3 Parent: 4 4. Give an example of a situation in which ordinary pipes are more suitable than named pipes and an example of a situation in which named pipes are more suitable than ordinary pipes Consider the situation in which the producer writes the file to the pipe and the consumer reads the file and counts the number of characters in that file, in this situation, ordinary pipe is more suitable than named pipe. Now consider another situation in which several processes write messages to a log, a server reads the messages from the pipe and writes them to the log file. In this situation, named pipes are more suitable. 5. Using the program in Figure 3.34, identify the values of pid at lines A, B, C, and D. (Assume that the actual pids of the parent and child are 2600 and 2603, respectively #include <sys/types.h> #include <stdio.h> #include <unistd.h> int main() { pid t pid, pid1; /   fork a child process   / pid = fork(); if (pid < 0) { /   error occurred   / fprintf(stderr, "Fork Failed"); return 1;
} else if (pid == 0) { /   child process   / pid1 = getpid(); printf("child: pid = %d",pid); /   A   / printf("child: pid1 = %d",pid1); /   B   / } else { /   parent process   / pid1 = getpid(); printf("parent: pid = %d",pid); /   C   / printf("parent: pid1 = %d",pid1); /   D   / wait(NULL); } return 0; } Solution: A=0 B=2603 C=2603 D=2600
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help