Implement the UNIX program sleep for xv6; your sleep should pause for a user-specified number of ticks. A tick is a notion of time defined by the xv6 kernel, namely the time between two interrupts from the timer chip. Your solution should be in the file user/sleep.c.
Write a program that uses UNIX system calls to ‘’ping-pong’’ a byte between two processes over a pair of pipes, one for each direction. The parent should send a byte to the child; the child should print “: received ping”, where is its process ID, write the byte on the pipe to the parent, and exit; the parent should read the byte from the child, print “: received pong”, and exit. Your solution should be in the file user/pingpong.c.
intmain(int argc,char * argv[]) { int fd1[2],fd2[2]; pipe(fd1); pipe(fd2); int pid = fork();
// child // print "<pid>: received ping" // write back to parent a byte and exit // close fd[1] // zero for read and 1 for read if(pid==0) { close(fd1[1]);//close write close(fd2[0]);//close read char buf[3]; read(fd1[0],&buf,1);//read a byte from parent printf("%d: received ping\n",getpid()); write(fd2[1],&buf,1);//write back to parent exit(0); }
// parent // send a byte to the child // close fd[0] else { close(fd1[0]);//close read close(fd2[1]);//close write char buf[3]; write(fd1[1],"A",1);// send a byte to the child read(fd2[0],&buf,1); printf("%d: received pong\n",getpid()); } exit(0); }
primes
Write a concurrent version of prime sieve using pipes. This idea is due to Doug McIlroy, inventor of Unix pipes. The picture halfway down this page and the surrounding text explain how to do it. Your solution should be in the file user/primes.c.
Your goal is to use pipe and fork to set up the pipeline. The first process feeds the numbers 2 through 35 into the pipeline. For each prime number, you will arrange to create one process that reads from its left neighbor over a pipe and writes to its right neighbor over another pipe. Since xv6 has limited number of file descriptors and processes, the first process can stop at 35.
Write a simple version of the UNIX find program: find all the files in a directory tree whose name matches a string. Your solution should be in the file user/find.c.
Write a simple version of the UNIX xargs program: read lines from the standard input and run a command for each line, supplying the line as arguments to the command. Your solution should be in the file user/xargs.c.
对于管道连接的命令,以find . b | xargs grep hello为例,argv[0]是 xargs,argv[1]是grep,一共有三个参数,如果想要读取find . b,那么需要从标准输入中读取,也即是使用read函数读取fd为0时的数据