Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering

How To Create Process In Linux (Part 10/15)

By Ashish Vara

How to Create a Process in Linux

The Process is an instance of a program code which executes within the operating system. The Process is nothing but carry out of the task.  Refer the previous tutorial Process in Linux. This tutorial will explain how to create a process in Linux.

Fork () system call

The process is created by fork () system call. Fork () creates a new process from the existing process. The existing process from which function called is known as parent process and newly created process is known as child process. Child process has its own process ID.  Fork ( ) takes no argument and return process ID. If successfully fork ( ) system call run, it returns twice.

System call fork ( ) returns negative value if the process is not created.

System call fork ( ) returns zero if child process is created.

System call fork ( ) returns a child process ID and a positive value if it returns from parent process.

The following error may be occurring in fork ( ) system call:

EAGAIN – System cannot duplicate parent’s page table and task structure to child process due to limitation of sufficient memory.

EAGAIN – The child process does not possible create due to limitation of caller’s resource allocation to child process.

ENOMEM – fork ( ) failed to allocate the necessary kernel structures because memory is tight.

ENOSYS – fork ( ) is not supported on this platform (for example, hardware without a Memory-Management Unit).

System call fork ( ) duplicate the same address space of the parent process and allocate to the child process.  The Child process does not inherit timer and semaphore adjustment from parent process. Resource initialization and timer counter is reset to zero in the child process. The Child process copy the task structure and descriptor of the parent process. Fork ( ) system call is located in <sys/types.h> library.

System call getpid ( ) returns the Process ID of the current process and getppid ( ) returns the process ID of the current process’s parent process.

Let’s see how to create child process using fork ( ) system call.

//********************************Process.c*********************************//

#include <unistd.h>

#include <sys/types.h>

#include <errno.h>

#include <stdio.h>

#include <sys/wait.h>

#include <stdlib.h>

int main( )

{

    pid_t child_pid;   

    child_pid = fork ( );                                    // Create a new child process;

 

    if (child_pid >= 0)                         

    {

        if (child_pid == 0)                       

        {

            printf (“child process successfully created!!n”);

 

            printf (“child PID =  %d, parent PID = %dn”, getpid( ), getppid( ) );

          

            exit(0);

 

         }

    }

    else

    {

        perror(“fork”);

        exit(0);

    }

}

//********************************Process.c*********************************//

Save the file in a directory and compile it. After successful compilation, run the executable file from command terminal. You should refer the tutorial How to make first C program in Linux if you not aware of compilation and execution process.

pid_t is a data type which represents the process ID. It is created for process identification. Each process has a unique ID number. Next, we call the system call fork ( ) which will create a new process from calling process. The Calling function is a parent process, and a new process is a child process. The system call fork ( ) is returns zero or positive value if the process is successfully created.

If the system call fork ( ) return a Zero, following output id display on command terminal:

child process successfully created!!

child PID =  11457, parent PID = 2291

Here perror ( ) produce the message of error occurrence from the specific system call. It gives the error information if process is not created successfully.


Filed Under: Tutorials

 

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Introduction to Brain Waves & its Types (Part 1/13)
  • Understanding NeuroSky EEG Chip in Detail (Part 2/13)
  • Performing Experiments with Brainwaves (Part 3/13)
  • Amplification of EEG Signal and Interfacing with Arduino (Part 4/13)
  • Controlling Led brightness using Meditation and attention level (Part 5/13)
  • Control Motor’s Speed using Meditation and Attention Level of Brain (Part 6/13)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • What are the battery-selection criteria for low-power design?
  • Key factors to optimize power consumption in an embedded device
  • EdgeLock A5000 Secure Authenticator
  • How to interface a DS18B20 temperature sensor with MicroPython’s Onewire driver
  • Introduction to Brain Waves & its Types (Part 1/13)

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr bluetooth dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Timer MC14541B wrong delay
  • Pull up via GPIO
  • Electrical Lenght of Microstrip Transmission Lines
  • How to Draw/Display a BMP or PNG on 3.2" TFT Display with inbuilt ILI9341 IC
  • file edit

RSS Electro-Tech-Online.com Discussions

  • DIY bluetooth speaker
  • Turn CD4029 on/off with TTP223
  • Need a ducted soldering fan for solder smoke extraction
  • Question about ultrasonic mist maker
  • Power failure relay options
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering