
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Creating multiple process using fork() in C
In this section we will see how to use the fork() to make child process in C. We also do some different tasks in each process. So in our parent process we will print different values.
When fork() is called, it returns a value. If the value is greater than 0, then currently it is in parent process, otherwise it is in child process. So using this we can distinguish between the processes.
Example Code
#include <stdio.h> #include <unistd.h> int main() { int n = fork(); //subdivide process if (n > 0) { //when n is not 0, then it is parent process printf("Parent process
"; } else { //when n is 0, then it is child process printf("Child process
"); } return 0; }
Output
soumyadeep@soumyadeep-VirtualBox:~$ ./a.out Parent process soumyadeep@soumyadeep-VirtualBox:~$ Child process soumyadeep@soumyadeep-VirtualBox:~$
- Related Articles
- Creating child process using fork() in Python
- Calculation in parent and child process using fork() in C++
- Creating Linear Gradients using Multiple Color Stops in CSS
- fork() in C
- Prevent fork bomb by limiting user process in linux
- Process Multiple Input Files Using Awk
- C vs BASH Fork bomb in C/C++?
- fork() to execute processes from bottom to up using wait() in C++
- Creating multiple boxplots on the same graph from a dictionary, using Matplotlib
- C vs BASH Fork bomb?
- Difference between fork() and exec() in C
- Creating multiple Java objects by one type only
- C program to demonstrate fork() and pipe()
- Creating and using packages in Java
- Creating Arrays using Javascript

Advertisements