Skip to content

Spawning a process #3325

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

Open
HumanEntity opened this issue Mar 24, 2024 · 4 comments
Open

Spawning a process #3325

HumanEntity opened this issue Mar 24, 2024 · 4 comments

Comments

@HumanEntity
Copy link

Description

I am a odin newbie so I might overlooked something but as a way of learning odin I wanted to make simple build system for odin. And when I was trying to spawn shell process I didn't find anything to do this in core.

Example solution in Odin

For example something like this:

package main

import "core:process"
import "core:fmt"

main :: proc () {
    process_handle, process_spawn_err := process.spawn({"echo", "'Hello, World"})
    if process_spawn_err != nil {
        fmt.panicf("Failed to start command: %v", process_spawn_err)
    }
    output := process.output(process_handle)
    fmt.println(output)
}

Solution in Rust

use std::process::Command;

let output = Command::new("echo")
    .arg("Hello world")
    .output()
    .expect("Failed to start command")
println!("{output}")
@gordonshamway23
Copy link

There is a procedure "process_start" inside core/os/os2/process.odin. But it is not implemented yet or better the whole os2 folder is under construction.
If you are on windows you could use the "CreateProcessW" procedure in core/sys/windows/kernel32.odin.
If you are on linux there is a "execvp" procedure in core/os/os_linux.odin.
Perhaps that helps.
Maybe there are other alternatives I couldnt find.

@flysand7
Copy link
Contributor

flysand7 commented Mar 25, 2024

This is currently being addressed by the following PR: #3310

If I'm not mistaken the code in the PR practically handles the use-case specified, though it requires a tiny bit more work before I can confidently suggest you copying the code for your own usage.

@github-actions github-actions bot added the stale label Jul 23, 2024
@edyu
Copy link
Contributor

edyu commented Aug 23, 2024

It would be great to have something similar to go's os/exec.

@laytan
Copy link
Collaborator

laytan commented Aug 23, 2024

Like @flysand7 said, this is being worked on in the new os package, currently at core:os/os2 and not intended for general use yet.

The example provided here can be done there with the current functionality like so:

package main

import os "core:os/os2"
import    "core:fmt"

main :: proc() {
	if err := echo(); err != nil {
		os.print_error(os.stderr, err, "failed to execute echo")
	}
}

echo :: proc() -> (err: os.Error) {
	r, w := os.pipe() or_return
	defer os.close(r)

	p: os.Process; {
		defer os.close(w)

		p = os.process_start({
			command = {"echo", "Hello world"},
			stdout  = w,
		}) or_return
	}

	output := os.read_entire_file(r, context.temp_allocator) or_return

	_ = os.process_wait(p) or_return

	fmt.print(string(output))
	return
}

@github-actions github-actions bot added the stale label Dec 21, 2024
@laytan laytan mentioned this issue Jan 17, 2025
61 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants