Question

Use vi to make a python program that defines three functions waiting for two, three, and...


Use vi to make a python program that defines three functions waiting for two, three, and four seconds respectively and creates a process for each function, then starts the processes one by one.

Output the number of CPU/ processes once all processes have been started, then for each child process, output its name and id. Then indicate the end of the program.

Each function first outputs the function name, then waits for certain amount of time as specified previously, and indicates the end of the function.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here's the Python 2.7 code, both the screenshot and the text version including the screenshot of the output.

Text version :

import multiprocessing
import os
import time
def one():
    print("name - one ")
    print("one's pid",os.getpid())
    time.sleep(2)
    print("one completed")

def two():
    print("name - two ")
    print("two's pid",os.getpid())
    time.sleep(3)
    print("two completed")

def three():
    print("name - three ")
    print("three's pid",os.getpid())
    time.sleep(4)
    print("three completed")

if __name__== "__main__":
    # creating separate processes
    p1 = multiprocessing.Process(target=one,)
    p2 = multiprocessing.Process(target=two,)
    p3 = multiprocessing.Process(target=three,)

    # Print Main's Process id
    print("main's pid",os.getpid())
    # Starting Each Process one by one
    p1.start()
    p2.start()
    p3.start()
    p1.join()
    p2.join()
    p3.join()
    print("done") # print once all process has finsihed executing

For the total number of processes running, One parent is calling three children, so the total number of processes running is 4. The generalized method to calculate the total number of process is the number of child processes + 1(for the parent).

Note -> If you have any query, please ask in the comments sections. Also, if this answer is helpful to you, your feedback will be greatly appreciated.

Add a comment
Know the answer?
Add Answer to:
Use vi to make a python program that defines three functions waiting for two, three, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Questions: 1) Write a simple program to create three processes using fork() commands. Use any three...

    Questions: 1) Write a simple program to create three processes using fork() commands. Use any three of the six system calls to show how each child process executes new sub-programs using exec’s minions. Show the output of each of the child process with different data samples.   2) Write a simple program in your Linux environment using C++ to identify PID, PPID and GID for the processes created in Question 1 and display the real and effective user ID.   3) Modify...

  • write a program that defines two different structures as follows: 1) A cPu structure that contains...

    write a program that defines two different structures as follows: 1) A cPu structure that contains a manufacturers Name (string), a model number (int), a speed (float), and a price (float). a model number (int), 2) A Motherboard structure that contains a manufacturer's ID (int), and a price (float). Your program use arrays of each type of structure. All arrays wi contain a total of five will structures. Your program will also read in two separate files. You can obtain...

  • Please help me create this CLI CPU Scheduling Simulator in java: First Come First Serve (FCFS)...

    Please help me create this CLI CPU Scheduling Simulator in java: First Come First Serve (FCFS) Round Robin (RR) Process information The process information will be read from an input file. The format is: pid arrival_time burst_time All of fields are integer type where: pid is a unique numeric process ID arrival_time is the time when the task arrives in the unit of milliseconds burst_time the is the CPU time requested by a task, in the unit of milliseconds The...

  • PROJECT 6    Functions You are to write a PYTHON program which: *Defines the following 4  functions: whoamI()...

    PROJECT 6    Functions You are to write a PYTHON program which: *Defines the following 4  functions: whoamI() This function prints out your name and lists any programming course you have taken prior to this course. isEven(number) This function accepts one parameter, a number, and prints out a message indicating if the number is even or odd. Keep in mind that a number is even if there is no remainder when the number is divided by two. printEven(number) This function accepts one...

  • Python please! Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods...

    Python please! Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) returns the maximum value in the an array int arrayMin(int[] arr) returns the minimum value in an array void arraySquared(int[] arr) changes every value in the array to its square (value²) void arrayReverse(int[] arr) reverses the array (for example: array storing 7 8 9 becomes 9 8 7 ) The program main method creates a single-dimensional array of length...

  • Write a program (C++) that requests the current time and a waiting time as two integers...

    Write a program (C++) that requests the current time and a waiting time as two integers for the number of hours and the number of minutes to wait. The program then outputs what the time will be after the waiting period. Use 24-hour notation for the times. Include a loop that lets the user repeat this calculation for additional input values until the user says she or he wants to end the program.You can assume the wait time will always...

  • Write a program in python or c++ that has two functions: Function one is called: _encrypt...

    Write a program in python or c++ that has two functions: Function one is called: _encrypt Function Two is called: _decrypt Function one takes plain text ,and assesses the indexed value of each letter in the string. For example, a=1,b=2 . It then adds three to the indexed valus , and produces encrypted text , based off of the plain text. Function two reverse this. here is sample output: _encrypt(‘how do you do’) Unsecured: howdoyoudo Secured: lsahscsyhs _decrypt(‘lsahscsyhs’) Unsecured: lsahscsyhs...

  • String Processing Labs Directions: Write a main program to test the three functions described below, Input...

    String Processing Labs Directions: Write a main program to test the three functions described below, Input is from a file, and output is to a file. 1. The function Another parameter is a char variable with a letter value. The function outputs the number of times the char value appears in the string. processes a string containing letters of the alphabet, which is a parameter 2. This Boolean function has two string parameters. It returns true when the first str...

  • Description In this homework, you are asked to implement a multithreaded program that will allow ...

    Description In this homework, you are asked to implement a multithreaded program that will allow us to measure the performance (i.e, CPU utilization, Throughput, Turnaround time, and Waiting time in Ready Queue) of the four basic CPU scheduling algorithms (namely, FIFO, SJE PR, and RR). Your program will be emulating/simulating the processes whose priority, sequence of CPU burst time(ms) and I'O burst time(ms) will be given in an input file. Assume that all scheduling algorithms except RR will be non-preemptive,...

  • Please use Python 3, thank you~ Write a program that: Defines a function called find_item_in_grid, as...

    Please use Python 3, thank you~ Write a program that: Defines a function called find_item_in_grid, as described below. Calls the function find_item_in_grid and prints the result. The find_item_in_grid function should have one parameter, which it should assume is a list of lists (a 2D list). The function should ask the user for a row number and a column number. It should return (not print) the item in the 2D list at the row and column specified by the user. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT