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.
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实现素数筛。
解决思路:父进程产生2-35的数,然后子进程按以下算法进行筛选即可。
1 2 3 4 5 6
p = getanumberfrom left neighbor print p loop: n = getanumberfrom left neighbor if (p does notdivide n) send n toright neighbor
intgetline(int fd, char *buf, int max) { int i, cc; char c;
for (i = 0; i + 1 < max;) { cc = read(fd, &c, 1); if (cc < 1) { return0; } if (c == '\n' || c == '\r') break; buf[i++] = c; } buf[i] = '\0'; return i; }
intgetnum(char *buf, int *pos) { int num = 0, i = *pos; while(buf[i] >= '0' && buf[i] <= '9') { num = num * 10 + buf[i] - '0'; i++; } *pos = i - 1; return num; }
intprimer(int read_fd) { char buf[BUFSIZE]; int len = getline(read_fd, buf, BUFSIZE); // printf("%s\n", buf); close(read_fd);
int pipe_fd[2]; pipe(pipe_fd);
int i = 0, first_print_flag = 1; int is_have = 0; int first_num = getnum(buf, &i), num_tmp; printf("prime %d\n", first_num); for (i = 0; i < len; ++i) { if (buf[i] >= '0' && buf[i] <= '9') { num_tmp = getnum(buf, &i); if (num_tmp % first_num == 0) { continue; } is_have = 1; break; } } if (is_have) { if (fork() == 0) { // child close(pipe_fd[1]); primer(pipe_fd[0]); exit(0); } else { close(pipe_fd[0]); for (i = 0; i < len; ++i) { if (buf[i] >= '0' && buf[i] <= '9') { num_tmp = getnum(buf, &i); if (num_tmp % first_num == 0) { continue; } if (first_print_flag) { fprintf(pipe_fd[1], "%d", num_tmp); first_print_flag = 0; } else { fprintf(pipe_fd[1], " %d", num_tmp); } } } fprintf(pipe_fd[1], "\n", num_tmp); close(pipe_fd[1]); wait(0); } } else { close(pipe_fd[0]); close(pipe_fd[1]); } return0; }
intmain(int argc, char *argv[]) { int pipe_fd[2]; // read, write if (argc != 1) { fprintf(2, "Usage: primes\n"); exit(1); }
pipe(pipe_fd); if (fork() == 0) { // child close(pipe_fd[1]); primer(pipe_fd[0]); exit(0); } else { // father close(pipe_fd[0]); for (int i = 2; i <= MAX_SEQ; ++i) { fprintf(pipe_fd[1], "%d", i); if (i != MAX_SEQ) { fprintf(pipe_fd[1], " ", i); } else { fprintf(pipe_fd[1], "\n", i); } } close(pipe_fd[1]); wait(0); }
exit(0); }
task 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.
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.