Skip to content

Instantly share code, notes, and snippets.

@nhoffman
Created July 24, 2012 19:11
Show Gist options
  • Save nhoffman/3171967 to your computer and use it in GitHub Desktop.
Save nhoffman/3171967 to your computer and use it in GitHub Desktop.
Demonstrate parallel processes using xargs
#!/bin/bash
# try me out:
# two processors
# $ echo {1..9} | xargs -n1 -P2 ./ps.sh
# four processors
# $ echo {1..9} | xargs -n1 -P4 ./ps.sh
# sleeptime set to a random integer between 2 and 5
sleeptime=$(shuf -i 2-5 -n 1)
# '$$' is the current process id
# 'ps -o pid,psr' gives the process id and cpu id of current processes
# '$(ps -o pid,psr | grep $$)' gives the process id and cpu id of the current process
echo i,process,cpu,sleeptime: $1 $(ps -o pid,psr | grep $$) $sleeptime
sleep $sleeptime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment