Fall 2015 COMP Operating Systems. Lab #3

Similar documents
CS240: Programming in C

CSC 1600 Unix Processes. Goals of This Lecture

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

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

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

CS240: Programming in C

Processes. CS3026 Operating Systems Lecture 05

Operating Systems. Lecture 05

518 Lecture Notes Week 3

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

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.

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

Operating systems and concurrency - B03

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.

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

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

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

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

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

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

Process Management 1

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

PROCESS PROGRAMMING INTERFACE

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

CS 201. Processes. Gerson Robboy Portland State University

System Programming. Process Control III

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

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

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

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

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

Operating System Structure

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

Processes. Dr. Yingwu Zhu

Processes. Process Concept

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

csci3411: Operating Systems

Operating Systems & Concurrency: Process Concepts

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

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

This document gives a general overview of the work done by an operating system and gives specific examples from UNIX.

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Process management 1

Programming Assignment #1: A Simple Shell

Operating Systems CMPSC 473. Process Management January 29, Lecture 4 Instructor: Trent Jaeger

Introduction to Processes

Chapter 2: System Structures. Operating System Concepts 9 th Edition

Operating Systems Lab

Mon Sep 17, 2007 Lecture 3: Process Management

Part II Processes and Threads Process Basics

Roadmap. Tevfik Ko!ar. CSC Operating Systems Spring Lecture - III Processes. Louisiana State University. Virtual Machines Processes

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

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

Operating Systems. II. Processes

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

Process Management! Goals of this Lecture!

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CS510 Operating System Foundations. Jonathan Walpole

CSCE 313: Intro to Computer Systems

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

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

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

Chapter 3 Process Description and Control

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

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

Process Management 1

ENGR 3950U / CSCI 3020U Midterm Exam SOLUTIONS, Fall 2012 SOLUTIONS

TCSS 422: OPERATING SYSTEMS

fork System-Level Function

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

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

Computer Systems II. First Two Major Computer System Evolution Steps

Operating System Labs. Yuanbin Wu

Unix Processes 1 / 31

CSE 380 Computer Operating Systems. Instructor: Insup Lee. University of Pennsylvania Fall 2003

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

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

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

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

SOFTWARE ARCHITECTURE 3. SHELL

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

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

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

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

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

Process Concepts 8/21/2014. CS341: Operating System. Scheduling: Theoretical Analysis

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

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

Chapter 2: Operating-System Structures

Chapter 3: Process Concept

Chapter 3: Process Concept

Chapter 3: Processes

* What are the different states for a task in an OS?

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

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

Processes. Processes (cont d)

Transcription:

Fall 2015 COMP 3511 Operating Systems Lab #3

Outline n Operating System Debugging, Generation and System Boot n Review Questions n Process Control n UNIX fork() and Examples on fork() n exec family: execute a program

Operating-System Debugging n Debugging is finding and fixing errors, or bugs n Usually the OS generates log files containing error information n Failure of an application can generate core dump file capturing memory of the process n Operating system failure can generate crash dump file containing kernel memory n Beyond crashes, performance tuning can optimize system performance n Sometimes using trace listings of activities, recorded for analysis n Profiling is periodic sampling of instruction pointer to look for statistical trends

Performance Tuning n Improve performance by removing bottlenecks n OS must provide means of computing and displaying measures of system behavior n For example, top program or Windows Task Manager

Operating System Generation n Operating systems are designed to run on any of a class of machines; the system must be configured for each specific computer site n SYSGEN program obtains information concerning the specific configuration of the hardware system n Used to build system-specific compiled kernel or system-tuned n Can general more efficient code than one general kernel

System Boot n When power initialized on system, execution starts at a fixed memory location n Firmware ROM used to hold initial boot code n Operating system must be made available to hardware so hardware can start it n Small piece of code bootstrap loader, stored in ROM or EEPROM locates the kernel, loads it into memory, and starts it n Sometimes two-step process where boot block at fixed location loaded by ROM code, which loads bootstrap loader from disk n Common bootstrap loader, GRUB, allows selection of kernel from multiple disks, versions, kernel options n Kernel loads and system is then running

Q. 1 n What is the main difference between a program and a process? A program is static (lines of codes stored) A process is active in execution, which has a life cycle and can be in different states Process program code program counter stack r e g i s t e r data section heap

Q. 2 n Briefly describe the process lifecycle with different states.

Q. 3 n When a process creates a child process, what are the four tasks that need to be done? Creates a new PCB for the child process allocate address space for the child process copy data from the parent process copy I/O state if any

Q. 4 n What are the two possibilities in terms of the address space of a newly created process? A. The child process has a new program loaded into it B. The child process has an exact copy of the address space of the parent process including program C. The child process has an exact copy of the address space of the parent process including data D. The child process has an exact copy of the address space of the parent process including program and data

Cont. n What are the two possibilities in terms of the address space of a newly created process? A. The child process has a new program loaded into it B. The child process has an exact copy of the address space of the parent process including program C. The child process has an exact copy of the address space of the parent process including data D. The child process has an exact copy of the address space of the parent process including program and data

Q. 5 n Describe the differences between short-term and long-term scheduling. n Short-term scheduling (CPU scheduler) selects which process should be executed next and allocates CPU n Long-term scheduling (job scheduler) determines which processes should be brought into the ready queue

Cont. n The primary difference is in the frequency of their execution n Short-term scheduling must select a new process quite often n Long-term scheduling is used much less often since it handles placing jobs in the system and may wait a while for a job to finish before it admits another one

else { /* when fork() return a positive integer, we are in the parent process */ /* the return value of the process id of the newly created child process */ int status; (void) waitpid(pid, &status, 0); } return EXIT_SUCCESS; Fork() example in C int main(void) { pid_t pid = fork(); if (pid == -1 ) { /* when fork() return -1, an error occurred */ fprintf(stderr, "Fork Failed"); exit(exit_failure); } else if (pid == 0) { /* when fork() return 0, we are in the child process */ printf( Hello from the child process! ); _exit(exit_success); }

Q. 6 n Consider the following code segment: n pid_t pid; n pid = fork(); n if (pid == 0) { n n } fork(); n if (pid > 0) { n n n } fork(); fork(); fork(); n Q: How many distinct child processes will be generated?

Q. 6 n Consider the following code segment: n pid_t pid; n pid = fork(); n if (pid == 0) { n n } fork(); n if (pid > 0) { n n n } fork(); fork(); fork(); pid > 0 pid = 0 n Q: How many distinct child processes will be generated?

Process control n When UNIX runs a process, it gives each process a unique number called process ID, or pid. n may be a "system'' program (e.g., login, csh) n or a program initiated by the user (e.g., textedit, dbxtool or a user written one).

Process control n The UNIX command "ps" will list all current processes running on your machine with their pid. n The C function int getpid() will return the process id of process that called this function.

fork(): create a new process n The fork() system call will spawn a new child process, which is an identical process to the parent except that has a new system process ID. fork()=x getpid()=y n The process is copied in memory from the parent and a new process structure is assigned by the kernel. Parent end fork() fork()=0 getpid()=x Child end

fork(): create a new process n Synopsis: n #include <sys/types.h> #include <unistd.h> n pid_t fork(void);

fork(): create a new process parent child Program code Data Heap Stack

fork(): create a new process parent child invoke fork() Program code Data Heap Stack

fork(): create a new process parent invoke fork() child Program code Data Heap Stack

fork(): create a new process parent invoke fork() child Program code Program code Data Data Heap Heap Stack Stack

fork(): create a new process parent invoke fork() a new process structure is assigned by the kernel child Program code Program code Data Data Heap Heap Stack Stack

fork(): create a new process Parent & Child: n Duplicated n Address space n Global & local variables n Current working directory n Root directory n Different n PID n Running time n Running state n Return values from fork() n Process resources n Resource limits n Etc

Return values of fork() n Successful fork() 0 Child s PID child parent n Not Successful child fork() -1 parent errno is set to indicate error

Return values of fork() n The return value of the function is which discriminates the two processes of execution. n Upon successful completion, fork() return 0 to the child process and return the process ID of the child process to the parent process. n Otherwise, (pid_t)-1 is returned to the parent process, no child process is created, and errno is set to indicate the error.

A simple C program on fork() #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 5; int main() { pid_t pid; pid = fork(); if (pid == 0) { /* child process */ value += 15; printf( CHILD: value = %d\n, value); /* Line A */ } else if (pid > 0) { /* parent process */ wait (NULL); value -=10; printf( PARENT: value = %d\n, value); /* Line B */ exit(0); } }

A simple C program on fork() n Code can be downloaded from: n http://course.cs.ust.hk/comp3511/lab/lab03/testfork.c n Compile n gcc -o testfork testfork.c n Run and understand the result n./testfork

A simple C program on fork() n Line A: CHILD: value = 20 n Line B: PARENT: value = -5 n Upon fork() system call, the variable value is made a copy in the child process, so it prints out 5+15=20; n after wait() system call, the child terminates and its copy of value is destroyed. n The parent has value=5, so it prints out 5-10= -5.

More example on fork() n Consider the program segment with fork() instruction below, and suppose each process can run to completion, i.e., no interrupt in the middle of a process execution. n Please answer the following two questions. n n Which part runs as the parent process and which as the child process? Without making any assumption on the order of executions, please show all possible outputs (suppose each process runs to completion, i.e. no interruption) main() { int x; x=0; if (fork()) { x=x+1; /* A part */ printf( A produces %2d\n, x); } else { x=x+1; /* B part */ printf( B produces %2d\n, x); } }

More example on fork() n Which part runs as the parent process and which as the child process? n A part: Parent B part: Child n All possible outputs A produces 1 B produces 1 Or B produces 1 A produces 1

More example on fork() n How many processes are created, if the following program finishes successfully? int main() { int i=0; for (i=1; i<=100; i++) { fork(); } } n Answer: 2^100 or 2^100-1. This can be deduced by 1 fork() generates 2 processes,2 fork() generates 4 process and so on

More example on fork() n How many processes are created after the following program executes? int main() { if (fork()>0) fork(); } n Answer: 3 (if you do not count the original processes, the answer is 2)

exec family: execute a program n fork() can only duplicate a process n How to execute other programs like ls? //ProgramA.c int main() { : want to execute ls : }

exec family: execute a program n The exec system call family has the following members: n execl n execlp n execle n execv n execvp n execve n The exec system call family changes the process image of the calling process.

exec(): execute a file n Each of the functions in the exec family replaces the current process image with a new process image. n The new image is constructed from a regular, executable file called the new process image file. This file is either an executable object file or a file of data for an interpreter. n There is no return from a successful call to one of these functions because the calling process image is overlaid by the new process image.

exec family: execute a program //ProgramA.c n Before calling exec Process PID int main() { : //want to execute ls execlp("/bin/ls", "ls", NULL); : } program code program counter r e g i s t e r ProgramA.c stack data section heap

exec family: execute a program //ProgramA.c n While calling exec Preserved Process PID Reset int main() { : //want to execute ls execlp("/bin/ls", "ls", NULL); : } program code program counter r e g i s t e r /bin/ls Change program code stack data section Overwritten by the new program heap process is running on a different program!

execlp() n execlp() initiates a new program in the same environment in which it is operating. n An executable and arguments are passed to the function. n int execlp(const char *file, const char *arg0,..., const char *argn, char * /*NULL*/); n e.g. execlp("/bin/ls", "ls", NULL); n It will use environment variable PATH to determine which executable to process. Thus a fully qualified path name would not have to be used.

execlp() n What is the PATH environment variable? n The locations for the exec call to search for the executables. n e.g.1 execlp("/bin/ls", "ls", NULL); n e.g.2 execlp( ls, ls, NULL) PATH=/bin;/usr/bin. n Then, the execlp call will only search the specified two locations, /bin and /usr/bin, for the executable named ls. n PATH is stored inside.cshrc

exec family: execute a program n There is no return from a successful call! n Because the calling process image is overlaid by the new process image //ProgramA.c int main() { : //execute ls execlp("/bin/ls", "ls", NULL); } printf( successful! );

exec family: execute a program n There is no return from a successful call! n Because the calling process image is overlaid by the new process image //ProgramA.c int main() { : //execute ls execlp("/bin/ls", "ls", NULL); This WILL NOT be printed if ls is successfully executed! } printf( successful! );

exec family: execute a program n There is no return from a successful call! n Because the calling process image is overlaid by the new process image //ProgramA.c int main() { : //execute ls execlp("/bin/abc", abc", NULL); } printf( command not found! );

exec family: execute a program n There is no return from a successful call! n Because the calling process image is overlaid by the new process image //ProgramA.c int main() { : //execute ls execlp("/bin/abc", abc", NULL); This WILL be printed if abc is NOT successfully executed! } printf( command not found! );

Useful links n For more about fork, exec, and process control: http://www.yolinux.com/tutorials/forkexecprocesses.html n Use info to learn more details from UNIX Manual Pages for fork, exec, and execlp