Question

In: Computer Science

2. Run the following C program, and submit the screenshot of the result. Also briefly explain...

2. Run the following C program, and submit the screenshot of the result. Also briefly explain the result.

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

#define SIZE 5

int nums[SIZE] = {0,1,2,3,4};

int main() { int i; pid_t pid; pid = fork();

if (pid == 0) { for (i = 0; i < SIZE; i++) { nums[i] *= -i; printf("CHILD: %d ",nums[i]); /* LINE X */ } } else if (pid > 0) { wait(NULL);

for (i = 0; i < SIZE; i++) printf("PARENT: %d ",nums[i]); /* LINE Y */
}

return 0;
}














3. Run the following program and take a screenshot of the result. Explain the relationship between the created processes. (Hint: if we draw an arrow that points from a process to its child process, what shape can we get?)


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

int main (int argc, char *argv[]) { pid_t childpid = 0; int i, nbrOfProcesses; if (argc != 2) { /* Check for valid number of command-line arguments */ fprintf(stderr, "Usage: %s <processes>\n", argv[0]); return 1; }

/* Convert character string to integer */ nbrOfProcesses = atoi(argv[1]); for (i = 1; i < nbrOfProcesses; i++) { childpid = fork(); if (childpid == -1) { printf("Fork failed"); exit(1); } else if (childpid != 0) // True for a parent break; } // End for

// Each parent prints this line fprintf(stderr, "i: %d process ID: %4ld parent ID: %4ld child ID: %4ld\n", i, (long)getpid(), (long)getppid(), (long)childpid);

sleep(5); // Sleep five seconds

return 0;

} // End main

Solutions

Expert Solution

Code:

#include <sys/types.h>

#include <stdio.h>

#include <unistd.h>

#define SIZE 5

int nums[SIZE] = {0, 1, 2, 3, 4};

int main() {

int i;

pid_t pid;

//fork() is used to create a new child process

pid = fork();

// if(pid == 0) tells if the current process is child process

// LINE X is a part of the child process

if (pid == 0)

{

    for (i = 0; i < SIZE; i++) {

      nums[i] *= -i;

      printf("CHILD: %d ", nums[i]); /* LINE X */

    }

}

// if pid > 0 the the current process is parent process

else if (pid > 0)

{

    // wait(NULL) functions tells parent to wait for child process to complete

    // if wait(NULL) isn't used then the parent process will execute first

    // and LINE Y will be printed first because

    // LINE Y is a part of Parent Process

    wait(NULL);

    for (i = 0; i < SIZE; i++)

      printf("PARENT: %d ", nums[i]); /* LINE Y */

}

return 0;

}

The given program shows the working of Parent and child Process

  • pid = fork() is used to create a child process.
  • The if (pid == 0) tells the system to execute the true block if child process is running, i.e. LINE X will be executed by the child process.
  • The if (pid > 0) tells the system to execute the true block if parent process is running, i.e. LINE Y will be executed by the parent process.
  • The function wait(NULL) used in the else if block tells parent process to wait for the child process to complete.
  • Usually if wait(NULL) is not used the parent process will be executed first and then the child process will be executed.
  • As shown in the Output, LINE X is executed first because wait(NULL) made parent process to wait for child process to complete. After that LINE Y is executed and Parent process is completed.

Output:



Related Solutions

Please submit a screenshot of where your code got compiled, executed, showing the execution result Write...
Please submit a screenshot of where your code got compiled, executed, showing the execution result Write a program/function in python that will perform the following functions, when the program is executed, to demonstrate the features of an OOP language—ADT, inheritance, and polymorphism: Prompt a list of options—the main menu, below to compute the area of selected shape with input parameters: triangle rectangle square circle parallelogram Exit Per the selected option, prompt grader to enter the corresponding required parameters as described...
Present a screenshot of the following Program to open up the created file in C++ language....
Present a screenshot of the following Program to open up the created file in C++ language. The list of random numbers will be below the instructions I have provided for you a file named Random.txt for use in this program. This file contains a long list of random numbers. You are to write a program that opens the file, reads all the numbers from the file and calculates the following: A. The number of numbers in the file B. The...
using C thank you Must submit as MS Word file with a screenshot of the 3...
using C thank you Must submit as MS Word file with a screenshot of the 3 outputs. Run your program 3 times. the output must be in a screenshot not typed for the each time you run the program. thank you Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will...
in C++ Compile and run the program for the following values: r = 2 Cm, h...
in C++ Compile and run the program for the following values: r = 2 Cm, h = 10 Cm. The answer should be: The cross section area of the cylinder is 3.8955634 c The side area of the cylinder is 19.474819 inch-sqr Did you get the same answer? Explain the reason for such an error and fix the problem. Modify the previous program to include a new function called total_area, that computes the total suface area of a cylinder. The...
3.) Analyze the Java Program LinkRotator RUN the code and add the screenshot of the window...
3.) Analyze the Java Program LinkRotator RUN the code and add the screenshot of the window and output. Describe what code change you would make if you wanted each link to be displayed for 2.5 seconds. _______________________________________________________________ Below is the "LinkRotator" java program: 1: package com.java24hours; 2: 3: import java.awt.*; 4: import java.awt.event.*; 5: import java.io.*; 6: import javax.swing.*; 7: import java.net.*; 8:    9: public class LinkRotator extends JFrame 10: implements Runnable, ActionListener { 11: 12: String[] pageTitle =...
Please submit 1) the source code (.java file), and 2) the screenshot of running results of...
Please submit 1) the source code (.java file), and 2) the screenshot of running results of each question. (Factorials) Write an application that calculates the factorial of 20, and display the results. Note: 1) The factorial of a positive integer n (written n!) is equal to the product of the positive integers from 1 to n. 2) Use type long. (Largest and Smallest Integers) Write an application that reads five integers and determines and prints the largest and smallest integers...
I want to understand how this can be solved in c++. Please send a screenshot also...
I want to understand how this can be solved in c++. Please send a screenshot also with your code so I can see how it is supposed to be formatted or indented. Instructions: Your program will read in a file of commands. There are three types of commands: Warrior creates a new warrior with the specified name and strength. Battle causes a battle to occur between two warriors. Status lists all warriors, alive or dead, and their strengths. A sample...
C++(screenshot output please) Part 2 - Tree programs Program 1 Implement a tree using an array...
C++(screenshot output please) Part 2 - Tree programs Program 1 Implement a tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template
Take the following program and include overload functions into it. Display result of function. C++ Program:...
Take the following program and include overload functions into it. Display result of function. C++ Program: #include <iostream> using namespace std; // this is the additional function string read() {     string input;     cout << "Enter input: ";     cin >> input;     return input; } int main() {     // call function here     string output = read();     cout << "You entered: " << output; }
Operating Systems class Complete the following work with Linux commands and submit the screenshot. 1.Install a...
Operating Systems class Complete the following work with Linux commands and submit the screenshot. 1.Install a virtual machine with Lubuntu 2.Log in and display the content of your home directory 3.Create a directory called homework under your home directory and navigate to homework directory 4.Create a file called newfile under homework directory (this can be done by Leafpad) 5.Copy the file newfile to tempfile, then rename tempfile to myfile and delete newfile 6.Display the content of myfile
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT