SlideShare a Scribd company logo
SYSTEMS PROGRAMMING
LECTURE 4
PROCESSES
A UNIX PROCESS
• Each process is identified by a unique integer,
the process identifier (PID)
• Each identifier is associated with a process
descriptor inside the OS scheduler
• A list of PIDs can be obtained with the ps
aux command
• Information about each process can be found in
/proc/pidn which is a directory
corresponding to each PID
PROCESS STARTUP AND TERMINATION
PROCESS STARTUP AND TERMINATION
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]);
void exit(int status);
void _exit(int status);
• main() is passed the command line arguments and
should return the termination status
• exit() takes the termination status and causes any
files to be properly closed and memory released
• _exit() terminates the process immediately
closing all file descriptors but not cleanly
PROCESS STARTUP AND TERMINATION
#include <stdlib.h>
int atexit(void (*func)(void));
Returns 0 if OK, -1 on error
• The exit() function calls any registered user exit
functions before calling the termination functions
• Exit functions are registered using atexit() which
takes a pointer to a function that does not return
anything and takes no arguments
• Calling _exit() directly allows us to skip these
registered atexit functions
PROCESS STARTUP AND TERMINATION
ENVIRONMENT LIST
• Each program is passed an environment list
• The address of the array of pointers is
contained in the global variable environ:
extern char **environ;
C PROGRAM MEMORY LAYOUT
C PROGRAM MEMORY LAYOUT
• Text segment contains the machine instruction
that the CPU executes (shareable, read-only)
• Initialised data segment contains variables that
are specifically initialised in the program
• Uninitialized data segment (bss) which is
initialised by the kernel to arithmetic 0 or null
pointers before program execution
• Stack, where automatic variables and function
information are stored
• Heap for dynamic memory allocation
SHARED LIBRARIES
• Remove common library routines from the
executable file, maintaining a single copy
somewhere in memory
• Reduces the size of each executable, but adds
runtime overhead
• Library function can be replaced with new
versions without having to edit every program
which use them
• -static to gcc disables use of shared
memory
RESOURCE LIMITS
Every process has a set of resource limits, some
of which can be queried/changed by
#include <sys/resources.h>
int getrlimit(int resource, struct rlimit
*rlptr)
int setrlimit(int resource, const struct
rlimit rlptr);
Both return 0 if OK, nonzero on error
RESOURCE LIMITS
• A process can change its soft limit to a value
less than or equal to its hard limit
• A process can lower its hard limit to a value
greater then or equal to its soft limit
• Only a superuser process can raise hard limits
• Resource limits affect the calling process and
are inherited by its children
• Limits include process available memory, open
files, max CPU time, max number of locks…
PROCESS IDENTIFIERS
• Every process has a unique ID
• PIDs are reused (delay reuse)
• PID 0 is usually the scheduler process
(swapper), and is part of the kernel
• PID 1 is usually init and is invoked by the
kernel at the end of the bootstrap procedure
• It is responsible for bringing up a UNIX system,
bootstrapping the kernel and running read
system-dependent initialisation files
(/etc/rc*)
FORK
• An existing process can create a new one by
calling the fork function
• The new process is called the child process
• Function returns twice, once in the child and
once in the parent process
#include <unistd.h>
pid_t fork(void);
Returns 0 in child, child ID in
parent and 1 on error
FORK
• Both processes continue executing with the instruction
that follows the fork
• The child is a copy of the parent (child gets a copy of
data space, heap and stack)
• Parent and child do not share portions of memory,
however they do share text segment
• Copy-on-write (COW) is generally used, where
regions are shared and have their protections
changed by the kernel to read-only. If either process
tries to modify them, the kernel makes a copy of that
place of memory only (typically page in a virtual
memory system)
FORK
int main(void)
{
pid_t pid;
if ((pid = fork()) < 0)
{
fprintf(stderr, "fork error");
}
else if (pid == 0)
{
// Child process
} else
{
// Parent process
}
exit(0);
}
FORK USES
• Two main reasons:
• When a process wants to duplicate itself so
that the parent and child can execute
different sections of code at the same time
(common for network servers)
• When a process wants to execute a different
program (common for shells)
FILE SHARING
• All file descriptors that are open in the parent
are duplicated in the child (dup)
• The parent and child share a file table entry
for every open descriptor, sharing the same
offset
• Two cases for handling descriptors after fork:
• Parent waits for child process to complete
• Both processes go their own way, parent and
child close descriptors they don’t need. This is
usually the case for network servers
FILE SHARING
SHARED PROPERTIES
• Real, effective, supplementary IDs
• Process group ID
• Controlling terminal
• CWD, root directory
• File creation mode mask
• Signal mask and dispositions
• Environment
• Attached shared memory segments
• Memory mappings
• Resource limits
VFORK
• vfork is intended to create a new process when
the purpose of the new process is to exec a new
program
• Creates a new process like fork without copying
the address space of the parent into the child
• The child runs in the address space of the parent
• This optimisation provides an efficient gain on
some paged virtual-memory implementations
• Guarantees that the child runs first until it calls
exec or exit
EXIT FUNCTIONS
• A process can terminate normally in 5 ways:
• Executing a return from main
• Calling the exit function
• Calling _exit or _Exit
• Executing a return from the start routine of the
last thread in the process
• Calling pthread_exit from the last thread in
the process
• Abnormal termination include calling abort and
reception of certain signals
EXIT FUNCTIONS
• The init process becomes the parent of any
process whose parent terminates (the process is
inherited by init)
• Whenever a process terminates the kernel goes
through all active processes to see whether the
terminating process is the parent of any
process that still exists
• If so, the parent process ID of the surviving
process is changed to 1
WAIT FUNCTIONS
• When a process terminates the kernel notifies
the parent by sending the SIGCHLD signal
• This has to be an asynchronous notification
• Parent can ignore signal or provide a function
to handle it (default is to ignore)
#include <sys/wait>
pid_t wait(int *statloc);
pid_t waitpid(pid_t pid, int *statloc,
int options);
Return PID if OK, 1 on error or 0
WAIT FUNCTIONS
• A process that calls wait or waitpid can:
• Block if all its children are still running
• Return immediately with the termination
status of a child
• Return immediately with an error if it doesn’t
have any child processes
• If a child already terminated and is a zombie
wait returns immediately with the child’s status
• statloc is a pointer to an integer
WAIT FUNCTIONS
Two additional functions allow the kernel to return a
summary of the resources used by the terminated process
and all its children
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resources.h>
pid_t wait3(int *statloc, int options,
struct rusage *rusage);
pid_t wait4(pid_t pid, int *statloc, int
options, struct rusage *rusage);
Return PID if OK, 1 on error or 0
RACE CONDITIONS
• A race condition occurs when multiple
processes are trying to do something with
shared data and the final outcome depends
on the order in which the processes run
• A process that wants to wait for a child to
terminate must call one of the wait
functions
• To avoid race conditions (and polling)
signalling between multiple processes is
required
EXEC FUNCTIONS
• When exec is called, the process is completely
replaced by the new program and the new
program starts executing its main function
• The PID does not change, because a new
process is not created
• Process text, data, heap and stack segments
are replaced by the new program from disk
• The exec, fork and wait functions are the only
process control primitives, except for additional
built-in functions
EXEC FUNCTIONS
#include <unistd.h>
int execl(const char *pathname, const char* arg0
… /* (char *) 0 */);
int execv(const char *pathname, char *const
argv[]);
int execle(const char *pathname, const char
*arg0, … /*(char *) 0, char envp[] */);
int execve(const char *pathname, char *const
argv[], char *const envp[]);
int execlp(const char *filename, const char
*arg0, … /* (char *)0 */);
int execvp(const char *filename, char *const
argv[]);
Return 1 on error, no return on success
EXEC FUNCTIONS
CHANGING USER/GROUP IDS
EXEC FUNCTIONS
• New program inherits some properties from the
calling process:
• PID and parent PID
• User and group IDs
• Controlling terminal
• CWD, root directory
• File mode creation mask
• File locks
• Process signal mask
• Pending signals
• Resource limits
MISC FUNCTIONS
• system allows the user to execute a command
string from within the program
• getlogin return the user login name
#include <stdlib.h>
#include <unistd.h>
int system(const char* cmdstring)
Return value depends on operation
char *getlogin(void)
Return NULL on error
PROCESS GROUPS
• A collection of one or more processes (usually
associated with the same job) that can receive signals
from the same terminal
• Each group has a unique process group ID
• Each process group can have a leader (Group ID =
process ID)
• Leader can create a process group, create processes
in the group and then terminate
• Group exists as long as at least one process is in the
group (group lifetime)

More Related Content

What's hot

Kernel Recipes 2017: Using Linux perf at Netflix
Kernel Recipes 2017: Using Linux perf at NetflixKernel Recipes 2017: Using Linux perf at Netflix
Kernel Recipes 2017: Using Linux perf at Netflix
Brendan Gregg
 
101 3.5 create, monitor and kill processes v2
101 3.5 create, monitor and kill processes v2101 3.5 create, monitor and kill processes v2
101 3.5 create, monitor and kill processes v2
Acácio Oliveira
 
Modern Linux Tracing Landscape
Modern Linux Tracing LandscapeModern Linux Tracing Landscape
Modern Linux Tracing Landscape
Sasha Goldshtein
 
Process injection - Malware style
Process injection - Malware styleProcess injection - Malware style
Process injection - Malware style
Sander Demeester
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloud
Andrea Righi
 
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CanSecWest
 
Compromising Linux Virtual Machines with Debugging Mechanisms
Compromising Linux Virtual Machines with Debugging MechanismsCompromising Linux Virtual Machines with Debugging Mechanisms
Compromising Linux Virtual Machines with Debugging Mechanisms
Russell Sanford
 
Bypassing ASLR Exploiting CVE 2015-7545
Bypassing ASLR Exploiting CVE 2015-7545Bypassing ASLR Exploiting CVE 2015-7545
Bypassing ASLR Exploiting CVE 2015-7545
Kernel TLV
 
DTrace Topics: Introduction
DTrace Topics: IntroductionDTrace Topics: Introduction
DTrace Topics: Introduction
Brendan Gregg
 
Tracing MariaDB server with bpftrace - MariaDB Server Fest 2021
Tracing MariaDB server with bpftrace - MariaDB Server Fest 2021Tracing MariaDB server with bpftrace - MariaDB Server Fest 2021
Tracing MariaDB server with bpftrace - MariaDB Server Fest 2021
Valeriy Kravchuk
 
DEF CON 23 - Ryan o'neil - advances in linux forensics with ecfs
DEF CON 23 - Ryan o'neil - advances in linux forensics with ecfsDEF CON 23 - Ryan o'neil - advances in linux forensics with ecfs
DEF CON 23 - Ryan o'neil - advances in linux forensics with ecfs
Felipe Prado
 
Overview of FreeBSD PMC Tools
Overview of FreeBSD PMC ToolsOverview of FreeBSD PMC Tools
Overview of FreeBSD PMC Tools
ACMBangalore
 
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
OpenEBS
 
Cache profiling on ARM Linux
Cache profiling on ARM LinuxCache profiling on ARM Linux
Cache profiling on ARM Linux
Prabindh Sundareson
 
Operating Systems - A Primer
Operating Systems - A PrimerOperating Systems - A Primer
Operating Systems - A Primer
Saumil Shah
 
Kernel Recipes 2017: Performance Analysis with BPF
Kernel Recipes 2017: Performance Analysis with BPFKernel Recipes 2017: Performance Analysis with BPF
Kernel Recipes 2017: Performance Analysis with BPF
Brendan Gregg
 
Performance Analysis: The USE Method
Performance Analysis: The USE MethodPerformance Analysis: The USE Method
Performance Analysis: The USE Method
Brendan Gregg
 
Accumulo 1.4 Features and Roadmap
Accumulo 1.4 Features and RoadmapAccumulo 1.4 Features and Roadmap
Accumulo 1.4 Features and Roadmap
Aaron Cordova
 
Profiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf ToolsProfiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf Tools
emBO_Conference
 
Ixgbe internals
Ixgbe internalsIxgbe internals
Ixgbe internals
SUSE Labs Taipei
 

What's hot (20)

Kernel Recipes 2017: Using Linux perf at Netflix
Kernel Recipes 2017: Using Linux perf at NetflixKernel Recipes 2017: Using Linux perf at Netflix
Kernel Recipes 2017: Using Linux perf at Netflix
 
101 3.5 create, monitor and kill processes v2
101 3.5 create, monitor and kill processes v2101 3.5 create, monitor and kill processes v2
101 3.5 create, monitor and kill processes v2
 
Modern Linux Tracing Landscape
Modern Linux Tracing LandscapeModern Linux Tracing Landscape
Modern Linux Tracing Landscape
 
Process injection - Malware style
Process injection - Malware styleProcess injection - Malware style
Process injection - Malware style
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloud
 
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
 
Compromising Linux Virtual Machines with Debugging Mechanisms
Compromising Linux Virtual Machines with Debugging MechanismsCompromising Linux Virtual Machines with Debugging Mechanisms
Compromising Linux Virtual Machines with Debugging Mechanisms
 
Bypassing ASLR Exploiting CVE 2015-7545
Bypassing ASLR Exploiting CVE 2015-7545Bypassing ASLR Exploiting CVE 2015-7545
Bypassing ASLR Exploiting CVE 2015-7545
 
DTrace Topics: Introduction
DTrace Topics: IntroductionDTrace Topics: Introduction
DTrace Topics: Introduction
 
Tracing MariaDB server with bpftrace - MariaDB Server Fest 2021
Tracing MariaDB server with bpftrace - MariaDB Server Fest 2021Tracing MariaDB server with bpftrace - MariaDB Server Fest 2021
Tracing MariaDB server with bpftrace - MariaDB Server Fest 2021
 
DEF CON 23 - Ryan o'neil - advances in linux forensics with ecfs
DEF CON 23 - Ryan o'neil - advances in linux forensics with ecfsDEF CON 23 - Ryan o'neil - advances in linux forensics with ecfs
DEF CON 23 - Ryan o'neil - advances in linux forensics with ecfs
 
Overview of FreeBSD PMC Tools
Overview of FreeBSD PMC ToolsOverview of FreeBSD PMC Tools
Overview of FreeBSD PMC Tools
 
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
 
Cache profiling on ARM Linux
Cache profiling on ARM LinuxCache profiling on ARM Linux
Cache profiling on ARM Linux
 
Operating Systems - A Primer
Operating Systems - A PrimerOperating Systems - A Primer
Operating Systems - A Primer
 
Kernel Recipes 2017: Performance Analysis with BPF
Kernel Recipes 2017: Performance Analysis with BPFKernel Recipes 2017: Performance Analysis with BPF
Kernel Recipes 2017: Performance Analysis with BPF
 
Performance Analysis: The USE Method
Performance Analysis: The USE MethodPerformance Analysis: The USE Method
Performance Analysis: The USE Method
 
Accumulo 1.4 Features and Roadmap
Accumulo 1.4 Features and RoadmapAccumulo 1.4 Features and Roadmap
Accumulo 1.4 Features and Roadmap
 
Profiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf ToolsProfiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf Tools
 
Ixgbe internals
Ixgbe internalsIxgbe internals
Ixgbe internals
 

Viewers also liked

Mothers day ideas for college students
Mothers day ideas for college studentsMothers day ideas for college students
Mothers day ideas for college studentsHelpWithAssignment.com
 
Forecasting Assignment Help
Forecasting Assignment HelpForecasting Assignment Help
Forecasting Assignment Help
HelpWithAssignment.com
 
Performance appraisal in human resource management
Performance appraisal in human resource managementPerformance appraisal in human resource management
Performance appraisal in human resource management
HelpWithAssignment.com
 
International Accounting
International AccountingInternational Accounting
International Accounting
HelpWithAssignment.com
 
Target market selection
Target market selectionTarget market selection
Target market selection
HelpWithAssignment.com
 
Rights of the Parties and Discharge; Remedies for Breach of Contract
Rights of the Parties and Discharge; Remedies for Breach of ContractRights of the Parties and Discharge; Remedies for Breach of Contract
Rights of the Parties and Discharge; Remedies for Breach of Contract
HelpWithAssignment.com
 
Quantum Assignment Help
Quantum Assignment HelpQuantum Assignment Help
Quantum Assignment Help
HelpWithAssignment.com
 
Systems Programming - File IO
Systems Programming - File IOSystems Programming - File IO
Systems Programming - File IO
HelpWithAssignment.com
 
Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715
HelpWithAssignment.com
 
Significance of information in marketing
Significance of information in marketingSignificance of information in marketing
Significance of information in marketing
HelpWithAssignment.com
 
Halloween Party, Costume and Theme Ideas for Students
Halloween Party, Costume and Theme Ideas for StudentsHalloween Party, Costume and Theme Ideas for Students
Halloween Party, Costume and Theme Ideas for Students
HelpWithAssignment.com
 
Proportions and Confidence Intervals in Biostatistics
Proportions and Confidence Intervals in BiostatisticsProportions and Confidence Intervals in Biostatistics
Proportions and Confidence Intervals in Biostatistics
HelpWithAssignment.com
 
Hypothesis Testing Assignment Help
Hypothesis Testing Assignment HelpHypothesis Testing Assignment Help
Hypothesis Testing Assignment Help
HelpWithAssignment.com
 
Constructivism assignment help
Constructivism assignment helpConstructivism assignment help
Constructivism assignment help
HelpWithAssignment.com
 

Viewers also liked (14)

Mothers day ideas for college students
Mothers day ideas for college studentsMothers day ideas for college students
Mothers day ideas for college students
 
Forecasting Assignment Help
Forecasting Assignment HelpForecasting Assignment Help
Forecasting Assignment Help
 
Performance appraisal in human resource management
Performance appraisal in human resource managementPerformance appraisal in human resource management
Performance appraisal in human resource management
 
International Accounting
International AccountingInternational Accounting
International Accounting
 
Target market selection
Target market selectionTarget market selection
Target market selection
 
Rights of the Parties and Discharge; Remedies for Breach of Contract
Rights of the Parties and Discharge; Remedies for Breach of ContractRights of the Parties and Discharge; Remedies for Breach of Contract
Rights of the Parties and Discharge; Remedies for Breach of Contract
 
Quantum Assignment Help
Quantum Assignment HelpQuantum Assignment Help
Quantum Assignment Help
 
Systems Programming - File IO
Systems Programming - File IOSystems Programming - File IO
Systems Programming - File IO
 
Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715
 
Significance of information in marketing
Significance of information in marketingSignificance of information in marketing
Significance of information in marketing
 
Halloween Party, Costume and Theme Ideas for Students
Halloween Party, Costume and Theme Ideas for StudentsHalloween Party, Costume and Theme Ideas for Students
Halloween Party, Costume and Theme Ideas for Students
 
Proportions and Confidence Intervals in Biostatistics
Proportions and Confidence Intervals in BiostatisticsProportions and Confidence Intervals in Biostatistics
Proportions and Confidence Intervals in Biostatistics
 
Hypothesis Testing Assignment Help
Hypothesis Testing Assignment HelpHypothesis Testing Assignment Help
Hypothesis Testing Assignment Help
 
Constructivism assignment help
Constructivism assignment helpConstructivism assignment help
Constructivism assignment help
 

Similar to Systems Programming Assignment Help - Processes

Lecture2 process structure and programming
Lecture2   process structure and programmingLecture2   process structure and programming
Lecture2 process structure and programming
Mohammed Farrag
 
04_ForkPipe.pptx
04_ForkPipe.pptx04_ForkPipe.pptx
04_ForkPipe.pptx
vnwzympx
 
3 process management
3 process management3 process management
3 process management
Dr. Loganathan R
 
Process management
Process managementProcess management
Process management
Akshay Ithape
 
Lect3 process
Lect3 processLect3 process
Lect3 process
santosh rao
 
Process management in operating system | process states | PCB | FORK() | Zomb...
Process management in operating system | process states | PCB | FORK() | Zomb...Process management in operating system | process states | PCB | FORK() | Zomb...
Process management in operating system | process states | PCB | FORK() | Zomb...
Shivam Mitra
 
Chapter -2 operating system presentation
Chapter -2 operating system presentationChapter -2 operating system presentation
Chapter -2 operating system presentation
chnrketan
 
Processes, Threads.pptx
Processes, Threads.pptxProcesses, Threads.pptx
Processes, Threads.pptx
LECO9
 
Processes, Threads.pptx
Processes, Threads.pptxProcesses, Threads.pptx
Processes, Threads.pptx
SKUP1
 
Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - Threads
Peter Tröger
 
Char Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesChar Drivers And Debugging Techniques
Char Drivers And Debugging Techniques
YourHelper1
 
Systemcall1
Systemcall1Systemcall1
Systemcall1
pavimalpani
 
Advanced Operating Systems......Process Management
Advanced Operating Systems......Process ManagementAdvanced Operating Systems......Process Management
Advanced Operating Systems......Process Management
Veejeya Kumbhar
 
Lecture_Slide_4.pptx
Lecture_Slide_4.pptxLecture_Slide_4.pptx
Lecture_Slide_4.pptx
DiptoRoy21
 
process creation OS
process creation OSprocess creation OS
process creation OS
Kiran Kumar Thota
 
Unix Shell and System Boot Process
Unix Shell and System Boot ProcessUnix Shell and System Boot Process
Unix Shell and System Boot Process
Arvind Krishnaa
 
Os lectures
Os lecturesOs lectures
Os lectures
Adnan Ghafoor
 
How many ways to monitor oracle golden gate-Collaborate 14
How many ways to monitor oracle golden gate-Collaborate 14How many ways to monitor oracle golden gate-Collaborate 14
How many ways to monitor oracle golden gate-Collaborate 14
Bobby Curtis
 

Similar to Systems Programming Assignment Help - Processes (20)

Lecture2 process structure and programming
Lecture2   process structure and programmingLecture2   process structure and programming
Lecture2 process structure and programming
 
04_ForkPipe.pptx
04_ForkPipe.pptx04_ForkPipe.pptx
04_ForkPipe.pptx
 
3 process management
3 process management3 process management
3 process management
 
Process management
Process managementProcess management
Process management
 
Lect3 process
Lect3 processLect3 process
Lect3 process
 
Process management in operating system | process states | PCB | FORK() | Zomb...
Process management in operating system | process states | PCB | FORK() | Zomb...Process management in operating system | process states | PCB | FORK() | Zomb...
Process management in operating system | process states | PCB | FORK() | Zomb...
 
Lecture 5 process concept
Lecture 5   process conceptLecture 5   process concept
Lecture 5 process concept
 
Chapter -2 operating system presentation
Chapter -2 operating system presentationChapter -2 operating system presentation
Chapter -2 operating system presentation
 
Processes, Threads.pptx
Processes, Threads.pptxProcesses, Threads.pptx
Processes, Threads.pptx
 
Processes, Threads.pptx
Processes, Threads.pptxProcesses, Threads.pptx
Processes, Threads.pptx
 
Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - Threads
 
Char Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesChar Drivers And Debugging Techniques
Char Drivers And Debugging Techniques
 
Systemcall1
Systemcall1Systemcall1
Systemcall1
 
Advanced Operating Systems......Process Management
Advanced Operating Systems......Process ManagementAdvanced Operating Systems......Process Management
Advanced Operating Systems......Process Management
 
Lecture_Slide_4.pptx
Lecture_Slide_4.pptxLecture_Slide_4.pptx
Lecture_Slide_4.pptx
 
process creation OS
process creation OSprocess creation OS
process creation OS
 
Unix Shell and System Boot Process
Unix Shell and System Boot ProcessUnix Shell and System Boot Process
Unix Shell and System Boot Process
 
Os lectures
Os lecturesOs lectures
Os lectures
 
Unix kernal
Unix kernalUnix kernal
Unix kernal
 
How many ways to monitor oracle golden gate-Collaborate 14
How many ways to monitor oracle golden gate-Collaborate 14How many ways to monitor oracle golden gate-Collaborate 14
How many ways to monitor oracle golden gate-Collaborate 14
 

Recently uploaded

Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 

Recently uploaded (20)

Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 

Systems Programming Assignment Help - Processes

  • 2. A UNIX PROCESS • Each process is identified by a unique integer, the process identifier (PID) • Each identifier is associated with a process descriptor inside the OS scheduler • A list of PIDs can be obtained with the ps aux command • Information about each process can be found in /proc/pidn which is a directory corresponding to each PID
  • 3. PROCESS STARTUP AND TERMINATION
  • 4. PROCESS STARTUP AND TERMINATION #include <stdlib.h> #include <unistd.h> int main(int argc, char *argv[]); void exit(int status); void _exit(int status); • main() is passed the command line arguments and should return the termination status • exit() takes the termination status and causes any files to be properly closed and memory released • _exit() terminates the process immediately closing all file descriptors but not cleanly
  • 5. PROCESS STARTUP AND TERMINATION #include <stdlib.h> int atexit(void (*func)(void)); Returns 0 if OK, -1 on error • The exit() function calls any registered user exit functions before calling the termination functions • Exit functions are registered using atexit() which takes a pointer to a function that does not return anything and takes no arguments • Calling _exit() directly allows us to skip these registered atexit functions
  • 6. PROCESS STARTUP AND TERMINATION
  • 7. ENVIRONMENT LIST • Each program is passed an environment list • The address of the array of pointers is contained in the global variable environ: extern char **environ;
  • 9. C PROGRAM MEMORY LAYOUT • Text segment contains the machine instruction that the CPU executes (shareable, read-only) • Initialised data segment contains variables that are specifically initialised in the program • Uninitialized data segment (bss) which is initialised by the kernel to arithmetic 0 or null pointers before program execution • Stack, where automatic variables and function information are stored • Heap for dynamic memory allocation
  • 10. SHARED LIBRARIES • Remove common library routines from the executable file, maintaining a single copy somewhere in memory • Reduces the size of each executable, but adds runtime overhead • Library function can be replaced with new versions without having to edit every program which use them • -static to gcc disables use of shared memory
  • 11. RESOURCE LIMITS Every process has a set of resource limits, some of which can be queried/changed by #include <sys/resources.h> int getrlimit(int resource, struct rlimit *rlptr) int setrlimit(int resource, const struct rlimit rlptr); Both return 0 if OK, nonzero on error
  • 12. RESOURCE LIMITS • A process can change its soft limit to a value less than or equal to its hard limit • A process can lower its hard limit to a value greater then or equal to its soft limit • Only a superuser process can raise hard limits • Resource limits affect the calling process and are inherited by its children • Limits include process available memory, open files, max CPU time, max number of locks…
  • 13. PROCESS IDENTIFIERS • Every process has a unique ID • PIDs are reused (delay reuse) • PID 0 is usually the scheduler process (swapper), and is part of the kernel • PID 1 is usually init and is invoked by the kernel at the end of the bootstrap procedure • It is responsible for bringing up a UNIX system, bootstrapping the kernel and running read system-dependent initialisation files (/etc/rc*)
  • 14. FORK • An existing process can create a new one by calling the fork function • The new process is called the child process • Function returns twice, once in the child and once in the parent process #include <unistd.h> pid_t fork(void); Returns 0 in child, child ID in parent and 1 on error
  • 15. FORK • Both processes continue executing with the instruction that follows the fork • The child is a copy of the parent (child gets a copy of data space, heap and stack) • Parent and child do not share portions of memory, however they do share text segment • Copy-on-write (COW) is generally used, where regions are shared and have their protections changed by the kernel to read-only. If either process tries to modify them, the kernel makes a copy of that place of memory only (typically page in a virtual memory system)
  • 16. FORK int main(void) { pid_t pid; if ((pid = fork()) < 0) { fprintf(stderr, "fork error"); } else if (pid == 0) { // Child process } else { // Parent process } exit(0); }
  • 17. FORK USES • Two main reasons: • When a process wants to duplicate itself so that the parent and child can execute different sections of code at the same time (common for network servers) • When a process wants to execute a different program (common for shells)
  • 18. FILE SHARING • All file descriptors that are open in the parent are duplicated in the child (dup) • The parent and child share a file table entry for every open descriptor, sharing the same offset • Two cases for handling descriptors after fork: • Parent waits for child process to complete • Both processes go their own way, parent and child close descriptors they don’t need. This is usually the case for network servers
  • 20. SHARED PROPERTIES • Real, effective, supplementary IDs • Process group ID • Controlling terminal • CWD, root directory • File creation mode mask • Signal mask and dispositions • Environment • Attached shared memory segments • Memory mappings • Resource limits
  • 21. VFORK • vfork is intended to create a new process when the purpose of the new process is to exec a new program • Creates a new process like fork without copying the address space of the parent into the child • The child runs in the address space of the parent • This optimisation provides an efficient gain on some paged virtual-memory implementations • Guarantees that the child runs first until it calls exec or exit
  • 22. EXIT FUNCTIONS • A process can terminate normally in 5 ways: • Executing a return from main • Calling the exit function • Calling _exit or _Exit • Executing a return from the start routine of the last thread in the process • Calling pthread_exit from the last thread in the process • Abnormal termination include calling abort and reception of certain signals
  • 23. EXIT FUNCTIONS • The init process becomes the parent of any process whose parent terminates (the process is inherited by init) • Whenever a process terminates the kernel goes through all active processes to see whether the terminating process is the parent of any process that still exists • If so, the parent process ID of the surviving process is changed to 1
  • 24. WAIT FUNCTIONS • When a process terminates the kernel notifies the parent by sending the SIGCHLD signal • This has to be an asynchronous notification • Parent can ignore signal or provide a function to handle it (default is to ignore) #include <sys/wait> pid_t wait(int *statloc); pid_t waitpid(pid_t pid, int *statloc, int options); Return PID if OK, 1 on error or 0
  • 25. WAIT FUNCTIONS • A process that calls wait or waitpid can: • Block if all its children are still running • Return immediately with the termination status of a child • Return immediately with an error if it doesn’t have any child processes • If a child already terminated and is a zombie wait returns immediately with the child’s status • statloc is a pointer to an integer
  • 26. WAIT FUNCTIONS Two additional functions allow the kernel to return a summary of the resources used by the terminated process and all its children #include <sys/types.h> #include <sys/wait.h> #include <sys/time.h> #include <sys/resources.h> pid_t wait3(int *statloc, int options, struct rusage *rusage); pid_t wait4(pid_t pid, int *statloc, int options, struct rusage *rusage); Return PID if OK, 1 on error or 0
  • 27. RACE CONDITIONS • A race condition occurs when multiple processes are trying to do something with shared data and the final outcome depends on the order in which the processes run • A process that wants to wait for a child to terminate must call one of the wait functions • To avoid race conditions (and polling) signalling between multiple processes is required
  • 28. EXEC FUNCTIONS • When exec is called, the process is completely replaced by the new program and the new program starts executing its main function • The PID does not change, because a new process is not created • Process text, data, heap and stack segments are replaced by the new program from disk • The exec, fork and wait functions are the only process control primitives, except for additional built-in functions
  • 29. EXEC FUNCTIONS #include <unistd.h> int execl(const char *pathname, const char* arg0 … /* (char *) 0 */); int execv(const char *pathname, char *const argv[]); int execle(const char *pathname, const char *arg0, … /*(char *) 0, char envp[] */); int execve(const char *pathname, char *const argv[], char *const envp[]); int execlp(const char *filename, const char *arg0, … /* (char *)0 */); int execvp(const char *filename, char *const argv[]); Return 1 on error, no return on success
  • 32. EXEC FUNCTIONS • New program inherits some properties from the calling process: • PID and parent PID • User and group IDs • Controlling terminal • CWD, root directory • File mode creation mask • File locks • Process signal mask • Pending signals • Resource limits
  • 33. MISC FUNCTIONS • system allows the user to execute a command string from within the program • getlogin return the user login name #include <stdlib.h> #include <unistd.h> int system(const char* cmdstring) Return value depends on operation char *getlogin(void) Return NULL on error
  • 34. PROCESS GROUPS • A collection of one or more processes (usually associated with the same job) that can receive signals from the same terminal • Each group has a unique process group ID • Each process group can have a leader (Group ID = process ID) • Leader can create a process group, create processes in the group and then terminate • Group exists as long as at least one process is in the group (group lifetime)