UNIX Processes. by Armin R. Mikler. 1: Introduction

Similar documents
Operating System Structure

Operating Systems. Lecture 05

CSC 1600 Unix Processes. Goals of This Lecture

CS240: Programming in C

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 1 Solution Due 3:15 PM, Wednesday, 2/1/17

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

Introduction to Processes

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

Process Management 1

CSCB09: Software Tools and Systems Programming. Bianca Schroeder IC 460

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

Operating Systems Lab

Princeton University. Computer Science 217: Introduction to Programming Systems. Process Management

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

System Programming. Process Control II

Operating systems and concurrency - B03

fork System-Level Function

Chap 4, 5: Process. Dongkun Shin, SKKU

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

Process Description and Control. Chapter 3

Process! Process Creation / Termination! Process Transitions in" the Two-State Process Model! A Two-State Process Model!

Processes. Process Management Chapter 3. When does a process gets created? When does a process gets terminated?

CS 201. Processes. Gerson Robboy Portland State University

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

Processes. Overview. Processes. Process Creation. Process Creation fork() Processes. CPU scheduling. Pål Halvorsen 21/9-2005

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

Fall 2015 COMP Operating Systems. Lab #3

CS 261 Fall Mike Lam, Professor. Processes

CS 355 Operating Systems. Keeping Track of Processes. When are processes created? Process States 1/26/18. Processes, Unix Processes and System Calls

Process Creation and Control

Processes: Introduction. CS 241 February 13, 2012

CS 261 Fall Mike Lam, Professor. Exceptional Control Flow and Processes

CSCE 313: Intro to Computer Systems

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

EXPERIMENT NO : M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM,500GB HDD

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

CS3733: Operating Systems

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

Altering the Control Flow

CSC209 Fall Karen Reid 1

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

CSC 252: Computer Organization Spring 2018: Lecture 19

Announcement (1) sys.skku.edu is now available

Processes COMPSCI 386

Exceptional Control Flow Part I

Lesson 2. process id = 1000 text data i = 5 pid = 1200

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Altering the Control Flow

Operating System. Chapter 3. Process. Lynn Choi School of Electrical Engineering

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

Process management 1

Control Flow. Systemprogrammering 2007 Föreläsning 2 Exceptional Control Flow Part I. Exceptional Control Flow. Altering the Control Flow

What is a Process. Preview. What is a Process. What is a Process. Process Instruction Cycle. Process Instruction Cycle 3/14/2018.

CS240: Programming in C

Part II Processes and Threads Process Basics

CMPSCI 230 Computer Systems Principles. Processes

Discussion of Assignments 2. Line buffered vs. full buffered I/O. Some often encountered issues in the submissions.

Process Control. Philipp Koehn. 23 April 2018

OS Structure, Processes & Process Management. Don Porter Portions courtesy Emmett Witchel

Exceptional Control Flow Part I Oct. 17, 2002

REVIEW OF COMMONLY USED DATA STRUCTURES IN OS

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Processes. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! Scheduling processes

Chapter 3 Process Description and Control

Computer Systems II. First Two Major Computer System Evolution Steps

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

What is a Process? Processes and Process Management Details for running a program

Today: Process Management. The Big Picture So Far. What's in a Process? Example Process State in Memory

Processes. CS439: Principles of Computer Systems January 24, 2018

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Lecture 4: Process Management

Processes. Dr. Yingwu Zhu

2 Processes. 2 Processes. 2 Processes. 2.1 The Process Model. 2.1 The Process Model PROCESSES OPERATING SYSTEMS

The Big Picture So Far. Today: Process Management

Processes. CS439: Principles of Computer Systems January 30, 2019

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

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

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

W4118 Operating Systems. Junfeng Yang

Processes. CS3026 Operating Systems Lecture 05

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

Processes. CSE 351 Autumn Instructor: Justin Hsia

Announcement. Exercise #2 will be out today. Due date is next Monday

Exceptional Control Flow Part I

Giving credit where credit is due

Processes. q Process concept q Process model and implementation q Multiprocessing once again q Next Time: Scheduling

Q & A (1) Where were string literals stored? Virtual Address. SSE2033: System Software Experiment 2 Spring 2016 Jin-Soo Kim

Processes. CSE 351 Autumn Instructor: Justin Hsia

CS 31: Intro to Systems Processes. Kevin Webb Swarthmore College March 31, 2016

INF1060: Introduction to Operating Systems and Data Communication. Pål Halvorsen. Wednesday, September 29, 2010

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

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

Exceptional Control Flow Part I September 22, 2008

Transcription:

UNIX Processes by Armin R. Mikler

Overview The UNIX Process What is a Process Representing a process States of a process Creating and managing processes fork() wait() getpid() exit() etc. Files in UNIX Directories and Paths User vs. System Mode UNIX I/O primitives A simple example The basic I/O system calls Buffered vs. unbuffered I/O

UNIX Processes A program that has started is manifested in the context of a process. A process in the system is represented: Process Identification Elements Process State Information Process Control Information User Stack Private User Address Space, Programs and Data Shared Address Space

Process Control Block Process Information, Process State Information, and Process Control Information constitute the PCB. All Process State Information is stored in the Process Status Word (PSW). All information needed by the OS to manage the process is contained in the PCB. A UNIX process can be in a variety of states:

States of a UNIX Process User running: Process executes in user mode Kernel running: Process executes in kernel mode Ready to run in memory: process is waiting to be scheduled Asleep in memory: waiting for an event Ready to run swapped: ready to run but requires swapping in Preempted: Process is returning from kernel to user-mode but the system has scheduled another process instead Created: Process is newly created and not ready to run Zombie: Process no longer exists, but it leaves a record for its parent process to collect. See Process State Diagram!!

Process State Transitions 1 user running sys-call return kernel running sleep 2 schedule interrupt/ interrupt return 4 wakeup 3 asleep ready to run

Creating a new process In UNIX, a new process is created by means of the fork() - system call. The OS performs the following functions: It allocates a slot in the process table for the new process It assigns a unique ID to the new process It makes a copy of process image of the parent (except shared memory) It assigns the child process to the Ready to Run State It returns the ID of the child to the parent process, and 0 to the child. Note, the fork() call actually is called once but returns twice - namely in the parent and the child process.

Fork() Pid_t fork(void) is the prototype of the fork() call. Remember that fork() returns twice in the newly created (child) process with return value 0 in the calling process (parent) with return value = pid of the new process. A negative return value (-1) indicates that the call has failed Different return values are the key for distinguishing parent process from child process! The child process is an exact copy of the parent, yet, it is a copy i.e. an identical but separate process image.

A fork() Example #include <unistd.h> main() { pid_t pid /* process id */ printf( just one process before the fork()\n ); pid = fork(); if(pid == 0) printf( I am the child process\n ); else if(pid > 0) printf( I am the parent process\n ); else printf( DANGER Mr. Robinson - the fork() has failed\n ) }

Basic Process Coordination The exit() call is used to terminate a process. Its prototype is: void exit(int status), where status is used as the return value of the process. exit(i) can be used to announce success and failure to the calling process. The wait() call is used to temporarily suspend the parent process until one of the child processes terminates. The prototype is: pid_t wait(int *status), where status is a pointer to an integer to which the child s status information is being assigned. wait() will return with a pid when any one of the children terminates or with -1 when no children exist.

more coordination To wait for a particular child process to terminate, we can use the waitpid() call. Prototype: pid_t waitpid(pid_t pid, int *status, int opt) Sometimes we want to get information about the process or its parent. getpid() returns the process id getppid() returns the parent s process id getuid() returns the users id use the manual pages for more id information.

Orphans and Zombies or MIAs A child process whose parent has terminated is referred to as orphan. When a child exits when its parent is not currently executing a wait(), a zombie emerges. A zombie is not really a process as it has terminated but the system retains an entry in the process table for the non-existing child process. A zombie is put to rest when the parent finally executes a wait(). When a parent terminates, orphans and zombies are adopted by the init process (prosess-id -1) of the system.

Fun with Zombies! Consider the following program: #include <unistd.h> main() { pid_t pid; int num1, num2; pid = fork(); printf( pid=%d: enter 2 numbers:, getpid()); scanf( %d %d, &num1 &num2); printf( \n pid=%d: the sum is:\n, getpid(), num1 + num2); }

What is going on?? Who is getting to write to the standard output - parent or child?? Who is going to receive the first input? What happens if the child process completes while the parent is still waiting for input? What UNIX tools would you use to get some indication of what is going on? Exercise: Go try it!!