KProcess Nice level

Oswald Buddenhagen ossi at kde.org
Tue May 27 12:28:22 BST 2003


On Mon, May 26, 2003 at 12:57:40PM +0200, Oswald Buddenhagen wrote:
> On Fri, May 23, 2003 at 08:41:14PM +0200, Luis Pedro Coelho wrote:
> > > > but i could add a setNice() function, too.
> > >
> > > If you do that, please rename it to setPriority() afterwards :]
> > 
> > Good idea.
> >
> here is a patch.
> 
and here is a revised one.
the Prio* constant name prefix is somewhat ominous, but "Priority" is
too long for my taste, and if i put it in a namespace to get names like
Priority::High as luis suggested, we once more risk name clashes because
some bonehead #defined High or something like that.

greetings

-- 
Hi! I'm a .signature virus! Copy me into your ~/.signature, please!
--
Chaos, panic, and disorder - my work here is done.
-------------- next part --------------
Index: kprocess.h
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kprocess.h,v
retrieving revision 1.77
diff -u -r1.77 kprocess.h
--- kprocess.h	6 May 2003 17:00:25 -0000	1.77
+++ kprocess.h	27 May 2003 11:18:21 -0000
@@ -499,6 +499,20 @@
    */
   KPty *pty() const;
 
+  /**
+   * More or less intuitive constants for use with setPriority().
+   */
+  enum { PrioLowest = 20, PrioLow = 10, PrioLower = 5, PrioNormal = 0,
+    PrioHigher = -5, PrioHigh = -10, PrioHighest = -19 };
+
+  /**
+   * Sets the scheduling priority of the process.
+   * @param prio the new priority in the range -20 (high) to 19 (low).
+   * @return false on error; see setpriority(2) for possible reasons.
+   * @since 3.2
+   */
+  bool setPriority(int prio);
+
 signals:
   /**
    * Emitted after the process has terminated when
Index: kprocess.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kprocess.cpp,v
retrieving revision 1.123
diff -u -r1.123 kprocess.cpp
--- kprocess.cpp	6 May 2003 16:57:52 -0000	1.123
+++ kprocess.cpp	27 May 2003 11:18:22 -0000
@@ -86,7 +86,7 @@
    KProcessPrivate() : 
      usePty(KProcess::NoCommunication),
      addUtmp(false), useShell(false),
-     pty(0)
+     pty(0), priority(0)
    {
    }
 
@@ -97,6 +97,8 @@
    KPty *pty;
    const char *user;
 
+   int priority;
+
    QMap<QString,QString> env;
    QString wd;
    QCString shell;
@@ -196,6 +198,19 @@
    return keepPrivs;
 }
 
+bool
+KProcess::setPriority(int prio)
+{
+    if (runs) {
+        if (setpriority(PRIO_PROCESS, pid_, prio))
+            return false;
+    } else {
+        if (prio > 19 || prio < (geteuid() ? 0 : -20))
+            return false;
+    }
+    d->priority = prio;
+    return true;
+}
 
 KProcess::~KProcess()
 {
@@ -350,6 +365,9 @@
 
         if (!commSetupDoneC())
           kdDebug(175) << "Could not finish comm setup in child!" << endl;
+
+        if (d->priority)
+            setpriority(PRIO_PROCESS, 0, d->priority);
 
         if (!runPrivileged())
         {
-------------- next part --------------
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<


More information about the kde-core-devel mailing list