Professional Documents
Culture Documents
Session 4.7
Parag Narkhede
2
System call
3
System Calls - Process Creation
Process
Create
pid_t fork()
4
What is fork() ?
5
fork()
Initial process
fork()
Returns a new
Returns Zero
PID
6
Concurrent Running of Parent and Child Processes
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
Create a child process at this
{ instance
fork();
After this two processes run
printf("Hello world!\n"); concurrently
return 0;
} Parent Process Child Process
printf("Hello world!\n"); printf("Hello world!\n");
Output
7
Concurrent Running – example 2
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
fork(); Output
fork();
fork();
printf("Hello world!\n");
return 0;
}
8
fork() call
• pid_t fork();
• If fork() succeeds,
• it returns the PID of the child to the parent process
• returns 0 to the child process.
• If it fails,
• it returns -1 to the parent process
• no child is created.
9
What happens when fork() gets successful?
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void forkexample()
{
if (fork() == 0) // child process because return value zero
printf("Hello from Child!\n");
else // parent process because return value non-zero.
printf("Hello from Parent!\n");
} Output
int main(){
forkexample();
return 0; fork() returns 0 in the child
} process and positive integer
(PID) in the parent process. 10
Getting PID and PPID
• A process may obtain its own process id (PID) and parent process id
(PPID) numbers using system calls
• PID - > int getpid()
• PPID - > int getppid()
pid_t getpid()
11
Print process ID and parent process ID
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
printf("PID=%d",getpid());
printf(" PPID=%d",getppid());
}
Output
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
fork();
printf("PID=%d",getpid());
printf(" PPID=%d",getppid());
}
13
Print PID & PPID from Parent and Child Processes
#include<stdio.h> void childprocess(void)
#include<sys/types.h> {
#include<unistd.h> printf("\nChildprocess
PID=%d\tPPID=%d\n",getpid(),getppid());
void childprocess(void);
}
void parentprocess(void);
void main(void){
void parentprocess(void)
pid_t pid;
{
pid=fork();
printf("\nParentprocess
if(pid==0){ PID=%d\tPPID=%d\n", getpid(),getppid());
printf("pid (if) = %d",pid); }
childprocess();}
else{
printf("pid (else)= %d",pid);
parentprocess();}
printf("pid value (returned by fork) %d
terminates\n", pid);
printf("PID %d terminates\n", getpid());
14
}
When Child terminates first
#include<stdio.h> void childprocess(void)
#include<sys/types.h> {
#include<unistd.h> printf("\nChildprocess
PID=%d\tPPID=%d\n",getpid(),getppid());
void childprocess(void);
}
void parentprocess(void);
void main(void){
void parentprocess(void)
pid_t pid;
{
pid=fork();
sleep(10); // just to ensure child
if(pid==0){ terminates first
printf("pid (if) = %d",pid); printf("\nParentprocess
childprocess();} PID=%d\tPPID=%d\n", getpid(),getppid());
else{ }
printf("pid (else)= %d",pid);
parentprocess();}
printf("pid value (returned by fork) %d
terminates\n", pid);
printf("PID %d terminates\n", getpid());
15
}
Orphan Process
16
Orphan Process Example…
#include<stdio.h> void childprocess(void){
#include<sys/types.h> sleep(10); // just to ensure
#include<unistd.h> parent terminates first
printf("\nChildprocess
void childprocess(void); PID=%d\tPPID=%d\n",getpid(),getppid());
void parentprocess(void); }
void main(void){
pid_t pid; void parentprocess(void){
pid=fork(); printf("\nParentprocess
if(pid==0){ PID=%d\tPPID=%d\n", getpid(),getppid());
18
exec() system call family
1.execl
2.execle
3.execlp
4.execv
5.execve
6.execvp
• All these functions have same base exec() followed by some letters.
They basically vary with arguments.
Refer: https://linuxhint.com/linux-exec-system-
call/#:~:text=The%20exec%20system%20call%20is,a%20new%20file%20or%20program
19
Example 1 - execv()
main.c newfile.c
#include<stdio.h> #include<stdio.h>
#include<stdlib.h> #include<unistd.h>
#include<unistd.h>
int main() { int main()
/* A null terminated array of character {
pointers */
printf("I am newfile called
char *args[]={"./newfile",NULL}; by execvp()");
execvp(args[0],args); printf("\n");
return 0;
/*All statements are ignored after }
execvp() call as this whole
process(main.c) is replaced by another
process (newfile.c)*/
printf("file end, priting after
execv()");
return 0;
} 20
Example 2- execv() – getting PID
main.c newfile.c
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
int main() int main()
{ {
printf("PID of main.c = %d\n", printf("PID of newfile.c = %d\n",
getpid()); getpid());
char *args[] = {"./newfile", NULL}; return 0;
execv(args[0], args); }
printf("Back to main.c");
return 0;
}
Same PID
21
fork() and execv() together
main.c newfile.c
#include <stdio.h>
#include <stdio.h>
#include <unistd.h>
#include <unistd.h>
#include <stdlib.h>
int main() #include <stdlib.h>
{ int main()
printf("PID of main.c = %d\n", getpid()); {
char *args[] = {"./newfile", NULL}; printf("PID of newfile.c = %d\n",
pid_t var = fork(); getpid());
if(var == 0) { return 0;
printf("Hi from child process\n"); }
execv(args[0], args);
}
else{
printf("Hi from parent process\n");
printf("PID of parent = %d\n", getpid());
}
printf("printing from main.c");
return 0;
22
}
Other Process management System Calls
• … and many
23