Linux系统调用之目录操作

Linux系统调用之目录操作

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>


int main(void)
{
    int fd;
    int flag;

    // 测试字符串
    char *p = "我们是一个有中国特色的社会主义国家!!!!!!";
    char *q = "呵呵, 社会主义好哇。。。。。。";


    // 只写的方式打开文件
    fd = open("test.txt", O_WRONLY);
    if(fd == -1)
    {
        perror("open");
        exit(1);
    }

    // 输入新的内容,该部分会覆盖原来旧的内容
    if(write(fd, p, strlen(p)) == -1)
    {
        perror("write");
        exit(1);
    }

    // 使用 F_GETFL 命令得到文件状态标志
    flag = fcntl(fd, F_GETFL, 0);
    if(flag == -1)
    {
        perror("fcntl");
        exit(1);
    }

    // 将文件状态标志添加 ”追加写“ 选项
    flag |= O_APPEND;
    // 将文件状态修改为追加写
    if(fcntl(fd, F_SETFL, flag) == -1)
    {
        perror("fcntl -- append write");
        exit(1);
    }

    // 再次输入新内容,该内容会追加到旧内容的后面
    if(write(fd, q, strlen(q)) == -1)
    {
        perror("write again");
        exit(1);
    }

    // 关闭文件
    close(fd);

    return 0;
}

文件描述符的拷贝dup2.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main()
{
    int fd = open("english.txt", O_RDWR);
    if(fd == -1)
    {
        perror("open");
        exit(1);
    }

    int fd1 = open("a.txt", O_RDWR);
    if(fd1 == -1)
    {
        perror("open");
        exit(1);
    }

    printf("fd = %d\n", fd);
    printf("fd1 = %d\n", fd1);

    int ret = dup2(fd1, fd);
    if(ret == -1)
    {
        perror("dup2");
        exit(1);
    }
    printf("current fd = %d\n", ret);
    char* buf = "主要看气质 ^_^!!!!!!!!!!\n";
    write(fd, buf, strlen(buf));
    write(fd1, "hello, world!", 13);

    close(fd);
    close(fd1);
    return 0;
}

文件描述符的拷贝dup

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main()
{
    int fd = open("a.txt", O_RDWR);
    if(fd == -1)
    {
        perror("open");
        exit(1);
    }

    printf("file open fd = %d\n", fd);

    // 找到进程文件描述表中 ==第一个== 可用的文件描述符
    // 将参数指定的文件复制到该描述符后,返回这个描述符
    int ret = dup(fd);
    if(ret == -1)
    {
        perror("dup");
        exit(1);
    }
    printf("dup fd = %d\n", ret);
    char* buf = "你是猴子派来的救兵吗????\n";
    char* buf1 = "你大爷的,我是程序猿!!!\n";
    write(fd, buf, strlen(buf));
    write(ret, buf1, strlen(buf1));

    close(fd);
    return 0;
}

获得目录下文件的个数(递归计算)

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int getFileNum(char* root)
{
        //打开指定的路径
        DIR* dir=opendir(root);
        if(dir ==NULL)
        {
                perror("open dir");
                exit(0);
        }

        //递归读取文件
        int total=0;
        char path[1024]={0};
        struct dirent* ptr=NULL;
        while((ptr = readdir(dir)) !=NULL )
        {
                //忽略掉.和..
                if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0 )
                {
                        continue;
                }
                //if is regular file
                if(ptr->d_type==DT_REG)
                {
                        total ++;
                }
                //如果是目录
                if(ptr->d_type==DT_DIR)
                {
                        sprintf(path,"%s/%s",root,ptr->d_name);
                        total+=getFileNum(path);
                }

        }
        closedir(dir);
        return total;
}

int main(int argc,char* argv[])
{
        int total=getFileNum(argv[1]);
        printf("%s has file number: %d\n",argv[1],total);
        return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Linux系统调用号是指操作系统提供给用户程序调用的接口函数的编号。每个系统调用都有一个唯一的调用号,用于标识该函数。在Linux中,系统调用号是通过一个整数来表示的,不同的系统调用对应不同的整数值。用户程序可以通过系统调用号来调用相应的系统调用,从而实现对操作系统的各种功能的访问和控制。常见的Linux系统调用包括open、read、write、close、fork、execve等。 ### 回答2: Linux系统调用号是一个唯一的标识符,用于标识操作系统提供给用户程序调用的各种功能和服务。通过系统调用,用户程序可以请求操作系统执行特定的操作,如打开文件、创建进程、读取网络数据等。 在Linux中,每个系统调用都有一个特定的号码,这个号码是由操作系统内核分配的。这些号码是在系统的头文件中定义的,例如unistd.h文件中包含了系统调用号的定义。 系统调用号在调用系统调用时使用,用户程序可以使用相关的系统调用接口来执行操作系统提供的功能。用户程序通常会使用C语言的库函数封装系统调用,以提供更方便的接口给开发者使用。 系统调用号的分配通常是由操作系统的开发者决定的,他们会根据不同的功能和服务进行划分和分配。在Linux中,常见的系统调用号包括打开文件(open)、读取文件(read)、写入文件(write)、关闭文件(close)等。 系统调用号的使用可以在用户程序中通过系统调用指令实现,用户程序将需要执行的系统调用号存放在相应的寄存器中,并调用int 0x80或sysenter指令触发系统调用。 总之,Linux系统调用号是一种用于标识和调用操作系统功能的机制,它允许用户程序直接访问操作系统提供的各种服务和功能。这种机制使得用户程序可以与操作系统交互,实现更加强大和灵活的应用程序开发。 ### 回答3: Linux系统调用号是用于在用户空间程序和内核空间之间进行交互的接口标识符。当用户空间程序需要执行某些操作时,如创建进程、读写文件、网络通信等,就会调用相应的系统调用系统调用号是一个整数,每个系统调用都有一个唯一的号码与之对应。Linux内核通过系统调用号来识别用户空间程序请求的具体操作系统调用号由内核定义并存储在一个表中,用户程序通过中断指令或软中断指令触发系统调用,将调用号传递给内核。 对于不同的操作,有不同的系统调用号。例如: 1. 创建进程的系统调用号是`fork`,对应的调用号是2; 2. 打开文件的系统调用号是`open`,对应的调用号是5; 3. 写入文件的系统调用号是`write`,对应的调用号是1; 4. 进程退出的系统调用号是`exit`,对应的调用号是60。 用户程序通过指定正确的系统调用号,将自己的请求传递给内核。内核收到请求后,根据调用号执行相应的操作,完成后再返回结果给用户程序。系统调用号的定义与使用遵循一定的规范,保证了用户程序与内核之间的正确通信和操作,是Linux系统中非常重要的一个概念。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值