cs133 Lab Diary: Thomas Cowley CS133 Lab Diary - Thomas Cowley, u1807654 Toggle navigation Lab 3 Go to lab ReflectionLab 1Lab 2 Lab 3Lab 4Lab 5Lab 6 Lab 7 Lab 3 Processes Everything that is considered an application, e.g. the editor atom, can be considered as a process. To find out the list of processes running, I used the ps command. Without any additional options or arguments, ps will output the current user’s launched processes selectively from controlling terminals. To display a total list of processes, including other users’ processes, including those without controlling terminals [8], the -A flag can be used. ps -A The top command can be used to see the activity associated with each currently running process [1]. For continued use of the terminal, you can launch a process in the background. This is done by post-fixing an ampersand: atom&. The output of the ps command can be piped through to the grep command using the pipe operator. This can then be used to find, and output instances of a given word or regex pattern: ps | grep atom Processes can be send a message to terminate using the pkill command with the argument of the process name to kill. This can be dangerous in occasions where multiple processes have identical names, as all processes with this name will be sent a termination message. This may not always be the desired behaviour. A preferred method of process termination is via the kill command, which operates on the process’ ID. The process ID (PID) of the given process must first be found. ps | grep process kill
Scripting With Bash I made the runloop script executable by me, and readable by all other users. Note that I have softlinked cs133 to my home directory; its complete path is in ~/public_html. I then executed the script. chmod 744 ~/cs133/lab3/runloop ~/cs133/lab3/runloop When I ran the runloop bash script, it compiled the Loop.java file and then ran the created class file. This java program contained a while(true) statement within it, leading to an infinite loop. Due to the process scheduler attempting to optimize computation, it maximises CPU usage and this can be seen when loading up the program top [24]. I then wrote a bash script, called product-of-two, that performed multiplication on two given arguments. I then improved the script by adding some checks to ensure that there is at least one argument given to the script. This was implemented using conditional logic. I then added scalability, such that any number of inputs could be entered. This was done through the use of a for loop through the arguments: arg. #!/bin/bash
if [[ "$#" -eq 0 ]]; then #check ensuring at least one argument
echo "Script requires at least one argument";
exit 1; #report failure
fi
#calculates the product by multiplying every argument
product=1;
for arg; do
product=$(($product * $arg));
done
echo "The product is $product"; #result outputted
exit 0; #report successful execution See the script here In this case, the for loop differs from java in the sense that firstly Java uses curly braces to delineate scope, whereas the bash for loop uses the do and done to contain the for loop’s body of execution. In the example given in the lab diary (for a in $), it will loop through each positional parameter as a single word, with new lines between them. This means that in each execution, $a represents its corresponding parameter. Bash inherently differs from Java as it dynamically typed, whereas in Java variable types must be declared, hence why Java is statically typed. I wrote a small script for the lab’s final exercise, wherein the script outputted "OK" if the script received exactly two single digit numeric arguments, and "Error" if otherwise. My solution checked all of the error cases, chained with lazy or operators for better efficiency. My script can be found here [9].