Implement the UNIX program sleep for xv6; your sleep should pause fora user-specified numberofticks. A tick is a notion oftime defined bythe xv6 kernel, namely thetime between two interrupts fromthe timer chip. Your solution should be inthefile 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
1
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
1
Write a concurrent versionof prime sieve using pipes. This idea is due to Doug McIlroy, inventor of Unix pipes. The picture halfway down this page andthe surrounding text explain how todoit. Your solution should be inthefile user/primes.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
1
Write a simple versionof the UNIX xargs program: read lines from the standard inputand run a command foreachline, supplying the lineas arguments to the command. Your solution should be in the file user/xargs.c.
Write an uptime program that prints the uptime in terms ofticksusingthe uptime system call. (easy)
regular
1
Support regular expressions in name matching for find. grep.c has some primitive support for regular expressions. (easy)
sh:
1
The xv6 shell (user/sh.c) is just another user program and you can improve it. It is a minimal shelland lacks many features found in real shell. For example, modify theshelltonot print a $ when processing shell commands fromafile (moderate), modify theshellto support wait (easy), modify theshellto support lists of commands, separated by";" (moderate), modify theshellto support sub-shells by implementing "("and")" (moderate), modify theshellto support tab completion (easy), modify theshellto keep a history of passed shell commands (moderate), or anything else you would like your shelltodo. (If you are very ambitious, you may have to modify the kernel to support the kernel features you need; xv6 doesn't support much.)