|
I know this is the C/C++ forum, but here's an example of how to do this in C#. It was an interesting little research project.
The result can be seen in Task Manager quite easily. The CPU is limited to an AVERAGE of 20% in this example. It'll go as low as 8% and as high as 25% on my 13900K.
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace CsJobObjectSandbox
{
internal class Program
{
[StructLayout(LayoutKind.Explicit, Size = 8)]
public struct JobObject_CPU_Rate_Control_Information
{
[FieldOffset(0)]
public uint ControlFlags;
[FieldOffset(4)]
public uint CpuRate;
[FieldOffset(4)]
public uint Weight;
[FieldOffset(4)]
public ushort MinRate;
[FieldOffset(6)]
public ushort MaxRate;
}
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;
}
public enum JobObject_Info_Class
{
BasicLimitInformation = 2,
BasicUiRestrictions = 4,
SecurityLimitInformation = 5,
EndOfJobItmeInformation = 6,
AssociateCompletionPortInformation = 7,
ExtendedLimitInformation = 9,
GroupInformation = 11,
NoticiationLimitInformation = 12,
GroupInformationEx = 14,
CpuRateControlInformation = 15,
NetRateControlinformation = 32,
NotificationLimitInformation = 33,
LimitViolationInformation2 = 34
}
private const uint JOBOBJECT_CPU_RATE_CONTROL_ENABLE = 0x1;
private const uint JOBOBJECT_CPU_RATE_CONTROL_WEIGHT_BASED = 0x2;
private const uint JOBOBJECT_CPU_RATE_CONTROL_HARD_CAP = 0x4;
private const uint JOBOBJECT_CPU_RATE_CONTROL_NOTIFY = 0x8;
private const uint JOBOBJECT_CPU_RATE_CONTROL_MIN_MAX_RATE = 0x10;
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern IntPtr CreateJobObject([In] ref SECURITY_ATTRIBUTES lpJobAttributes, string lpName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern bool SetInformationJobObject([In] IntPtr hJob, [In] JobObject_Info_Class jobObjectInfoClass, IntPtr lpJobObjectInfo, int cbJobObjectInfoLength);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern IntPtr AssignProcessToJobObject([In] IntPtr hJob, [In] IntPtr hprocess);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern bool CloseHandle([In] IntPtr hObject);
static void Main(string[] args)
{
SECURITY_ATTRIBUTES attributes = new();
attributes.nLength = Marshal.SizeOf(attributes);
attributes.lpSecurityDescriptor = IntPtr.Zero;
attributes.bInheritHandle = 0;
IntPtr jobHandle = CreateJobObject(ref attributes, "SandboxJobObject");
JobObject_CPU_Rate_Control_Information cpuLimitInfo = new()
{
ControlFlags = JOBOBJECT_CPU_RATE_CONTROL_ENABLE | JOBOBJECT_CPU_RATE_CONTROL_HARD_CAP,
CpuRate = 2000
};
int size = Marshal.SizeOf(typeof(JobObject_CPU_Rate_Control_Information));
IntPtr infoPointer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(cpuLimitInfo, infoPointer, false);
bool result = SetInformationJobObject(jobHandle, JobObject_Info_Class.CpuRateControlInformation, infoPointer, size);
if (result)
{
Marshal.FreeHGlobal(infoPointer);
IntPtr processHandle = Process.GetCurrentProcess().Handle;
_ = AssignProcessToJobObject(jobHandle, processHandle);
Task task = TestMethodAsync();
task.Wait();
}
CloseHandle(jobHandle);
}
static Task TestMethodAsync()
{
return Task.Factory.StartNew(() =>
{
long y = 0;
Parallel.For((long)0, (long)10000000000, (x) =>
{
y = x++ * 2;
});
});
}
}
}
|
|
|
|
|
Just out of curiosity, if you set a hard cap of 20%, what causes it to exceed that?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
I don't know for sure, but I suspect thread scheduling on different cores running at different speeds.
On a 13900, you have 8 performance cores, which support HT, and 16 efficiency cores, which don't support HT. So I have 24 cores that can run at vastly different speeds, supporting 32 threads.
|
|
|
|
|
Nice. Learn something new every day.
|
|
|
|
|
I am revoking my license to this
modified 20-May-23 16:42pm.
|
|
|
|
|
This is what we refer to as a "code dump". Usually pointless except in the mind of the poster.
Taking credit for someone else's code it appears. "Signing" with some trivial changes.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Three more kicks and he will be removed.
|
|
|
|
|
Message Closed
modified 19-May-23 21:10pm.
|
|
|
|
|
But failing to address the first comment "Usually pointless except in the mind of the poster."
|
|
|
|
|
Below is my code but note working same header footer is set to all page, please suggest best solution for this.
oWorkSheets2007 = oWorkBook2007.GetWorksheets();
oWorkSheet2007 = oWorkSheets2007.GetItem(COleVariant((short)sheetNum));
XLS2007::Window owindObj = oApp2007.GetActiveWindow();
XLS2007::Pages oPages = owindObj.GetPanes();
oPageSetup = oWorkSheet2007.GetPageSetup();
oPages = oPageSetup.GetPages();
long a = oPages.GetCount();
int l=0;
for( l=l+1;l<=a;l++)
{
oPage= oPages.GetItem(COleVariant((short)l));
oPageSetup = oWorkSheet2007.GetPageSetup();
oPageSetup.SetPrintErrors(1);
oPageSetup.SetAlignMarginsHeaderFooter(HFI.bAlignWithMargins);
oPageSetup.SetScaleWithDocHeaderFooter(HFI.bScaleWithDoc);
oPageSetup.SetCenterHeader(LPCTSTR(HFI.FirstCentreHeader));
oPageSetup.SetCenterFooter(LPCTSTR(HFI.FirstCentreFooter));
oPageSetup.SetLeftHeader(LPCTSTR(HFI.FirstLeftHeader));
oPageSetup.SetLeftFooter(LPCTSTR(HFI.FirstLeftFooter));
oPageSetup.SetRightHeader(LPCTSTR (HFI.FirstRightHeader));
oPageSetup.SetRightFooter(LPCTSTR(HFI.FirstRightFooter));
oPageSetup.SetCenterHeader(LPCTSTR(HFI.OddCentreHeader));
oPageSetup.SetCenterFooter(LPCTSTR(HFI.OddCentreFooter));
oPageSetup.SetLeftHeader(LPCTSTR(HFI.OddLeftHeader));
oPageSetup.SetLeftFooter(LPCTSTR(HFI.OddLeftFooter));
oPageSetup.SetRightHeader(LPCTSTR(HFI.OddRightHeader));
oPageSetup.SetRightFooter(LPCTSTR(HFI.OddRightFooter));
|
|
|
|
|
The (incomplete) for() loop looks a bit odd in that it is using the same values for the header and footer and the set methods are called twice. If you are wanting the header/footer on the even pages to be different than the header/footer on the odd pages then you are going to have to check whether l is odd or even and use a different value accordingly.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Message Closed
modified 18-May-23 10:18am.
|
|
|
|
|
|
Googling suggests that is a standard problem with answers provided.
|
|
|
|
|
Message Closed
modified 19-May-23 21:11pm.
|
|
|
|
|
It doesn't matter. There is no consequential difference here between simulating the code in an emulator or running it directly, except perhaps by making it less obvious what's going to happen so you can fool yourself for longer.
|
|
|
|
|
Message Closed
modified 19-May-23 21:09pm.
|
|
|
|
|
Are you trying to trick me into doing your homework?
E: oh I see now, it's worse than that. You already wrote papers about this.. Apparently you're some sort of crank scientist then. You cannot disprove the undecidability of the halting problem, especially not with simple things like "just emulate the code LMAO". The interesting part about the famous undecidability proof is that it doesn't matter how H works, if you take that away you just get something that doesn't work for mundane reasons.
modified 15-May-23 16:12pm.
|
|
|
|
|
Message Closed
modified 19-May-23 21:18pm.
|
|
|
|
|
polcott wrote: Any competent software engineer...
Any competent civil engineer can design a bridge that doesn't fall down. Yet they do.
You said in the OP
"Will D ever reach its own.."
Ever means just that. In no situation in no time period.
Any competent engineer (of any discipline) understands that there is a big difference between one single case and all cases for all time.
The problem that you are looking at has been proven to be impossible.
Any compentent engineer then understands that they must then do the following to achieve what you want.
1. Invalidate the original proof
2. Provide a new proof that it can fail. This by itself might provide the first.
That is mathematics and not software.
|
|
|
|
|
Message Closed
modified 19-May-23 21:21pm.
|
|
|
|
|
Try submitting it here for more peer review.
Frontiers | Publisher of peer-reviewed articles in open access journals[^]
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Message Closed
modified 19-May-23 21:23pm.
|
|
|
|
|
OK, then how about starting here.
Author Guidelines | Communications of the ACM[^]
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Message Closed
modified 19-May-23 21:26pm.
|
|
|
|