Linux C Programming: Process Control (iv) process scheduling

Source: Internet
Author: User

When there is more than one process in the system, which process executes first, and which process after execution is determined by the priority of the process. The priority of a process is determined by the Nice value, and the lower the Nice value, the higher the priority. Can be seen as more friendly then the scheduling priority is lower. A process can get its nice value changed by the nice function, which can only affect its nice value and not affect the other processes ' Nice value

#include <unistd.h>

int nice (int incr)

The incr parameter is added to the nice value of the calling process , and the system directly drops it to the maximum legal value if the good value is too large.

You can view the default nice values for the system with the nice command . It's usually 0 .

[Email protected]:/home/zhf/ Desktop # Nice

0

You can also view the nice value of the current process using ps-l . NI is the nice value

[Email protected]:/home/zhf/ Desktop # ps-l

F S UID PID PPID C PRI NI ADDR SZ Wchan TTY time CMD

4 S 0 6678 6668 0 0-15612 wait pts/0 00:00:00 su

4 S 0 6679 6678 0 0-5850 wait pts/0 00:00:00 bash

4 R 0 6900 6679 0 0-8809-pts/0 00:00:00 P

The following program we run the program by changing the nice value of 2 parallel running processes , and counting the processes. Count value of the last statistical process

unsigned long-long count; # set global variables for process count

struct Timeval end;

#checktime Determine the run time of the process

void Checktime (char *str) {

struct Timeval TV;

Gettimeofday (&tv,null);

if (Tv.tv_sec > End.tv_sec && tv.tv_sec >= end.tv_usec) {

printf ("%s Count =%lld\n", Str,count);

Exit (0);

}

}

void Schedule_try () {

pid_t pid;

Char *s;

int adj=0;

int ret;

int nzero=20;

Gettimeofday (&end,null);

end.tv_sec+=10;

if ((Pid=fork ()) = = 0) {

s= "Child";

printf ("Current nice value in child are%d,adjusting by%d\n", Nice (0) +nzero,adj);

Ret=nice (adj);

printf ("Now child nice value is%d\n", Ret+nzero);

}

else{

s= "Parent";

printf ("Current nice value in parent was%d\n", nice (0) +nzero);

}

for (;;) {

if (++count = = 0)

printf ("error occured");

Checktime (s);

}

}

First set the adj that is the nice value of the adjustment amount is 0. at this point the parent-child process Nice values are .

The results of the operation are as follows: You can see that the nice is the parent-child process . so in Ten In seconds, the parent-child process computes basically the same. You can assume that a parent-child process consumes the same CPU resources.

In addition to the nice function, we can also Get and set the priority of the process through the Getpriority/setpriority function

#include <sys/resource.h>

int getpriority (int which, int who);

int setpriority (int which,int who,int value);

1, prio_process, a specific process, at this time who's value is the process ID

2, Prio_pgrp, all processes of a process group, at this time who's value is the ID of the process group

3,prio_user, a user owned all processes, at this time who 's value is the actual user ID

usage of getpriority:

void Schedule_try2 () {

pid_t pid;

int prio;

Pid=fork ();

if (PID = = 0) {

Prio=getpriority (Prio_process,getpid ());

printf ("The priority of a child is%d\n", prio);

}

}

So The nice function can also be implemented with getpriority and setpriority .

int nice (int increment)

{  

 int Oldprio = getpriority (prio_process, Getpid ());

 Return SetPriority (Prio_process, Getpid (), Oldprio + increment);

}

Linux C Programming: Process Control (iv) process scheduling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.