Implement the C Program to create a child process using fork(), display parent and child process id. Child process will display the message “I am Child Process” and the parent process should display “I am Parent Process” | 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.


 Operations on Processes


Process: A process is basically a program in execution. We write our computer programs in a
text file, during execution it is loaded in computer’s memory and then it becomes a process. A
process performs all the tasks mentioned in the program. A process can be divided into four
sections ─ stack, heap, text and data.

Stack: The process Stack contains the temporary data such as method/function
parameters, return address and local variables.
Heap: This is dynamically allocated memory to a process during its run time.
Text: This includes all instructions specified in the program.
Data: This section contains the global and static variables.

In Linux operating system, new processes are created through fork() system call.
Fork() System Call :

System call fork() is used to create new processes. It takes no arguments and returns a
process ID. The newly created process is called child process and the caller process is called as
parent process. After a new child process is created, both parent and child processes will
execute the next instruction following the fork() system call. Therefore, we have to distinguish
the parent from the child. This can be done by testing the returned value of fork():
fork() returns a zero to the newly created child process and returns a positive value (process
ID of the child process) to the parent.
If fork() returns a negative value, the creation of a child process was unsuccessful.
The returned process ID is of type pid_t defined in sys/types.h. Normally, the process ID is an
integer. Moreover, a process can use function getpid() to retrieve the process ID assigned to this
process.

Example :

int main()
printf(“Before Forking”);
fork();
printf(“After Forking”);
return 0;
}

Practical Assignments

Set A

1.) Implement the C Program to create a child process using fork(), display parent and child
process id. Child process will display the message “I am Child Process” and the parent
process should display “I am Parent Process”.


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    // fork() Create a child process

    int pid = fork();
    if (pid > 0) {
        printf("I am Parent process\n");
        printf("ID : %d\n\n", getpid());
    }
    else if (pid == 0) {
        printf("I am Child process\n");
        // getpid() will return process id of child process
        printf("ID: %d\n", getpid());
    
    }
    else {
        printf("Failed to create child process");
    }

    return 0;
}

Output :

@kali-linux:~/Desktop/Ty$ ./a.out
I am Parent process
ID : 3698

I am Child process
ID: 3699


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.


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
if (pid == 0)
{
printf("\nI am child process, id=%d\n",getpid());
printf("\nPriority :%d,id=%d\n",nice (-7),getpid());
}
else
{
printf("\nI am parent process, id=%d\n",getpid());
nice(1);
printf("\nPriority :%d,id=%d\n",nice (15),getpid());
}
return 0;
}

Output:

I am parent process, id=6555
Priority :6,id=6555
I am child process, id=6556
Priority :-17,id=6556

Explaination :


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).

#include<stdio.h>
main()
{
int pid, retnice;
printf("press DEL to stop process \n");
pid=fork();
for(;;)
{
if(pid == 0)
{
retnice = nice (−5);
print("child gets higher CPU priority %d \n", retnice);
sleep(1);
}
else
{
retnice=nice(4);
print("Parent gets lower CPU priority %d \n", retnice);
sleep(1);
}
}
}

Orphan process :

The child processes whose parent process has completed execution or terminated are
called orphan process. Usually, a parent process waits for its child to terminate or finish their job
and report to it after execution but if parent fails to do so its child results in the Orphan process.
In most cases, the Orphan process is immediately adopted by the init process (a very first process
of the system).



Post a Comment

0 Comments