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

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

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

CSC 1600 Unix Processes. Goals of This Lecture

Environment Variables

Part II Processes and Threads Process Basics

UNIX System Calls. Sys Calls versus Library Func

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

CS 550 Operating Systems Spring Process II

CS240: Programming in C

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

CMPSCI 230 Computer Systems Principles. Processes

Operating System Structure

CS Operating Systems Lab 3: UNIX Processes

518 Lecture Notes Week 3

Processes COMPSCI 386

Interrupts, Fork, I/O Basics

Operating systems and concurrency - B03

Lecture 4: Process Management

Fall 2015 COMP Operating Systems. Lab #3

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

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

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CS 201. Processes. Gerson Robboy Portland State University

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

Computer Systems Assignment 2: Fork and Threads Package

CITS2002 Systems Programming. Creating a new process using fork() 1 next CITS2002 CITS2002 schedule

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

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

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

Dept. of Computer Science & Engineering 1 Knowledge & Data Engineering Lab.

Introduction to Processes

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

This tutorial covers a foundational understanding of IPC. Each of the chapters contain related topics with simple and useful examples.

Network Socket Programming - 1 BUPT/QMUL

Process Creation in UNIX

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

Operating Systems and Networks Assignment 2

CSE 421/521 - Operating Systems Fall 2013 Recitations. Recitation - II Unix Processes Prof. Tevfik Kosar. University at Buffalo

CS 550 Operating Systems Spring Process III

Operating System Labs. Yuanbin Wu

CS510 Operating System Foundations. Jonathan Walpole

File Descriptors and Piping

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

COE518 Lecture Notes Week 4 (Sept. 26, 2011)

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Lecture 8: Unix Pipes and Signals (Feb 10, 2005) Yap

CS 33. Shells and Files. CS33 Intro to Computer Systems XX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Recitation 8: Tshlab + VM

System Programming. Process Control II

TCSS 422: OPERATING SYSTEMS

CS240: Programming in C

everything is a file main.c a.out /dev/sda1 /dev/tty2 /proc/cpuinfo file descriptor int

System Calls and Signals: Communication with the OS. System Call. strace./hello. Kernel. Context Switch

fork System-Level Function

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Process Management! Goals of this Lecture!

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

Chap 4, 5: Process. Dongkun Shin, SKKU

IC221: Systems Programming 12-Week Written Exam [SOLUTIONS]

System Programming. Introduction to Unix

Operating Systems. II. Processes

University of Washington What is a process?

Section 2: Processes

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

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

UNIX System Programming. Overview. 1. A UNIX System. 2. Processes (review) 2.1. Context. Pipes/FIFOs

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

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

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

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

A process. the stack

Operating systems. Lecture 7

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

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

Process Management 1

Unix Processes 1 / 31

Processes: Introduction. CS 241 February 13, 2012

Process Management! Goals of this Lecture!

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

Operating Systems 2230

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

Processes. CS3026 Operating Systems Lecture 05

Process management 1

COE518 Lecture Notes Week 2 (Sept. 12, 2011)

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

CSC 252: Computer Organization Spring 2018: Lecture 19

Process Turnaround Time Total Wait Time P 1 12 ms 0 ms P 2 21 ms 12 ms P 3 23 ms 18 ms P 4 20 ms 17 ms

System Programming. Process Control III

Preview. System Call. System Call. System Call. System Call. Library Functions 9/20/2018. System Call

Operating Systems. Lecture 05

Processes. Johan Montelius KTH

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

Carnegie Mellon. Processes. Lecture 12, May 19 th Alexandre David. Credits to Randy Bryant & Dave O Hallaron from Carnegie Mellon

September 2 nd, 2015 Prof. John Kubiatowicz

CSE 153 Design of Operating Systems Fall 2018

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

Transcription:

Preview Process Control What is process? Process identifier A key concept in OS is the process Process a program in execution Once a process is created, OS not only reserve space (in Memory) for the process but also need spaces (process table, page table )to keep tracking the process. Process associated with Address space where the executable program, program data and stack are allocated in a memory Set of registers (Program counter, stack pointer, and other registers) All other information for executing the process. 1 2 CPU Registers ALU RAM Job1 Job2 1 Running 3 Three States for a Process 2 Job3 Blocked Ready Control Unit OS 4 1. A process blocked for I/O 2. A process time out 3. A process scheduled 4. A process finish it s I/O 3 4 The microprocessor s main task is to execute instructions. The instruction cycle is therefore at the heart of understanding the function and operation of the microprocessor. Fetch Cycle Instruction Cycle Execute Cycle Fetch cycle 1. Reading the address of the instruction in (PC) to be executed from the memory and 2. Loading it into the Instruction register (IR). 3. Program Counter register (PC) is modified to point at the next valid instruction. 5 6 1

(7) PC=PC+1 PC MAR (1) 0020 0020 IR Load Accumulator with M(A) MDR (6) Load Accumulator with M(A) (2) (5) Address Bus 0020 Data Bus Load Accumulator with M(A) Load Accumulator with 0020 M(A) (3) (4) Execute cycle The contents of the IR are decoded and executed. The execution may result in a variety of actions depending on the type of instruction. It may be a self contained instruction, or it can involve interaction with memory and ALU. 7 8 All information about each process is stored in an operating system table called process table (process control block). If a process is suspended (ready or block state), information for the snapshot of the process are stored in its process table. Once the process resume a CPU time, all information for the process execution are copy back from its process table When a process is created, kernel provides unique process ID, a nonnegative integer. When a process terminate, its ID becomes reusable for a newly created process. There are couple of process ID numbers which is used by system itself. Process ID 0: a process scheduler Process ID 1: the init process 9 10 The ps command show the process we are running, the another user is running, or all the process on the system. To see every process on the system using standard syntax: ps -e ps -ef ps -ef ps -ely To see every process on the system using BSD syntax: ps ax ps axu To print a process tree: ps -ejh ps axjf To get security info: ps axz ps -em 11 12 2

/* processid.c get a process information */ #include <stdlib.h> #include <unistd.h> int main() /* Process ID of calling process */ printf ("Process ID = %d \n", getpid()); /* Parent s ID of calling process */ printf ("Parent's ProcessID = %d \n", getppid()); /* Real user s ID of calling process */ printf ("Real User's ID = %d \n", getuid()); /* Effective user s ID of calling */ printf ("Effective User's ID = %d \n", geteuid()); /* Real group ID */ printf ("Real Group's ID = %d \n", getgid()); /* Effective group ID of calling */ printf ("Effective Group ID = %d \n", getegid()); An existing process can create a new process by calling the fork system call. The fork() calls once but returns twice: a child returns 0 to its parent and a parent returns child s process ID number to the child. The child process get a copy of the data space heap and stack and they don t share the memory space. They only share the text segment. 13 14 /* fork.c demonstrate fork() system call */ #define MAX_COUNT 10000 ; /* child process prototype */ void /* parent process prototype */ high address stack void main(void) ppid = getpid(); /* get parent process ID */ /* create a child */ /* means a child process*/ heap Uninitialized data (bss) Initialized to zero by exec for (i = 1; i <= MAX_COUNT; i++) printf(" This line is from child process value = %d\n", i); printf(" *** Child process is done ***\n"); low address Initialized data text read from program file by exec 15 for (i = 1; i <= MAX_COUNT; i++) printf("this line is from parent process value = %d\n", i); printf("*** Parent is done ***\n"); 16 Parent child pid = 3469 pid = 0 Parent child pid = 3469 pid = 0 17 18 3

/* fork2.c */ #include <string.h> #define MAX_COUNT 200 #define BUF_SIZE 100 int main(void) pid_t pid, ppid; char buf[buf_size]; ppid = getpid(); /* this is parent process ID */ fork(); /*create a child */ for (i = 1; i <= MAX_COUNT; i++) pid = getpid(); if (pid == ppid)/* parent works here */ sprintf(buf, "Parent(%d) process executed %d times\n", ppid, i); write(1, buf, strlen(buf)); /* child work here */ sprintf(buf, "Child(%d) process executed %d times\n", pid, i); write(1, buf, strlen(buf)); Consider a process that has two different files opened for input and output. On return from fork, parent and child process share file table, since a child copy all from its parent. Even offset of file will be shared with both processes. 19 20 Parent Process Table File table V-node table Parent Process Table File table V-node table Shared input file table Child Process Table Child Process Table Output file table for parent Output file table for child 21 22 include <unistd.h> #include <fcntl.h> #include <ctype.h> #include <sys/stat.h> void error_sys(char *msg) printf("%s\n", msg); exit(1); int main(int argc, char *argv[]) int input, chout, nbyte, pout; char buff[1]; if (argc!= 2) error_sys("usage: Argument number error\n"); input = open(argv[1], O_RDONLY); /* create a child */ /* child process */ if ((chout = open("child.txt", O_WRONLY O_CREAT, S_IREAD S_IWRITE)) == -1) error_sys("output File Create Error"); while ((nbyte = read(input, buff, 1)) > 0) if (write(chout, buff, 1)!= 1) error_sys("write Error"); /* parent */ if ((pout = open("parent.txt", O_WRONLY O_CREAT, S_IREAD S_IWRITE)) == -1) error_sys("output File Create Error"); while ((nbyte = read(input, buff, 1)) > 0) if (write(pout, buff, 1)!= 1) error_sys("write Error"); If both parent and child write to the same file descriptor, without any form of synchronization what will be happen? The output will be intermixed between child and parent s works. Solution: The parent waits for the child to complete Both the parent and the child go their own ways 23 24 4

The vfork() System Call There are two uses for fork: When a process want to duplicate itself so that parent and child can each execute different section of code at the same time Network Server When a process wants to execute a different program the child does an exec right after it returns from the forks The semantics of vfork() differs from the system call fork(). vfork() function is used to create a new process when the purpose of the new process is to exec a new program. vfork() function create a process without copying the address space of the parent. A child runs in the address space of the parent. vfork guarantees that the child runs first, until the child calls exec or _exit it might lead to deadlock. 25 26 /* fileshare3.c child process run different program /* fork3.c */ #include <string.h> int glob =6; /*global variable */ int main() int local; local =88; /*local variable */ printf ("before vfork\n"); if ((pid = vfork())<0) /* create a child */ printf("vfork error"); exit (1); /* for child process */ glob++; local++; printf("child pid = %d, global = %d, local = %d\n",getpid(),glob, local); _exit(0); /* for parent process */ printf(" Parent pid = %d, global = %d, local = %d\n",getpid(),glob, local); exit(0); #include <unistd.h> #include <fcntl.h> #include <ctype.h> #include <sys/stat.h> void error_sys(char *msg) printf("%s\n", msg); exit(1); int main(int argc, char *argv[]) int input, chout, nbyte, pout; char buff[1]; if (argc!= 2) error_sys("usage: Argument number error\n"); input = open(argv[1], O_RDONLY); pid = vfork(); /* create a child */ /* child process */ execv("reverse", argv); /* child process run different program _exit(0); /* parent */ if ((pout = open("parent.txt", O_WRONLY O_CREAT, S_IREAD S_IWRITE)) == -1) error_sys("output File Create Error"); lseek(input, 0, SEEK_SET); while ((nbyte = read(input, buff, 1)) > 0) if (write(pout, buff, 1)!= 1) error_sys("write Error"); 27 28 A situation where two or more processes are reading or writing some shared data and the final result depends on who runs precisely when, are called race condition. Slots for file names When a process want to print a file, it enter a file name in a special spooler directory Printer daemon periodically check spooler directory any file need to be printed. 29 30 5

Process A tried to send a job to spooler, Process A read in = 7, process A time out and go to ready state before updating in = in + 1. Process B tried to send a job to spooler. Process B read in = 7, load its job name in slot 7, update i = i + 1 = 8 and then go to block state for waiting for job. Process A is rescheduled by scheduler. Process A already read in = 7, Process A load its job name in slot 7, update i = i + 1 = 9 and then go to blocked state waiting for this job finish. /*race.c : shows example of race condition*/ #include <stdlib.h> #include <string.h> static void charatatime(char *); int main() /* create a child */ if ( (pid = fork()) < 0) printf("fork error"); exit (1); /* a child and parent call same function */ charatatime("output from child\n"); charatatime("output from parent\n"); exit(0); static void charatatime(char *str) char *ptr; /* buffer for sharing between child and parent */ int c; setbuf(stdout, NULL); /*set unbuffered */ for (ptr =str; c = *ptr++;) putc(c, stdout); 31 32 6