BASH RETURN PROC CPUINFO CODE
Then, already before the script reached the second echo (an echo likely being an expensive code statement to run), the output 1 was shown. The first output ( 223561) shows us that a background process was started, with PID 223561 and the identifier number 1 was given to it. We see: 223561Īnd there is also an empty line in between, which is the result of background processes still running whilst waiting for the next command input (try this command a few times at the command line, as well as some light variations, and you will get a feel how this works). You can think about & in a similar way to with the difference that & will tell the operating system to ‘keep running the next command, keep processing the code’ whereas will wait for the current executing command (terminated by ) to terminate/finish before returning to the command prompt / before continuing to process and execute the next code. This simple change makes for an almost completely different program, and our code is now multi-threaded! Both echo’s will process more or less at the same time, with a small delay in the operating system still having to execute the second loop run (to echo ‘2’). Instead of using – an EOL (end of line) Bash syntax idiom which terminates a given command (you may think about it like Enter/Execute/Go ahead), we used &. In the second for loop, we have changed only one character. You can use the $(.) syntax anywhere within a command line to start a subshell: it is a very powerful and versatile way to code subshells directly into other command lines! ), we simply output the variable $i which will range from 1 to 2 (due to our use of the seq command), which – interestingly – is started in a subshell!
BASH RETURN PROC CPUINFO HOW TO
In the first for loop (see our article on Bash loops to learn how to code loops Let us start with a simple one-liner multi-threaded example, of which the output may look somewhat confusing at first: $ for i in $(seq 1 2) do echo $i done Great! Now that we know what subshells are, and a little about how they work, let’s dive into some multi-threaded coding examples and learn more! Simple multi-threading in Bash Can you see how the process identifier changed once we were inside a subshell? There is a special variable in bash $$, which contains the PID of the current shell in use. Finally we exited from the subshell via exit and returned to the parent subshell! Can we somehow proof this is really what happened? Yes: $ echo $ So the second $ in the example above is actually a different Bash shell, with a different PID ( PID is the process identifier a unique number identifier which uniquely identifies each running process in an operating system). What happened here? First we started another Bash shell ( bash) which started and in turn yielded a command prompt ( $). Let’s do something easy, and start one from within an opened Bash terminal prompt: $ bash
![bash return proc cpuinfo bash return proc cpuinfo](https://proc-cpuinfo.fixstars.com/wp-content/uploads/2017/06/f4619ad3c06be7ebab8f11d9fbb6160d.png)
Can you start seeing how if we would be able to execute code – eight parallel threads all at the same time, each running on a different CPU thread (or shared across all threads) – this way it would execute much faster then a single-threaded process running on a single CPU thread (which may be co-shared with other running processes)? The gains realized will depend a bit on what is being executed, but gains there will be, almost always!įirst we need to understand what a subshell is, how it is started, why you would use one, and how it can be used to implement multi-threaded Bash code.Ī subshell is another Bash client process executed/started from within the current one. The reason for this is simple as soon as a secondary ‘thread’ (read: subshell) is started, then that subsequent thread can (and often will) use a different CPU thread.Īssume for a moment that you have a modern machine with 8 or more threads. If your machine has at least two CPU threads, you will be able to max-out CPU resources using multi-threaded scripting in Bash. When you execute a Bash script, it will at maximum use a single CPU thread, unless you start subshells/threads. $ – requires given linux commands to be executed as a regular non-privileged user # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
![bash return proc cpuinfo bash return proc cpuinfo](https://i.stack.imgur.com/hCKsw.png)
BASH RETURN PROC CPUINFO SOFTWARE
Requirements, Conventions or Software Version Usedĭistribution-independent, Bash version-dependent Multi-threaded Bash scripting & process management Software requirements and conventions used Software Requirements and Linux Command Line Conventions Category