xv6-examples-输出helloworld

任务:在控制台输出 "hello world"。

  • 使用 printf
  • 使用 fork 和 write
  • 使用 dup 和 write

使用 printf

helloworld.c

c 复制代码
#include "user/user.h"

int main(void){
    printf("hello world\n");
    return 0;
}

使用 fork 和 write

helloworld2.c

c 复制代码
#include "user/user.h"

int main(void){
    int pid = fork();
    if (pid == 0) {
        write(1, "hello ", 6);
        exit(0);
    }else {
        wait(0);
        write(1, "world\n ", 6);
    }
    return 0;
}

使用 dup 和 write

helloworld3.c

c 复制代码
#include "user/user.h"

int main(void){
    int fd = dup(1);
    write(1, "hello ", 6);
    write(fd, "world\n", 6);
    return 0;
}
相关推荐
2402_882893868 天前
Linux进程控制(上):fork、写时拷贝、进程终止与进程等待,一篇吃透!
linux·进程·fork·写时拷贝
2401_872418788 天前
操作系统|进程管理、线程、系统调用、进程间通信与信号
操作系统·pcb·os
花 满 楼9 天前
RTA-OS 任务调度机制深度拆解:从 alarm 到期到任务切换的全链路(PowerPC e200)
os·嵌入式软件·汽车电子
我不会起名字32211 天前
Redis RDB 落盘时的 fork 停顿:COW 机制与 3 个降低影响的配置
redis·持久化·fork·cow·rdb
wunaiqiezixin22 天前
MIT 6.S081 util 实验篇(lab1):primes (moderate/hard)
linux·unix·os·xv6·mit6.s081
半仙白桑1 个月前
内核篇第一讲:linux创建进程
linux·fork·创建进程
wunaiqiezixin1 个月前
MIT 6.S081 file system 实验篇(lab9):Large files (moderate)
linux·unix·os·xv6·mit6.s081
TechEdu2026062 个月前
[操作系统]操作系统04:进程间通信(IPC)-机制、模式与实践
操作系统·os
消失的旧时光-19432 个月前
Android 系统层扫盲 05:Android 开机后发生了什么?从 Bootloader 到 Launcher
android·zygote·fork·aosp·cow
深念Y2 个月前
Fork 仓库瘦身指南:从 90MB 到 11MB
git·github·仓库·历史·瘦身·fork·上游