8.11. How many "hello" output lines does this program print? -code/ecf/forkprob1.c #include "csapp.h" int main() { int i; for (i = 0; i < 2; 1++) Fork(); printf("hello\n"); exit(0); } -code/ecf/forkprob1.c
Added by Nuria R.
Close
Step 1
Step 1: The program starts with one process. Show more…
Show all steps
Your feedback will help us improve your experience
Akash M and 74 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
```c++ #include <unistd.h> #include <stdio.h> int main() { write(1, "j", 1); int c1 = fork(); write(1, "m", 1); int c2 = fork(); write(1, "u", 1); if (c1 == 0 || c2 == 0) { write(1, "!", 1); } return 0; } ```
Akash M.
int main(void) { int i = 0; printf("A"); i++; fork(); printf("B"); i++; fork(); printf("C"); i++; fork(); printf("D"); i++; return 0; } 1. Is "ABCBCDCDCDDDDDD" a feasible output? If no, why? 2. Is "ABBCDCCDDCDDDDD" a feasible output? If no, why? 3. Is "ABCDDCDDBCDDDCD" a feasible output? If no, why? 4. Is "ABCCDDCBCDDDDDD" a feasible output? If no, why? 5. Is "ABCBDCCDCDDDDDD" a feasible output? If no, why? 6. How many processes are created (including the top level process) with the above code? What is the largest value of i among all the processes before they exit?
/* CHILD <16741> process is executing A program! CHILD <16742> process is executing B program! //Function B does something here. CHILD <16742> process has done with B program ! //Function A does something here. CHILD <16741> process has done with A program ! */ This is an example related to this practice. This program will fork two child processes running the two programs generated in programming zero in parallel. Hint: You may need to use fork(), exec() family, wait(), exit(), getpid() and etc ... Requirements: 1) Exactly two child processes are created, one to run the testspecial program and the other is to run the testalphabet program; 2) When a program starts to run, print a message to the output screen showing which process (with PID) is running which program, for example: "CHILD <16741> process is executing testspecial program!" 3) When a program is done, the process exits and at the same time, a message should be printed to the output screen showing which process (with PID) is done with the program, for example: "CHILD <16741> process has done with testspecial program!" 4) The messages should match the real execution orders, i.e. when the testspecial program starts/ends, the right message should be printed out. So you need to figure out how to get the starting/ending time of each process. #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> int main(int argc, char *argv[]) { printf("I am %d ", getpid()); pid_t pid = fork(); printf("fork returned: %d ", (int) pid); if (pid < 0) { perror("Fork failed"); } if (pid == 0) { printf("I am the child with pid %d ", (int) getpid()); sleep(5); printf("Child exiting.. "); exit(0); } // We must be the parent printf("I am the parent, waiting for child to end. "); wait(NULL); printf("Parent ending. "); return 0; }
Supreeta N.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
Watch the video solution with this free unlock.
EMAIL
PASSWORD