linux-系统函数

Linux 系统函数详解

Linux 系统函数是用户程序与内核交互的底层接口,通过系统调用(syscall)实现。以下是核心分类及典型函数:

1. 文件操作函数
c 复制代码
#include <fcntl.h>
int open(const char *pathname, int flags, mode_t mode); // 打开文件
ssize_t read(int fd, void *buf, size_t count);          // 读取文件
ssize_t write(int fd, const void *buf, size_t count);  // 写入文件
int close(int fd);                                     // 关闭文件
  • 示例 :复制文件内容

    c 复制代码
    int src = open("a.txt", O_RDONLY);
    int dst = open("b.txt", O_WRONLY | O_CREAT, 0644);
    char buf[1024];
    ssize_t bytes = read(src, buf, sizeof(buf));
    write(dst, buf, bytes);
    close(src); close(dst);
2. 进程控制函数
c 复制代码
#include <unistd.h>
pid_t fork(void);                   // 创建子进程
int execve(const char *pathname, char *const argv[], char *const envp[]); // 执行程序
pid_t waitpid(pid_t pid, int *status, int options); // 等待进程结束
  • 进程创建流程

    c 复制代码
    pid_t pid = fork();
    if (pid == 0) { 
        execl("/bin/ls", "ls", "-l", NULL);  // 子进程执行命令
    } else {
        waitpid(pid, NULL, 0);  // 父进程等待
    }
3. 进程间通信(IPC)
  • 管道

    c 复制代码
    int pipe(int pipefd[2]);  // 创建管道
    // pipefd[0]读端, pipefd[1]写端
  • 共享内存

    c 复制代码
    void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
4. 内存管理函数
c 复制代码
void *malloc(size_t size);     // 动态内存分配
void free(void *ptr);          // 释放内存
int brk(void *addr);           // 调整堆内存边界
void *sbrk(intptr_t increment);
5. 信号处理函数
c 复制代码
#include <signal.h>
int kill(pid_t pid, int sig);          // 发送信号
sighandler_t signal(int signum, sighandler_t handler); // 注册信号处理器
  • 常用信号
    • SIGINT (Ctrl+C)
    • SIGKILL (强制终止)
    • SIGSEGV (段错误)
6. 网络通信函数
c 复制代码
#include <sys/socket.h>
int socket(int domain, int type, int protocol);  // 创建套接字
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); // 绑定地址
int listen(int sockfd, int backlog);             // 监听连接
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); // 接受连接
7. 系统信息函数
c 复制代码
#include <unistd.h>
long sysconf(int name);  // 获取系统配置值
// 示例:获取页大小
long page_size = sysconf(_SC_PAGESIZE);

关键特性

  1. 直接内核交互

    系统调用通过软中断(如 int 0x80syscall 指令)触发内核模式切换。

  2. 错误处理

    失败时返回 -1 并设置 errno

    c 复制代码
    if (open("file", O_RDONLY) == -1) {
        perror("open failed");  // 输出"open failed: No such file or directory"
    }
  3. 性能影响

    上下文切换开销较高,需避免频繁调用(如批量读写替代单字节操作)。

开发建议

  1. 使用 man 2 <函数名> 查看手册(如 man 2 open
  2. 优先使用标准库封装(如 fopen 替代 open)以提升可移植性
  3. 多线程环境下需注意函数可重入性(如用 _r 后缀的线程安全版本)

通过 strace 命令可追踪进程的系统调用:

bash 复制代码
strace -e trace=open,read ./my_program

完整函数列表详见 Linux 内核文档或 syscalls(2) 手册页。

相关推荐
辞旧 lekkk10 小时前
【Qt】信号和槽
linux·开发语言·数据库·qt·学习·mysql·萌新
腾讯蓝鲸智云10 小时前
【运维自动化-节点管理】节点管理的插件策略如何使用
运维·自动化·云计算·sass·paas
疯狂成瘾者12 小时前
服务器的单体和集群
运维·服务器
liuhuizuikeai12 小时前
可视化门禁---Linux/Qt+SqLite篇
linux·运维·qt
初願致夕霞13 小时前
基于系统调用的Linux网络编程——UDP与TCP
linux·网络·c++·tcp/ip·udp
飞Link15 小时前
GPT-5.5 Instant 震撼发布:Realtime-2 API 如何重新定义多模态交互?
人工智能·gpt·microsoft·交互·语音识别
charlie11451419115 小时前
嵌入式Linux驱动开发——新 API 字符设备驱动完整教程 - 从设备结构体到应用测试
linux·运维·驱动开发
飞Link16 小时前
2000 亿砸向算力:字节跳动 AI 基建跨越,后端与运维的“万亿 Token”生死战
运维·人工智能
消失的旧时光-194316 小时前
C语言对象模型系列(四)《Linux 内核里的 container_of 到底是什么黑魔法?》—— 一篇讲透 Linux 内核的“对象模型”核心技巧
linux·c语言·算法
SWAGGY..16 小时前
Linux系统编程:(二)基础指令详解
linux·运维·服务器