![bash return proc cpuinfo bash return proc cpuinfo](https://img2020.cnblogs.com/blog/1809959/202004/1809959-20200413174551643-305950082.png)
- #BASH RETURN PROC CPUINFO HOW TO#
- #BASH RETURN PROC CPUINFO SOFTWARE#
- #BASH RETURN PROC CPUINFO CODE#
#BASH RETURN PROC CPUINFO 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’).
![bash return proc cpuinfo bash return proc cpuinfo](https://cdn.hackaday.io/images/1994701550494233806.jpg)
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://xn----ctbeed2afqgdf5acu0lzbm.xn--p1ai/800/600/https/www.gas-smolensk.ru/files/442/kvitanciya.jpg)
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
![bash return proc cpuinfo bash return proc cpuinfo](https://ostechnix.com/wp-content/uploads/2017/08/View-number-of-CPU-cores-in-Linux-300x197.png)
# – 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 SOFTWARE#
Requirements, Conventions or Software Version Usedĭistribution-independent, Bash version-dependent Model = Intel(R) Core(TM) i7-7700HQ CPU 2.Multi-threaded Bash scripting & process management Software requirements and conventions used Software Requirements and Linux Command Line Conventions Category When you execute above function it will generate following output: # OS information # We can use below syntax to define function: function function_name '` If any function accepts arguments then those can be provided from command line as follows: $ my_func arg1 arg2 arg3 Defining function