You are on page 1of 23

Embedded Systems and RTOS

Session 4.7

Linux System Calls

Parag Narkhede

Electronics & Telecommunication Engineering


Symbiosis Institute of Technology, Pune
Symbiosis International (Deemed University), Pune
Few important commands

• Create a .c file with name myfile


gedit myfile.c

• Compile the C code present in myfile.c and generate execulable file


with name myexefile
gcc -o myexefile myfile.c

• Execute the output file


./myexefile

2
System call

• It is a programmatic way in which a computer program requests a


service from the kernel of the operating system.
• It is a way to interact with operating system.
• Services provided by System Calls:
• Process Management
• Memory Management
• File management
• I/O management
• Network management
• Protection, etc

3
System Calls - Process Creation

Process
Create
pid_t fork()

4
What is fork() ?

• It is used for creating a new process.


• Newly created process is called CHILD PROCESS
• fork() creates a copy of process where it is called.
• Child process inherits a copy of its parent's code, data, stack, open
file descriptors, and signal table.
• After creation of child process, both (child and parent process) run
concurrently
• The process that makes fork() call is called as PARENT PROCESS
• However, the parent and child have different process id numbers
and parent process id numbers.

5
fork()

Initial process

fork()

Returns a new
Returns Zero
PID

Original Process New Process


continues (Child)

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

IDs will change in every run


12
Print process ID and parent process ID

#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

• If a PARENT dies before the CHILD, then CHILD process is called as


Orphan Process.
• In such a case, Child is adopted by the original INIT 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());

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());
17
}
exec() call

• The exec system call is used to execute a file which is residing in an


active process.
• When exec is called the previous executable file is replaced and new
file is executed.
• exec system call will replace the old file or program from the process
with a new file or program.
• The entire content of the process is replaced with a new program.
• The current process is just turned into a new process and hence the
process id PID is not changed, this is because we are not creating a
new process we are just replacing a process with another process in
exec.

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

• Terminate Process: exit()

• Wait for some process: wait()

• … and many

23

You might also like