#include"kernel/types.h"#include"user/user.h"intmain(intargc,charconst*argv[]){/* code */if(argc<=1){fprintf(2,"error: you must type a number!\n");}elseif(argc==2){intnumber=atoi(argv[1]);sleep(number);}exit(0);}
代码没啥难度,确实是easy
pingpong(easy)
1
2
3
4
intfork()//Create a process, return child's PID
intpipe(intp[])//Create a pipe, put read/write file descriptors in p[0] and [1]
intread(intfd,char*buf,intn)//Readnbyteintobuf;returnsnumberread;or0ifendoffileintwrite(intfd,char*buf,intn)//Writenbytesfrombuftofiledescriptorfd;returnsn
#include"kernel/types.h"#include"kernel/stat.h"#include"user/user.h"#include"kernel/fs.h"char*fmtname(char*path){staticcharbuf[DIRSIZ+1];char*p;// Find first character after last slash.
for(p=path+strlen(path);p>=path&&*p!='/';p--);p++;// Return blank-padded name.
if(strlen(p)>=DIRSIZ)returnp;memmove(buf,p,strlen(p));memset(buf+strlen(p),' ',DIRSIZ-strlen(p));buf[strlen(p)]=0;returnbuf;}voidfind(char*path,char*name){charbuf[512],*p;intfd;structdirentde;structstatst;if((fd=open(path,0))<0){fprintf(2,"ls: cannot open %s\n",path);return;}if(fstat(fd,&st)<0){fprintf(2,"ls: cannot stat %s\n",path);close(fd);return;}switch(st.type){caseT_FILE:if(!strcmp(fmtname(path),name)){printf("%s\n",path);}break;caseT_DIR:if(strlen(path)+1+DIRSIZ+1>sizeofbuf){printf("ls: path too long\n");break;}strcpy(buf,path);p=buf+strlen(buf);*p++='/';while(read(fd,&de,sizeof(de))==sizeof(de)){if(de.inum==0)continue;memmove(p,de.name,DIRSIZ);p[DIRSIZ]=0;if(!strcmp(de.name,".")||!strcmp(de.name,"..")){continue;}find(buf,name);}break;}close(fd);}intmain(intargc,char*argv[]){find(argv[1],argv[2]);return0;}
就使用ls的递归写法就好,虽然不太习惯他的写法。
xargs(moderate)
要实现的命令的功能如下:
1
2
3
4
5
6
7
8
9
10
11
12
$ echo hello too | xargs echo bye
bye hello too
$ mkdir a
$ echo hello > a/b
$ mkdir c
$ echo hello > c/b
$ echo hello > b
$ find . b | xargs grep hello
$ $ $ $ $ $ hello
hello
hello