任务1: fork前创建一个int a,父子进程中是否都有变量a,虚拟地址是否相同,物理地址是否相同
#include
#include
#include
int main(){
int a=10;
pid_t cpid = fork();
if(cpid<0){
printf("error in fork!");
}else if(cpid == 0){
printf("子进程中a=%d\n",a);
printf("子进程中a的虚拟地址:%p\n",&a);
printf("i am a child process\n");//由子进程打印
}else{
a=20;//在父进程中更改a的值
printf("父进程中a=%d\n",a);
printf("父进程中a的虚拟地址:%p\n",&a);
printf("i am a parent process\n");//由父进程打印
}
return 0;
}
运行结果:
父进程中a=20
父进程中a的虚拟地址:0x7fffb35a9860
i am a parent process
子进程中a=10
子进程中a的虚拟地址:0x7fffb35a9860
i am a child process该结果说明父子进程中都含有a变量,虚拟地址是相同的.在父进程中更改a的值后,子进程中a的结果并未发生改变,因此父子进程中a的物理地址是不同的.
任务2: fork函数后,在父进程中int b,父子进程中是否都有变量b,虚拟地址是否相同,物理地址是否相同
#include
#include
#include
int main(){
pid_t cpid = fork();
if(cpid<0){
printf("error in fork!");
}else if(cpid == 0){
printf("子进程中b=%d\n",b);
printf("子进程中b的虚拟地址:%p\n",&b);
printf("i am a child process\n");//由子进程打印
}else{
int b=20;//在父进程中更改b的值
printf("父进程中b=%d\n",b);
printf("父进程中b的虚拟地址:%p\n",&b);
printf("i am a parent process\n");//由父进程打印
}
return 0;
}
main.c: In function ‘main’:
main.c:2703:31: error: ‘b’ undeclared (first use in this function)
printf("子进程中b=%d\n",b);
^
main.c:2703:31: note: each undeclared identifier is reported only once for each function it appears in该结果说明只有父进程含有b变量,子进程中不含有b变量,程序无法通过编译.
任务3: fork函数后,在子进程中int c,父子进程中是否都有变量c,虚拟地址是否相同,物理地址是否相同
#include
#include
#include
int main(){
pid_t cpid = fork();
if(cpid<0){
printf("error in fork!");
}else if(cpid == 0){
int c=10;
printf("子进程中b=%d\n",c);
printf("子进程中b的虚拟地址:%p\n",&c);
printf("i am a child process\n");//由子进程打印
}else{
c=20;//在父进程中更改c的值
printf("父进程中b=%d\n",c);
printf("父进程中b的虚拟地址:%p\n",&c);
printf("i am a parent process\n");//由父进程打印
}
return 0;
}
main.c: In function ‘main’:
main.c:2708:3: error: ‘c’ undeclared (first use in this function)
c=20;//在父进程中更改c的值
^
main.c:2708:3: note: each undeclared identifier is reported only once for each function it appears in
该结果说明只有子进程含有c变量,父进程中不含有c变量,程序无法通过编译.
作业四
#include
#include
#include
#include
#include
#include
#include
#include
#include
int permissionInfo(mode_t mode){
int m=0400;
for(int i=0;i<3;i++){
if(mode &m)
putchar('r');
else
putchar('-');
m=m>>1;
if(mode &m)
putchar('w');
else
putchar('-');
m=m>>1;
if(mode &m)
putchar('x');
else
putchar('-');
m=m>>1;
}
}
//获取文件类型
void getFileType(mode_t m){
if(S_ISREG(m))
putchar('-');
else if(S_ISDIR(m))
putchar('d');
else if(S_ISCHR(m))
putchar('c');
}
int main(int argc, const char *argv[]){
//利用readdir获取文件名
DIR *dir=opendir("../program_DS");
if(dir==NULL){
printf("打开失败\n");
return -1;
}
struct dirent *dirname=NULL;
struct stat buf;
while(1){
dirname=readdir(dir);
if(NULL==dirname){
if(0==errno){
printf("目录文件读取完毕\n");
break;
}else
{
perror("readdir");
return -1;
}
}
//检测到隐藏文件夹则不打印
if(dirname->d_name[0]!='.'){
if(stat(dirname->d_name,&buf)<0)
{
perror("stat");
return -1;
}
//文件类型
getFileType(buf.st_mode);
//文件权限
permissionInfo(buf.st_mode);
putchar('\t');
//文件硬链接
printf("link:%ld\t",buf.st_nlink);
// printf("uid:%d gid:%d\n",buf.st_uid,buf.st_gid);
//用户名
struct passwd *pwd = getpwuid(buf.st_uid);
if(NULL== pwd){
perror("getpwuid");
return -1;
}
printf("uid:%s ",pwd->pw_name);
//组用户名
struct group *grp=getgrgid(buf.st_gid);
if(NULL==grp){
perror("getgrgid");
return -1;
}
printf("gid:%s ",grp->gr_name);
//文件大小
printf("%10ld ",buf.st_size);
//时间
// printf("time:%ld\n", buf.st_ctime);
struct tm *info=NULL;
info=localtime(&buf.st_ctime);
if(info==NULL) printf("获取失败");
// printf("%d",info->tm_year+1900);
printf("%02d",info->tm_mon+1);
printf("-%02d",info->tm_mday);
printf(" %02d",info->tm_hour);
printf(":%02d",info->tm_min);
printf(":%02d",info->tm_sec);
putchar('\t');
printf("%s\n",dirname->d_name);
}
}
closedir(dir);
return 0;
}
运行结果
-rwxrwxr-x link:1 uid:ubuntu gid:ubuntu 294460 05-04 14:02:50 pic1.png
-rw-rw-r-- link:1 uid:ubuntu gid:ubuntu 1343 05-01 11:39:46 pro.c
-rwxrwxrwx link:1 uid:ubuntu gid:ubuntu 1343 05-04 13:52:31 open.txt
-rwxrwxr-x link:1 uid:ubuntu gid:ubuntu 13088 05-05 20:29:01 a.out
drwxr-xr-x link:2 uid:ubuntu gid:ubuntu 4096 05-05 19:41:04 file1
-rw-rw-r-- link:1 uid:ubuntu gid:ubuntu 18870 05-03 11:06:34 fun.c
-rw-rw-r-- link:1 uid:ubuntu gid:ubuntu 189 05-03 18:49:19 usr.txt
-rw-rw-r-- link:1 uid:ubuntu gid:ubuntu 1342 04-29 16:24:08 text.txt
-rw-rw-r-- link:1 uid:ubuntu gid:ubuntu 45204 05-05 20:28:58 main.c
-rw-rw-r-- link:1 uid:ubuntu gid:ubuntu 0 04-27 13:50:52 fopen.txt
drwxr-xr-x link:2 uid:ubuntu gid:ubuntu 4096 05-05 19:41:13 file2
-rw-rw-r-- link:1 uid:ubuntu gid:ubuntu 6836 05-03 11:06:34 head.h
-rw-rw-r-- link:1 uid:ubuntu gid:ubuntu 294460 05-04 13:59:49 pic.png
-rw-rw-r-- link:1 uid:ubuntu gid:ubuntu 12 04-29 15:30:52 fwrite.txt
-rw-rw-r-- link:1 uid:ubuntu gid:ubuntu 140 04-27 18:58:05 passwordNote.txt
-rw------- link:1 uid:ubuntu gid:ubuntu 380928 04-17 13:41:00 core
目录文件读取完毕