目录
1.要求输入目录的路径后,能够打印出指定路径下所有文件的详细信息,类似ls -l
2. 用父子进程拷贝一张图片,用文件IO实现,要求 子进程拷贝后半部分,父进程拷贝前半部 分,按照cpu调度机制同时执行
1.要求输入目录的路径后,能够打印出指定路径下所有文件的详细信息,类似ls -l
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <string.h>
#include <grp.h>
#include <time.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
//获取文件权限
void get_filemode(mode_t m)
{
int i,p = 0400;
char arr[64] = "";
char brr[4] = "rwx";
for(i=0;i<9;i++){
if((m&(p>>i))==0){
arr[i] = '-';
continue;
}
arr[i] = brr[i%3];
}
printf("%s ",arr);
}
//获取文件类型
void get_filetype(mode_t m)
{
if(S_ISREG(m)){
putchar('-');
}
if(S_ISDIR(m)){
putchar('d');
}
if(S_ISCHR(m)){
putchar('c');
}
if(S_ISBLK(m)){
putchar('b');
}
if(S_ISFIFO(m)){
putchar('p');
}
if(S_ISLNK(m)){
putchar('l');
}
if(S_ISSOCK(m)){
putchar('s');
}
return;
}
//获取当前文件用户名
void get_fileUname(uid_t uid)
{
struct passwd* name;
name = getpwuid(uid);
if(name==NULL){
perror("getpwuid");
return ;
}
char arr[20] = "";
strcpy(arr,name->pw_name);
printf("%s ",arr);
return ;
}
//获取文件所属组用户名
void get_fileGname(gid_t gid)
{
struct group* name;
name = getgrgid(gid);
if(name == NULL){
perror("getgrgid");
return ;
}
char arr[20] = "";
strcpy(arr,name->gr_name);
printf("%s ",arr);
}
//获取文件时间戳
void get_filetime(time_t time)
{
struct tm* p = NULL;
p = localtime(&time);
switch(p->tm_mon+1){
case 1:
printf("一 ");
break;
case 2:
printf("二 ");
break;
case 3:
printf("三 ");
break;
case 4:
printf("四 ");
break;
case 5:
printf("五 ");
break;
case 6:
printf("六 ");
break;
case 7:
printf("七 ");
break;
case 8:
printf("八 ");
break;
case 9:
printf("九 ");
break;
case 10:
printf("十 ");
break;
case 11:
printf("十一 ");
break;
case 12:
printf("十二 ");
break;
}
printf("%2d %2d:%02d ",p->tm_mday,p->tm_hour,
p->tm_min);
return ;
}
int main(int argc, const char *argv[])
{
DIR* fp;
fp = opendir(argv[1]);
if(fp==NULL){
perror("opendir");
return -1;
}
char str[64] = "";
while(1){
struct dirent* fn;
fn = readdir(fp);
if(fn==NULL){
if(errno==0){
printf("文件提取完毕\n");
break;
}
}
strcpy(str,argv[1]);
strcat(str,fn->d_name);
struct stat buf;
if(stat(str,&buf)){
perror("stat");
return -1;
}
//文件类型及权限
// printf("mode = %o\n",buf.st_mode);
get_filetype(buf.st_mode);
get_filemode(buf.st_mode);
//文件硬链接个数
printf("%2ld ",buf.st_nlink);
//文件当前用户
get_fileUname(buf.st_uid);
//文件所属组用户
get_fileGname(buf.st_gid);
//文件大小
printf("%5ld ",buf.st_size);
//文件时间戳
get_filetime(buf.st_atim.tv_sec);
//文件名
printf("%s/\n",fn->d_name);
}
closedir(fp);
return 0;
}
运行示例
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[])
{
pid_t pid;
pid = fork();
int fp;
sleep(1);
fp = open(argv[2],O_RDONLY);
if(fp<0){
perror("open");
return -1;
}
int fn;
fn = open(argv[1],O_RDWR | O_CREAT | O_TRUNC,0660);
if(fn<0){
perror("open");
return -1;
}
int i;
off_t num = lseek(fp,0,SEEK_END);
if(pid>0){
// sleep(5);
lseek(fp,0,SEEK_SET);
lseek(fn,0,SEEK_SET);
char a ='0';
for(i=0;i<num/2;i++){
read(fp,&a,1);
write(fn,&a,1);
}
printf("前半段拷贝完毕!\n");
}else if(pid==0){
lseek(fp,num/2,SEEK_SET);
lseek(fn,num/2,SEEK_SET);
char arr[64] ="";
ssize_t res;
while((res = read(fp,arr,sizeof(arr)))!=0){
write(fn,arr,res);
bzero(arr,res);
}
printf("后半部分拷贝完毕!\n");
}
close(fp);
close(fn);
return 0;
}
运行示例
3.创建一个孤儿进程,创建一个僵尸进程;
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
pid_t pid1 = fork();
if(pid1>0){//父
pid_t pid2 = fork();
if(pid2==0){//子2
sleep(10);
printf("pid2:%d ppid:%d\n",getpid(),getppid());
}else{//父
sleep(5);
printf("pid1:%d pid2:%d ppid:%d\n",
pid1,pid2,getpid());
}
}else if(pid1==0){//子1
printf("pid1:%d ppid:%d\n",getpid(),getppid());
}
return 0;
}
运行示例