Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
All the processes in an operating system are created when a process executes the fork() system call except the startup process. The process that used the fork () system call is the parent process. In other words, a parent process is one that creates a child's process. A parent process may have multiple child processes but a child process only one parent process.
On the success of a fork () system call, the PID of the child process is returned to the parent process and 0 is returned to the child process. On the failure of a fork () system call, -1 is returned to the parent process and a child process is not created.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
void main() {
int pid,pid1,pid2;
pid=fork();
if(pid==-1)
{
printf("ERROR IN PROCESS CREATION n");
exit(1);
}
if(pid!=0) {
pid1=getpid();
printf("n the parent process ID is %dn", pid1);
}
else {
pid2=getpid();
printf("n the child process ID is %dn", pid2);
}
}
PID and PPID
In Linux, an executable stored on a disk is called a program, and a program loaded into memory and running is called a process. When a process is started, it is given a unique number called process ID (PID) that identifies that process to the system. If you ever need to kill a process, for example, you can refer to it by its PID.
In addition to a unique process ID, each process is assigned a parent process ID (PPID) that tells which process started it. The PPID is the PID of the process’s parent.
For example, if process1 with a PID of 101 starts a process named process2, then process2 will be given a unique PID, such as 3240, but it will be given the PPID of 101. It’s a parent-child relationship. A single parent process may spawn several child processes, each with a unique PID but all sharing the same PPID
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int pid, pid1, pid2;
pid = fork(); // variable pid will store the value returned from fork() system call
// If fork() returns zero then it means it is child process.
if (pid == 0) {
// First child needs to be printed later hence this process ismade to sleep for 3 secs.
sleep(3);
// This is first child process getpid() gives the process id and getppid() gives the
// parent id of that process.
printf("child[1] --> pid = %d and ppid = %dn", getpid(), getppid());
}
else {
pid1 = fork();
if (pid1 == 0) {
sleep(2);
printf("child[2] --> pid = %d and ppid = %dn", getpid(), getppid());
}
else {
pid2 = fork();
if (pid2 == 0) {
// This is third child which is needed to be printed first.
printf("child[3] --> pid = %d and ppid = %dn", getpid(), getppid());
}
// If value returned from fork() in not zero and >0 that means this is parent process.
else {
// This is asked to be printed at last hence made to sleep for 3 seconds.
sleep(3);
printf("parent --> pid = %dn", getpid());
}
}
}
return 0;
}
![]() |
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team