OS-(I) Assignment 1: Set A- 2)

(2) Write a program that demonstrates the use of nice() system call. After a child process is started using fork(), assign higher priority to the child using nice() system call.

Using nice() system call, we can change the priority of the process in multi-tasking system. The new priority number is added to the already existing value.

int nice(int inc);

nice() adds inc to the nice value. A higher nice value means a lower priority. The range of the nice value is +19 (low priority) to -20 (high priority).

Solution:
#include<stdio.h>
#include<sys/types.h>
void main()
{
    int pid,retnice;
    pid=fork();
    for(i=0;i<5;i++)
           {
        if(pid==0)
        {
            retnice=nice(-5);
      printf("Child gets higher CPU priority %d \n",retnice);
            sleep(1);
        }
        else
        {
            retnice=nice(4);
       printf("Parent gets lower CPU priority %d \n",retnice);
            sleep(1);
        }
    }
}

0 Comments:

Post a Comment