任务1:fork前创建一个int a,父子进程中是否都有变量a,虚拟地址是否相同,物理地址是否相同
任务2:fork函数后,在父进程中int b,父子进程中是否都有变量b,虚拟地址是否相同,物理地址是否相同
任务3:fork函数后,在子进程中int c,父子进程中是否都有变量c,虚拟地址是否相同,物理地址是否相同
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
int a=10;
pid_t cpid = fork();
printf("cpid = %d\n",cpid);
printf("a的地址:%p\n",&a);
if(cpid>0)
{
int b = 20;
printf("b=%d\n",b);
printf("b的地址:%p\n",&b);
}
if(cpid==0)
{
int c =30;
printf("c=%d\n",c);
printf("c的地址:%p\n",&c);
}
while(1)
sleep(1);
return 0;
}
1 fork前创建int a 父子进程中都有a 虚拟地址相同 物理地址不同
2 fork后 在父中int b 父中有 但子中没有
3 fork 后 子中int c 子中含c 父中不含
cpid = 14607
a的地址:0x7ffc83ff51ac
b=20
b的地址:0x7ffc83ff51b0
cpid = 0
a的地址:0x7ffc83ff51ac
c=30
c的地址:0x7ffc83ff51b0
作业4:完成ls -l
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
void getFilemission(mode_t m)
{
for(int i = 0;i<9;i++)
{
if((m & (0400>>i))==0)
{
putchar('-');
continue;
}
switch(i%3)
{
case 0:
putchar('r');
break;
case 1:
putchar('w');
break;
case 2:
putchar('x');
break;
}
}
}
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;
}
int main(int argc, const char *argv[])
{
if(argc<=1)
{
printf("请输入路径\n");//fprintf(stderr,"请输入路径\n");
return -1;
}
DIR *fp = opendir(argv[1]);
if(NULL == fp)
{
perror("opendir:");
return -1;
}
struct dirent *fp1 ={NULL};
while(1)
{
fp1 = readdir(fp);
if(fp1 == NULL)
{
if(errno == 0)
{
break;
}
else
{
perror("readdir:");
return -1;
}
}
char arr[32] ="";
strcpy(arr,argv[1]);
strcat(arr,fp1->d_name);
struct stat a;
if(stat(arr,&a)<0)
{
perror("stat:");
return -1;
}
// printf("mode:%o\n",a.st_mode);
get_fileType(a.st_mode);
getFilemission(a.st_mode);
printf(" %2ld ",a.st_nlink);
struct passwd* pwd = getpwuid(a.st_uid);
if(NULL == pwd)
{
perror("getpwuid:");
return -1;
}
printf("%s ",pwd->pw_name);
// printf("uid:%d gid:%d\n",a.st_uid,a.st_gid);
struct group* grp = getgrgid(a.st_gid);
if(NULL == grp)
{
perror("getgrgid:");
return -1;
}
printf("%s ",grp->gr_name);
printf("%7ld",a.st_size);
// printf("%ld ",a.st_atime);
struct tm *t = NULL;
t = localtime(&a.st_atime);
if(NULL == t)
{
perror("localtime:");
return -1;
}
printf("%2d月 %d %02d:%02d",\
t->tm_mon+1,t->tm_mday,\
t->tm_hour,t->tm_min);
printf(" %s\n",fp1->d_name);
}
return 0;
}
jieguo
ubuntu@ubuntu:~/io/day3$ ./a.out ./
drwxrwxr-x 6 ubuntu ubuntu 4096 5月 5 09:01 ..
-rw-rw-r-- 1 ubuntu ubuntu 1987168 5月 4 18:54 1.png
-rwxrwxrwx 1 ubuntu ubuntu 129 5月 4 22:00 open.txt
-rw-rw-r-- 1 ubuntu ubuntu 500 5月 4 13:32 open.c
-rwxrwxr-x 1 ubuntu ubuntu 13128 5月 5 20:33 a.out
-rw-rw-r-- 1 ubuntu ubuntu 199 5月 5 16:16 zuihou.c
-rw-rw-r-- 1 ubuntu ubuntu 1153 5月 5 19:59 1.c
-rw-rw-r-- 1 ubuntu ubuntu 779 5月 4 19:04 tupiancopy.c
-rw-rw-r-- 1 ubuntu ubuntu 133 5月 5 09:46 open1.txt
-rwxrwxrwx 1 ubuntu ubuntu 3974336 5月 4 19:00 11.png
-rw-rw-r-- 1 ubuntu ubuntu 1190 5月 5 09:28 stat.c
drwxrwxr-x 2 ubuntu ubuntu 4096 5月 5 20:33 .
-rw-rw-r-- 1 ubuntu ubuntu 752 5月 4 14:37 read.c
-rw-rw-r-- 1 ubuntu ubuntu 654 5月 4 21:21 chule.mulu.c
-rw-rw-r-- 1 ubuntu ubuntu 2094 5月 5 20:33 quanxain.c
-rw-rw-r-- 1 ubuntu ubuntu 620 5月 5 11:22 muluwenjianduqu.c
-rw-rw-r-- 1 ubuntu ubuntu 462 5月 4 19:07 lseek.c
-rw-rw-r-- 1 ubuntu ubuntu 535 5月 4 19:20 opendir.c
-rw------- 1 ubuntu ubuntu 385024 5月 4 19:24 core