Created
July 25, 2022 15:00
-
-
Save musaprg/0c723342d818de02222155624add5054 to your computer and use it in GitHub Desktop.
親の顔より見たfork-exec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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