Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on

Published in: Technology, Business
  • People used to laugh at me behind my back before I was in shape or successful. Once I lost a lot of weight, I was so excited that I opened my own gym, and began helping others. I began to get quite a large following of students, and finally, I didn't catch someone laughing at me behind my back any longer. CLICK HERE NOW ●●●
    Are you sure you want to  Yes  No
    Your message goes here


  1. 1. Processes© 2010 Anil Kumar Pugalia <> All Rights Reserved.
  2. 2. What to Expect?Ws of a ProcessProcesses in Linux Scheduling & Preemption Process States & Transitions Process Management Programming the Processes © 2010 Anil Kumar Pugalia <> 2 All Rights Reserved.
  3. 3. 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 <> 3 All Rights Reserved.
  4. 4. 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 <> 4 All Rights Reserved.
  5. 5. 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 <> 5 All Rights Reserved.
  6. 6. Processes in Linux© 2010 Anil Kumar Pugalia <> 6 All Rights Reserved.
  7. 7. 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 <> 7 All Rights Reserved.
  8. 8. 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 <> 8 All Rights Reserved.
  9. 9. 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 <> 9 All Rights Reserved.
  10. 10. Kernel Preemption Visualization Time User Space System Call Interface Kernel Space Process A Process B © 2010 Anil Kumar Pugalia <> 10 All Rights Reserved.
  11. 11. 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 <> 11 All Rights Reserved.
  12. 12. 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 <> 12 All Rights Reserved.
  13. 13. 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 <> 13 All Rights Reserved.
  14. 14. 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 <> 14 All Rights Reserved.
  15. 15. 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 <> 15 All Rights Reserved.
  16. 16. 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 <> 16 All Rights Reserved.
  17. 17. Programming Linux Processes © 2010 Anil Kumar Pugalia <> 17 All Rights Reserved.
  18. 18. 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 <> 18 All Rights Reserved.
  19. 19. 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 <> 19 All Rights Reserved.
  20. 20. 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 <> 20 All Rights Reserved.
  21. 21. 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 <> 21 All Rights Reserved.
  22. 22. 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 <> 22 All Rights Reserved.
  23. 23. 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 <> 23 All Rights Reserved.
  24. 24. 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 <> 24 All Rights Reserved.
  26. 26. 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 <> 26 All Rights Reserved.
  27. 27. 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 <> 27 All Rights Reserved.
  28. 28. 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 <> 28 All Rights Reserved.
  29. 29. Any Queries?© 2010 Anil Kumar Pugalia <> 29 All Rights Reserved.