std::penggunaan sys
std::sys ialah lapisan abstraksi OS di bawah modul peringkat tinggi.
std(high-level)
-> sys dispatcher
-> sys/linux or sys/macos
-> syscall
peraturan asas
- Kebanyakan fungsi mengembalikan nilai syscall mentah.
>= 0berjaya,< 0gagal (-errno).- Dalam kod apl peringkat tinggi, jika boleh, gunakan
std::sys,std::netdanstd::timedahulu dan bukannyastd::env.
1) Contoh bacaan fail (std::sys::fs)
import("std::sys::fs");
fun main() {
var fd: i64 = open("/etc/hosts", 0, 0);
if (fd < 0) {
return;
}
var buf: array<u8, 256>;
var n: i64 = read(fd, &buf[0], 256);
close(fd);
}
2) Contoh soket (std::sys::socket)
import("std::sys::socket");
fun main() {
var fd: i64 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd < 0) {
return;
}
shutdown(fd, SHUT_RDWR);
}
3) Contoh ingatan (std::sys::memory)
import("std::sys::memory");
fun main() {
var p: ptr<u8> = sys_alloc(4096);
if (p == null) {
return;
}
sys_free(p, 4096);
}
modul penghantar
std::sys::socketstd::sys::fsstd::sys::envstd::sys::memorystd::sys::processstd::sys::timestd::sys::tty
Cawangan #[target(os="linux")], #[target(os="macos")] hanya digunakan di dalam penghantar.
