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; }
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...
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        {           ...
C++ Modify the class unorderedList to include a recursive forward print and a recursive reverse print...
C++ Modify the class unorderedList to include a recursive forward print and a recursive reverse print Make your unorderedList a list of characters instead of integers Insert ten characters into the list and print it out both ways #include <iostream> #include <string> #include <cstdlib> using namespace std; struct node { int info; node* next; }; class unorderedList { private: int length; node* listPtr; public: unorderedList() {length = 0; listPtr = NULL;} void makeEmpty(); void insertItem(int item); void printList(); bool isFull()...
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...
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an...
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an earthquake is in the range [0, 3), “medium” if M is in the range [3, 6), “large” if M is in the range [6, 9) and “epic” if M is greater than or equal to 9, where M is input by a user via the keyboard. (in c++)
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT