2.24 作业

  1. 孤儿进程:没有父进程的进程,父进程退出,子进程不退出。

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, const char *argv[])
{
    pid_t cpid=fork();
    if(cpid)
    {
        return -1;
    }
    else
    {
        while(1)
        {
            sleep(1);
        }
    }
    
    return 0;
}
  1. 僵尸进程:父进程不退出,子进程退出。

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, const char *argv[])
{
    pid_t cpid=fork();
    if(!cpid)
    {
        return -1;
    }
    else
    {
        while(1)
        {
            sleep(1);
        }
    }
    
    return 0;
}
  1. 外部输入一个路径,要求显示该路径下,所有文件的详细信息,除了隐藏文件。

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

#define MY_ERROR_MSG_R1(msg) {perror(msg);return -1;}
#define MY_ERROR_MSG(msg) {perror(msg);return;}
char *month[]={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};

void ll(const char *pathname,const char *name);
void get_filePermission(mode_t mode);
char get_fileType(mode_t mode);
void get_fileUsrName(uid_t uid);
void get_fileGrpName(gid_t gid);

int main(int argc, const char *argv[])
{
    DIR *dp=opendir(argv[1]);
    char dir[20];
    strcpy(dir,argv[1]);
    int n=strlen(dir);
    if(dir[n-1]!='/')
    {
        dir[n++]='/';
        dir[n]='\0';
    }
    if(!dp)
        MY_ERROR_MSG_R1("opendir");

    struct dirent *rp=NULL;
    while (1)
    {
        rp=readdir(dp);
        if(!rp)
        {
            if(!errno)
                break;
            else
                MY_ERROR_MSG_R1("readdir");
        }
        if(rp->d_name[0]!='.')
        {
            ll(strcat(dir,rp->d_name),rp->d_name);
            dir[n]='\0';
        }
    }
    
    closedir(dp);
    return 0;
}

void ll(const char *bathname,const char *name)
{    
    struct stat buf;
    if(stat(bathname,&buf)<0)
    {
        MY_ERROR_MSG("stat");
    }

    get_filePermission(buf.st_mode);
    get_fileType(buf.st_mode);
    printf("%lu ",buf.st_nlink);
    get_fileUsrName(buf.st_uid);
    get_fileGrpName(buf.st_gid);
    printf("%ld ",buf.st_size);

    time_t t=buf.st_ctime;
    struct tm *tmp=localtime(&t);
    printf("%s %02d %02d:%02d %s\n",\
            month[tmp->tm_mon],tmp->tm_mday,tmp->tm_hour,\
            tmp->tm_min,name);
}

void get_filePermission(mode_t mode)
{
    char per[]="rwx";
    char ptr[10];
    for(int i=0;i<9;i++)
    {
        if(mode>>(8-i)&1)
        {
            ptr[i]=per[i%3];
        }else
        {
            ptr[i]='-';
        }
    }
    ptr[9]='\0';
    printf("%s ",ptr);
}


char get_fileType(mode_t mode)
{
    switch(mode&S_IFMT)
    {
    case S_IFSOCK:return 's';
    case S_IFLNK:return 'l';
    case S_IFREG:return '-';
    case S_IFBLK:return 'b';
    case S_IFDIR:return 'd';
    case S_IFCHR:return 'c';
    case S_IFIFO:return 'p';
    default:
                 printf("类型提取失败");
    }
}

void get_fileUsrName(uid_t uid)
{
    struct passwd *pwd=getpwuid(uid);
    if(!pwd) MY_ERROR_MSG("getpwuid");
    printf("%s ",pwd->pw_name);
}

void get_fileGrpName(gid_t gid)
{
    struct group *grp=getgrgid(gid);
    if(!grp) MY_ERROR_MSG("getgrgid");
    printf("%s ",grp->gr_name);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值