Processes and Non-Preemptive Scheduling

Total Page:16

File Type:pdf, Size:1020Kb

Processes and Non-Preemptive Scheduling An aside on concurrency • Timing and sequence of events are key concurrency issues • We will study classical OS concurrency issues, including Processes and implementation and use of classic OS mechanisms to support Non-Preemptive Scheduling concurrency • In a later course on parallel programming may revisit this material Otto J. Anshus • Later course on distributed systems you may want to use formal University of Tromsø, University of Oslo tools to understand and model timing and sequencing better Tore Larsen • Single CPU computers are designed to uphold a simple and rigid model of sequencing and timing. ”Under the hood,” even University of Tromsø single CPU systems are distributed in nature, and are carefully Kai Li organized to uphold strict external requirements Princeton University 28.08.03 1 28.08.03 2 Process Supporting and Using Processes • An instance of a program under execution • Multiprogramming – Program specifying (logical) control-flow (thread) – Supporting concurrent execution (parallel or transparently interleaved) – Data of multiple processes (or threads). – Private address space – Achieved by process- or context switching, switching the CPU(s) back – Open files and forth among the individual processes, keeping track of each process’ progress – Running environment • Concurrent programs • The most important operating system concept – Programs (or threads) that exploit multiprogramming for some purpose • Used for supporting the concurrent execution of independent or (e.g. performance, structure) cooperating program instances – Independent or cooperating • Used to structure applications and systems – Operating systems is important application area for concurrent programming. Many others (event driven programs, servers, ++) 28.08.03 3 28.08.03 4 1 Implementing processes Primitives of Processes • Os needs to keep track of all processes • Creation and termination – Keep track of it’s progress – fork, exec, wait, kill – Parent process • Signals – Metadata (priorities etc.) used by OS – Action, Return, Handler – Memory management – File management • Operations • Process table with one entry (Process Control Block) – block, yield per process • Synchronization • Will also align processes in queues – We will talk about this later 28.08.03 5 28.08.03 6 fork (UNIX) fork, exec, wait, kill • Spawns a new process (with new PID) • Return value tested for error, zero, or positive • Called in parent process • Zero, this is the child process • Returns in parent and child process – Typically redirect standard files, and – Call Exec to load a new program instead of the old • Return value in parent is child’s PID • Positive, this is the parent process • Return value in child is ’0’ • Wait, parent waits for child’s termination • Child gets duplicate, but separate, copy of parent’s – Wait before corresponding exit, parent blocks until exit user-level virtual address space – Exit before corresponding wait, child becomes zombie (un-dead) until • Child gets identical copy of parent’s open file wait descriptors • Kill, specified process terminates 28.08.03 7 28.08.03 8 2 When may OS switch contexts? Context Switching Issues • Performance – Should be no more than a few microseconds • Only when OS runs – Most time is spent SAVING and RESTORING the context of processes • Events potentially causing a context switch: • Less processor state to save, the better – Process created ( ) Non-Preemptive fork – Pentium has a multitasking mechanism, but SW can be faster if it saves – Process exits (exit) scheduling less of the state – Process blocks implicitly (I/O, IPC) • How to save time on the copying of context state? – Process blocks explicitly (yield) – Re-map (address) instead of copy (data) – User or System Level Trap •HW • Where to store Kernel data structures “shared” by all processes • SW: User level System Call • Memory Preemptive • Exception scheduling • How to give processes a fair share of CPU time – Kernel preempts current process • Preemptive scheduling, time-slice defines maximum time interval • Potential scheduling decision at “any of above” between scheduling decisions • +“Timer” to be able to limit running time of processes 28.08.03 9 28.08.03 10 Example Process State Transitions PC U s e r L e v e l P r o c e s s e s Process State Transition of P1 P2 P3 P4 MULTIPROGRAMMING •Uniprocessor: Interleaving Non-Preemptive Scheduling KERNEL (“pseudoparallelism”) Trap Service •Multiprocessor: Overlapping (“true Handler paralellism”) Terminate Trap Return P2 BlockedQueue (call scheduler) Handler Scheduler Terminate Running P3 P4 (call scheduler) dispatch Scheduler ReadyQueue Block for resource Scheduler Running (call scheduler) Current P1 dispatch Yield Dispatcher PCB’s Block for resource (call scheduler) (call scheduler) Create Yield a process Create (call scheduler) Ready a process Blocked Ready Memory resident part Blocked Resource becomes available Resource becomes available (move to ready queue) 28.08.03 (move to ready queue) 11 28.08.03 12 3 Stacks Scheduler • Remember: We have only one copy of the Kernel in memory => all processes “execute” the same kernel code • Non-preemptive scheduler invoked by explicit block or => Must have a kernel stack for each process yield calls, possibly also fork and exit • Used for storing parameters, return address, locally created • The simplest form variables in frames or activation records Scheduler: • Each process save current process state (store to PCB) – user stack choose next process to run – kernel stack dispatch (load PCB and run) • always empty when process is in user mode executing • Does this work? instructions • PCB (something) must be resident in memory • Remember the stacks • Does the Kernel need its own stack(s)? 28.08.03 13 28.08.03 14 More on Scheduler • Should the scheduler use a special stack? Win NT Idle –Yes, • because a user process can overflow and it would require another stack to deal with stack overflow • No runable thread exists on the processor • because it makes it simpler to pop and push to rebuild a process’s – Dispatch Idle Process (really a thread) context • Must have a stack when booting... • Idle is really a dispatcher in the kernel • Should the scheduler simply be a “kernel process”? – Enable interrupt; Receive pending interrupts; Disable interrupts; – You can view it that way because it has a stack, code and its data – Analyze interrupts; Dispatch a thread if so needed; structure – Check for deferred work; Dispatch thread if so needed; – This process always runs when there is no user process • “Idle” process – Perform power management; – In kernel or at user level? 28.08.03 15 28.08.03 16 4 Threads and Processes Project OpSys Trad. Threads Process Processes in individual address spaces Kernel threads User Level Threads Kernel Address Space 28.08.03 Kernel Level 17 28.08.03 18 Where Should PCB Be Saved? Job swapping • Save the PCB on user stack • The processes competing for resources may have combined – Many processors have a special instruction to do it demands that results in poor system performance efficiently • Reducing the degree of multiprogramming by moving some – But, need to deal with the overflow problem processes to disk, and temporarily not consider them for execution may be a strategy to enhance overall system – When the process terminates, the PCB vanishes performance • Save the PCB on the kernel heap data structure – From which states(s), to which state(s)? Try extending the following – May not be as efficient as saving it on stack examples using two suspended states. – But, it is very flexible and no other problems • The term is also used in a slightly different setting, see MOS Ch. 4.2 pp. 196-197 28.08.03 19 28.08.03 20 5 Add Job Swapping to Job Swapping, ii State Transition Diagram Terminate (call scheduler) Swap in Partially executed Swap out Scheduler Running dispatch swapped-out processes Block for resource Yield (call scheduler) Create (call scheduler) a process Ready Ready Queue CPU Blocked Terminate Resource becomes available I/O Waiting (move to ready queue) I/O Swap out queues Swapped Swap in 28.08.03 21 28.08.03 22 I/O Multiplexing: Concurrent Programming w/ Processes More than one State Machine per Process • Clean programming model • select blocks for any of multiple events – File tables are shared • Handle (one of the events) that unblocks select – User address space is private – Advance appropriate state machine • Processes are protected from each other • Repeat • Sharing requires some sort of IPC (InterProcess Communication) • Slower execution – Process switch, process control expensive – IPC expensive 28.08.03 23 28.08.03 24 6 Concurrent prog. w/ I/O Multiplexing • Establishes several control flows (state machines) in one process • Uses select • Offers application programmer more control than processor model (How?) • Easy sharing of data among state machines • More efficient (no process switch to switch between control flows in same process) • Difficult programming 28.08.03 25 7.
Recommended publications
  • Processes and Job Control
    Processes and Job Control Hour 17 PObjectives < Definitions: process, orphan, and zombie < System processes < Process creation < Examining processes: the ps command < Job control: &, nohup, fg, bg, jobs, ( ), and kill < Exit status Copyright © 1998-2002 Delroy A. Brinkerhoff. All Rights Reserved. Hour 17 Unix Slide 1 of 12 Process Also called a job by C and Korn shells PWhen a program or executable file is loaded from disk and started running (i.e., when a command is run), it is called a process vi pid 641 < identified by a unique process ID (PID) number < has an owner vi < private data vi PA program can be loaded more than once pid 895 < creates multiple processes vi < each process has a different PID < each process may have a different owner PPIDs are unique, nonnegative integers < numbers recycle without collisions Hour 17 Unix Slide 2 of 12 System Processes Processes created during system boot P0System kernel < “hand crafted” at boot < called swap in older versions (swaps the CPU between processes) < called sched in newer versions (schedules processes) < creates process 1 P1 init (the parent of all processes except process 0) < general process spawner < begins building locale-related environment < sets or changes the system run-level P2 page daemon (pageout on most systems) P3 file system flusher (fsflush) Hour 17 Unix Slide 3 of 12 Process Life Cycle Overview of creating new processes fork init init pid 467 Pfork creates two identical pid 1 exec processes (parent and child) getty pid 467 Pexec < replaces the process’s instructions
    [Show full text]
  • Lecture 4: September 13 4.1 Process State
    CMPSCI 377 Operating Systems Fall 2012 Lecture 4: September 13 Lecturer: Prashant Shenoy TA: Sean Barker & Demetre Lavigne 4.1 Process State 4.1.1 Process A process is a dynamic instance of a computer program that is being sequentially executed by a computer system that has the ability to run several computer programs concurrently. A computer program itself is just a passive collection of instructions, while a process is the actual execution of those instructions. Several processes may be associated with the same program; for example, opening up several windows of the same program typically means more than one process is being executed. The state of a process consists of - code for the running program (text segment), its static data, its heap and the heap pointer (HP) where dynamic data is kept, program counter (PC), stack and the stack pointer (SP), value of CPU registers, set of OS resources in use (list of open files etc.), and the current process execution state (new, ready, running etc.). Some state may be stored in registers, such as the program counter. 4.1.2 Process Execution States Processes go through various process states which determine how the process is handled by the operating system kernel. The specific implementations of these states vary in different operating systems, and the names of these states are not standardised, but the general high-level functionality is the same. When a process is first started/created, it is in new state. It needs to wait for the process scheduler (of the operating system) to set its status to "new" and load it into main memory from secondary storage device (such as a hard disk or a CD-ROM).
    [Show full text]
  • System Calls & Signals
    CS345 OPERATING SYSTEMS System calls & Signals Panagiotis Papadopoulos [email protected] 1 SYSTEM CALL When a program invokes a system call, it is interrupted and the system switches to Kernel space. The Kernel then saves the process execution context (so that it can resume the program later) and determines what is being requested. The Kernel carefully checks that the request is valid and that the process invoking the system call has enough privilege. For instance some system calls can only be called by a user with superuser privilege (often referred to as root). If everything is good, the Kernel processes the request in Kernel Mode and can access the device drivers in charge of controlling the hardware (e.g. reading a character inputted from the keyboard). The Kernel can read and modify the data of the calling process as it has access to memory in User Space (e.g. it can copy the keyboard character into a buffer that the calling process has access to) When the Kernel is done processing the request, it restores the process execution context that was saved when the system call was invoked, and control returns to the calling program which continues executing. 2 SYSTEM CALLS FORK() 3 THE FORK() SYSTEM CALL (1/2) • A process calling fork()spawns a child process. • The child is almost an identical clone of the parent: • Program Text (segment .text) • Stack (ss) • PCB (eg. registers) • Data (segment .data) #include <sys/types.h> #include <unistd.h> pid_t fork(void); 4 THE FORK() SYSTEM CALL (2/2) • The fork()is one of the those system calls, which is called once, but returns twice! Consider a piece of program • After fork()both the parent and the child are ..
    [Show full text]
  • Troubleshoot Zombie/Defunct Processes in UC Servers
    Troubleshoot Zombie/Defunct Processes in UC Servers Contents Introduction Prerequisites Requirements Components Used Background Information Check Zombies using UCOS Admin CLI Troubleshoot/Clear the Zombies Manually Restart the Appropriate Service Reboot the Server Kill the Parent Process Verify Introduction This document describes how to work with zombie processes seen on CUCM, IMnP, and other Cisco UC products when logged in using Admin CLI. Prerequisites Requirements Cisco recommends that you have knowledge of using Admin CLI of the UC servers: ● Cisco Unified Communications Manager (CUCM) ● Cisco Unified Instant Messaging and Presence Server (IMnP) ● Cisco Unity Connection Server (CUC) Components Used This document is not restricted to specific software and hardware versions. The information in this document was created from the devices in a specific lab environment. All of the devices used in this document started with a cleared (default) configuration. If your network is live, ensure that you understand the potential impact of any command. Background Information The Unified Communications servers are essentially Linux OS based applications. When a process dies on Linux, it is not all removed from memory immediately, its process descriptor (PID) stays in memory which only takes a tiny amount of memory. This process becomes a defunct process and the process's parent is notified that its child process has died. The parent process is then supposed to read the dead process's exit status and completely remove it from the memory. Once this is done using the wait() system call, the zombie process is eliminated from the process table. This is known as reaping the zombie process.
    [Show full text]
  • Operating Systems Homework Assignment 2
    Operating Systems Homework Assignment 2 Preparation For this homework assignment, you need to get familiar with a Linux system (Ubuntu, Fedora, CentOS, etc.). There are two options to run Linux on your Desktop (or Laptop) computer: 1. Run a copy of a virtual machine freely available on-line in your own PC environment using Virtual Box. Create your own Linux virtual machine (Linux as a guest operating system) in Windows. You may consider installing MS Windows as a guest OS in Linux host. OS-X can host Linux and/or Windows as guest Operating systems by installing Virtual Box. 2. Or on any hardware system that you can control, you need to install a Linux system (Fedora, CentOS Server, Ubuntu, Ubuntu Server). Tasks to Be Performed • Write the programs listed (questions from Q1 to Q6), run the program examples, and try to answer the questions. Reference for the Assignment Some useful shell commands for process control Command Descriptions & When placed at the end of a command, execute that command in the background. Interrupt and stop the currently running process program. The program remains CTRL + "Z" stopped and waiting in the background for you to resume it. Bring a command in the background to the foreground or resume an interrupted program. fg [%jobnum] If the job number is omitted, the most recently interrupted or background job is put in the foreground. Place an interrupted command into the background. bg [%jobnum] If the job number is omitted, the most recently interrupted job is put in the background. jobs List all background jobs.
    [Show full text]
  • Processes in Linux/Unix
    Processes in Linux/Unix A program/command when executed, a special instance is provided by the system to the process. This instance consists of all the services/resources that may be utilized by the process under execution. • Whenever a command is issued in unix/linux, it creates/starts a new process. For example, pwd when issued which is used to list the current directory location the user is in, a process starts. • Through a 5 digit ID number unix/linux keeps account of the processes, this number is call process id or pid. Each process in the system has a unique pid. • Used up pid’s can be used in again for a newer process since all the possible combinations are used. • At any point of time, no two processes with the same pid exist in the system because it is the pid that Unix uses to track each process. Initializing a process A process can be run in two ways: 1. Foreground Process : Every process when started runs in foreground by default, receives input from the keyboard and sends output to the screen. When issuing pwd command $ ls pwd Output: $ /home/geeksforgeeks/root When a command/process is running in the foreground and is taking a lot of time, no other processes can be run or started because the prompt would not be available until the program finishes processing and comes out. 2. Backround Process : It runs in the background without keyboard input and waits till keyboard input is required. Thus, other processes can be done in parallel with the process running in background since they do not have to wait for the previous process to be completed.
    [Show full text]
  • Lecture 22 Systems Programming Process Control
    Lecture 22 Systems Programming Process Control A process is defined as an instance of a program that is currently running. A uni processor system can still execute multiple processes giving the appearance of a multi- processor machine. A call to a program spawns a process. If a mail program is called by n users then n processes or instances are created and executed by the unix system. Many operating systems including windows and unix executes many processes at the same time. When a program is called, a process is created and a process ID is issued. The process ID is given by the function getpid() defined in <unistd.h>. The prototype for pid( ) is given by #include < unistd.h > pid_t getpid(void); In a uni-processor machine, each process takes turns running and for a short duration, a process takes time interval called a timeslice. The unix command ps can be used to list all current process status. ps PID TTY TIME CMD 10150 pts/16 00:00:00 csh 31462 pts/16 00:00:00 ps The command ps lists the process ID (PID), the terminal name(TTY), the amount of time the process has used so far(TIME) and the command it is executing(CMD). Ps command only displays the current user processes. But we can get all the processes with the flag (- a) and in long format with flag (-l) ps –a ps -l ps -al Information provided by each process may include the following. PID The process ID in integer form PPID The parent process ID in integer form STAT The state of the process TIME CPU time used by the process (in seconds) TT Control terminal of the process COMMAND The user command that started the process Copyright @ 2008 Ananda Gunawardena Each process has a process ID that can be obtained using the getpid() a system call declared in unistd.h.
    [Show full text]
  • Processes and Threads
    Lecture Notes for CS347: Operating Systems Mythili Vutukuru, Department of Computer Science and Engineering, IIT Bombay 2. Processes and Threads 2.1 Life cycle of a process • Recall that the process is a basic unit of execution in an OS. The main job of any OS is to run processes, while managing their lifecycle from creation to termination. Processes are typically created in Unix-like systems by forking from an existing process. The OS starts the first init process after bootup, and all subsequent processes are descendents of this process. • A process can create a child process using the fork system call. After the fork, the memory image of the child process is a complete copy of the memory image of the parent (the child and parent memory images may diverge subsequently). The fork system call returns in both the parent and child processes, with different return values. In the parent, it returns the pid of the new child process. In the child, it returns 0. Both processes then resume execution from the instruction right after fork, and can be scheduled as independent entities by the CPU scheduler. • A typical use case of fork is to create a child and run the exec system call in the child. The exec system call loads a new executable into the memory image of the process calling it, enabling the child process to do something different from what the parent is already doing. All processes (beyond the first process) are created with this fork+exec combination in Unix-like operating systems. • A simple OS would create a complete copy of the parent’s memory image for the child.
    [Show full text]
  • Kill a Zombie! a Process Is Called a Zombie Process If the Process Has
    Kill a Zombie! A process is called a zombie process if the process has been completed, but its PID and process entry remains in the Linux process table. A process is removed from the process table when the process is completed, and its parent process reads the completed process' exit status by using the wait() system call. If a parent process fails to call wait() for whatever reason, its child process will be left in the process table, becoming a zombie. To find zombie processes on Linux: $ ps axo stat,ppid,pid,comm | grep -w defunct Z 250 10242 fake-prog <defunct> The above command searches for processes with zombie (defunct) state, and displays them in (state, PPID, PID, command-name) format. The sample output shows that there is one zombie process associated with "fake-prog", and it was spawned by a parent process with PID 250. Killing zombie processes is not obvious since zombie processes are already dead. You can try two options to kill a zombie process on Linux as follows. First, you can try sending SIGCHLD signal to the zombie's parent process using the kill command. Note that the above command gives you PPID (PID of parent process) of each zombie. In our example, PPID of the zombie is 250. $ sudo kill -s SIGCHLD 250 If a zombie process still does not go away, you can kill the parent process (e.g., 250) of the zombie. $ sudo kill -9 250 Once its parent process gets killed, the zombie will be adopted by the init process, which is a parent of all processes in Linux.
    [Show full text]
  • Lecture 25 Systems Programming Process Control
    Lecture 25 Systems Programming Process Control A process is defined as an instance of a program that is currently running. A uni processor system or single core system can still execute multiple processes giving the appearance of a multi-core machine. A call to an executable program spawns a process. For examples, if a mail program is called by n users then n processes or instances are created and executed by the unix system. Many operating systems including windows and unix can execute many processes at the same time. When a program is called, a process is created and a process ID is issued. The process ID is given by the function getpid() defined in <unistd.h>. The prototype for pid( ) is given by #include < unistd.h > pid_t getpid(void); In a single core machine, each process takes turns running for a short duration in time interval called a timeslice. The unix command ps can be used to list all current process status in your shell. ps PID TTY TIME CMD 10150 pts/16 00:00:00 csh 31462 pts/16 00:00:00 ps The command ps lists the process ID (PID), the terminal name(TTY), the amount of time the process has used so far(TIME) and the command it is executing(CMD). Ps command only displays the current user processes. But we can get all the processes with the flag (-a) and in long format with flag (-l) ps –a ps -l ps -al Information provided by each process may include the following. PID The process ID in integer form PPID The parent process ID in integer form STAT The state of the process TIME CPU time used by the process (in seconds) TT Control terminal of the process COMMAND The user command that started the process Copyright @ 2008 Ananda Gunawardena Each process has a process ID that can be obtained using the getpid() a system call declared in unistd.h.
    [Show full text]
  • Assignment 1
    Assignment 1 Teaching Assistant: Michalis Pachilakis ( [email protected]) System Calls If a process is running a user program in user mode and needs a system service, such as reading data from a file, it has to execute a trap instruction to transfer control the operating system. Application –USER SPACE System call KERNEL SPACE System Calls A system call is a request for service that a program makes of the kernel. The service is generally something that only the kernel has the privilege to do, such as doing I/O. SYSTEM CALLS PROCESS CONTROL fork(), wait(), exec(),exit() FILE open(), close(), MANIPULATION read(), write() DIRECTORIES mkdir(),rmdir(), MANAGEMENT mount(),link() OTHER chdir(),chmod(), kill(),time() Fork() Fork creates a new process(child process). It creates an exact duplicate of the original process, including all the file descriptors, registers etc. The fork is called once, but returns twice! After the fork, the original process and the copy(the parent and the child) go their separate ways. The fork call returns a value, which is zero in the child and equal to the child’s process identifier (PID) in the parent. Now consider how fork is used by the shell. When a command is typed, the shell forks off a new process. This child process must execute the user command. Fork() – PID (Process IDentity) pid < 0 the creation of a child process was unsuccessful. pid == 0 the newly created child. pid > 0 the process ID of the child process passes to the parent Consider the program: #include <unistd.h> P1 fork() C1 PID:28 PID:34 pid_t pid = fork(); printf(“PID:%d\n”,pid); … The parent will print: PID:34 The child will always print: PID:0 Fork() #define TRUE 1 while (TRUE) { /* repeat forever */ type_prompt(); /* display prompt on the screen */ read_command(command, parameters); /* read input from terminal */ if (fork() != 0) { /* fork off child process */ /* Parent code.
    [Show full text]
  • Asiabsdcon 2013 Proceedings
    Copyright c 2013 AsiaBSDCon 2013 Organizing Committee. All rights reserved. ! Unauthorized republication is prohibited. Published in Japan, March 2013 5 6 7 8 OpenIKED Reyk Floeter ([email protected]) February 2013 Abstract history and my personal motivation of creating it. It is inteded to provide some complementary This paper introduces the OpenIKED project[14], information for my talk at AsiaBSDCon 2013 . the latest portable subproject of OpenBSD[13]. OpenIKED is a FREE implementation of the most ad- The project aims to provide a free implementa- vanced Internet security Internet Key Exchange ver- tion of the Internet Key Exchange (IKEv2) protocol sion 2 (IKEv2) Virtual Private Network (VPN) pro- which performs mutual authentication and which tocol using the strongest security, authentication and establishes and maintains IPsec VPN security policies encryption techniques. The project was born in need and Security Associations (SAs) between peers. of a modern Internet Protocol Security (IPsec) imple- The IKEv2 protocol is de ned in Request for Com- mentation for OpenBSD, but also for interoperability ments (RFC) 5996, which combines and updates the with the integrated IKEv2 client since Windows 7 and previous standards: Internet Security Association and to provide a compliant solution for the US Government Key Management Protocol (ISAKMP)/Oakley (RFC IPv6 (USGv6) standard. The project is still under ac- 2408[8]), Internet Key Exchange version 1 (IKE) tive development; it was started by Reyk Floeter as (RFC 2409[3]), and the Internet DOI (RFC 2407[17]). iked for OpenBSD in 2010 but ported to other plat- OpenIKED only supports the IKEv2 protocol; sup- forms including Linux, FreeBSD and NetBSD in late port for ISAKMP/Oakley and IKE is provided by 2012 using the OpenIKED project name.
    [Show full text]