1.孤儿进程:没有父进程的进程,父进程退出,子进程不退出。
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main(int argc, const char *argv[])
{
int a = 0;
pid_t cpid = fork();
printf("%d\n",cpid);
if(0 == cpid)
{
while(1)
{
sleep(2);
printf("child\n");
}
}
return 0;
}
2.僵尸进程:父进程不退出,子进程退出。
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main(int argc, const char *argv[])
{
int a = 0;
pid_t cpid = fork();
printf("%d\n",cpid);
if(0 != cpid)
{
while(1)
{
sleep(2);
printf("child\n");
}
}
return 0;
}
3.外部输入一个路径,要求显示该路径下,所有文件的详细信息,除了隐藏文件。
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <grp.h>
#include <pwd.h>
char *get_filePermission(mode_t mode,char ptr[],long size)
{
if(size<10)
return NULL;
bzero(ptr,size);
char per[]="rwx";
char buf[10]="";
int i=0;
for(i=0;i<9;i++)
{
if((mode &(0400>>i))==0)
buf[i]='-';
else
buf[i]=per[i%3];
}
strcpy(ptr,buf);
return ptr;
}
void get_fileType(mode_t m)
{
switch(m&S_IFMT)
{
case S_IFSOCK:putchar('s');break;
case S_IFLNK:putchar('l');break;
case S_IFREG:putchar('-');break;
case S_IFBLK:putchar('b');break;
case S_IFDIR:putchar('d');break;
case S_IFCHR:putchar('c');break;
case S_IFIFO:putchar('p');break;
default:
printf("提取失败\n");
}
return;
}
void get_fileUsrName(uid_t uid)
{
struct passwd *pwd=getpwuid(uid);
if(NULL==pwd)
{
perror("getpwuid");
return;
}
printf(" %s ",pwd->pw_name);
return;
}
void get_fileGrpName(gid_t gid)
{
struct group *grp=getgrgid(gid);
if(NULL==grp)
{
perror("getgrgid");
return;
}
printf(" %s ",grp->gr_name);
return;
}
void get_fileTime(long a)
{
struct tm *timeA=localtime(&a);
int mon=timeA->tm_mon+1;
char month[10]="";
switch(mon)
{
case 1:strcpy(month,"一月");break;
case 2:strcpy(month,"二月");break;
case 3:strcpy(month,"三月");break;
case 4:strcpy(month,"四月");break;
case 5:strcpy(month,"五月");break;
case 6:strcpy(month,"六月");break;
case 7:strcpy(month,"七月");break;
case 8:strcpy(month,"八月");break;
case 9:strcpy(month,"九月");break;
case 10:strcpy(month,"十月");break;
case 11:strcpy(month,"十一月");break;
case 12:strcpy(month,"十二月");break;
}
printf(" %s %d %d:%d ",month,timeA->tm_hour,timeA->tm_min,timeA->tm_sec);
}
int main(int argc, const char *argv[])
{
DIR *dp=opendir(argv[1]);
if(NULL==dp)
{
perror("opendir");
return -1;
}
struct dirent *rp=NULL;
while(1)
{
rp=readdir(dp);
if(NULL==rp)
{
if( 0 == errno)
{
printf("目录读取完毕\n");
break;
}
else
{
perror("readdir");
return -1;
}
}
if(rp->d_name[0]!='.')
{
struct stat buf;
char arr[50]="";
strcat(arr,argv[1]);
strcat(arr,rp->d_name);
if(stat(arr,&buf)<0)
{
perror("stat");
return -1;
}
char perm[10]="";
get_fileType(buf.st_mode);
get_filePermission(buf.st_mode,perm,sizeof(perm));//原有基础上,封装一个函数
printf(" %s ",perm);
printf(" %lu ",buf.st_nlink);//硬连接数
get_fileUsrName(buf.st_uid);
get_fileGrpName(buf.st_gid);
printf(" %ld ",buf.st_size);//文件大小
get_fileTime(buf.st_ctime);
printf("%s\n",rp->d_name);
}
}
if(closedir(dp)<0)
{
perror("clseodir");
return -1;
}
return 0;
}