Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib] Adds functionality to spawn and manage processes from exec. file #3998

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

izo0x90
Copy link

@izo0x90 izo0x90 commented Feb 14, 2025

  • Adds vfork, execvp, kill system call utils. to Mojos cLib binds

  • Adds utility struct. to allow spawning and managing child processes
    from an executable file

    • run factory method that creates process and returns "management"
      object
    • Instance methods to send signals to child process, INT, HUP, KILL

Ex.:

import os

fn main():
 p = os.Process.run("ls", List[String]("-lha"))
 p. interrupt()

@izo0x90 izo0x90 changed the title [] Adds functionality to spawn and manage processes from exec. file [stdlib] Adds functionality to spawn and manage processes from exec. file Feb 14, 2025
Copy link
Contributor

@owenhilyard owenhilyard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work so far, just a few fixes.

stdlib/src/sys/_libc.mojo Outdated Show resolved Hide resolved
stdlib/src/sys/_libc.mojo Outdated Show resolved Hide resolved
stdlib/src/sys/ffi.mojo Outdated Show resolved Hide resolved
stdlib/src/os/os.mojo Outdated Show resolved Hide resolved
stdlib/src/os/os.mojo Outdated Show resolved Hide resolved
stdlib/src/os/os.mojo Outdated Show resolved Hide resolved
stdlib/src/os/os.mojo Outdated Show resolved Hide resolved

def main():
test_process_run()
# TODO: How can exception being raised on missing file be asserted
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue here from what I understand seems to be that I am rasing the exception in the forked child process when we don't find the executable file ... which try actually handles gracefully ... so this works fine:

try:
  _ = Process.run("file_does_not_exist", List[String]())
except e:
 print(e)

But for some reason (I assume related to the raise being in a different process) when try to validate the raise with:

with assert_raise():
  _ = Process.run("file_does_not_exist", List[String]())

It crashed the test suite with a no such process error. 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that raises in a fork pre-exec is unsound, you'll need to check before you fork.

- Adds fork, execvp, kill system call utils. to Mojos cLib binds

- Adds utility struct. to allow spawning and managing child processes
  from an executable file
  - `run` factory method that creates process and returns "management"
    object
  - Instance methods to send signals to child process, INT, HUP, KIL

Signed-off-by: Hristo I. Gueorguiev <[email protected]>
@izo0x90 izo0x90 force-pushed the Hristo/Add-basic-process-exec-facilities branch 2 times, most recently from ba4b3a1 to 6342634 Compare February 15, 2025 13:24
- Move `process` related code to its own file
- Use `vfork` to avoid issues with threading until they are resolved in
  Mojo
- Sending signals to process returns success status
- Docstr additions

Signed-off-by: Hristo I. Gueorguiev <[email protected]>
@izo0x90 izo0x90 force-pushed the Hristo/Add-basic-process-exec-facilities branch from 6342634 to 63a5a80 Compare February 15, 2025 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants