Skip to content

Instantly share code, notes, and snippets.

@musaprg
Created July 25, 2022 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save musaprg/0c723342d818de02222155624add5054 to your computer and use it in GitHub Desktop.
Save musaprg/0c723342d818de02222155624add5054 to your computer and use it in GitHub Desktop.
親の顔より見たfork-exec
const std = @import("std");
const os = std.os;
const linux = os.linux;
const print = std.debug.print;
fn parent(_: usize) !void {
os.exit(0);
}
fn child() !void {
const child_args = [_:null]?[*:0]const u8{ "/bin/echo", "hello world", null };
const envp = [_:null]?[*:0]const u8{null};
os.execveZ("/bin/echo", &child_args, &envp) catch os.exit(1);
}
pub fn main() !void {
var pid = linux.fork();
if (pid == -1) {
print("fork failed\n", .{});
} else if (pid == 0) {
try child();
} else {
try parent(pid);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment