[Mono-list] To set process priority for Unix

Adrien Dessemond adrien.dessemond at softhome.net
Thu May 24 11:27:59 EDT 2007


Hi,

>   This is my first mail to this list, and I am a new Mono Developer. I
> hope
> to use this list to learn more about mono and C# on linux platforms.
>   I would like to know how it is possible to change the priority of a
> process running on Unix based platform, say SuSe Linux, using C# compiled
> on
> Mono.
>  I have tried using this following Code snippet,


>
>            Process Simple = new Process();
>
>           Simple.StartInfo.FileName   = "mono";
>           Simple.StartInfo.Arguments = "/home/ksrijith/Simple.exe";
>
>           Simple.Start();
>
>           Console.WriteLine("Simple Running with priority {0}",
> Simple.PriorityClass);
>           Simple.PriorityClass = ProcessPriorityClass.BelowNormal ;
>
>    But the priority of the process does not change and is always "Normal".
> Is there any kind of privileges i need to set for this to work on Unix. or
> is there any way i can do this using Mono.Unix namespace. ?
>
>    Please excuse me, if this has already been discussed in the list. I am
> a
> newbie and didn`t know how to search the existing archives..!

What you have to keep in mind is that Mono relies on your operating system
API at the end : in the Linux world (as well as the other Unices/like
ones), processes are created using exec?() and the so famous fork() 
system calls and it is not possible to tell them a new priority for the
newly created process ("child processes are created like their father").

But.. you can alter a process priority later and this operation is known
as "renicing" : you can use either the renice command from the shell (see
renice(1)) or use the setpriority() system call in your code (this implies
a P/Invoke in your C# code). AFAK, "renicing" just gives the process
scheduler a /hint/ about how it has to schedule your process /relatively/
to the others and you are left at its mercy. Windows and Unices/likes
handles that in a very different way, I do not have a precise knowledge
about Windows and Unix scheduler internals so will I leave the explanation
to someone else ;-)

Under normal conditions, there are no reasons to alter a process nice
value (may your application has to do heavy computations that could
seriously slow down the system ?). If your program has to be run only on
Linux/Unix, the best option for you is to launch it from a shell-script
next put in the shell-script a renice call. Two reasons for that :

1. You follow the Unix "philosphy"
2. It will make you life easier (lowering a process nice value can be done
under your standard user account but increasing it requires super-user
privileges, so you could do "sudo" later).

Hope that will help...

Adrien.



More information about the Mono-list mailing list