Our first (non-trivial) program! Now we can create something non-trivial. The ob
ID: 3757408 • Letter: O
Question
Our first (non-trivial) program! Now we can create something non-trivial. The objective in the programming exercise is to communicate text between processes. Later, we will find elegant ways to do this, but for now, one method that will work is to use text files stored on disk as a medium of exchange. I would like you to write a program as follows: Generate and store I would like you to write a program with the following behavior: Prompt the user to enter some text After text is entered, use fork to create a new process. Make sure the child is aware of the text that has been entered. Let's call this string userData. At this point the main routine goes back to waiting for new user input. User input of "Done" should terminate the main process and all its children. Each child process should open a separate text file on the hard disk. The name of the file is up to you, so long as it allows you to distinguish one process from another (tip: use getpid). Once per second the child should write the new string, userData, that it received at the fork to the file on a new line. Example: I typed in 2 different inputs, I should have 2 children. Each of these children has their own userData writing in their own separate files. o Children never terminate unless externally killed. .Explanation / Answer
In Python Language
import os
defmain():
prompt = ""
i = 1
a = ""
while (prompt != "Done"):
prompt = str(input("Enter some text: "))
n = os.fork()
if n == 0:
userdata = prompt
a = "file" + str(i)
f = open(a, "w+")
f.write(userdata)
i = i+1
main()
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.