Operations On Processes
Operations On Processes
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);
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
}
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;
}