Tips & Tricks
Your SlideShare is downloading.
Like this presentation? Why not share!
by Anil Kumar Pugalia
Linux File System
by Anil Kumar Pugalia
Introduction to Linux
by Anil Kumar Pugalia
by Anil Kumar Pugalia
by Anil Kumar Pugalia
by Anil Kumar Pugalia
Email sent successfully!
Show related SlideShares at end
Anil Kumar Pugalia
, Linux Geek and Open Source Hardware & Software Freak, Corporate Trainer, Entrepreneur in Automation
eSrijan Innovations Private Limited
Oct 03, 2010
Comment goes here.
12 hours ago
Are you sure you want to
Your message goes here
Be the first to comment
4 months ago
11 months ago
2 years ago
Number of Embeds
Flagged as inappropriate
Flag as inappropriate
No notes for slide
Transcript of "Processes"
1. Processes© 2010 Anil Kumar Pugalia <firstname.lastname@example.org> All Rights Reserved.
What to Expect?Ws of a ProcessProcesses in Linux Scheduling & Preemption Process States & Transitions Process Management Programming the Processes © 2010 Anil Kumar Pugalia <email@example.com> 2 All Rights Reserved.
What is a Process?Program in ExecutionExecutable/Program Loaded → ProcessProgram is just the Code & initial Data partAdditionally Value of Variables Stack Heap Program Counter Processor Registers And any other OS resources, needed by the Programmake it a Process © 2010 Anil Kumar Pugalia <firstname.lastname@example.org> 3 All Rights Reserved.
Why we need a Process?To do any task or jobMoreover, to achieve multi-processing Really do multiple tasks at a time (in multi-processor systems), Or At least get a feel of doing multiple tasks at a time (on uni-processor systems)In turn needs Timesharing (on same processor) Scheduling Priority And for all these: Process Identifier (pid) © 2010 Anil Kumar Pugalia <email@example.com> 4 All Rights Reserved.
Lets ViewShell Local Processes: psConsole attached System Processes: ps aAll System Processes: ps axList many more details: Add lObserve uid, pid, ppid, priority, nice, status, tty, timeDynamic Process Status: topTry pid.c © 2010 Anil Kumar Pugalia <firstname.lastname@example.org> 5 All Rights Reserved.
Processes in Linux© 2010 Anil Kumar Pugalia <email@example.com> 6 All Rights Reserved.
Linux SchedulersProvide multi-tasking capabilities by Time Slicing Preemption Based on various task priorities Specified by its scheduling policiesUnderstand the following execution instances Kernel Thread User Process User Thread © 2010 Anil Kumar Pugalia <firstname.lastname@example.org> 7 All Rights Reserved.
Linux Schedulers ...Linux Basic Scheduling Normal (SCHED_OTHER) – Fairness SchedulingOther Advanced Scheduling supported Round Robin (SCHED_RR) FIFO (SCHED_FIFO)All Schedulers are O(1) © 2010 Anil Kumar Pugalia <email@example.com> 8 All Rights Reserved.
Linux Kernel Preemption LevelsNone (PREEMPT_NONE) No forced preemption Overall throughput, on average, is goodVoluntary (PREEMPT_VOLUNTARY) First stage of latency reduction Explicit preemption points are placed at strategic locationsStandard (PREEMPT_DESKTOP) Preemption enabled everywhere except within critical sections Good for soft real-time applications, like audio, multimedia, …Kernel Parameter: preempt © 2010 Anil Kumar Pugalia <firstname.lastname@example.org> 9 All Rights Reserved.
Kernel Preemption Visualization Time User Space System Call Interface Kernel Space Process A Process B © 2010 Anil Kumar Pugalia <email@example.com> 10 All Rights Reserved.
Process Context Switch Time Save State Save State into PCB A into PCB B Reload State Reload State from PCB B from PCB AInterrupt or System Call Interrupt or System Call Process A Time Wasted in Context Switch Process B © 2010 Anil Kumar Pugalia <firstname.lastname@example.org> 11 All Rights Reserved.
Generic Process State Diagram Time Run-Out New Terminated Admitted Dispatch Exit Ready Running Zombie Wakeup on Block on I/O or event I/O or completion wait event Blocked Stopped Uninterruptible or InterruptibleReady & Blocked states have QueuesAdditional States in Linux Defunct / Zombie (Terminated but not reaped by its parent) Stopped (By job control signal or because being traced) © 2010 Anil Kumar Pugalia <email@example.com> 12 All Rights Reserved.
Linux Process StatesProcess States as in Linux TASK_RUNNING (R) – ready or running TASK_INTERRUPTIBLE (S) – blocked (waiting for an event) TASK_UNINTERRUPTIBLE (D) – blocked (usually for I/O) TASK_ZOMBIE (Z) – terminated but not cleaned up by its parent TASK_STOPPED (T) – execution stoppedMutually exclusiveAdditional Information: Foreground (+), Threaded (l) © 2010 Anil Kumar Pugalia <firstname.lastname@example.org> 13 All Rights Reserved.
Process Management in LinuxNeeds collation of all this information Address Space (Code, Variables, Stack, Heap) Processor State (PC, Registers, …) OS resources in use (File descriptors, ...) Scheduling Info, Priority, ... Preemption Info, Process State, ...for every ProcessStored in structure of type task_struct Maintained by Linux Kernel on a per process basis Also called the Process Descriptor / Process Control Block © 2010 Anil Kumar Pugalia <email@example.com> 14 All Rights Reserved.
Process Control Block (PCB)Listing: <kernel_source>/include/linux/sched.hSome of its fields are volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ void *stack; unsigned int flags; /* per process flags */ int prio, static_prio, normal_prio; unsigned int rt_priority; const struct sched_class *sched_class; unsigned int policy; struct mm_struct *mm, active_mm; / Pointers to Memory Regions, Descriptors */ pid_t pid, tgid; struct task_struct *real_parent; /* real parent process */ struct task_struct *parent; /* recipient of SIGCHLD, wait4() reports */ struct list_head children /* list of its children */, sibling; /* linkage in its parents children list */ struct task_struct *group_leader; /* threadgroup leader */ struct list_head thread_group; struct fs_struct fs; /* file system info like current directory, … */ struct files_struct files; / file descriptors */ struct signal_struct signal; / signal handlers */ sigset_t blocked, real_blocked, saved_sigmask; struct sigpending pending; /* signals received */ © 2010 Anil Kumar Pugalia <firstname.lastname@example.org> 15 All Rights Reserved.
Basic Process Managementbg - Starts a suspended process in thebackgroundfg - Starts a suspended process in theforegroundjobs - Lists the jobs runningpidof - Find the process ID of a runningprogramtop - Display the processes that are usingthe most CPU resources © 2010 Anil Kumar Pugalia <email@example.com> 16 All Rights Reserved.
Programming Linux Processes © 2010 Anil Kumar Pugalia <firstname.lastname@example.org> 17 All Rights Reserved.
Process CreationFrom Shell By running a Command / Program / Script (Even by .) By exec ing a Command / ProgramBy Programming Using system() Simple but Inefficient Security risks Using fork() and exec() family function Comparatively complex Greater flexibility, speed, and security © 2010 Anil Kumar Pugalia <email@example.com> 18 All Rights Reserved.
Using the exec familyAll exec* functions do the same thing Just in different waysReplaces current program by a new oneAnd hence never returns, unless an errorNew program is immediately startedProcess remains the same Process Id, Parent Process Id Current directory, ... Open file descriptor tables, ... © 2010 Anil Kumar Pugalia <firstname.lastname@example.org> 19 All Rights Reserved.
exec* function specificsexec*p (execvp, execlp) Accepts a program name Searches it in the current execution path Others must be given the full pathexecv* (execv, execvp, execve) Argument list should be a NULL-terminated array of pointers to stringsexecl* (execl, execlp, execle) Argument list uses the varargs mechanismexec*e (execve, execle) Accepts an additional argument: an array of environment variables It should be a NULL-terminated array of pointers to character strings Each character string should be of the form “VARIABLE=value” © 2010 Anil Kumar Pugalia <email@example.com> 20 All Rights Reserved.
Cons of using forkForked child process executes the copy ofparent process programAnd typically, a fork is followed by exec Replacing the copy by the new programWhat is the point of copying?Overhead!! What else?Is there any way out to prevent this?Yes. And it is ... © 2010 Anil Kumar Pugalia <firstname.lastname@example.org> 21 All Rights Reserved.
Copy On Write (COW)Parent and Child shares the Address Space (ro)Data & other Resources are marked COWIf written to, a duplicate is made and eachprocess receives a unique copyConsequently, the duplication of resources occursonly when they are written toAvoids copy in cases of immediate execfork()s only overheads Duplication of the parents page tables Creation of a unique PCB for the child © 2010 Anil Kumar Pugalia <email@example.com> 22 All Rights Reserved.
Process TerminationParent & Children Processes terminate as usual Success Exit – Normal Success Error Exit – Normal Failure Fatal Exit – Signaled from Kernel Space for a Bug Kill Exit – Signaled by a ProcessBut which one of them terminates, first?Does it matter?If it matters, parents can wait for their children Using wait family of system calls And can retrieve information about its child’s terminationIn fact, wait does the cleanup act for the exited child © 2010 Anil Kumar Pugalia <firstname.lastname@example.org> 23 All Rights Reserved.
Using the wait familyFour different system calls in the wait family wait(): Block until one of its child processes exits waitpid(): Wait for a specific child to exit/stop/resume wait3(): Along with, return resource usage information about the exiting/stopping/resuming child process wait4(): wait3() for a specific child or childrenAll of these fill up a status code in an integer pointer argument about how the child process exited which can be decoded using ... © 2010 Anil Kumar Pugalia <email@example.com> 24 All Rights Reserved.
wait status macrosWIFEXITED, WEXITSTATUSWIFSIGNALED, WTERMSIG, WCOREDUMPWIFSTOPPED, WSTOPSIGWIFCONTINUED © 2010 Anil Kumar Pugalia <firstname.lastname@example.org> 25 All Rights Reserved.
What if Parents Dont Wait?If Parent dies before the Children Children become Orphans And are adopted by the Init Process which then does the cleanup on their exitIf a Child exits before the Parent Thats a sad thing :) It will become a Ghost / Zombie Note that, even if the parent does a wait later It remains a Zombie till then © 2010 Anil Kumar Pugalia <email@example.com> 26 All Rights Reserved.
Who Cleans Up a Zombie?Typically, again a Parent By doing a wait on itWhat if the parent exits without “wait”?Does it stay around in the system?Not really. It gets inherited by init which then cleans it up, right there © 2010 Anil Kumar Pugalia <firstname.lastname@example.org> 27 All Rights Reserved.
What all have we learnt?Ws of a Process?Process Scheduling & Preemption in LinuxProcess States & Transitions (Linux specific)Linux Process Management using PCBProgramming Linux Processes Creation Techniques: fork, exec* & COW Termination & Waiting Techniques Orphans & Zombies © 2010 Anil Kumar Pugalia <email@example.com> 28 All Rights Reserved.
Any Queries?© 2010 Anil Kumar Pugalia <firstname.lastname@example.org> 29 All Rights Reserved.