任务1:fork前创建一个int a,父子进程中是否都有变量a,虚拟地址是否相同,物理地址是否相同
任务2:fork函数后,在父进程中int b,父子进程中是否都有变量b,虚拟地址是否相同,物理地址是否相同
任务3:fork函数后,在子进程中int c,父子进程中是否都有变量c,虚拟地址是否相同,物理地址是否相同
1.都有变量a,虚拟地址相同,物理地址不同
2.在父进程中定义与子进程无关
3.在子进程中定义与父进程无关
实现 ls -l 的功能
#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
#include<errno.h>
#include<string.h>
#include<sys/stat.h>
#include<unistd.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>void get_filetype(mode_t m)
{if(S_ISREG(m))putchar('-');else if(S_ISDIR(m))putchar('d');else if(S_ISCHR(m))putchar('c');else if(S_ISBLK(m))putchar('b');else if(S_ISFIFO(m)) putchar('p');else if(S_ISLNK(m))putchar('l');else if(S_ISSOCK(m))putchar('s');return;
}void get_filePermission(mode_t m)
{char str[] = "rwx";int i;for(i=0;i<9;i++){if((m & (0400>>i))==0){putchar('-');continue;}putchar(str[i%3]);}return;
}int main(int argc,const char *argv[])
{if(argc < 2){printf("请输入参数\n");return -1;}DIR* dp = opendir(argv[1]);if(NULL==dp){perror("opendir");return -1;}struct dirent * rp = NULL;struct passwd * gp = NULL;struct group * gp1 = NULL;struct tm * tp = NULL; struct stat buf;char str[128] = "";while(1){rp = readdir(dp);if(NULL==rp){if(0==errno){break;}else{perror("readdir");return -1;}}if(rp->d_name[0]=='.')continue;strcpy(str,argv[1]);strcat(str,rp->d_name);int fp = stat(str,&buf);if(fp < 0){perror("stat");return -1;}//文件类型get_filetype(buf.st_mode);//文件权限get_filePermission(buf.st_mode);putchar(' ');//printf("%d ",buf.st_nlink);//文件所属用户名称gp = getpwuid(buf.st_uid);printf("%s ",gp->pw_name);//文件所属组用户名称gp1 = getgrgid(buf.st_gid);printf("%s ",gp1->gr_name);//文件大小printf("%6ld ",buf.st_size);//时间tp = localtime(&buf.st_ctime);printf("%d-%02d-%02d %02d:%02d:%02d ",\tp->tm_year+1900,tp->tm_mon+1,tp->tm_mday,\tp->tm_hour,tp->tm_min,tp->tm_sec);//文件名称printf("%s\n",rp->d_name);}closedir(dp);return 0;
}