huh-foundation / huh

programmer-friendly langauge. Contribute to Huh development !
Apache License 2.0
4 stars 0 forks source link

Proposal: Add Support for revive #9

Open onee-only opened 7 months ago

onee-only commented 7 months ago

Related: #5 When you want to rollback from kill(), you should have function like revive() which revives the process that have been killed by current process.


the pid = 1

kill(pid)
// damn, I made a mistake! 😱

revive(pid)
// everything back to normal 👍 
esperar commented 2 weeks ago

The suggestion for a revive() function to roll back from kill() warrants reconsideration for several reasons:

Complexity of Process Management: The kill() function is a crucial feature in operating systems for safely terminating processes and freeing up system resources. Introducing a revive() function could complicate process management unnecessarily.

Stability Issues: Once a process is terminated, there may be concerns about the integrity of the resources or data it was using. Simply reviving a process does not guarantee that it will return to a stable state, which could compromise system stability.

Developer Responsibility: Terminating a process is ultimately the developer's responsibility. In cases of accidental termination, it is more appropriate to focus on proper error handling rather than introducing a recovery mechanism. Understanding and managing the lifecycle of processes should be a priority for developers.

Alternative Approaches: Instead, implementing a confirmation mechanism before terminating a process or establishing policies to prevent critical processes from being killed could be more effective.

In conclusion, a revive() function may introduce unnecessary complexity to system design, and adhering to fundamental principles of process management is more important.

onee-only commented 2 weeks ago

You could respond by acknowledging their points but explaining why a revive-like function or alternative might still be helpful in specific scenarios. Here’s a draft for your response:


Thank you for the feedback! You’ve raised some great points about the complexities of process management and stability concerns, especially with terminating and potentially reviving processes. I agree that managing a process lifecycle should be a developer's responsibility, and introducing such a function could complicate the OS design.

However, the intent behind a revive() function is less about routine usage and more about mitigating accidental terminations, especially in development and testing environments where mistakes are common. Although kill() is designed for process termination, sometimes there’s a need to "soft-delete" or temporarily suspend processes rather than permanently kill them. A rollback mechanism could prevent minor errors from disrupting the workflow entirely.

To address your suggestions:

  1. Confirmation Mechanism: I agree that adding a confirmation prompt could reduce mistakes, especially with critical processes.
  2. Alternative: An alternative could be to create a "safe mode" where processes are paused instead of killed and can be resumed if necessary. This would provide developers with a safety net without risking system stability.

In short, while a full revive() might be overkill, having options for softer process handling might offer flexibility in environments where mistakes are more likely. Thank you for the insights!

esperar commented 2 weeks ago

"Can you implement a process management mechanism that is independent of the OS? If possible, please implement it and submit a PR."

Let me know if you need any further assistance!

esperar commented 2 weeks ago
import time
import random

class Process:
    def __init__(self, pid):
        self.pid = pid
        self.is_running = True

    def kill(self):
        if self.is_running:
            print(f"Process {self.pid} is killed.")
            self.is_running = False
        else:
            print(f"Process {self.pid} is already dead.")

    def revive(self):
        if not self.is_running:
            print(f"Process {self.pid} is revived.")
            self.is_running = True
        else:
            print(f"Process {self.pid} is already running.")

class ProcessManager:
    def __init__(self):
        self.processes = {}

    def create_process(self):
        pid = random.randint(1000, 9999)
        new_process = Process(pid)
        self.processes[pid] = new_process
        print(f"Process {pid} created.")
        return new_process

    def kill_process(self, pid):
        if pid in self.processes:
            self.processes[pid].kill()
        else:
            print(f"Process {pid} not found.")

    def revive_process(self, pid):
        if pid in self.processes:
            self.processes[pid].revive()
        else:
            print(f"Process {pid} not found.")

# Example usage
if __name__ == "__main__":
    manager = ProcessManager()

    # Create processes
    p1 = manager.create_process()
    p2 = manager.create_process()

    # Kill a process
    manager.kill_process(p1.pid)

    # Revive the killed process
    manager.revive_process(p1.pid)

    # Try to revive an already running process
    manager.revive_process(p2.pid)
onee-only commented 2 weeks ago

Seems nice enough. But we have to consider that we have only been using C and Assembly until now. So adding python to our codebase might increase complexity in our system even further. As we already are going to increase it by adding revive().