IOday4:进程

1、孤儿进程

#include <stdio.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
	//创建一个子进程
	int a = 10; //父进程的栈区
	pid_t cpid = fork(); //拷贝父进程的0-3g用户空间
	if(cpid > 0)
	{
		//只有父进程执行
	printf("father\n");
	sleep(1);
	printf("father exit\n");
	}
	else if(0==cpid)
	{
		//只有子进程能执行	
		printf("child\n");
		sleep(10);//睡10秒等父进程先退出
	
		printf("child exit\n");

	}
	else if(cpid < 0)
	{
		perror("fork");
		return -1;
	}	

	return 0;
}

 

2、僵尸进程

#include <stdio.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
	//创建一个子进程
	pid_t cpid = fork(); //拷贝父进程的0-3g用户空间
	if(cpid > 0)
	{
		//只有父进程执行
	while(1)//死循环执行父进程
	{
	printf("father\n");
	sleep(1);
	}
	}
	else if(0==cpid)
	{
		//只有子进程能执行	
		printf("child\n");
		
	}
	else if(cpid < 0)
	{
		perror("fork");
		return -1;
	}	

	return 0;
}

 

3、外部输入一个路径,要求显示该路径下,所有文件的详细信息,除了隐藏文件。

 

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
/*void Filepermission(mode_t mode){{{
  {
  putchar('-');
  int i=0;
  while(i<9)
  {
  int t=mode&(0400>>i);
  if(0 == i || 3==i || 6==i)
  {
  if(t!=0)
  putchar('r');
  else if(t==0)
  putchar('-');
  }
  else if(1 == i||4 == i||7 == i)
  {
  if(t!=0)
  putchar('w');
  else if(t==0)
  putchar('-');
  }
  else if(2 == i||5 == i||8 == i)	
  {
  if(t!=0)
  putchar('x');
  else if(t==0)
  putchar('-');
  }
  i++;
  }
  return;
  }
  */
/*}}}*/
//在形参位置char* ptr等价于char ptr[]等价于 char ptr[10]
//char ptr[]可以提示用户,需要传入一个char类型数组首地址,容量为10
//char ptr[10] 可以提示用户,需要传入一个char类型数组首地址,容量为10
char* get_filePermission(mode_t mode,char ptr[10],long size)
{
	//判断输入的数组容量是否大于等于10,如果不是,则返回失败情况
	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;
}
/*char get_fileType(mode_t m)
  {
  char n;
  if (S_ISREG(m))
  {
  n='-';
  }
  else if(S_ISDIR(m))
  {
  n='d';
  }
  else if(S_ISCHR(m))
  {
  n='c';
  }
  else if(S_ISBLK(m))
  {
  n='b';
  }
  else if(S_ISFIFO(m))
  {
  n='p';
  }
  else if(S_ISLNK(m))
  {
  n='l';
  }
  else if(S_ISSOCK(m))
  {
  n='s';
  }
  return n;
  }*/
char get_fileType(mode_t m)
{
	char n;
	switch(m&S_IFMT)
	{
	case S_IFSOCK:
		n='s';
		break;
	case S_IFLNK:
		n='l';
		break;
	case S_IFREG:
		n='-';
		break;
	case S_IFDIR:
		n='d';
		break;
	case S_IFCHR:
		n='c';
		break;
	case S_IFBLK:
		n='b';
		break;
	case S_IFIFO:
		n='p';
		break;
defalut:
		printf("提取失败\n");
	}
	return n;
}
int main(int argc, const char *argv[])
{
	if(argc<2)
	{
		fprintf(stderr,"请输入文件名:");
		return -1;
	}
	DIR* dp = opendir(argv[1]);
	if(NULL == dp)
	{
		perror("opendir");
		return -1;
	}
	struct dirent *rp=NULL;
	while(1)
	{
		int i=0;
		rp = readdir(dp);
		if(NULL == rp)//判断rp是否为空
		{

			if(0==errno)
			{
				printf("目录读取完毕\n");
				break;
			}
			else
			{
				perror("readdir");
				return -1;
			}


		}
		char *p=rp->d_name;//rp不为空定义一个指针P指向rp
		if(rp->d_name[0] == '.')
			continue;
		struct stat buf;
		char arr1[20]="";
		strcpy(arr1,argv[1]);
		if(stat(strcat(arr1,rp->d_name),&buf)<0)
		{
			perror("stat");
			return -1;
		}
		//文件类型及权限
		//	printf("mode:0%o\n",buf.st_mode);
		//Filepermission(buf.st_mode);
		char perm[10] = "";
		get_filePermission(buf.st_mode,perm,sizeof(perm));
		char n=get_fileType(buf.st_mode);
		printf("%c%s",n,perm);
		//硬链接数
		printf(" %lu",buf.st_nlink);
		//文件所属用户
		//	printf(" %d",buf.st_uid);
		struct passwd* pwd = getpwuid(buf.st_uid);
		if(NULL==pwd)
		{
			perror("getpwuid");
			return -1;
		}
		printf(" %s",pwd->pw_name);
		//文件所属组用户
		//printf(" %d",buf.st_gid);
		struct group* grp = getgrgid(buf.st_gid);
		if(NULL==grp)
		{
			perror("getgrgid");
			return -1;
		}
		printf(" %s",grp->gr_name);

		//文件大小
		printf("  %ld",buf.st_size);
		//日期
		char *str[12]={"一月","二月","三月","四月","五月","六月","七月",\
			"八月","九月","十月","十一月","十二月"};
		struct tm *info=NULL;
		info = localtime(&buf.st_ctime);
		printf(" %s %d %02d:%02d",\
				str[info->tm_mon],info->tm_year-100,\
				info->tm_hour,info->tm_min);
		printf(" %s\n",rp->d_name);
	}
		return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值