0% found this document useful (0 votes)
273 views

Operations On Processes

The program demonstrates the creation of an orphan process. The parent process creates a child using fork() and then terminates before the child finishes running. This causes the child to become orphaned as its parent process ID will now return 1 after the parent terminates. The child prints its process ID and parent process ID before and after sleeping to illustrate this change, showing it has become an orphan process.

Uploaded by

Anushka
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
273 views

Operations On Processes

The program demonstrates the creation of an orphan process. The parent process creates a child using fork() and then terminates before the child finishes running. This causes the child to become orphaned as its parent process ID will now return 1 after the parent terminates. The child prints its process ID and parent process ID before and after sleeping to illustrate this change, showing it has become an orphan process.

Uploaded by

Anushka
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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<unistd.h>
#include <sys/types.h>
void ChildProcess(); /* child process prototype */
void ParentProcess(); /* parent process prototype */
int main()
{
pid_t pid;
pid = fork();
if (pid == 0)
{
ChildProcess();
}
else
ParentProcess();
return 0;
}
void ChildProcess()
{ pid_t child_p;
child_p=getpid();//child process id
printf("\n Process id of Child process = %d\n",child_p);
printf("\nI am child process..");
}
void ParentProcess()
{
pid_t parent_pid;
parent_pid = getpid();
printf("\n Parent Process ID = %d\n",parent_pid);
printf("\nI 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.

#include<stdio.h>
#include <sys/types.h>
#include<unistd.h>
#include <sys/resource.h> // for getpriority function
int main()
{
int which = PRIO_PROCESS;
int pid, retnice,ppid,ret,cpid;
ppid = getpid();
ret = getpriority(which, ppid);
printf("\n Priority of parent process = %d\n",ret);

printf("press DEL to stop process \n");


pid=fork();

if(pid == 0)
{
cpid = getpid();
ret = getpriority(which, cpid);
printf("\n Priority of child process = %d\n",ret);

retnice=nice(-4);
printf("child gets higher CPU priority %d \n", retnice);
sleep(10);
}
else
{
retnice=nice(5);
printf("Parent gets lower CPU priority %d \n", retnice);
sleep(10);
}

SET B

Implement the C program to accept n integers to be sorted.


Main function creates child process using fork system call.
Parent process sorts the integers using bubble sort and waits
for child process using wait system call. Child process sorts
the integers using insertion sort.
#include <stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include<sys/wait.h>
void ChildProcess(); /* child process prototype */
void ParentProcess(); /* parent process prototype */
int arrnum[10],n,i;
int main()
{
pid_t pid;

printf("\n Enter value of n");


scanf("%d",&n);
printf("\n Enter %d values",n);
for(i=0;i<n;i++)
{
scanf("%d",&arrnum[i]);
}
pid = fork();//
if (pid == 0)
{
ChildProcess();// insertion
}
else// parent process
{
wait(NULL);
ParentProcess();// bubble sort
printf("\n Sorted array in Parent process:-\n") ;
for(i=0;i<n;i++)
printf("\t[%d]=%d",i,arrnum[i]);
//wait(NULL);
printf("\n Child Complete");
}
return 0;
}
void ChildProcess()
{
int j,pivot;
printf("\nI am child process..");
for(i=1;i<n;i++)
{
pivot = arrnum[i];
j=i-1;
while((j>=0) && (pivot<arrnum[j]))
{
arrnum[j+1]=arrnum[j];
j=j-1;
}
arrnum[j+1]=pivot;
}
printf("\n Sorted array in Child process:-\n") ;
for(i=0;i<n;i++)
printf("\t[%d]=%d",i,arrnum[i]);

}
void ParentProcess()
{
int temp,j;
printf("\nI am parent process..");
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(arrnum[j]>arrnum[j+1])
{
temp = arrnum[j];
arrnum[j]=arrnum[j+1];
arrnum[j+1]=temp;
}

}
Write a C program to illustrate the concept of orphan process.
Parent process creates a child and terminates before child has
finished its task. So child process becomes orphan process.
(Usefork(),sleep(),getpid(),getppid()).
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int pid = fork();
if (pid > 0) {
printf("I am in Parent process\n");
printf("Parent Process ID : %d\n\n", getpid());
}
else if (pid == 0) {
printf("I am in Child process\n");
printf("Child ID: %d\n", getpid());
printf("Parent -ID before sleep: %d\n\n",
getppid());

sleep(10);
printf("\nAfter sleep Child process \n");
printf("Child Process ID: %d\n", getpid());
printf("Parent -ID After sleep: %d\n", getppid());
}
else {
printf("Failed to create child process");
}

return 0;
}

You might also like