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) 手册页。

相关推荐
男孩李14 分钟前
浅谈Linux的last命令
java·linux·服务器
yunqi18 分钟前
ELK日志平台架构详解:从Filebeat采集到Elasticsearch检索的企业级日志方案实践
linux
手揽回忆怎么睡40 分钟前
Ubuntu 22.04 64位安装MinIO
linux·前端·ubuntu
阳光九叶草LXGZXJ42 分钟前
达梦数据库-报错-11-cmd 13 validate error
linux·运维·数据库·sql·学习
~光~~1 小时前
【嵌入式linux学习_OV8858 bring up】L1_从Sensor到RK3588发生了什么
linux·运维·学习
运维大师1 小时前
【K8S 运维实战】32-GitOps实践ArgoCD
运维·kubernetes·argocd
Darkwanderor1 小时前
使用管道实现进程间通信 (IPC, Inter-Process Communitation)
linux·c语言·c++
Nuanyt1 小时前
Linux系统的 Shell 常见指令和常见知识点(以bash为主)
linux·运维·chrome·bash
程序员老陆1 小时前
FFmpeg 硬解在 Linux 上:从“能跑”到“跑满 GPU”的实战笔记
linux·笔记·ffmpeg
星恒随风1 小时前
Linux 权限详解:从 rwx、chmod 到 umask、目录权限与粘滞位
linux·运维·服务器·笔记·学习