美文网首页
系统 第三天 access 进程

系统 第三天 access 进程

作者: ie大博 | 来源:发表于2016-12-01 11:30 被阅读0次

    access判断文件是否存在

    #include<unistd.h>
    #include<stdio.h>
    int main(int argc,char *argv[])
    {
        int ret =-1;
        ret = access(argv[1],F_OK);//文件是否存在
            //R_OK,W_OK,X_OK可读,可写可执行
        if(-1 == ret)
        {
            perror("access");//返回相应的错误信息
            return -1;
        }
        else if(0 == ret)
        {
            printf("file exist\n");
        }
        return 0;
    }
    

    opendir :打开目录句柄

    #include <stdio.h>
    #include <string.h>
    /*stat()*/
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    /*opendir()*/
    #include <sys/types.h>
    #include <dirent.h>
    
    int main(int argc, char *argv[])
    {
        //打开指定的目录,打开失败返回NULL,并设置错误号
        //成功返回一个指向目录的指针
        DIR *pDir = opendir(argv[1]);
        if (NULL == pDir)
        {
            perror("opendir:");
            return -1;
        }
        printf("opendir ok\n");
        struct dirent *pDirent = NULL;
        struct stat fileStat;
        int ret = -1;
        pDirent = readdir(pDir);
        while (NULL != pDirent)
        {
            printf("%s ", pDirent->d_name);
            ret = stat(pDirent->d_name, &fileStat);
            if (0 == ret)
            {
                switch (fileStat.st_mode & S_IFMT)
                {
                case S_IFREG:
                    printf("这是一个普通的文件\n");
                    break;
                case S_IFDIR:
                    printf("这是一个目录的文件\n");
                    break;
                default:
                    printf("其他类型的文件\n");
                    break;
                }
            }
            else if (-1 == ret)
            {
                perror("stat");
                break;
            }
            pDirent = readdir(pDir);
        }
        printf("\n");
    
        //返回到目录的头部
        //rewinddir(pDirent);
        //创建一个目录
        //mkdir(pathname, mode);
        //删除一个目录
        //rmdir(pDrient);
    
        closedir(pDir);
    
        return 0;
    }
    

    进程

    • ps -A:显示所有进程
    • kill - 9 +进程号
    • top:查看进程的运行状态
    • fork函数:创建进程
    • ./a.out &:其意义隐藏运行的内容

    相关文章

      网友评论

          本文标题:系统 第三天 access 进程

          本文链接:https://www.haomeiwen.com/subject/phzzpttx.html