0% found this document useful (0 votes)
16 views2 pages

Day 8 1

The document explains the process creation in Unix using the fork() system call, which creates a child process from a parent process. It details how to differentiate between the parent and child processes based on the return values of fork() and provides example C code demonstrating this functionality. Additionally, it includes practice exercises for creating child processes that perform specific tasks such as checking for prime numbers and generating sequences.

Uploaded by

pratikbarainda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Day 8 1

The document explains the process creation in Unix using the fork() system call, which creates a child process from a parent process. It details how to differentiate between the parent and child processes based on the return values of fork() and provides example C code demonstrating this functionality. Additionally, it includes practice exercises for creating child processes that perform specific tasks such as checking for prime numbers and generating sequences.

Uploaded by

pratikbarainda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Process

Process creation is achieved through the fork() system call. The newly created process is
called the child process and the process that initiated it (or the process when execution is
started) is called the parent process. After the fork() system call, now we have two processes -
parent and child processes. How to differentiate them? Very simple, it is through their return
values.
After a successful fork(), two processes exist:
❖ The Parent process (original)
❖ The Child process (newly created)
Both processes continue execution from the same line just after the fork() call, but with
different return values from fork().

System call fork() is used to create processes. It takes no arguments and returns a process ID.
The purpose of fork() is to create a new process, which becomes the child process of the
caller. After a new child process is created, both 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():

Returns:

 0 to the child process


 > 0 (child PID) to the parent process
 -1 if the fork fails

Therefore, after the system call to fork(), a simple test can tell which process is the child.
Please note that Unix will make an exact copy of the parent's address space and give it
to the child. Therefore, the parent and child processes have separate address spaces.

#include <stdio.h>
#include <sys/types.h> // For pid_t
#include <unistd.h> // For fork(), getpid(), getppid()
#include <stdlib.h> // For exit()

int main() {
pid_t pid;

// First fork: creates a child process


pid = fork();

// Second fork: executes for both parent and child processes created above
fork();

// 'fork()' returns 0 in child, >0 in parent


if (pid == 0) {
// This block is executed by the process that is the first child
printf("Child: Current Process ID = %d\n", getpid());
printf("Child: Parent Process ID = %d\n", getppid());
}
else {
// This block is executed by the original parent process
printf("Parent: Current Process ID = %d\n", getpid());
printf("Parent: Parent Process ID = %d\n", getppid());

// Uncomment to wait for child process to finish (optional)


// wait(NULL);
}

return 0;
}

Practice
1. Write a C program to create a child process. Parent process checks if a number
is prime or not and the child process will check if a number is palindrome or not.

2. Write a C program to create a child process using fork(), and use a variable that
is accessible by both the parent and child processes through shared memory.
Print the value and memory address of the variable from both the parent and
child processes.

3. Write a C program using the fork() system call that generates this sequence in
the child process. The starting number will be provided from the user. For
example, if 8 is given, the child process will output 8, 4, 2, 1. Because the parent
and child processes have their own copies of the data, it will be necessary for the
child to output the sequence. Have the parent invoke the wait() call to wait for
the child process to complete before exiting the program. Perform necessary
error checking to ensure that a positive integer is passed on the command line.

You might also like