Question

(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";
        } else {
                std::cout << "I just became a parent!\n";
        }

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

fork() function will create a new process and store the id from the OS library or os will allot id if id is negative -1 then error will be thrown to creating process, if id is 0 then that process will be called a child process or else any non-zero and positive process number eg 2546 or 5542 will be a parent process.

if you have any doubt then please ask me without any hesitation in the comment section below , if you like my answer then please thumbs up for the answer , before giving thumbs down please discuss the question it may possible that we may understand the question different way and we can edit and change the answers if you argue, thanks :)

Add a comment
Know the answer?
Add Answer to:
(C++/Linux) I am a little confused as to what the first line in the main exactly...
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
  • a) The purpose of the code on line 19, i.e., what does this line of code...

    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. 4 @author: student namel, [email protected] 5 author: student name2, [email protected] 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...

  • 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

    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 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", getpid());         }     return 0;}

  • 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()...

  • 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",...

  • 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...

  • Write a C program for Linux called pipes.c that does the following: In the main() function,...

    Write a C program for Linux called pipes.c that does the following: In the main() function, it creates a pipe using the pipe() function, then creates two child processes with fork(). Child 1 redirects stdout to the write end of the pipe and then executes with execlp() the "ps -aux" command. Child 2 redirects its input from stdin to the read end of the pipe, then it executes the "sort -r -n -k 5" command. After creating both children, the...

  • plz help me

    #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 10; int main() { pid_t pid;  pid = fork();  if (pid == 0) {  value = value + 100;  } else if (pid > 0) { value = value -100;  printf("PARENT: value= %d \n", value); //Line A  wait (NULL); } }  (i) What will be the output in Line A? Justify your answer. (ii) Do you think there is synchronization problem in updating the variable value? Justify your answer.

  • 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);

  • This is a C++ Program I created in Visual Studio for class. The problem I am...

    This is a C++ Program I created in Visual Studio for class. The problem I am having is I get an error msg that says, "no matching token found for line 8." What am I missing? I see an opening bracket and a closing bracket. But, the program will not build because it says more or less the bracket is missing. FYI....Line 8, is the bracket directly under int main (). Can someone help me understand where I am going...

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