Question

4 @author: student namel, student2@uncc.edu 5 author: student name2, student2@uncc.edu 7 @description: A program that demonstrates processes. 9 @course: ITSC 3146 10 @assignment: In-class activity [n] 12 13 14 #include<iostream> 15 #include«unistd.h> 16 17 int mainOl 18 19 20 21 pid_t id = fork(); if(id 1) { ) else if (id -- 0) std::cout <<Error creating process\n; std::cout <<I am a child process!n; std::cout <<I just became a parent!\n; 23 24 25 26 27 28 29 30 31 else return 0;

a) The purpose of the code on line 19, i.e., what does this line of code accomplish?

b) The purpose of the code on lines 21-27 (if statement). Make sure to explain each possibility handled by the if.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a) The line- pid_t id = fork()​ creates or forks a child process for the parent process main(). The process id of the child process is returned to the parent. The child process is an exact duplicate of the parent process but operates in a different memory space.

b) the first if condition- if(id == -1) ​tests for the failure of the creation of the child process, as -1 will be returned to parent if failure occurs.

the second if condition- if(id == 0)​ checks if the creation of child process was successful as on successful creation, 0 is returned in child process.

and finally, if any other value other than these two is returned then it means that child process is no more running and we are currently in the parent process.

To print the process id's of these processes you can use the getpid() function.

Add a comment
Know the answer?
Add Answer to:
a) The purpose of the code on line 19, i.e., what does this line of code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (C++/Linux) I am a little confused as to what the first line in the main exactly...

    (C++/Linux) I am a little confused as to what the first line in the main exactly does. I am also in need of clarity about all the possibilities in the if statement and what they mean and correspond to (all in the code below). Thank you. #include <iostream> #include <unistd.h> int main() { pid_t id = fork(); if(id == -1) { std::cout << "Error creating process\n"; } else if (id == 0) { std::cout << "I am a child process!\n";...

  • Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork...

    Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork a child process*/ pid = fork(); if (pid<0){ /*error occured*/ fprintf(stderr,"Fork failed\n"); return 1; } else if (pid == 0) { /*child process */ printf("I am the child %d\n",pid); execlp("/bin/ls","ls",NULL); } else { /*parent process */ /*parent will wait for the child to complete*/ printf("I am the parent %d\n",pid); wait(NULL); printf("Child Complete\n"); } return 0; } 1. Write program codes for 3.21 a and...

  • 10. Does the following code compile? Does it run? Is there any problem with the code?...

    10. Does the following code compile? Does it run? Is there any problem with the code? If yes, how do you fix it? 1. #include < iostream > 2. using namespace std; 4 class Computer int Id; 7. public: Computer(int id) this -Id- id; ) void process() cout << "Computer::process()"; 9 10. H; 12. class Employee 13. 14. 15. public: 16 Computer* c; Employee )cnew Computer (123); 17.Employee() C 18. 19. 20 21. 22. ^; 23. 24. int main(){ 25....

  • Given the following pseudo-code, please show what the outputs will be at LINE A, B, C...

    Given the following pseudo-code, please show what the outputs will be at LINE A, B, C and D. please put down “inconclusive” if you think the CPU process scheduling could potentially result in multiple values for integer i. You will receive partial credit if you draw the process tree. int i = 0; int main(){                         if (fork() == 0) {                         i++;                         if (fork() == 0){                                     if (fork() == 0){                                                 i++;                                                 print value...

  • What is the functionality of the following code? #include #include mainO <stdio.h> <unistd.h> int i,j; j-0:...

    What is the functionality of the following code? #include #include mainO <stdio.h> <unistd.h> int i,j; j-0: printf ("Ready to fork.n) i-fork; if 0) this code.\n"); printf "The child executes for (i-0; i?5; i++) printf("Child j-dn".j); else j-vaito printf("The parent executes this code. Ln" printf ("Parent j-dn",j);

  • Question 3: [2+2] a)What, to whom and how many values a fork system call returns? b)Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in

    Arid No is 19-arid-898Question 3:                  [2+2]a) What, to whom and how many values a fork system call returns? b) Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in which Terminal login must be your arid number.int main(){      pid_t  fork_return;        fork_return = fork(); if (fork_return == 0) {                  execlp("/bin/ls", "ls", NULL);                 printf("Child process ID: %d\n", getpid());      exit(0);        }        else { wait (NULL);                 printf("Parent process ID: %d\n",...

  • Linux & C code help with creating a child process and kills itself with SIGNAL. I...

    Linux & C code help with creating a child process and kills itself with SIGNAL. I provided most of the code, I need some help to finish. Please provide output screen shot as well. Thank you! A Process creates a child process. Child process will print out "hello 0" "hello 1" ..... 1 line per second. After a certain number of seconds(user input), it kills itself WITH signal mechanism ONLY, don't use exit() on the child. You can use alarm()...

  • I have the following code....from the previous lab....the above needs to be added to what is...

    I have the following code....from the previous lab....the above needs to be added to what is already existing. ALSO MODIFY SEMAPHORES TO USE pthreads instead of the pipe constructs P() & V() #include <stdio.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h> #include <sys/stat.h> void printStat(char *filename); //Main int main(int argc, char *argv[]) { //Process Id (storing)    pid_t pid;    int j;    //printf("Welcome to Project Three\n”);    // For loop*/    for (j = 1; j...

  • Computer Science Problem C++ cla5a.cpp 1 // cla10a BY student name, CSCI 2170-section 2 //File: cla10a.cpp...

    Computer Science Problem C++ cla5a.cpp 1 // cla10a BY student name, CSCI 2170-section 2 //File: cla10a.cpp 3 //Author: Dr. Roland H. Untch 4 //Purpose: This program displays a student's classification 5 // (e.g., "freshman", "sophmore", etc.) based on an 6 // integer classification code that is read in. In 7 // it's current form, this program uses nested IF 8 // statements to encode a "multiway branch" or "case 9 // construct" to determine the student's classification. 10 // As...

  • Contents? Introduction Existing code & UML UML Code Changes to be made to the program tester_with_mods.cpp...

    Contents? Introduction Existing code & UML UML Code Changes to be made to the program tester_with_mods.cpp Submit Instructions? Introduction The purpose of this assignment is to provide you with experience in working with objects in C++. Existing code & UML You have been provided with a class that describes the products maintained in a company’s inventory. The class maintains the following attributes: Name of attribute Data Type productId string productPrice float Accessor and Mutator methods have been created for these...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT