作业-11.10

1、要求输入目录的路径后,能够打印出指定路径下所有文件的详细信息,类似ls -l

#include <stdio.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
#include <errno.h>

int printf_file_mes(const char file[], int num);
void get_file_permission(unsigned int tmp_mod, char file_mod[], int num);
char get_file_type(unsigned int tmp_filetype);

int main(int argc, const char *argv[])
{
    if(argc < 2)
    {
        fprintf(stderr, "请在命令行输入文件名\n");
        return -1;
    } //打开目录
    DIR *dp = opendir(argv[1]);
    if(NULL == dp)
    {
        perror("opendir");
        return -1;
    }
    int i = 0;
    struct dirent *rp = NULL;
    char filepathname[256]="0";
    while(1)
    {
        rp = readdir(dp);
        if(NULL == rp)
        {
            if(0 == errno)
            {
                printf("目录读取完毕\n");
                break;
            } else
            {
                perror("readdir");
                return -1;
            }
        }
        bzero(filepathname, sizeof(filepathname));
        strcat(filepathname, argv[1]);
        strcat(filepathname, rp->d_name);
        int num = strlen(argv[1]);
        printf_file_mes(filepathname, num);
    }
    
    //关闭目录
    if(closedir(dp) < 0)
    {
        perror("closedir");
        return -1;
    }
    return 0;
}
void get_file_permission(unsigned int tmp_mod, char file_mod[], int num)
{
    bzero(file_mod,num);
    unsigned int flag = 1<<9;
    char str[4]="rwx";
    for(int i=0; i<9; i++)
    {
        flag >>= 1;
        if(!(tmp_mod & flag))
        {
            file_mod[i] = '-';
        } else
        {
            file_mod[i] = str[i%3];
        }
    }
}
char get_file_type(unsigned int tmp_filetype)
{
    char file_type[8] = "pcdb-ls";
    unsigned int num_filetype[7] = {010,020,040,060,0100,0120,0140};
    for(int i=0; i<8; i++)
    {
        if(0 == (tmp_filetype ^ num_filetype[i]))
        {
            return file_type[i];
        }
    }
}
int printf_file_mes(const char file[], int num)
{
    struct stat buf;
    if(lstat(file, &buf) < 0)
    {
        perror("stata");
        return -1;
    }

    //文件的类型和权限
    char file_mod[10]; //将buf.st_mode中的文件权限提取出来
    unsigned int tmp_mod = buf.st_mode & ~((~1)<<10);
    unsigned int tmp_filetype = (buf.st_mode & S_IFMT) >> 9;
    char c = get_file_type(tmp_filetype);
    printf("%c", c);

    //将文件权限由数字转化为字母
    get_file_permission(tmp_mod, file_mod, 10);

    //打印转换后的文件权限
    printf("%s ", file_mod);

    //硬连接数
    printf("%4ld ", buf.st_nlink);

    //文件所属用户
    //printf("%d ", buf.st_uid);
    struct passwd *tmp_usname = getpwuid(buf.st_uid);
    printf("%s ", tmp_usname->pw_name);

    //文件所属组名
    struct group *tmp_groupname = getgrgid(buf.st_gid);
    printf("%s ", tmp_groupname->gr_name);

    //文件大小
    printf("%10ld ", buf.st_size);

    //时间
    time_t t = buf.st_ctime;
    struct tm *tmp_time = localtime(&t);
    printf("%02d %02d %02d:%02d ", tmp_time->tm_mon+1, tmp_time->tm_mday, \

                tmp_time->tm_hour\ , tmp_time->tm_min);


    printf("%s\n", file+num);


    return 0;
}

(1)使用相对路径

(2)使用绝对路径

2、用父子进程拷贝一张图片,用文件IO实现,要求 子进程拷贝后半部分,

父进程拷贝前半部分,按照cpu调度机制同时执行

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
    if(argc < 3)
    {
        fprintf(stderr, "请从命令行输入图片名和复制后的文件名\n");
        return -1;
    }
    if(!strcmp(argv[1], argv[2]))
    {
        fprintf(stderr, "复制后的文件名和原文件名不能相同!!!\n");
        return -1;
    }

    //打开文件
    int fd = open(argv[1], O_RDONLY);
    int fd_w = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0777);
    if(fd < 0)
    {
        perror("open");
        return -1;
    }
    if(fd_w < 0)
    {
        perror("open");
        return -1;
    }
    ssize_t res = 0;
    ssize_t res_w = 0;
    char arr[128] = "0";
    int len = sizeof(arr);
    int count = 0;
    count = (lseek(fd, 0, SEEK_END)/len);
    count++;
    printf("count = %d.\n", count);

    //创建子进程
    pid_t pid = fork();
    if(pid < 0)
    {
        perror("fork");
        return -1;
    }

    //父进程复制前半部分
    if(!pid)
    {
        sleep(2);
        printf("子进程\n");

        //将指针偏移到子进程写入数据的末尾
        lseek(fd, (count/2)*len, SEEK_SET);
        lseek(fd_w, (count/2)*len, SEEK_SET);
        while(1)
        {
            bzero(arr, sizeof(arr));
            res = read(fd, arr , len);
            if(!res)
            {
                break;
            } else if(res < 0)
            {
                perror("read");
                return -1;
            }
            res_w = write(fd_w, arr, res);
            if(res_w < 0)
            {
                perror("write");
                return -1;
            }
        }

        //关闭父进程中的文件读取和写入
        close(fd);
        close(fd_w);
        putchar(10);
    } else {


        //子进程复制后半部分
        printf("父进程\n");
        int tmp = count;

        //将指针偏移到文件开头
        lseek(fd , 0, SEEK_SET);
        lseek(fd_w, 0, SEEK_SET);
        while(1)
        {
            bzero(arr, sizeof(arr));
            res = read(fd, arr , len);
            if(res < 0)
            {
                perror("read");
                return -1;
            }

            //写入数据
            res_w = write(fd_w, arr, res);
            if(res_w < 0)
            {
                perror("write");
                return -1;
            }

            //所需要的拷贝次数--
            count -= 1;

            //如果拷贝的数据比总数据的一半就退出循环
            if(count <= (tmp/2))
            {
                break;
            }
        }

        //关闭子进程中的文件读取和写入
        close(fd);
        close(fd_w);
    }


    return 0;
}

(1)复制前文件列表

(2)复制后文件列表

(3)复制的图片打开

3、创建一个孤儿进程,创建一个僵尸进程

(1)孤儿进程

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
    int jc = fork();
    if(!jc)
    {
        while(1)
        {
            printf("child:pid = %d,ppid=%d\n",getpid(),getppid());
            sleep(5);
        }
    }

    sleep(5);
    return 0;
}

(2)僵尸进程

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
    int jc = fork();
    if(!jc)
    {
        exit(0);
    } else
    {
        while(1);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. spyder 5.4.1 requires pyqt5<5.16, which is not installed. spyder 5.4.1 requires pyqtwebengine<5.16, which is not installed. Successfully installed aiofiles-23.1.0 altair-4.2.2 blinker-1.6.2 cachetools-5.3.1 chardet-5.1.0 cmake-3.26.3 cpm_kernels-1.0.11 fastapi-0.95.2 ffmpy-0.3.0 gitdb-4.0.10 gitpython-3.1.31 gradio-3.32.0 gradio-client-0.2.5 h11-0.14.0 httpcore-0.17.2 httpx-0.24.1 latex2mathml-3.76.0 linkify-it-py-2.0.2 lit-16.0.5 markdown-it-py-2.2.0 mdit-py-plugins-0.3.3 mdtex2html-1.2.0 mdurl-0.1.2 nvidia-cublas-cu11-11.10.3.66 nvidia-cuda-cupti-cu11-11.7.101 nvidia-cuda-nvrtc-cu11-11.7.99 nvidia-cuda-runtime-cu11-11.7.99 nvidia-cudnn-cu11-8.5.0.96 nvidia-cufft-cu11-10.9.0.58 nvidia-curand-cu11-10.2.10.91 nvidia-cusolver-cu11-11.4.0.1 nvidia-cusparse-cu11-11.7.4.91 nvidia-nccl-cu11-2.14.3 nvidia-nvtx-cu11-11.7.91 orjson-3.8.14 protobuf-3.20.3 pydantic-1.10.8 pydeck-0.8.1b0 pydub-0.25.1 pygments-2.15.1 pympler-1.0.1 python-multipart-0.0.6 rich-13.4.1 semantic-version-2.10.0 sentencepiece-0.1.99 smmap-5.0.0 starlette-0.27.0 streamlit-1.22.0 streamlit-chat-0.0.2.2 torch-2.0.1 transformers-4.27.1 triton-2.0.0 tzlocal-5.0.1 uc-micro-py-1.0.2 uvicorn-0.22.0 validators-0.20.0 websockets-11.0.3 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv 解释下
06-02

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值