操作系统基础概念与架构

操作系统基础概念与架构 操作系统基础概念与架构1. 技术分析1.1 操作系统概述操作系统是计算机系统的核心软件操作系统功能 资源管理: CPU、内存、I/O设备 用户接口: 命令行、图形界面 程序执行: 进程管理、调度 文件管理: 文件系统、存储 操作系统类型: 批处理系统 分时系统 实时系统 分布式系统1.2 操作系统架构架构层次 用户层: 用户程序、Shell 系统调用层: API接口 内核层: 核心功能 硬件层: CPU、内存、外设 内核类型: 单内核: Linux 微内核: Minix 混合内核: Windows NT1.3 操作系统分类类型特点适用场景批处理自动执行任务服务器分时多用户交互桌面实时确定性响应嵌入式分布式多节点协同云计算2. 核心功能实现2.1 系统调用#include stdio.h #include unistd.h #include sys/wait.h int main() { pid_t pid fork(); if (pid 0) { printf(子进程: PID%d\n, getpid()); execl(/bin/ls, ls, -l, NULL); perror(execl failed); return 1; } else if (pid 0) { printf(父进程: PID%d, 子进程PID%d\n, getpid(), pid); int status; wait(status); printf(子进程结束状态: %d\n, WEXITSTATUS(status)); } else { perror(fork failed); return 1; } return 0; }2.2 进程控制#include stdio.h #include stdlib.h #include unistd.h #include signal.h void handle_signal(int sig) { printf(收到信号: %d\n, sig); if (sig SIGINT) { printf(程序退出\n); exit(0); } } int main() { signal(SIGINT, handle_signal); signal(SIGTERM, handle_signal); printf(进程PID: %d\n, getpid()); printf(等待信号... (按CtrlC退出)\n); while (1) { sleep(1); printf(运行中...\n); } return 0; }2.3 文件操作#include stdio.h #include stdlib.h #include fcntl.h #include unistd.h int main() { int fd open(example.txt, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd -1) { perror(open failed); return 1; } const char *message Hello, Operating System!\n; ssize_t bytes_written write(fd, message, sizeof(message) - 1); if (bytes_written -1) { perror(write failed); close(fd); return 1; } printf(写入 %zd 字节\n, bytes_written); close(fd); fd open(example.txt, O_RDONLY); if (fd -1) { perror(open failed); return 1; } char buffer[1024]; ssize_t bytes_read read(fd, buffer, sizeof(buffer)); if (bytes_read -1) { perror(read failed); close(fd); return 1; } printf(读取 %zd 字节: %s, bytes_read, buffer); close(fd); return 0; }3. 性能对比3.1 操作系统对比操作系统内核类型稳定性性能Linux单内核高高Windows混合内核中中macOS混合内核高高3.2 内核架构对比架构优点缺点单内核性能高可扩展性差微内核可扩展性好性能较低混合内核平衡复杂度高3.3 文件系统对比文件系统性能可靠性特性ext4高高日志NTFS中高加密ZFS很高很高快照4. 最佳实践4.1 进程管理# 查看进程 ps aux | grep myprocess # 查看进程树 pstree # 终止进程 kill -9 pid # 查看系统负载 top # 查看内存使用 free -h # 查看磁盘使用 df -h4.2 资源监控# CPU使用监控 mpstat # 磁盘I/O监控 iostat # 网络监控 netstat # 进程资源使用 time ./myprogram # 内存映射 cat /proc/pid/maps5. 总结操作系统是计算机系统的基石资源管理高效管理CPU、内存、I/O进程管理创建、调度、终止进程文件系统组织和存储数据系统调用用户程序与内核的接口对比数据如下Linux是服务器领域的首选ext4是Linux的默认文件系统单内核架构性能最优推荐使用top/htop监控系统状态