Part A of the first assignment had four user programs to be implemented-
-
uptime.c: Write a program to print the up time of the xv6 virtual machine. Use the wrapper around the uptime system call. Add
$U/_uptime
toUPROGS
list in xv6-riscv/Makefile. If you run "make qemu" without double quotes, it will compileuptime.c
and bring up the virtual machine. Now if you runls
, you should see uptime in that list. You can test your implementation by running uptime at the$
prompt of xv6. [5 points] -
forksleep.c: This program takes two integers
m
andn
as command line arguments.m
can take any positive integer value andn
can be 0 or 1. You should check these conditions. The main process forks a child. Ifn
is zero, the child process should sleep form
ticks (achieved by callingsleep(m)
) and then should print out "pid: Child." without the double quotes where pid can be obtained by callinggetpid()
. In this case, the parent process should print out "pid: Parent." without the double quotes immediately after returning fromfork()
. Ifn
is one, the parent process should sleep form
ticks after returning fromfork()
and then print out "pid: Parent.", while in this case, the child process should print out "pid: Child." immediately without sleeping. As usual, test your program by booting the virtual machine and running forksleep at the$
prompt. You should see the prints appearing in opposite orders for two different input values ofn
. Two sample outputs are shown below. [8 points]$ forksleep 10 0 3: Parent. 4: Child. $
$ forksleep 10 1 6: Child. 5: Parent. $
-
pipeline.c: This program takes two integers
n
andx
as command line arguments.n
must be a positive integer, whilex
can take any integer value. You should check these. The program creates a pipeline ofn
processes (including the top-level process that starts themain
function). Each process adds itspid
tox
and passes the new value ofx
to the next process in the pipeline using apipe
. Each process, after incrementingx
, prints "pid: x". Make sure to close a file descriptor after you are done using it; otherwise you will run out of file descriptors for largen
. Also, ensure that a process waits for all its children to complete and apply this rule recursively. Two sample outputs are given below. [12 points]$ pipeline 10 0 7: 7 8: 15 9: 24 10: 34 11: 45 12: 57 13: 70 14: 84 15: 99 16: 115 $
$ pipeline 1 100 18: 118 $
-
primefactors.c: This program takes an integer
n
in the range [2, 100] as command line argument and prints the prime factors ofn
with multiplicity. You may assume that a global array of all primes in the range [2, 100] is given to you as follows.int primes[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
The program creates a pipeline of processes. The first process (top-level) divides
n
as many times as possible by 2, passes on the end-result to the next process through a pipe, and prints out 2 those many times followed by its pid. The next process divides n as many times as possible by the next prime number from the primes array, passes on the end-result to the next process through a pipe, and prints out that prime number those many times followed by itspid
. The procedure continues untiln
drops to 1. A few sample outputs are shown below. In each line, the number within the square brackets is the pid of the process responsible for printing that line. [15 points]$ primefactors 90 2, [3] 3, 3, [4] 5, [5] $
$ primefactors 65 5, [17] 13, [20] $