Professional Documents
Culture Documents
Compare Different Algorithms Used For Management and Scheduling of Processes
Compare Different Algorithms Used For Management and Scheduling of Processes
OUTCOME: Student can Compare different algorithms used for management and
scheduling of processes.
1. Write a Program for creating process using System call (E.g., fork ())
Create a child process. Display the details about that process using
getpid and getppid functions. In a child process, Open the file using file
system calls and read the contents and display.
• Explore the working of the fork () system call and document it with examples
Fork system call is used for creating a new process, which is called child process, which
runs concurrently with the process that makes the fork () call (parent process). After a
new child process is created, both processes will execute the next instruction following
the fork () system call. A child process uses the same pc(program counter), same CPU
registers, same open files which use in the parent process.
It takes no parameters and returns an integer value. Below are different values
returned by fork ().
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
// make two process which run same
// program after this instruction
fork();
printf("Hello world!\n");
return 0;
}
OUTPUT:
CODE:
#include <stdio.h>
#include <sys/types.h>
int main()
{
fork();
fork();
fork();
printf("hello\n");
return 0;
}
OUTPUT:
1. getppid() : returns the process ID of the parent of the calling process. If the calling
process was created by the fork() function and the parent process still exists at the
time of the getppid function call, this function returns the process ID of the parent
process. Otherwise, this function returns a value of 1 which is the process id for
init process.
Code:
// C++ Code to demonstrate getppid()
#include <iostream>
#include <unistd.h>
using namespace std;
// Driver Code
int main()
{
Department of Electronics and Telecommunication Engineering
2. getpid() : returns the process ID of the calling process. This is often used by
routines that generate unique temporary filenames.
CODE:
// C++ Code to demonstrate getpid()
#include <iostream>
#include <unistd.h>
using namespace std;
// Driver Code
int main()
{
int pid = fork();
if (pid == 0)
cout << "\nCurrent process id of Process : "
<< getpid() << endl;
return 0;
}
OUTPUT:
• Use the Value returned by the fork system call, to create if, elseif and else block, If for
system call failure, elseif for child block, else for parent process block. Further perform
assigned task as per question.
CODE:
Department of Electronics and Telecommunication Engineering
OUTPUT:
CODE:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t retval;
retval= fork();
if (retval == 0)
printf("This is the child process. My pid is %d and my
• Save the screenshot of the code and output of the above program.