栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

dotnet 进程优先级调整,Support Linux or Windows

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

dotnet 进程优先级调整,Support Linux or Windows

Windows: 进程变更到最大优先级

Linux: 进程变换为:

PR rt(实时模式-进程)

NI -20(最高优先级)

使用方法:

Priority.AdjustToHighestPriority();

源码实现: 

namespace MyServer.Utilits
{
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Security;

    public static unsafe class Priority
    {
        [Dllimport("libc", EntryPoint = "getpid", CallingConvention = CallingConvention.Cdecl)]
        private static extern int getpid();

        [Dllimport("libc", EntryPoint = "setpriority", CallingConvention = CallingConvention.Cdecl)]
        private static extern int setpriority(int which, int who, int prio);

        [Dllimport("libc", EntryPoint = "sched_get_priority_max", CallingConvention = CallingConvention.Cdecl)]
        private static extern int sched_get_priority_max(int policy);

        [Dllimport("libc", EntryPoint = "sched_setscheduler", CallingConvention = CallingConvention.Cdecl)]
        private static extern int sched_setscheduler(int __pid, int __policy, sched_param* __param);

        [Dllimport("kernel32", EntryPoint = "SetPriorityClass", CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetPriorityClass(IntPtr hProcess, uint dwPriorityClass);

        private const uint HIGH_PRIORITY_CLASS = 128;
        private const int PRIO_PROCESS = 0;
        private const int SCHED_FIFO = 1;
        private const int SCHED_RR = 2;

        
        [StructLayout(LayoutKind.Sequential)]
        private struct sched_param
        {
            public int sched_priority;
        };

        [SecurityCritical]
        [SecuritySafeCritical]
        public static void AdjustToHighestPriority()
        {
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                IntPtr hProcess = Process.GetCurrentProcess().Handle;
                SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS);
            }
            else
            {
                
                setpriority(PRIO_PROCESS, 0, -20);

                
                sched_param param_;
                param_.sched_priority = sched_get_priority_max(SCHED_FIFO); // SCHED_RR
                sched_setscheduler(getpid(), SCHED_RR, ¶m_);
            }
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/362167.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号