CS 550 Operating Systems Spring Process III

Similar documents
CS 550 Operating Systems Spring Process II

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

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

CSC209 Fall Karen Reid 1

CS 201. Processes. Gerson Robboy Portland State University

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

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

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

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

Processes: Introduction. CS 241 February 13, 2012

Assignment 1. Teaching Assistant: Michalis Pachilakis (

fork System-Level Function

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Process Control. Philipp Koehn. 23 April 2018

Operating System Structure

CSC 1600 Unix Processes. Goals of This Lecture

Exceptional Control Flow Part I Oct. 17, 2002

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

Process Management 1

Operating systems and concurrency - B03

Giving credit where credit is due

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

University of Washington What is a process?

Processes, Exceptional

Altering the Control Flow

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

Exceptional Control Flow Part I

Section 2: Processes

CS240: Programming in C

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

Exceptional Control Flow Part I September 22, 2008

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

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. Session 2. UNIX processes and forking fork system call exec system call death of process kill fork and I/O using it UNIX.

Altering the Control Flow

Process. Heechul Yun. Disclaimer: some slides are adopted from the book authors slides with permission 1

Processes. CSE 351 Autumn Instructor: Justin Hsia

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

CSC 252: Computer Organization Spring 2018: Lecture 19

Exceptional Control Flow Part I

by Marina Cholakyan, Hyduke Noshadi, Sepehr Sahba and Young Cha

Processes. CSE 351 Autumn Instructor: Justin Hsia

Are branches/calls the only way we can get the processor to go somewhere in a program? What is a program? A processor? A process?

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.

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

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

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

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

518 Lecture Notes Week 3

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

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

OS Interaction and Processes

Today. Introduction to Computer Systems /18 243, Fall th Lecture. Control Flow. Altering the Control Flow.

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Exceptional Control Flow Part I Oct. 28, 2009"

Fall 2015 COMP Operating Systems. Lab #3

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

csci3411: Operating Systems

CS3733: Operating Systems

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

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

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

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

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Today. Exceptional Control Flow Processes. Exceptions and Processes. Control Flow. Altering the Control Flow

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

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

The Kernel Abstraction. Chapter 2 OSPP Part I

Operating Systems. II. Processes

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

Exceptional Control Flow: Exceptions and Processes

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Computer Science & Engineering Department I. I. T. Kharagpur

CSCI 4061: Making Processes

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

SOFTWARE ARCHITECTURE 3. SHELL

Process Management! Goals of this Lecture!

CS 537 Lecture 2 - Processes

System Programming. Process Control II

Operating System Design

CSE351 Inaugural Edi7on Spring

Processes. CS3026 Operating Systems Lecture 05

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

OS lpr. www. nfsd gcc emacs ls 9/18/11. Process Management. CS 537 Lecture 4: Processes. The Process. Why Processes? Simplicity + Speed

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

Chap 4, 5: Process. Dongkun Shin, SKKU

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

CSE 153 Design of Operating Systems Fall 2018

Process. Heechul Yun. Disclaimer: some slides are adopted from the book authors slides with permission

www nfsd emacs lpr Process Management CS 537 Lecture 4: Processes Example OS in operation Why Processes? Simplicity + Speed

Process Management 1

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

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

Program and OS interac0ons: Excep0ons and Processes

Unix Processes 1 / 31

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

Parents and Children

Transcription:

CS 550 Operating Systems Spring 2018 Process III 1

Recap: Memory Layout of a typical process MAX Stack Function Call Arguments, Return Address, Return Values Gap Heap Data Dynamically allocated memory (e.g. malloc()) Global variables, constants etc 0 Text Program Code Stack and heap grow towards each other 2

Assembly Example int opr1 = 0x1234 int opr2 = 0x0002 int main() { int c = opr1 + opr2; printf( Value = %d, c); } return 0; section.data opr1 dw 1234h opr2 dw 0002h section.text global main extern printf message db "Value = %d", 10, 0 int main() mov eax, opr1 mov ebx, opr2 add eax,ebx push eax push message call printf add esp, 8 ret 3

Mappings section.data opr1 dw 1234h opr2 dw 0002h message db "Value = %d", 10, 0 ELF Program Virtual Memory Address Stack Physical Memory Address Stack section.text global main extern printf int main() mov eba, opr1 mov ebx, opr2 add eax,ebx push eax push message call printf add esp, 8 ret ELF Header.text.rodata.data.bss Heap.bss.data.rodata.text Heap Data Code 4

Recap: Process Creation Always using fork() system call. When? User runs a program at command line OS creates a process to provide a service Check the directory /etc/init.d/ on Linux for scripts that start off different services at boot time. One process starts another process For example in servers 5

Creating a New Process - fork() Example code fork_ex.c http://www.cs.binghamton.edu/~huilu/examples/fork_ex.c int main() { pid_t pid; pid = fork(); if (pid == -1) { fprintf(stderr, "fork failed\n"); exit(1); } if (pid == 0) { printf("this is the child\n"); exit(0); } if (pid > 0) { printf("this is parent. The child is %d\n", pid); exit(0); } return 0; } 6

Points to Note fork() is called once But it returns twice!! Once in the parent and Once in the child The parent and the child are two different processes. Child is an exact copy of the parent. So how to make the child process do something different? Return value of fork in child = 0 Return value of fork in parent = [process ID of the child] By examining fork s return value, the parent and the child can take different code paths. 7

Recap:The exec() system call Consider how a shell executes a command $ pwd /home/huilu How did that work? Shell forked a child process The child process executed /bin/pwd using the exec() system call Exec replaces the process memory with a new program image. 8

Running another program in child exec() Parent Fork Child Exec New program image in execution 9

exec() Example code exec_ex.c http://www.cs.binghamton.edu/~huilu/examples/exec_ex.c if ((pid = fork()) < 0) { fprintf(stderr, "fork failed\n"); exit(1); } if (pid == 0) { if( execlp("echo", "echo", "Hello from the child", (char *) NULL) == -1) fprintf(stderr, "execl failed\n"); } exit(2); printf("parent carries on\n"); 10

Properties of exec() Replaces current process image with new program image. In the last example, parents image was replaced by the echo program image. All I/O descriptors open before exec stay open after exec. I/O descriptors = file descriptors, socket descriptors, pipe descriptors etc. This property is very useful for implementing filters. 11

Different Types of exec() int execl(char * pathname, char * arg0,, (char *)0); Full pathname + long listing of arguments int execv(char * pathname, char * argv[]); Full pathname + arguments in an array int execle(char * pathname, char * arg0,, (char *)0, char envp[]); Full pathname + long listing of arguments + environment variables int execve(char * pathname, char * argv[], char envp[]); Full pathname + arguments in an array + environment variables int execlp(char * filename, char * arg0,, (char *)0); Short pathname + long listing of arguments int execvp(char * filename, char * argv[]); Short pathname + arguments in an array More info: check man 3 exec 12

wait() system call Helps the parent process to know when a child completes. to check the return status of child 13

wait( ) system call if ((pid = fork()) == 0) { /* in the child exec another program image */ ret = execlp("lpr", "lpr", "myfile", (char *) 0); if( ret == -1 ) fprintf(stderr, "exec failed\n"); } else { /* in the parent - do some other stuff */ /* now check for completion of child */ waitpid(pid, &status, 0); /* Alternative ==> while (wait(&status)!= pid); */ /* remove file */ unlink("myfile"); } 14

Few other useful syscalls sleep(seconds) suspend execution for certain time exit(status) Exit the program. Status is retrieved by the parent using wait(). 0 for normal status, non-zero for error 15

Orphan process When a parent process dies, child process becomes an orphan process. The init process (pid = 1) becomes the parent of the orphan processes. Here s an example: 16

Orphan process int main() { pid_t pid; if ((pid = fork()) < 0) { fprintf(stderr, "fork failed\n"); exit(1); } if (pid == 0) { printf("child pid = %d\n", getpid()); while(1); } printf("parent exits\n"); } Do a ps l after running the above program and check parent s PID of the orphan process. After you are done remember to kill the orphan process kill 9 <pid> 17

Zombie Process When a child dies, a SIGCHLD signal is sent to the parent. If parent doesn t wait()on the child, and child exit()s, it becomes a zombie (status Z seen with ps). Zombies hang around till parent calls wait() or waitpid(). But they don t take up any system resources. Just an integer status is kept in the OS. All other resources are freed up. 18

Typical Kernel-level data structure for each process See task_struct in Linux at http://elixir.freeelectrons.com/linux/latest/source 19

References Chapter 4 of OSTEP book Chapter 2 of the Tanenbaum s book Man pages for different system calls Try man 2 <syscall_name> E.g. man 2 exec Syscalls are normally listed in section 2 of the man page Google for Linux source code 20

Backup Slides 21

systemd(1) ModemManager(1331) {gdbus}(1413) {gmain}(1411) accounts-daemon(1346) {gdbus}(1387) {gmain}(1382) acpid(1336) at-spi-bus-laun(2856) dbus-daemon(2861) {dconf worker}(2857) {gdbus}(2860) {gmain}(2858) at-spi2-registr(2883) {gdbus}(2885) {gmain}(2884) atd(1356) avahi-daemon(1352) avahi-daemon(1367) cgmanager(1348) colord(3043) {gdbus}(3047) {gmain}(3045) cron(1324) cups-browsed(1383) {gdbus}(1457) {gmain}(1456) cupsd(1341) dbus(1455) dbus-daemon(1306) dbus-daemon(2841) dconf-service(2904) {gdbus}(2907) {gmain}(2906) dhclient(1554) dnsmasq(2920) dnsmasq(2921) dockerd(2400) docker-containe(2548) {docker-containe}(2561) {docker-containe}(2562) 22