Question

In: Computer Science

Using the program below, identify the values of pid at lines A, B, C, and D....

Using the program below, 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 lt; 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;

  }

Solutions

Expert Solution

Fork() function returns 0 to the child process and child process id to the parent process. getpid() function returns current process id. I included the comments in the lines of A B C D in the following code

 #include <sys/types.h>

#include <stdio.h>

#include <unistd.h>



int main()

{

pid_t pid, pid1;



/* fork a child process */

pid = fork();


if (pid lt; 0) { /* error occurred */

    fprintf(stderr, "Fork Failed");

    return 1;

}


else if (pid == 0) { /* child process */   // since this is child code
                                           // fork() return value will be 0
                                           // so pid holds 0

pid1 = getpid();                  // getpid() returns current process id i.e., child process id.

printf("child: pid = %d",pid); /* A */     // prints pid i.e., 0

printf("child: pid1 = %d",pid1); /* B */   // prints current process id (child process id) i.e., 2603

 }

else { /* parent process */       //since this is parent code
                                  //fork() return value will be child's process id
                                  // so pid holds child's process id i.e., 

pid1 = getpid();              //getpid() returns current process id ie., parent process id.

printf("parent: pid = %d",pid); /* C */ // prints pid i.e., 2603

printf("parent: pid1 = %d",pid1); /* D */ // prints current process id (parent process id) i.e., 2600

wait(NULL);

}

return 0;

}

Related Solutions

write a program that evaluates the following arithmetic expression: ((A+B)/C)*((D-A)+E). Assign test values to the variables...
write a program that evaluates the following arithmetic expression: ((A+B)/C)*((D-A)+E). Assign test values to the variables and display the resulting value.
The following graph contains four lines (A, B, C and D), each of which has a...
The following graph contains four lines (A, B, C and D), each of which has a slope that is either positive, negative, zero, or infinite.For each of the following scenarios, indicate whether the relationship between the two variables is positive or negative, as well as which line on the previous graph has a slope that reflects this type of relationship. Hint: The X-axis and Y-axis on the graph are not labeled intentionally. You need to substitute the variables from each scenario...
The following graph contains four lines (A, B, C and D), each of which has a...
The following graph contains four lines (A, B, C and D), each of which has a slope that is either positive, negative, zero, or infinite.For each of the following scenarios, indicate whether the relationship between the two variables is positive or negative, as well as which line on the previous graph has a slope that reflects this type of relationship. Hint: The X-axis and Y-axis on the graph are not labeled intentionally. You need to substitute the variables from each scenario...
Consider the cross: A/a; b/b; C/c; D/d; E/e x A/a; B/b; c/c; D/d; e/e a) what...
Consider the cross: A/a; b/b; C/c; D/d; E/e x A/a; B/b; c/c; D/d; e/e a) what proportion of the progeny will phenotypically resemble the first parent? b) what proportion of the progeny will genotypically resemble neither parent?
What are the values of a, b, c, d, and e after the following statements? int...
What are the values of a, b, c, d, and e after the following statements? int a = 5, b = 2, c = 8, d = 7, e = 4; int x = c - b + a; a = a + x - d; b = c * d - x; c = e + a / 2; d = x - c * a; e = x + d - c;
A. Rectangles B. Circles C. Diamonds D. Lines In an E-R diagram, entities are represented as:
A. Rectangles B. Circles C. Diamonds D. Lines In an E-R diagram, entities are represented as:
Below are the annual returns of the stock A, B, C and D and the market...
Below are the annual returns of the stock A, B, C and D and the market portfolio for the period 2018-2022. Find the expected return and standard deviation of the stock A, B, C, D and the market portfolio. PREPARED BY HANDWRITING. Year Prob. Asset A Asset B Asset C Asset D Market 2018 0,20 11,00 9,00 12,00 15,00 14,00 2019 0,25 13,00 11,00 15,00 13,00 12,00 2020 0,10 9,00 8,00 11,00 10,00 9,00 2021 0,20 12,00 12,00 11,00 13,00...
Write a C++ program to determine the sum of two 1-D matrices: A = [a b...
Write a C++ program to determine the sum of two 1-D matrices: A = [a b c d] B = [e f g h] The user will provide the values of a to h.
In the diagram, which point is at the lowest potential? (a) A (b) B (c) C (d) D
In the diagram, which point is at the lowest potential?(a) A(b) B(c) C(d) D
Using the data in the table, calculate the rate constant of this reaction. A+B⟶C+DA+B⟶C+D Trial [?]...
Using the data in the table, calculate the rate constant of this reaction. A+B⟶C+DA+B⟶C+D Trial [?] (?)[A] (M) [?] (?)[B] (M) Rate (M/s) 1 0.4000.400 0.2000.200 0.01600.0160 2 0.4000.400 0.5400.540 0.1170.117 3 0.4800.480 0.2000.200 0.01920.0192 ?=k= Units
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT