让AI在任务管理器中画正弦波

admin 2026-01-26 02:29:23 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文记录了利用AI解决微软面试题在任务管理器绘制正弦波的过程。作者通过AI生成C#代码,利用线程亲和性和忙闲循环控制CPU负载,并在参考《代码之美》代码后优化了波形平滑度。实验验证了AI在代码阅读、理解及跨语言迁移方面的能力,同时也指出了此类高精度CPU负载控制会导致系统操作卡顿的现象。 综合评分: 73 文章分类: 其他


cover_image

让 AI 在任务管理器中画正弦波

原创

hyang0 hyang0

生有可恋

2026年1月24日 08:20 湖北

这是一道微软的面试题,看 AI 做得怎么样。

AI 实现的程序波形如下:

其中代码绑定在第一个核心上运行,如果让代码在所有核心上运行,表现为:

下面是从逻辑核切到整体CPU利用率的波形图,波形有小毛刺不太光滑。

AI 生成的 C# 代码为:

using&nbsp;System;using&nbsp;System.Diagnostics;using&nbsp;System.Runtime.InteropServices;using&nbsp;System.Threading;class&nbsp;Program{&nbsp; &nbsp;&nbsp;static&nbsp;void&nbsp;Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;var&nbsp;process = Process.GetCurrentProcess();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;int&nbsp;coreCount = Environment.ProcessorCount;&nbsp; &nbsp; &nbsp; &nbsp; process.ProcessorAffinity = (IntPtr)((1L&nbsp;<< coreCount) -&nbsp;1);&nbsp; &nbsp; &nbsp; &nbsp; process.PriorityClass = ProcessPriorityClass.High;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;double&nbsp;periodSeconds =&nbsp;60.0;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;int&nbsp;resolutionMs =&nbsp;10;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;for&nbsp;(int&nbsp;i =&nbsp;0; i < coreCount; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;int&nbsp;coreIndex = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;var&nbsp;t =&nbsp;new&nbsp;Thread(() => GenerateCpuLoad(periodSeconds, resolutionMs, coreIndex));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.IsBackground =&nbsp;true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.Priority = ThreadPriority.Highest;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.Start();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("CPU 正在按正弦波负载运行,按 Enter 退出...");&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;static&nbsp;void&nbsp;GenerateCpuLoad(double&nbsp;periodSeconds,&nbsp;int&nbsp;resolutionMs,&nbsp;int&nbsp;coreIndex)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; BindCurrentThreadToCore(coreIndex);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;var&nbsp;sw = Stopwatch.StartNew();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;while&nbsp;(true)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;double&nbsp;t = sw.Elapsed.TotalSeconds;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;double&nbsp;u = (t % periodSeconds) / periodSeconds;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;double&nbsp;s =&nbsp;2.0&nbsp;* u;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;double&nbsp;x;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;double&nbsp;temp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;double&nbsp;y;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;(s <&nbsp;1.0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x =&nbsp;2.0&nbsp;* s -&nbsp;1.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp =&nbsp;1.0&nbsp;- x * x;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;(temp <&nbsp;0.0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp =&nbsp;0.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y =&nbsp;0.5&nbsp;+&nbsp;0.5&nbsp;* Math.Sqrt(temp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;double&nbsp;s2 = s -&nbsp;1.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x =&nbsp;2.0&nbsp;* s2 -&nbsp;1.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp =&nbsp;1.0&nbsp;- x * x;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;(temp <&nbsp;0.0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp =&nbsp;0.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y =&nbsp;0.5&nbsp;-&nbsp;0.5&nbsp;* Math.Sqrt(temp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;double&nbsp;cpuRatio = y;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;(cpuRatio <&nbsp;0.0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cpuRatio =&nbsp;0.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;else&nbsp;if&nbsp;(cpuRatio >&nbsp;1.0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cpuRatio =&nbsp;1.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;int&nbsp;busyMs = (int)(resolutionMs * cpuRatio);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;int&nbsp;idleMs = resolutionMs - busyMs;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;var&nbsp;slice = Stopwatch.StartNew();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;while&nbsp;(slice.ElapsedMilliseconds < busyMs)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;(idleMs >&nbsp;0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.Sleep(idleMs);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; [DllImport("kernel32.dll")]&nbsp; &nbsp;&nbsp;static&nbsp;extern&nbsp;IntPtr&nbsp;GetCurrentThread();&nbsp; &nbsp; [DllImport("kernel32.dll")]&nbsp; &nbsp;&nbsp;static&nbsp;extern&nbsp;IntPtr&nbsp;SetThreadAffinityMask(IntPtr hThread, IntPtr dwThreadAffinityMask);&nbsp; &nbsp;&nbsp;static&nbsp;void&nbsp;BindCurrentThreadToCore(int&nbsp;coreIndex)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;(coreIndex <&nbsp;0&nbsp;|| coreIndex >= Environment.ProcessorCount)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; IntPtr handle = GetCurrentThread();&nbsp; &nbsp; &nbsp; &nbsp; IntPtr mask = (IntPtr)(1L&nbsp;<< coreIndex);&nbsp; &nbsp; &nbsp; &nbsp; SetThreadAffinityMask(handle, mask);&nbsp; &nbsp; }}

原始波形不完美,让AI 参考《代码之美》的正弦波代码,重新优化后的CPU波形如下:

差不多已经比较平滑了,对比以前的方案会大量占用CPU,在进行其它操作时系统会变卡,鼠标明显变得卡顿。

从代码理解能力上来说,用 AI 读代码或改写代码是可行的。可以用 AI 实现不同编码间的迁移,比如从 c++ 迁移到 C#。

参考的《代码之美》中的正弦波代码:

const&nbsp;double&nbsp;SPLIT =&nbsp;0.01;const&nbsp;int&nbsp;COUNT =&nbsp;200;const&nbsp;double&nbsp;PI =&nbsp;3.14159265;const&nbsp;int&nbsp;INTERVAL =&nbsp;300;int&nbsp;main(){    DWORD busySpan[COUNT];&nbsp;//array of busy times   DWORD idleSpan[COUNT];&nbsp;//idleint&nbsp;half = INTERVAL/2;double&nbsp;radian =&nbsp;0.0;for&nbsp;(int&nbsp;i =&nbsp;0; i < COUNT; i++)   {       busySpan[i] = (DWORD)(half + sin(PI * radian) * half);      idleSpan[i] = INTERVAL - busySpan[i];       radian += SPLIT;    }   DWORD startTime =&nbsp;0;int&nbsp;j =&nbsp;0;while(true)    {       j = j % COUNT;      startTime = GetTickCount();while((GetTickCount() - startTime) <= busySpan[j])           ;       Sleep(idleSpan[j]);     j++;    }return&nbsp;0;}

给 AI 的提示词:

全文完。


免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:生有可恋 hyang0 hyang0《让 AI 在任务管理器中画正弦波》

评论:0   参与:  0