模仿shell指令 ls -l
#include <head.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
void get_filePermission(mode_t m, char *ptr)
{
bzero(ptr, sizeof(ptr));
char str[4] = "rwx";
for (int i = 0; i < 9; i++)
{
if ((m & (0400 >> i)))
ptr[i] = str[i % 3];
else
ptr[i] = '-';
}
return;
}
void getFileType(mode_t m, char *a)
{
switch (m & S_IFMT)
{
case S_IFBLK:
*a = 'b';
break;
case S_IFCHR:
*a = 'c';
break;
case S_IFDIR:
*a = 'd';
break;
case S_IFIFO:
*a = 'p';
break;
case S_IFLNK:
*a = 'l';
break;
case S_IFREG:
*a = '-';
break;
case S_IFSOCK:
*a = 's';
break;
default:
printf("unknown?\n");
break;
}
}
int main(int argc, const char *argv[])
{
IF(2);
struct stat st;
struct tm *tm;
char ptr[10];
char a;
char name[30];
DIR *dp = opendir(argv[1]);
if (dp == NULL)
{
PRINT_ERR(argv[1]);
}
struct dirent *rp = NULL;
while ((rp = readdir(dp)) != NULL)
{
if (rp->d_name[0] != '.')
{
strcpy(name,argv[1]);
strcat(name,rp->d_name);
if (stat(name, &st))
PRINT_ERR("get file stat error");
get_filePermission(st.st_mode, ptr);
getFileType(st.st_mode, &a);
printf("%c%s%3ld %s %s %7ld", a,
ptr, st.st_nlink, getpwuid(st.st_uid)->pw_name,
getgrgid(st.st_gid)->gr_name, st.st_size);
if ((tm = localtime(&st.st_atim.tv_sec)) == NULL)
PRINT_ERR("change time error");
switch (tm->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;
default:
break;
}
printf("%2d %02d:%02d %s\n",
tm->tm_mday, tm->tm_hour, tm->tm_min,rp->d_name);
}
}
closedir(dp);
return 0;
}
用父子进程拷贝一张图片,用文件IO实现,要求子进程拷贝后半部分,父进程拷贝前半部分,按照cpu调度机制同时执行
#include <head.h>
#include <sys/stat.h>
#include <fcntl.h>
void copy(int fd, int fd1, int start, int len)
{
lseek(fd, start, SEEK_SET);
lseek(fd1, start, SEEK_SET);
char buf[100] = " ";
int ret;
int count = 0;
while ((ret = (read(fd, buf, sizeof(buf)))) != 0)
{
count += ret;
if (count >= len)
{
write(fd1, buf, ret - count + len);
return;
}
write(fd1, buf, ret);
}
return;
}
int main(int argc, const char *argv[])
{
;
int fd, fd1;
IF(3);
if ((fd1 = open(argv[2], O_CREAT | O_TRUNC, 0666)) == -1)
PRINT_ERR("fd1");
close(fd1);
lseek(fd, 0, SEEK_SET);
pid_t pid;
pid = fork();
if (pid == -1)
{
PRINT_ERR("fork error");
}
else if (pid == 0)
{
//子进程
if ((fd = open(argv[1], O_RDONLY)) == -1)
PRINT_ERR("fd");
if ((fd1 = open(argv[2], O_WRONLY)) == -1)
PRINT_ERR("fd1");
int size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
copy(fd, fd1, size / 2, size / 2);
printf("后半部分拷贝完毕\n");
}
else
{
//父进程
if ((fd = open(argv[1], O_RDONLY)) == -1)
PRINT_ERR("fd");
if ((fd1 = open(argv[2], O_WRONLY)) == -1)
PRINT_ERR("fd1");
int size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
copy(fd, fd1, 0, size - size / 2);
printf("前半部分拷贝完毕\n");
}
close(fd);
close(fd1);
return 0;
}
创建孤儿进程
#include <head.h>
int main(int argc, const char* argv[])
{
pid_t pid;
pid = fork();
if (pid == -1) {
PRINT_ERR("fork error");
} else if (pid == 0) {
sleep(1);
printf("child:pid = %d,ppid=%d\n",getpid(),getppid());
} else {
printf("parent:pid = %d,cpid = %d,ppid=%d\n",getpid(),pid,getppid());
}
return 0;
}
创建僵尸进程
#include <head.h>
int main(int argc, const char* argv[])
{
pid_t pid;
pid = fork();
if (pid == -1) {
PRINT_ERR("fork error");
} else if (pid == 0) {
printf("child:pid = %d,ppid=%d\n",getpid(),getppid());
} else {
printf("parent:pid = %d,cpid = %d,ppid=%d\n",getpid(),pid,getppid());
while (1)
sleep(1);
}
//共同代码区
printf("这是父子进程共同的代码区\n");
return 0;
}