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

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

CSC 1600 Unix Processes. Goals of This Lecture

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

518 Lecture Notes Week 3

CS240: Programming in C

Fall 2015 COMP Operating Systems. Lab #3

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

Part II Processes and Threads Process Basics

Environment Variables

UNIX System Calls. Sys Calls versus Library Func

Interrupts, Fork, I/O Basics

CS Operating Systems Lab 3: UNIX Processes

Operating System Structure

CS 550 Operating Systems Spring Process II

Operating systems and concurrency - B03

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

CMPSCI 230 Computer Systems Principles. Processes

Lecture 4: Process Management

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.

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

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

Introduction to Processes

Processes COMPSCI 386

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

System Programming. Introduction to Unix

Process Creation in UNIX

Computer Systems Assignment 2: Fork and Threads Package

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

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

Process Management 1

CS240: Programming in C

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

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

Process Management! Goals of this Lecture!

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

Operating Systems. Lecture 05

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

Process Management! Goals of this Lecture!

Recitation 8: Tshlab + VM

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

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

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

TCSS 422: OPERATING SYSTEMS

File Descriptors and Piping

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

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

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

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

CS510 Operating System Foundations. Jonathan Walpole

System Programming. Process Control II

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

Operating Systems Lab

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Process management 1

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

PROCESS PROGRAMMING INTERFACE

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

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

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

Network Socket Programming - 1 BUPT/QMUL

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

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

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

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

System Programming. Process Control III

CSC209 Fall Karen Reid 1

September 2 nd, 2015 Prof. John Kubiatowicz

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

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

Parameter Passing in C. Pointer Arithmetic. Parameter Passing in C. Pointer Arithmetic. Pointer Arithmetic. Tevfik Ko!ar

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

CSC209H Lecture 7. Dan Zingaro. February 25, 2015

Operating systems. Lecture 7

Parents and Children

Operating Systems. II. Processes

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

Exceptional Control Flow Part I

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

Mon Sep 17, 2007 Lecture 3: Process Management

Operating Systems. Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) alphapeeler.sf.net/pubkeys/pkey.htm

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

Process Management 1

628 Lecture Notes Week 4

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

fork System-Level Function

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

Unix Processes 1 / 31

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

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

CS 322 Operating Systems Practice Midterm Questions

Section 2: Processes

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

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

프로세스간통신 (Interprocess communication) i 숙명여대창병모

Operating System Labs. Yuanbin Wu

ECE 650 Systems Programming & Engineering. Spring 2018

MMAP AND PIPE. UNIX Programming 2015 Fall by Euiseong Seo

Transcription:

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

Von Neumann Computer Architecture: An integrated set of algorithms and data structures capable of storing and executing programs. COSC350 System 2 Software, Fall 2015

Hardware CPU (Central Processing Unit) Control Unit Arithmetic Logical Unit (ALU) Inboard memory registers cache memory main memory COSC350 System 3 Software, Fall 2015 I/O devices - keyboard, mouse, disk drives, etc Outboard storage - disks, CDs

Hardware-level operations Fetch-execute cycle: Processor fetches an instruction from memory. Processor executes the fetched instruction Program counter- contains the address of the next instruction to be executed The fetched instruction is loaded into Instruction Register Program counter is incremented after fetching Processor interprets the bits stored in the IR and performs the required action COSC350 System 4 Software, Fall 2015

Processor Logic COSC350 System 5 Software, Fall 2015

What is a Process A key concept in OS is the process Process a program in execution 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. COSC350 System Software, Fall 2015 6

What is a Process 1 Running Three States for a Process 2 3 Blocked 4 Ready 1. A process blocked for I/O 2. A process time out 3. A process scheduled 4. A process finish it s I/O COSC350 System Software, Fall 2015 7

What is a Process CPU RAM Registers Job1 ALU Job2 Job3 Control Unit OS COSC350 System Software, Fall 2015 8

Registers Code Data Stack Program Counter (PC) Environment Pointer Heap COSC350 System Software, Fall 2015 9

Interested in Memory Management Registers, Code segment Registers Details of instruction set Program counter (PC) Stores address of the current program instruction (in the code segment area) and is normally incremented after each instruction Data Segment Stack contains data related to block entry/exit (called activation record) Heap contains data of varying lifetime Environment pointer points to current stack position COSC350 System Software, Fall 2015 10

What is a Process 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 COSC350 System Software, Fall 2015 11

Process Identifiers When a process is created, kernel provides unique process ID, a nonnegative integer. 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 COSC350 System Software, Fall 2015 12

Process Identifiers The ps command show the process we are running, the another user is running, or all the process on the system. COSC350 System Software, Fall 2015 13

Process Identifiers 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 COSC350 System Software, Fall 2015 14

Process Identifiers /* processid.c get a process information */ #include <stdio.h> #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()); return 0; COSC350 System Software, Fall 2015 15

The fork() System Call An existing process can create a new process by calling the fork function. The fork function 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 but they don t share the memory space. They only share the text segment. COSC350 System Software, Fall 2015 16

The fork() System Call high address stack heap low address Uninitialized data (bss) Initialized data text Initialized to zero by exec read from program file by exec COSC350 System Software, Fall 2015 17

/* fork.c demonstrate fork() system call */ #include <stdio.h> #include <sys/types.h> #define MAX_COUNT 100 void ChildProcess(); /* child process prototype */ void ParentProcess(); /* parent process prototype */ void main(void) pid_t pid, ppid; pid = fork(); /* create a child */ if (pid == 0) /* means a child process*/ ChildProcess(); else ParentProcess(); void ChildProcess() int i; for (i = 1; i <= MAX_COUNT; i++) printf(" This line is from child process value = %d\n", i); printf(" *** Child process is done ***\n"); void ParentProcess() int i; for (i = 1; i <= MAX_COUNT; i++) printf("this line is from parent process value = %d\n", i); printf("*** Parent is done ***\n"); COSC350 System Software, Fall 2015 18

The fork() System Call void main() pid_t pid; Parent void main() pid_t pid; child pid = 3469 pid = 0 pid = fork(); if (pid == 0) ChildProcess(); else ParentProcess(); pid = fork(); if (pid == 0) ChildProcess(); else ParentProcess(); void ChildProcess()... void ParentProcess()... void ChildProcess()... void ParentProcess()... COSC350 System Software, Fall 2015 19

The fork() System Call void main() pid_t pid; Parent void main() pid_t pid; child pid = 3469 pid = 0 pid = fork(); if (pid == 0) ChildProcess(); else ParentProcess(); pid = fork(); if (pid == 0) ChildProcess(); else ParentProcess(); void ChildProcess()... void ParentProcess()... void ChildProcess()... void ParentProcess()... COSC350 System Software, Fall 2015 20

/* fork2.c */ #include <stdio.h> #include <string.h> #include <sys/types.h> #define MAX_COUNT 200 #define BUF_SIZE 100 int main(void) pid_t pid, ppid; int i; 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)); else /* child work here */ sprintf(buf, "Child(%d) process executed %d times\n", pid, i); write(1, buf, strlen(buf)); return 0; COSC350 System Software, Fall 2015 21

File Sharing Consider a process that has two different files opened for standard input, standard 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. COSC350 System Software, Fall 2015 22

File Sharing Parent Process Table fd0 fd1 File table File status flags File offset V-nod pointer V-node table V-Node Information I-Node Information File size Child Process Table fd0 fd1 File status flags File offset V-nod pointer V-Node Information I-Node Information File size COSC350 System Software, Fall 2015 23

#include <unistd.h> #include <fcntl.h> #include <ctype.h> #include <stdio.h> #include <sys/types.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; int i; char buff[1]; if (argc!= 2) error_sys("usage: Argument number error\n"); input = open(argv[1], O_RDONLY); pid_t pid; pid = fork(); /* create a child */ if (pid == 0) /* 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"); else /* 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"); return 0; COSC350 System Software, Fall 2015 24

File Sharing If both parent and child write to the same 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 COSC350 System Software, Fall 2015 25

File Sharing 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 COSC350 System Software, Fall 2015 26

The vfork() System Call 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. It 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. COSC350 System Software, Fall 2015 27

/* fork3.c */ #include <stdio.h> #include <string.h> #include <sys/types.h> int glob =6; /*global variable */ int main() int local; pid_t pid; local =88; /*local variable */ printf ("before vfork\n"); if ((pid = vfork())<0) /* create a child */ printf("vfork error"); exit (1); else if (pid == 0) /* 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); COSC350 System Software, Fall 2015 28

Race Condition Race Condition 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. COSC350 System Software, Fall 2015 29

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. COSC350 System Software, Fall 2015 30

Race Condition 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 in = in + 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 in = in + 1 = 9 and then go to blocked state waiting for this job finish. COSC350 System Software, Fall 2015 31

/*race.c : shows example of race condition*/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> static void charatatime(char *); int main() pid_t pid; /* create a child */ if ( (pid = fork()) < 0) printf("fork error"); exit (1); /* a child and parent call same function */ if (pid == 0) charatatime("output from child\n"); else 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); COSC350 System Software, Fall 2015 32