Question

In: Computer Science

Read programs and find out their outputs. Please provide process tree with our answer to get...

Read programs and find out their outputs. Please provide process tree with our answer to get full points.

(a)
#include <unistd.h> #include <stdlib.h> #include <stdio.h>

int main()
{ for(i=0;i<3;i++)

if(fork()>0) printf(“A\n”); }

(b)
#include <unistd.h> #include <stdlib.h> #include <stdio.h>

int gVar=0;

int main() {

int lVar=0;
int *p;
p=(int *)malloc(sizeof(int));

int pid=fork(); if(pid>0){

gVar=1;
lVar=1;
*p=1;
printf("%d, %d, %d\n", gVar, lVar, *p);

}else{
gVar=2;

lVar=2;
*p=2;
printf("%d, %d, %d\n", gVar, lVar, *p);

} }

(c) Read the following code and answer the questions below:

#include <unistd.h> #include <stdio.h>

main() {

int i;

for(i=0;i<3;i++) if(i%2==1)

fork(); else{

fork();

fork(); }

}

(1) How many processes are created by the OS when it runs the following program?

(2) Suppose your answer to (1) is n, and let us denote the n processes as P1, P2, ..., Pn, please describe the parent-child relations among the processes.

Solutions

Expert Solution

Answer to the question no 1:  

Since the total number of processes will be 2^3 -1 = 7. So A will be printed 7 times.

Ans to question no. 2: when fork() will be called, there will be 2 processes parent and child. pid of the parent will be greater than 0 and the pid of the child process will be 0 so both if and else condition will be executed in a sequential manner. the output of second code will be 1,1,1 newline 2,2,2.

Ans to question no. 3: Total number of processes will be 10. Since the process P1, P2, P3 are created as same as question 1 in level 1 at i=0,1,2. because at i=0 and 2 fork of the forked process id introduces so at i=1 the number of processes will be doubled. In figure 1 the processes are 3. So at i =0, the processes will become 2*3 = 6. Since at i=2 no other process can be forked. An overall increase in the number of processes will be 3 fro i =0 due to else statement from question no 1.  


Related Solutions

The following is a set of tree-set test programs that show the following outputs: Switch to...
The following is a set of tree-set test programs that show the following outputs: Switch to ArrayList, LinkedList, Vector, TreeMap, and HashMap to display similar output results. Results: Tree set example! Treeset data: 12 34 45 63 Treeset Size: 4 First data: 12 Last Data: 63 Removing data from a tree set Current tree set elements: 12 34 63 Current tree set size :3 Tree set empty. Example code import java.util.Iterator; import java.util.TreeSet; public class TreeDemo2 { public static void...
Can you please provide a step by step instruction to get to the answer to the...
Can you please provide a step by step instruction to get to the answer to the following question WACC. Kose, Inc., has a target debt-equity ratio of .38. Its WACC is 10.1 percent and the tax rate is 25 percent. a) If the company’s cost of equity is 12 percent, what is its pretax cost of debt? b) If instead you know that the aftertax cost of debt is 6.4 percent, what is the cost of equity?
Answer using excel and please provide excel instructions for all outputs meaning what formula was used...
Answer using excel and please provide excel instructions for all outputs meaning what formula was used to calculate the output and how was it implemented using excel and which excel functions to use. Thank you. Conch Republic Electronics is a midsized electronics manufacturer located in Key West, Florida. The company president is Shelly Couts, who inherited the company. The company originally repaired radios and other household appliances when it was founded over 70 years ago. Over the years, the company...
Read case and get all the information before answering these questions. Please answer these questions specifically...
Read case and get all the information before answering these questions. Please answer these questions specifically what they are asking for with details 1. What specific type of wholesaler is Joe? 2. Evaluate Joe's marketing strategy for his current lumber business. Be and be sure to mention the specific components. 3. Contrast the components of the marketing strategy for the Arbor Products with Joe's current Hanratty lumber business. 4. What should Joe do now? Which option should he take? Be...
Please read the article and answer about questions. International Strategies When you are struggling to get...
Please read the article and answer about questions. International Strategies When you are struggling to get through that first year of business, international sales are about the last thing on your mind. The U.S. Department of Commerce, however, indicates that large compa- nies account for only about 4 percent of all exporters, meaning the other 98 percent of the exporters in 2010 were small businesses.22 Entrepreneurs typically fall into three categories. There are those who realistically will never go international...
C++(screenshot output please) Part 2 - Tree programs Program 1 Implement a tree using an array...
C++(screenshot output please) Part 2 - Tree programs Program 1 Implement a tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template
Please read and answer the question below. Quelling? ?Violence? ?in? ?Our? ?Country One? ?only? ?has? ?to?...
Please read and answer the question below. Quelling? ?Violence? ?in? ?Our? ?Country One? ?only? ?has? ?to? ?turn? ?to? ?social? ?media? ?or? ?the? ?news? ?to? ?conclude? ?we? ?have? ?a? ?major? ?problem? ?in? ?our country? ?with? ?human? ?violence.? ?? ?It? ?seems? ?like? ?every? ?day? ?we? ?read? ?in? ?the? ?paper? ?yet? ?another? ?teenager was? ?assaulted? ?or? ?even? ?shot? ?at? ?school? ?or? ?even? ?off? ?campus? ?(as? ?a? ?result? ?of? ?events? ?which? ?occur? ?on campus).? ?? ?In? ?Ridgewood? ?NJ,? ?last? ?week,? ?a? ?teenager? ?had? ?his?...
Please read the following study case "Hot Coffee" then answer: In a world of get-rich-quick schemes,...
Please read the following study case "Hot Coffee" then answer: In a world of get-rich-quick schemes, few are mentioned more frequently than lawsuits. One of the reasons is the infamous McDonald’s coffee case (Liebeck v. McDonald’s Restaurants). This is what happened in 1992 in Albuquerque, New Mexico. Stella Liebeck, seventy-nine, was riding in a car driven by her grandson. They stopped at a McDonald’s drive-through, where she purchased a Styrofoam cup of coffee. Wanting to add cream and sugar, she...
Please read carefully the following case study and provide a suitable answer to its questions. A...
Please read carefully the following case study and provide a suitable answer to its questions. A male baby born after a normal pregnancy appeared to be healthy until after the third day after his birth. He became lethargic, hypnotic (low muscle tone), his breathing was shallow and lead to apnea. First, he was hooked up to a mechanical respirator. A blood sample was taken to the lab and found that his plasma ammonia level was 474 µmol/L. As a healthcare...
PLEASE READ CAREFULY AND EXPLAIN YOUR WORK: (JavaScript) only Search the Tree: A binary search tree...
PLEASE READ CAREFULY AND EXPLAIN YOUR WORK: (JavaScript) only Search the Tree: A binary search tree is a data structure that consists of JavaScript objects called "nodes". A tree always has a root node which holds its own integer value property and can have up to two child nodes (or leaf nodes), a left and right property. A leaf node holds a value attribute and, likewise, a left and right attribute each potentially pointing to another node in the binary...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT