Prepared by Prof. Hui Jiang (COSC3221) 2/9/2007

Similar documents
Dept. of CS, York Univ. 1

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Processes. Electrical and Computer Engineering Stephen Kim ECE/IUPUI RTOS & Apps 1

Part Two - Process Management. Chapter 3: Processes

Processes. Operating System Concepts with Java. 4.1 Sana a University, Dr aimen

Chapter 4: Processes. Process Concept

Chapter 4: Processes

Chapter 3: Processes. Operating System Concepts 8th Edition,

Chapter 3: Processes. Chapter 3: Processes. Process in Memory. Process Concept. Process State. Diagram of Process State

Chapter 3: Processes. Operating System Concepts 8 th Edition,

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition,

Chapter 3: Processes. Operating System Concepts Essentials 8 th Edition

Processes and More. CSCI 315 Operating Systems Design Department of Computer Science

Processes. Process Concept

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

Processes. CSE 2431: Introduction to Operating Systems Reading: Chap. 3, [OSC]

The Big Picture So Far. Chapter 4: Processes

Chapter 3: Processes

The Big Picture So Far. Chapter 4: Processes

Chapter 3: Processes. Operating System Concepts 8 th Edition,

Chapter 3: Processes. Operating System Concepts Essentials 2 nd Edition

Process Concept. Chapter 4: Processes. Diagram of Process State. Process State. Process Control Block (PCB) Process Control Block (PCB)

Chapter 4: Processes

COP 4610: Introduction to Operating Systems (Spring 2016) Chapter 3: Process. Zhi Wang Florida State University

Chapter 3: Process Concept

Chapter 3: Process Concept

Chapter 3: Processes. Operating System Concepts 8 th Edition,

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

COP 4610: Introduction to Operating Systems (Spring 2014) Chapter 3: Process. Zhi Wang Florida State University

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

Chapter 4: Processes

Chapter 4: Processes. Process Concept. Process State

COMP 3100 Operating Systems

Chapter 3: Process Concept

Diagram of Process State Process Control Block (PCB)

Chapter 4: Processes. Process Concept

CSI Module 2: Processes

Chapter 3: Process Concept

Course: Operating Systems Instructor: M Umair. M Umair

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

INTER-PROCESS COMMUNICATION. UNIX Programming 2015 Fall by Euiseong Seo

Process Concept Process Scheduling Operations On Process Inter-Process Communication Communication in Client-Server Systems

W4118 Operating Systems. Junfeng Yang

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

Chapter 3: Processes. Operating System Concepts 8th Edition, modified by Stewart Weiss

Chapter 3 Processes. Process Concept. Process Concept. Process Concept (Cont.) Process Concept (Cont.) Process Concept (Cont.)

Notice: This set of slides is based on the notes by Professor Perrone of Bucknell and the textbook authors Silberschatz, Galvin, and Gagne

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

Chapter 3: Processes. Operating System Concepts 8th Edition

CSC 539: Operating Systems Structure and Design. Spring 2006

Chapter 3: Processes

OS Lab Tutorial 1. Spawning processes Shared memory

CS240: Programming in C

Process Concept. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

Process. Program Vs. process. During execution, the process may be in one of the following states

Lecture 2 Process Management

Processes and Threads

CHAPTER 3 - PROCESS CONCEPT

OPERATING SYSTEMS. UNIT II Sections A, B & D. An operating system executes a variety of programs:

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

Chapter 3: Processes. Operating System Concepts 9 th Edition

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

Part V. Process Management. Sadeghi, Cubaleska RUB Course Operating System Security Memory Management and Protection

Processes. Process Scheduling, Process Synchronization, and Deadlock will be discussed further in Chapters 5, 6, and 7, respectively.

Operating Systems. Figure: Process States. 1 P a g e

Process. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

CS240: Programming in C

! The Process Control Block (PCB) " is included in the context,

Lecture 8: Inter-process Communication. Lecturer: Prof. Zichen Xu

CS Lecture 2! Processes! George Mason University! Fall 2010!

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Chapter 3: Process Concept

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

COMP 2355 Introduction to Systems Programming

Signal Example 1. Signal Example 2

Killing Zombies, Working, Sleeping, and Spawning Children

Chapter 3: Processes. Operating System Concepts 9 th Edition

경희대학교컴퓨터공학과 조진성. UNIX System Programming

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Operating Systems. VI. Threads. Eurecom. Processes and Threads Multithreading Models

INTER-PROCESS COMMUNICATION Tanzir Ahmed CSCE 313 Fall 2018

Other Interprocess communication (Chapter 2.3.8, Tanenbaum)

CS370 Operating Systems

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CSC 1600 Unix Processes. Goals of This Lecture

Shared Memory Memory mapped files

Inter-process communication (IPC)

CS370 Operating Systems

Fall 2015 COMP Operating Systems. Lab #3

Chapter 3: Processes

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

CS510 Operating System Foundations. Jonathan Walpole

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

Transcription:

1 * * ' &% $ # " "! 4 ' Prepared by Prof Hui Jiang COSC1 /9/007 / 0 How CPU is used? Users run programs in CPU In a multiprogramming system a CPU always has several jobs to run How to define a CPU job? The important concept: Prof Hui Jiang Dept of Computer Science and Engineering York University PROCESS 5 * is a running program a program in execution is a basic unit of CPU activities a process is a unit of work in a multiprogramming system Many different processes in a multiprogramming system: User processes executing user code Word processor Web browser email editor etc System processes executing operating system codes CPU scheduling Memorymanagement I/O operation Multiple processes concurrently run in a CPU Program code Mov AX 0x10 Mov BX CX Push CX Mov CXDX OUT 0x11CX POP CX Code Stack PC CPU Registers Memory Dept of CS York Univ 1

0 Prepared by Prof Hui Jiang COSC1 /9/007 A includes: Main Memory index or Registers i Text Section: memory segment including program codes list i j PC Base Limit b h Program Counter PC: the address of the instruction to be executed next Context Other registers All CPU s Registers Stack: memory segment to save temporary data such as local variable function parameters return address Data Section: memory segment containing global and static variables A B h b Data Program code Context Data Program code Figure 8 Typical Implementation 0 & state Program counter CPU registers CPU scheduling information Memorymanagement information Accounting information I/O status information Control Block Dept of CS York Univ

& & & Prepared by Prof Hui Jiang COSC1 /9/007 * 5 0 0 0 * 5 0 Scheduling Queues: List of processes competing for the same resource Queues is generally implemented as linked lists Each item in the linked list is PCB of a process we extend each PCB to include a pointer to point to next PCB in the queue New: the process is just being created Running: instructions are being executed by CPU Waiting: waiting for some event I/O completion or a signal Ready: waiting to be assigned to CPU to run Terminated: it finished execution Examples of scheduling queues: Ready Queue: all processes waiting for CPU Device Queues: all processes waiting for a particular device; Each device has its own device queue ' * * 0 0 0 0 * 5 0 Dept of CS York Univ

' $ &/ & $ $ Prepared by Prof Hui Jiang COSC1 /9/007 5 0 0 The scheduler s role Scheduler categories: Longterm Scheduler Job scheduler: choose a job from job pool to load into memory to start Control the degree of multiprogramming number of process in memory Select a good mix of I/Obound processes and CPUbound processes Shortterm scheduler CPU scheduler Select a process from ready queue to run once CPU is free Executed very frequently once every 100 millisecond Must be fast for efficiency Mediumterm scheduler: SWAPPING Swap out / swap in & Context Switch: switching the CPU from one process to another Saving the state of old process to its PCB CPU scheduling: select a new process Loading the saved state in its PCB for the new process The context of a process is represented by its PCB Contextswitch time is pure overhead of the system typically from 1 1000 microseconds mainly depending on: Memory speed Number of registers Existence of special instruction The more complex OS the more to save Context switch has become such a performance bottleneck in a large multiprogramming system: New structure to reduce the overhead: THREAD Address 0 100 5000 8000 1000 Main Memory Dispatcher A B C Program Counter 8000 Figure Snapshot of Example Execution Figure 4 at Instruction Cycle 1 Dept of CS York Univ 4

Prepared by Prof Hui Jiang COSC1 /9/007 creation termination Interprocess communication IPC waiting Unix programming: Multipleprocess programming Cooperating process tasks Dept of CS York Univ 5

Prepared by Prof Hui Jiang COSC1 /9/007 A process can create some new processes via a createprocess system call: Parent process / children process All process in Unix form a tree structure Resource Allocation of child process The child process get its resource from OS directly Constrain to its parent s resources Parent status The parent continues to execute concurrently with its children The parent waits until its children terminate Initialization of child process address space Child process is a duplicate of its parent process Child process has a program loaded into it How to pass parameters initialization data from parent to child? In UNIX each process is identified by its process number pid In UNIX fork is used to create a new process Creating a new process with fork: New child process is created by fork Parent process address space is copied to new process space initially identical address space Both child and parent processes continue execution from the instruction after fork Return code of fork is different: in child process return code is zero in parent process return code is nonzero it is the process number of the new child process If desirable another system call execlp can be used by one of these two processes to load a new program to replace its original memory space #include <stdioh> void mainint argc char *argv[ ] { int pid ; /* fork another process */ pid = fork ; if pid < 0 { /* error occurred */ fprintfstderr Fork Failed!\n ; exit1 ; else if pid == 0 { /* child process*/ execlp /bin/ls ls NULL ; else { /* parent process */ /* parent will wait for the child to complete */ waitnull ; printf Child Complete\n ; exit0 ; Dept of CS York Univ 6

Prepared by Prof Hui Jiang COSC1 /9/007 Normal termination: Finishes executing its final instruction or call exit system call Abnormal termination: make system call abort The parent process can cause one of its child processes to terminate The child uses too much resources The task assigned to the child is no longer needed If the parent exits all its children must be terminated in some systems termination: The process returns data output to its parent process In UNIX the terminated child process number is return by wait in parent process All its resources are deallocated by OS Unix system calls for process control: getid: get process ID pid of calling process fork: create a new process exec: load a new program to run execlchar *pathname char *arg0 ; execvchar *pathname char* argv[ ] ; execle execve execlp execvp wait waitid: wait child process to terminate exit abort: a process terminates Concurrent processes executing in the operating system Independent: runs alone Cooperating: it can affect or be affected by other processes Why cooperating processes? Information sharing Computation speedup Modularity Convenience Need interprocess communication IPC mechanism for cooperating processes: Sharedmemory Messagepassing Dept of CS York Univ 7

Prepared by Prof Hui Jiang COSC1 /9/007 IPC with message passing provides a mechanism to allow processes to communicate and to synchronize their actions without sharing the same address space IPC based on messagepassing system: es communication without sharing space Communication is done through the passing of messages At least two operations: sendmessage receivemessage Message size: fixed vs variable Logical communication link: Direct vs indirect communication Symmetric vs asymmetric communication Automatic or explicit buffering Each process must explicitly name the recipient or sender of the communication sendpmessage ReceiveQmessage A link is established between each pair of processes A link is associated with exactly two processes Asymmetric direct communication: no need for recipient to name the sender sendpmessage receive&idmessage: id return the sender identity Disadvantage of direct communication: Limited modularity due to explicit process naming The messages are sent to and received from mailbox Mailbox is a logical unit where message can be placed or removed by processes each mailbox has a unique id sendamessage: A is mailbox ID receiveamessage A link is established in two processes which share mailbox A link may be associated with more than two processes A number of different link may exist between each pair of processes OS provides some operations on mailbox Create a new mailbox Send and receive message through the mailbox Delete a mailbox Message passing may be either blocking or nonblocking Blocking is considered synchronous Nonblocking is considered asynchronous send and receive primitives may be either blocking or nonblocking Blocking send Nonblocking send Blocking receive Nonblocking receive When both the send and receive are blocking we have a rendezvous between the sender and the receiver Dept of CS York Univ 8

Prepared by Prof Hui Jiang COSC1 /9/007 The buffering provided by the logical link: Zero capacity: the sender must block until the recipient receives the message no buffering Bounded capacity: the buffer has finite length The sender doesn t block unless the buffer is full Unbounded capacity: the sender never blocks Signals Pipes Message queues Shared memory Sockets others Signal is a technique to notify a process that some events have occurred The process has three choices to deal with the signal: Ignore the signal Let the default action occur Provide a function that is called when the signals occurs signal function: change the action function for a signal #include <signalh> void *signalint signo void *func int ; kill function: send a signal to another process #include <sys/typesh> #include <signalh> int kill int pid int signo ; Dept of CS York Univ 9

Prepared by Prof Hui Jiang COSC1 /9/007 #include <signalh> static void sig_intint ; int main { ifsignalsigintsig_int==sig_err err_sys signal error ; sleep100 ; void sig_intint signo { printf Interrupt\n ; Event SIGINT: type the interrupt key CtrlC The default action is to terminate the process Now we change the default action into printing a message to screen Halfduplex; only between parent and child Creating a pipe: Call pipe; Then call fork; Close some ends to be a halfduplex pipe #include <unistdh> int pipe int filedes[] ; user process fd[0] kernel fd[1] int main { int n fd[] ; int pid ; char line[00] ; pipe if pipefd < 0 err_sys pipe error ; fd[0] parent fd[1] fd[0] pipe child fd[1] if pid = fork < 0 err_sys fork error ; else if pid > 0 { closefd[0] ; writefd[1] hello word\n 1 ; else { closefd[1] ; n = readfd[0] line 00 ; writestdout_fileno line n ; exit0 ; Dept of CS York Univ 10

Prepared by Prof Hui Jiang COSC1 /9/007 #include <sys/typesh> #include <sys/ipch> #include <sys/msgh> int msggetkey_t key int flag ; int msgsndint msqid const void *ptr size_t nbytes int flag ; int msgrcvint msqid void *ptr size_t nbytes int flag ; int msggetkey_t key int flag ; key msgflg an integer to identify the message queue Should be unique in a system 0 : access to an existing queue IPC_CREAT bit set : create a queue return value 1 on error nonnegative integer on success: message id int msgsnd int msgid const void *msgp int msgsz int msgflg ; msgid msgp msgsz msgflg msg id returned by msgget ptr to a structure struct msgstruct{ long mtype ; //type of the message char mtext[max_len]; //actual data ; return value 1 on failure 0 on success size of data in msg always 0 in our cases int msgrcvint msgid const void *mshp int msgsz long msgtype int msgflg ; msgid msgp msgsz msgflg msgtype return value msg id returned by msgget ptr to a msg structure same as above size of buffer in msg 1 on failure always 0 in our cases 0: get first message in the queue >0 : get first message of type msgtype <0 : beyond our consideration No of bytes in the message on success Dept of CS York Univ 11

Prepared by Prof Hui Jiang COSC1 /9/007 #include <sys/typesh> #include <sys/ipch> #include <sys/msgh> #include <sys/typesh> #include <sys/ipch> #include <sys/msgh> #define KEY 894 /* your CS log in number */ int main { int msgid ; msgid = msggetkey0 ; if msgid < 0 { msgid = msggetkey IPC_CREAT 0666 ; ifmsgid < 0 printf"error in creating message queue!\n"; #define KEY 894 #define MAX_LEN 100 typedef struct { long mtype ; char mtext[max_len] ; Message ; int main { int msgid ; Message msg ; strcpymsgmtext "Hello world!" ; msgmtype = 1 ; msgid = msggetkey0 ; if msgid < 0 { printf"error in creating message queue!\n"; return 1 ; ifmsgsndmsgid &msgmax_len0 < 0 printf"error in sending message\n" ; else printf"sent message successfully\n" ; #include <sys/typesh> #include <sys/ipch> #include <sys/msgh> #define KEY 894 #define MAX_LEN 100 typedef struct { long mtype ; char mtext[max_len] ; Message ; int main { int msgid ; Message msg ; msgid = msggetkey0 ; if msgid < 0 { printf"error in creating message queue!\n"; return 1 ; ifmsgrcvmsgid &msgmax_len00 < 0 printf"error in receiving message\n" ; else printf"received message: %s\n"msgmtext ; #include <sys/shmh> int shmgetkey_t key size_t size int shmflg; void *shmatint shmid const void *shmaddr int shmflg; int shmdtconst void *shmaddr; int shmctlint shmid int cmd struct shmid_ds *buf; ifmsgctlmsgidipc_rmidnull<0 // Remove the message queue from system printf Error in removing message queue!\n ; else printf Removed message queue successfully!\n ; Dept of CS York Univ 1

Prepared by Prof Hui Jiang COSC1 /9/007 Tables are constructed for each entity the operating system manages Memory Memory Tables Image 1 Devices I/O Tables table: PCBs and process images Files es File Tables Memory table: Allocation of main memory to processes; Protection attributes for access to shared memory regions Primary Table File table: all opened files; location on hardware; Current Status 1 Image I/O table: all I/O devices being used; status of I/O operations n n Figure 11 General Structure of Operating System Control Tables ' $&% # "! P 1 P P n Mode switch Nonprocess Kernel Execute kernel outside of any process Operating system code is executed as a separate entity that operates in privileged mode Execution Within User es Operating system software within context of a user process executes in privileged mode when executing operating system code Kernel a Separate kernel P 1 P P n OS OS OS Functiontiontions Func Func Switching Functions b OS functions execute within user processes vs switch context switch P 1 P P n OS1 OS k Based Operating System Implement operating system as a collection of system processes Useful in multiprocessor or multicomputer environment Switching Functions c OS functions execute as separate processes Figure 15 Relationship Between Operatin System and User es Dept of CS York Univ 1