Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.

Slides:



Advertisements
Similar presentations
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Advertisements

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
The Process Model.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
Thursday, June 08, 2006 The number of UNIX installations has grown to 10, with more expected. The UNIX Programmer's Manual, 2nd Edition, June, 1972.
Processes CSCI 444/544 Operating Systems Fall 2008.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
1 CS 333 Introduction to Operating Systems Class 2 – OS-Related Hardware & Software The Process Concept Jonathan Walpole Computer Science Portland State.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Process Concept An operating system executes a variety of programs
University of Pennsylvania 9/12/00CSE 3801 Multiprogramming CSE 380 Lecture Note 3.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Introduction to Processes CS Intoduction to Operating Systems.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Threads and Processes.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Overview of Hardware.
Multiprogramming CSE451 Andrew Whitaker. Overview Multiprogramming: Running multiple programs “at the same time”  Requires multiplexing (sharing) the.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Thread Scheduling.
Creating and Executing Processes
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
CS333 Intro to Operating Systems Jonathan Walpole.
System calls for Process management
CS 153 Design of Operating Systems Spring 2015 Lecture 5: Processes and Threads.
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Processes and Virtual Memory
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
PRINCIPLES OF OPERATING SYSTEMS Tutorial-4: Multi-process and Multi-threaded Programming CPSC 457, Spring 2015 May 28/29, 2015 Department of Computer Science,
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
CS399 New Beginnings Jonathan Walpole.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Multithreading Tutorial
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Jonathan Walpole Computer Science Portland State University
Processes in Unix, Linux, and Windows
Multithreading Tutorial
Unix System Calls and Posix Threads
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Multithreading Tutorial
Jonathan Walpole Computer Science Portland State University
Threads Chapter 5 2/23/2019 B.Ramamurthy.
Multithreading Tutorial
Lecture 6: Multiprogramming and Context Switching
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
CS510 Operating System Foundations
Presentation transcript:

Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads

Overview  Process-related Unix system calls  Posix threads 2

Process-related Unix System Calls  A process in Unix consists of an address space and one thread  Unix provides several process-related system calls: o getpid(), getppid()  Get unique id for process, for parent process  Pid identifies both address space and thread o fork(), execv()  Create new processes o exit(), wait()  Terminate processes and synchronize with terminating process o kill(), sigaction()  Communicate with another process via signals 3

Fork System Call  fork() system call creates a new process o The original process is called the parent process o The new process is called the child of the parent process  Child process is an identical copy of the parent o Thread state (registers) and address space are copied 4 stack text data Address space SP PC Child stack text data Address space SP PC Parent

Fork  After fork system call returns: o Both parent and child process start executing the instruction after fork o To distinguish the two processes, fork returns PID of child in parent process, and 0 in child process o Parent and child run code and modify data completely independently, how?  Why the weird call? int n = 5; int pid = fork(); if (pid == 0) { // run child code n = n + 1; } else { // pid value > 0 // run parent code n = n - 1; } 5

Execv System Call  execv() call allows running a new program o Fork creates a new process that is a copy of its parent, but it doesn’t allow running a new program  When process calls execv, new program replaces current process 6 stack text data Address space SP PC After execve stack text data Address space SP PC Before execve

Execv  On execv system call: o New program is loaded from executable file on disk into current program’s address space o Code and data regions are copied from disk o Stack is initialized with activation frame of main() o Processor registers are reinitialized o New program’s process ID is the same as old program’s process ID o execv() does not return, why? char *cmd = “/bin/ls”; char *arg1 = “-l”; char *args[3]; args[1] = arg1; args[2] = NULL; execv(cmd, args); // code doesn’t execute 7

Exit System Call  A process can terminate itself by calling the exit() system call  On exit(retval) system call: o Address space of the process is destroyed (memory for code, data, etc., regions is reclaimed) o Process-specific OS state, e.g., open files, is destroyed o retval is saved, can be returned to parent process  Why is this useful? o Thread state is destroyed  When is this done? 8

Wait System Call  A parent process can wait for a child process to terminate by calling the wait() system call o When child issues exit(retval), wait() returns retval o What happens if child exits before parent issues wait? o What happens if parent never issues wait()? 9

Wait  Wait needs to handle 4 cases o Parent issues wait before or after child’s exit o Parent doesn’t issue wait, exits before or after child’s exit W: wait, C: continue, E: exit, D: destroy 10 ParentChild W CE D ParentChild W C E D ParentChild E E D ParentChild E E D

Kill and Sigaction System Calls  Unix allows processes to send signals (or messages) to itself or to other processes by calling the kill() system call  Receiver process handles signals similar to interrupts o Receiver process is immediately interrupted o It starts executing a function called a signal handler o When signal handler finishes, normal execution continues  The sigaction() system call allows a process to setup the signal handler function o When no handler is setup, receiver process is forced to exit o Receiver process exits when it is scheduled to run next, why? 11

Code Examples  Read the man pages of the Unix system calls if you have trouble understanding their semantics o On google: man fork  Some code examples are available on the web site  shell.c o A simple shell program in Unix  unix-signal.c o A program that shows how signals are used in Unix 12

Posix Threads  Recall that a Unix process consists of an address space with one thread o Within main(), this thread is running  POSIX Threads or the pthreads system calls allow creating additional threads in a Unix process 13

Posix Threads API  pthread_create(thread, attr, start_routine, arg) o Returns new thread ID in thread o Executes function specified by start_routine ( arg )  pthread_exit(status) o Terminates current thread, returns status to a joining thread  pthread_join(thread_id, status) o Blocks thread until thread specified by thread_id terminates o Return value from pthread_exit is passed in status  pthread_yield() o Thread gives up processor and enters the run queue 14

Posix Threads Synchronization API  Pthreads provides mutex, semaphores, and condition variables  Mutex  Semaphores 15 sem_t sem_name; sem_init(&sem_name, 0, 0); /* 2nd arg is flag, 3rd arg is init value */ sem_wait(&sem_name); /* wait operation */ sem_post(&sem); /* signal operations */ pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&mut); pthread_mutex_unlock(&mut);

Posix Monitor Example  Say a thread wishes to wait until x > y  Another thread signals when x > y 16 pthread_mutex_lock(&mut); /* modify x and y */ if (x > y) pthread_cond_signal(&cond); pthread_mutex_unlock(&mut); int x,y; pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_mutex_lock(&mut); while (x <= y) { pthread_cond_wait(&cond, &mut); } /* operate on x and y */ pthread_mutex_unlock(&mut);

Posix Code Examples  Some posix code examples are available on the web site  pthread-example.c o Several threads are created and run without synchronization  pthread-example-sync.c o Several threads are created and run with synchronization 17

Summary  Unix OS provides various system calls for managing processes o fork creates a new clone process o execv loads a new program in an existing process o exit ends a process o wait allows a parent process to wait for a child’s exit o kill and sigaction are used to send signals to processes, similar to how device controllers interrupt the CPU  Pthreads is a standardized API for creating and managing threads in Unix OSes 18

Think Time  What happens when the various system calls we have discussed fail (due to some error)?  How would you write a Process A that waits for the exit of another arbitrary Process B (not just a child process), using the system calls we have discussed? Can Process A poll to check if Process B has exited? How will you ensure that Process A does not miss the exit event?  Think about how the OS handles the various wait scenarios, i.e., how is the synchronization between the parent and the child implemented in the four cases?  What are the steps involved in sending and receiving signals? 19