Question

In: Computer Science

Q2) Using the program in Figure 2, identify the values of pid at lines A, B,...

Q2) Using the program in Figure 2, 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.) Give a brief explanation.

#include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <unistd.h>

int main() { pid_t pid, pid1; /*fork a child process*/

pid = fork(); if (pid < 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; }

Figure 2 - What are the pid values displayed?

Solutions

Expert Solution

Program:


#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>

int main() {
pid_t pid, pid1; /*fork a child process*/

pid = fork();
if (pid < 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;
}

Explanation:

/*at line A
* pid stores return value of fork(), fork() returns 0 for child process
* so child pid=0
*prints 0 at line A
*/
/*at line B
* pid1 stores return value of getpid(), getpid() returns process id of process
* so child pid1 = 2603 (since given that child pid = 2603)
*prints 2603 at line B
*/
/*at line C
* pid stores return value of fork(), fork() returns pid of child process for parent process
* so parent pid=2603 (since given that child pid = 2603)
*prints 2603 at line C
*/
/*at line D
* pid1 stores return value of getpid(), getpid() returns process id of process
* so parent pid1=2600 (since given that parent pid = 2600)
*prints 2600 at line D
*/

Output: (The output is same but there won't be any new lines)

child: pid = 0

child: pid1 = 2603

parent: pid = 2603

parent: pid1 = 2600


Related Solutions

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();       ...
In figure lines XY and MN intersect at O. If ∠POY = 90° and a : b = 2 : 3, find c
In figure lines XY and MN intersect at O. If ∠POY = 90° and a : b = 2 : 3, find c.
(a) Using the root locus plot shown in figure Q2 (a), find the system damped natural...
(a) Using the root locus plot shown in figure Q2 (a), find the system damped natural frequency ( d) when the system is marginally stable, then find the maximum gain for stable system. [5 Marks ] (b) Using the Root locus plot shown in figure Q2 (a), determine the open loop transfer function. [ 5 Marks ] Figure Q2 (a) open-loop Transfer function Root Locus Plot (c) G(s) is an open loop system transfer functions that include an adjustable gain...
1)What is The charge on 6 alpha particles? 2) Suppose the charge q2 in the figure...
1)What is The charge on 6 alpha particles? 2) Suppose the charge q2 in the figure can be moved left or right along the line connecting the charges q1 and q3. Given that q = +21
2. (a) Find the values of a and b such that the eigenvalues of A =...
2. (a) Find the values of a and b such that the eigenvalues of A = |a 1 are 2 and -5. (b) Find the values of a, b and c such that the eigenvalues of A = | 0 1 0 | 0 0 1 | a b c are 3, -2, and 5. b 1|
2. Create a php program to get all the values from the forms using various methods...
2. Create a php program to get all the values from the forms using various methods and control structures like switch, if else, for, foreach, while, do while The question is required to write a program
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
Hi! I am trying to compare 2 files, and figure out which lines from infile2 are...
Hi! I am trying to compare 2 files, and figure out which lines from infile2 are also in infile1, and output the line index, from infile2, of common lines. I would also want to see which lines (actual string line) are common. Below is my c++ program, and I am not sure what I am doing wrong. Can someone tell me what I am doing wrong and how it can be fixed? _________________________________________________________main.cpp #include<iostream> #include<fstream> #include<vector> #include<string> using namespace std;...
Q2-B) Answer the followings: I. Identify the addressing mode for Two of the following instructions: SBB...
Q2-B) Answer the followings: I. Identify the addressing mode for Two of the following instructions: SBB AX,SI MOV [BX]+1234H,AL XCHG DL,[BP+20H] II. True or False and why correct the false one: 1- A 20-bit address bus sports 20,000 memory addresses 2-A machine cycle refers to fetch an instruction 3-A memory with 256 addresses has 256 address lines I want the answer in 45 minutes
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT