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
  • In Python, write a program, poem.py, that defines a function that prints a short poem or...

    In Python, write a program, poem.py, that defines a function that prints a short poem or song verse. Give a meaningful name to the function. Have the program end by calling the function three times, so the poem or verse is repeated three times.

  • For Python 2.7.8, Write a program that defines a function that prints a short poem or...

    For Python 2.7.8, Write a program that defines a function that prints a short poem or song verse. Give a meaningful name to the function. Have the program end by calling the function three times, so the poem or verse is repeated three times.

  • This is a C program question you will implement a program to show the performance of...

    This is a C program question you will implement a program to show the performance of FCFS scheduling algorithm with I/O burst. Your program should get a file (e.g., “jobs.txt”) as the command-line input, and read the contents of the file. This file contains a set of processes. For example, consider the file with the following content: 1:(45,15);(16,20);(80,10);(40,-1) 2:(15,10);(60,15);(90,10);(85,20);(20,-1) 3:(30,15);(40,20);(5,15);(10,15);(15,-1) In this example we have 3 processes, each process is represented in a separate line. The general format of a...

  • 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...

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