4 ms·
Okay, I'll bite. This is my attempt at it: ---------------------- PID := $(shell cat /proc/$$$$/status | grep PPid | awk '{print $$2}') JOBS := $(shell ps -
by Quanticles 12y ago
Okay, I'll bite. This is my attempt at it:
----------------------
PID := $(shell cat /proc/$$$$/status | grep PPid | awk '{print $$2}')
JOBS := $(shell ps -p ${PID} -f | tail -n1 | grep -oP '\-j *\d+' | sed 's/-j//')
ifeq "${JOBS}" ""
JOBS := 1
endif
all:
echo ${JOBS}
----------------------
quanticles@glados $ make -j8
8
quanticles@glados $ make
1
----------
edit: for silly mistake
- anon4 12y agoNote: you can forego the ps and simply read /proc/$PID/cmdline, which is a \0-delimited list of strings. So, grep -oP '\-j\x00\d+' /proc/$PID/cmdline Edit: and no need for sed when a simple cut -b4- does the same job.
- protomyth 12y agoDoes that work on anything other than Linux?
- myhf 12y agoNot in exactly the same way, but every unixy system has some way of finding the parent pid and looking up its command line.
- protomyth 12y agoIs Quanticles's code portable to the other unix systems?
- myhf 12y agoIt should work on most systems that have a /proc filesystem: http://en.wikipedia.org/wiki/Procfs http://en.wikipedia.org/wiki/Procfs If you don't have /proc but do have ps (1), you could get the parent pid like this: PID := $(ps -p $$$$ -o ppid=)
- Someone 12y agoIf you are willing to use /proc, you basically have Linux, and can assume just as well that your shell is bash. If so, $$ is the easiest way to get the PID of the current shell. That works in several other shells, too, but I am not sure sh is guaranteed to have it (FreeBSD's sh has it. See http://www.freebsd.org/cgi/man.cgi?query=sh http://www.freebsd.org/cgi/man.cgi?query=sh)