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.
int main(int argc, char *argv[]) { int wait = 0; if(argc <= 1){ printf("sleep: need parameter error\n"); exit(0); } wait = atoi(argv[1]); sleep(wait); exit(0); }
2. pingpong
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 "<pid>: received ping", where <pid> 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 "<pid>: received pong", and exit. Your solution should be in the file user/pingpong.c.
int main(int argc, char *argv[]) { int p1[2]; int p2[2]; if(argc > 1){ fprintf(2,"Only 1 argument is needed!\n"); exit(1); }
// creat a pipe pipe(p1); pipe(p2); // child if(fork() == 0) { char buffer[32]; //read from the parent close(p1[1]); close(p2[0]); read(p1[0],buffer,4); close(p1[0]); // write a byte to the parent printf("%d: received ping\n",getpid()); write(p2[1],"pong", 4); close(p2[1]); } else { char buffer[32]; // send a byte close(p1[0]); close(p2[1]); write(p1[1],"ping",4); close(p1[1]); // receive from child read(p2[0],buffer,4); printf("%d: received pong\n",getpid()); close(p2[0]); } exit(0); }
3. 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.
pipe(p); for(int i = 2; i <= 35; ++i){ write(p[1],&i,4); } close(p[1]); process(p); close(p[0]); exit(0); }
4. find
Write a simple version of the UNIX find program: find all the files in a directory tree with a specific name. Your solution should be in the file user/find.c.
// open the dir if((fd = open(path, 0)) < 0){ fprintf(2, "ls: cannot open %s\n", path); return; } // open stat if(fstat(fd, &st) < 0){ fprintf(2, "ls: cannot stat %s\n", path); close(fd); return; } // check the current the file is if(st.type != T_DIR){ fprintf(2, "the current the file is not dictionary\n", path); close(fd); return; }
//read all the files under the dir strcpy(buf, path); p = buf + strlen(buf); *p++ = '/'; while(read(fd, &de, sizeof(de)) == sizeof(de)){ if(de.inum == 0) continue; // skip "." and ".." if(strcmp(de.name,".") == 0) continue; if(strcmp(de.name,"..") == 0) continue;
curr = p; memmove(curr, de.name, DIRSIZ); curr[DIRSIZ] = 0; if(stat(buf, &st) < 0){ printf("ls: cannot stat %s\n", buf); continue; } // printf("current file name is:%s \n",buf); // record the current file switch(st.type){ // we check the current file case T_FILE: fname = getname(buf); if(strcmp(fname,filename) == 0){ printf("%s\n",buf); } break; // we check the current dir case T_DIR: if(strlen(path) + 1 + DIRSIZ + 1 > sizeof(buf)){ printf("ls: path too long\n"); break; } find(buf,filename); break; } }
close(fd); }
int main(int argc, char *argv[]) { if(argc != 3){ printf("we need 3 paramters!\n"); exit(0); } find(argv[1],argv[2]); exit(0); }
5. xargs
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.
Write an uptime program that prints the uptime in terms of ticks using the uptime system call. (easy)
regular
Support regular expressions in name matching for find. grep.c has some primitive support for regular expressions. (easy)
sh:
The xv6 shell (user/sh.c) is just another user program and you can improve it. It is a minimal shell and lacks many features found in real shell. For example, modify the shell to not print a $ when processing shell commands from a file (moderate), modify the shell to support wait (easy), modify the shell to support lists of commands, separated by ";" (moderate), modify the shell to support sub-shells by implementing "(" and ")" (moderate), modify the shell to support tab completion (easy), modify the shell to keep a history of passed shell commands (moderate), or anything else you would like your shell to do. (If you are very ambitious, you may have to modify the kernel to support the kernel features you need; xv6 doesn't support much.)