Question

In: Computer Science

How many times should the following C code print "Example" and draw a process graph #include...

How many times should the following C code print "Example" and draw a process graph

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

void try()
{
fork();
printf("Example\n");
fork();
return;
}

int main()
{
try(); fork();
printf("Example\n");
exit(0);
}

Solutions

Expert Solution

Fork calls help in creating new processes which we call as child process.

It runs simultaneously with the process making fork call. Once the child process is created than both processes will execute the next instruction following the fork call.

It takes no parameters and returns integer value.

Three values that are returned by fork() as as follows: .

  1. Negative Value: Child process creation failed.
  2. Zero: returns to the newly created child process.
  3. Positive value: returns value to the caller and contains process Id of new child process created.

The above mentioned program returns " Example" 2 times.

No. of times ‘example’ is printed equal to the no. of process created.

Total Number of Processes = 2n, where n is no. of fork system calls therefore, 21 = 2.

Explanation:

fork() methods in the try() will not return anything since the return type of the try() is void. So the fork() methods present in the main() will run and it will create a child process and run.

Process Graph:

P0  ( Main Process )

|

P1 ( Process Created by Fork())


Related Solutions

C++ Visual Studios How many times will "!" print? int i = -5 while(-5 <= i...
C++ Visual Studios How many times will "!" print? int i = -5 while(-5 <= i <= 0) { cout << "!"; --i; }
;the task is to print out the result no matter how many digits ; it should...
;the task is to print out the result no matter how many digits ; it should work on Emu8086 ;the code should be in assembly language ; add comments to your code org 100h .data F db 0 num1 db 0 num2 db 0 op db 0 result db 0 msg db 0dh,0ah,"Error" .code mov ax,@data mov ds,ax LL1: call get1 cmp ax,0 je print cmp F,0 je LL1 LL2: call get2 cmp ax,0 je print cmp F,1 je LL2...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
C programming #include <stdio.h> #include <math.h> int main() { printf("============== Problem #1 =================\n"); printf("This should print...
C programming #include <stdio.h> #include <math.h> int main() { printf("============== Problem #1 =================\n"); printf("This should print down from 2 to 0 by 0.1 increments\n"); float f = 2.0; while (f != 0) { printf("%0.1f\n",f); f = f - 0.1; } printf("============== Problem #2 =================\n"); printf("This should find that the average is 5.5\n"); int total_score = 55; int total_grades = 10; double avg = total_score/total_grades; printf("Average: %0.2f\n",avg); printf("============== Problem #3 =================\n"); printf("If the population increases by 2.5 people per second, how...
14. How many times does "#" print? for(int i = 0; i < 10; ++i) {...
14. How many times does "#" print? for(int i = 0; i < 10; ++i) { if(i == 2 || i==4 || i==6) { continue; } cout << "#"; }
how to send background process to foreground process using c code?
how to send background process to foreground process using c code?
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits>...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits> #include <vector> using namespace std; // // CLASS: NODE // class Node{ public: int value = 0; // our node holds an integer Node *next = nullptr; // our node has a pointer to the next Node Node(int i){ // contructor for our Node class value = i; // store a copy of argument "i" in "value" next = nullptr; // be sure next...
This C++ CODE has many functions that needs to be tested piece by piece For example,...
This C++ CODE has many functions that needs to be tested piece by piece For example, you going to display each function with [ ] and a , separating each piece of data, like so: [1,2,3,4]? results must be copied and pasted as commnets at the bottom of this file this is the code: #include <iostream> #include <string> #include <cstddef> //for NULL using namespace std; class List {    private:        struct Node        {           ...
It should be tested whether a cube is fair or not. This tells how many times...
It should be tested whether a cube is fair or not. This tells how many times the number 1 appears. With a sample, a dice is thrown 720 times and it showed number one 95 times. The null hypothesis is Ho:p =1/6. Probability of error is specified α=0.05 a)Approximate the binomial distribution according to Moivre-Laplace by normal distribution (without correction ). b) derive a formula for the rejected area or accepted area.
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef struct node { struct node *left; struct node *right; long data; long leftSize; } node; void btreeInsert(node *new, node **rootptr) { node *parent = NULL, *cursor; /* Find parent */ cursor = *rootptr; while (cursor != NULL) { parent = cursor; if (new->data < cursor->data) { cursor->leftSize += 1; cursor = cursor->left; } else { cursor = cursor->right; } } /* Insert node below...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT