Q:
fork() function explanation and examples in Linux C programming Language
belongs to collection: C language important programs ( Advance Programs )
C language important programs ( Advance Programs )
- C program to calculate compound interest
- Program to print weekday of given date
- C program to find Binary Addition and Binary Subtraction
- C program to print character without using format specifiers
- C program to extract bytes from an integer (Hexadecimal) value
- C program to convert hexadecimal Byte to integer
- C program to design a digital clock
- C program to implement substring function
- C program to format/extract ip address octets
- C program to validate date (Check date is valid or not)
- EMI Calculator (C program to calculate EMI)
- C program to create your own header file/ Create your your own header file in C
- C program to remove consecutive repeated characters from string
- C program to store date in an integer variable
- C program to store time in an integer variable
- C program to demonstrate example of Variable Arguments
- C program to convert String into Hexadecimal
- C program to check given string is a valid IPv4 address or not
- C program to guess a random number
- C program to design love calculator
- Age Calculator (C program to calculate age)
- C program to design a TIC TAC TOE game
- C program to design flying characters Screen Saver
- C program to Set Computer System IP Address in Linux
- C program to Get Computer System IP Address in Linux
- fork() function explanation and examples in Linux C programming Language
- C program to get Process Id and Parent Process Id in Linux
- C program to find the size of a file in Linux
- gotoxy(), clrscr(), getch() and getche() functions for GCC Linux.
- C program to get current system date and time in Linux
- Function Pointer example program in C programming
- Stringizing Operator in C | How to print a variable name in C?
- C program to find class of an IP Address
- C program to find the sum of digits of a number until a single digit is occurred
- C program to find sum of array elements using Dynamic Memory Allocation
- C program to read and print name, where memory for variable should be declared at run time
- C program to declare memory for an integer variable dynamically
Consider the example
Output:
Explanation:
This is parent section [Process id: 1252].
1252 is the parent process id which is the process id of the main function too; this is the parent section of new created fork.
fork created [Process id: 1253].
This message will print under the fork section and assigns a new id to the newly created child process.
fork parent process id: 1252.
This message will also print under the fork section, it shows parent process id of the newly created child process which is equivalent to parent process’s id (main process id). Hence we can say newly created child process is the child process of main which is known as parent process.
Another Example using fork()
In this example, we will print natural numbers from 1 to 10 using for loop and we will create a fork() - you will see numbers will be printed twice, because of for(), it will create duplicate copy of the process.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer