How to Improve a batch script so that the affinity value rotates within a for loop

mentat13

Reputable
Mar 16, 2015
1
0
4,510
I'm converting my videos to HEVC, and I'm using a batch script with this command:

md hevc
start /affinity 0x7 for %%k in (*.mkv *.mp4) do ffmpeg -i "%%k" -map 0 -c copy -c:v libx265 -y "hevc\%%~nk.mkv"

I'm using the Start /affinity to limit FFmpeg to 3/4 threads, otherwise my computer gets chocked. It works fine, except it uses the same affinity value for an entire folder, and I'd rather rotate the affinity values between 0x7, 0xb, 0xd and x0e, so that it changes the active cores each time a another video starts. I've tried putting the start /affinity in different positions in the command line, and I've tried running a loop within a loop, but nothing I've tried has worked. Any ideas?
 


Rather than playing with the affinity, set the priority to low. This will prevent hevc from preempting processes that have real time constraints.
 


Huh? That's exactly what setting the process affinity does. It sets the default logical processor mask for kernel threads created by that process (including the main thread). The program can override the default as well as set both the affinity and priority on a per-thread basis but many programs don't do that.
 


That's what system calls do. They provide programs with controlled access to shared system resources. The program cannot execute its own code in supervisory mode (unless its a kernel module, which is something else entirely) but it can request that the operating system perform certain functions on its behalf.

A system call does the following:

1. Escalate privilege from user mode to supervisor mode

2. Copy system call parameters from the user call stack to the system call stack

3. Jump to the system call handler (which is protected from manipulation by user programs)

4. Execute the system call

5. Copy the return values from the system call stack to the user call stack

6. Deescalate privilege from supervisor mode to user mode

7. Jump to the return address of the next instruction in the user's program

Requesting that the kernel spawn new threads, terminate old threads, or tune existing threads is all performed through the kernel32.dll which is a part of the standard Win32 API. kernel32.dll makes the appropriate OS specific system calls to the kernel (ntdll.dll) on behalf of the user's program. If the programmer so wished, he or she could reimplement the kernel32.dll functionality and make the calls directly but this may present compatibility issues.