2023/2/24 进程练习:
1.孤儿进程:没有父进程的进程,父进程退出,子进程不退出。
#include "head.h"
int main(int argc, const char *argv[])
{
pid_t cpid = fork();
if(0 == cpid)
{
sleep(1);
printf("cpid= %d ppid = %d 子进程\n",cpid,getppid());
}else if(cpid > 0)
{
printf("cpid = %d 父进程\n",cpid);
exit(0);
}else if(cpid < 0)
{
ERR_MSG("fork");
return -1;
}
while(1)
sleep(1);
return 0;
}
2.僵尸进程:父进程不退出,子进程退出。
#include "head.h"
int main(int argc, const char *argv[])
{
pid_t cpid = fork();
if(cpid > 0)
{
printf("cpid = %d 父进程\n",cpid);
}else if(0 == cpid)
{
printf("cpid = %d ppid = %d 子进程\n",cpid,getppid());
exit(0);
}else if(cpid < 0)
{
ERR_MSG("fork");
return -1;
}
while(1)
sleep(1);
return 0;
}
3.外部输入一个路径,要求显示该路径下,所有文件的详细信息,除了隐藏文件。
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <dirent.h>
#include <errno.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0);
#endif
main.c
#include "head.h"
char *getfilePermiss(mode_t mode,char *dest)
{
int i=0,n=0;
int temp = 0400;
while(i<3)
{
(mode & temp)?(dest[n++]='r'):(dest[n++]='-');
temp = temp>>1;
(mode & temp)?(dest[n++]='w'):(dest[n++]='-');
temp = temp>>1;
(mode & temp)?(dest[n++]='x'):(dest[n++]='-');
temp = temp>>1;
i++;
}
dest[n]='\0';
return dest;
}
char get_filetype(mode_t mode,char *dest)
{
switch((mode & S_IFMT))
{
case S_IFREG:*dest = '-';break;
case S_IFCHR:*dest = 'c';break;
case S_IFBLK:*dest = 'b';break;
case S_IFDIR:*dest = 'd';break;
case S_IFIFO:*dest = 'p';break;
case S_IFLNK:*dest = 'l';break;
case S_IFSOCK:*dest = 's';break;
}
return *dest;
}
char *get_fileusername(uid_t uid)
{
struct passwd *pw = getpwuid(uid);
if(NULL == pw)
{
ERR_MSG("getpwuid");
return NULL;
}
return pw->pw_name;
//return dest;
}
char *get_filegroupname(gid_t gid)
{
struct group *gr = getgrgid(gid);
if(NULL == gr)
{
ERR_MSG("getgrgid");
return NULL;
}
return gr->gr_name;
//return dest;
}
int main(int argc, const char *argv[])
{
DIR* dp = opendir(argv[1]);
if(NULL == dp)
{
ERR_MSG("opendir");
return -1;
}
int i = 0;
struct dirent *rp = NULL;
while(1)
{
rp = readdir(dp);
if(NULL == rp)
{
if(0 == errno)
{
printf("目录读取完毕!\n");
break;
}else
{
ERR_MSG("readdir");
return -1;
}
}
if(rp->d_name[0] != '.')
{
struct stat buf;
char file[32] = "";
strcat(file,argv[1]);
strcat(file,rp->d_name);
if(stat(file,&buf) < 0)
{
ERR_MSG("stat");
return -1;
}
char dest[10]="";
char type = get_filetype(buf.st_mode,&type);
char *mode = getfilePermiss(buf.st_mode,dest);
unsigned long nlink = buf.st_nlink;
char *uname = get_fileusername(buf.st_uid);
char *gname = get_filegroupname(buf.st_gid);
long size = buf.st_size/1024;
char *mon[12] = {"一月","二月","三月","四月",\
"五月","六月","七月","八月",\
"九月","十月","十一月","十二月"};
struct tm *info = localtime(&buf.st_ctime);
char *month = mon[info->tm_mon];
int hour = info->tm_hour;
int min = info->tm_min;
int sec = info->tm_sec;
// printf("%c%s %lu %s %s %ld %s %02d:%02d:%02d %s\n",\
type,mode,nlink,uname,gname,size,month,hour,\
min,sec,rp->d_name);
printf("%c%s %lu %-6s %-6s %3ldK %3s %02d:%02d:%02d %s\n", \
type, mode, nlink, uname, gname, size, month,\
hour, min, sec, rp->d_name);
}
}
if(closedir(dp) < 0)
{
ERR_MSG("closedir");
return -1;
}
return 0;
}