Lec3 EEE13 2s1617

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

EEE 13 LECTURE 3

PROCESS MANAGEMENT
SYSTEM CALLS
MOTIVATION

WHAT IS THIS LESSON FOR?


Goal 1: Find out how to maximize hardware utilization in
Unix-based systems


Goal 2: Understand how Unix-based machines handle
multiple tasks all at the same time
MOTIVATION

HISTORY AND OVERVIEW OF UNIX-BASED OPERATING SYSTEMS

▸ Created in 1969 by Dennis Ritchie and Ken Thompson

▸ Ported to different CPU architectures

▸ UC Berkeley contributed in the development of TCP/IP,


Virtual Machines, and Demand Paging in Unix

▸ Linus Torvalds created Linux out of Unix in 1991

▸ Oldest commercial software that is still being used today?


MOTIVATION

WHY USE UNIX-BASED SYSTEMS?

▸ Stable

▸ Robust

▸ Powerful

▸ Scalable

▸ "Everything is a file"
MOTIVATION

OVERVIEW OF UNIX
SYSTEM CALLS

WHAT ARE SYSTEM CALLS?


System calls are API (Application Programming Interfaces)
that allow applications to communicate and use underlying
computer hardware.

They allow the kernel to give instructions to the hardware in


behalf of the application.
SYSTEM CALLS

WHY REQUIRE A LAYER OF ABSTRACTION?


Security and Stability!
SYSTEM CALLS

HOW ARE SYSTEM CALLS MADE AND ACCESSED?


System calls are made using C and Assembly language. They
are packaged as part of the standard C library.

The standard followed in developing System calls are


defined in what is called the POSIX (Portable Operating
System Interface) standard.
PROCESS MANAGEMENT SYSTEM CALLS

HOW DO SYSTEM CALLS "ENHANCE" THE UNIX EXPERIENCE?

Image Taken From: https://www.guru3d.com/miraserver/images/news/2008/bottom.jpg


PROCESS MANAGEMENT SYSTEM CALLS

SOME TERMS
‣ Process - a running program

‣ Thread - a lightweight process



Note: In Unix, Process == Thread
PROCESS MANAGEMENT SYSTEM CALLS

OVERVIEW OF THE PROCESSES THAT THE OS MANAGES


PROCESS MANAGEMENT SYSTEM CALLS

HOW TO CREATE A NEW PROCESS?

Image Taken From: https://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/images/


Chapter3/3_08_ProcessTree.jpg
PROCESS MANAGEMENT SYSTEM CALLS

HOW TO CREATE A NEW PROCESS?


System calls to create a new process:

> fork()

> exec() family functions


System call to terminate a process:

> exit()


System calls for parent to check the status of a child process:

> wait(), waitpid() and other wait() family functions
PROCESS MANAGEMENT SYSTEM CALLS

LIFE OF A PROCESS
PROCESS MANAGEMENT SYSTEM CALLS

REQUIREMENTS IN PROCESS CREATION


‣ Each process is given a unique process id

‣ Allocate memory for the process

‣ Initialize Process Control Block

‣ Set appropriate linkages

‣ Create or expand data structures


PROCESS MANAGEMENT SYSTEM CALLS

HOW TO GET PROCESS ID?


‣ getpid() - get the process id of the calling process

‣ getppid() - get the process id of the parent of the calling


process
PROCESS MANAGEMENT SYSTEM CALLS

FORK() - CREATE A CHILD PROCESS

PROCESS

CALL
FORK()

PARENT CHILD
PROCESS PROCESS
PROCESS MANAGEMENT SYSTEM CALLS

FORK() RETURNS TWO VALUES!

PARENT CHILD
PROCESS PROCESS

Sees pid of the child process Sees pid value of 0

Has its own copy of the Has its own copy of the
variables in the original variables in the original
process process
PROCESS MANAGEMENT SYSTEM CALLS

REMEMBER, THE PROCESSES ARE ORGANIZED LIKE A TREE

PARENT
PROCESS

CHILD
PROCESS
PROCESS MANAGEMENT SYSTEM CALLS

WOULDN’T IT BE WASTEFUL TO HAVE BOTH PROCESSES HAVE A COPY OF THE SAME VARIABLE?

EXACTLY!


That’s why there’s a technique called Copy-on-Write (COW)

Parent and child initially have and use the same variables
until any of them edits the shared variable
PROCESS MANAGEMENT SYSTEM CALLS

WOULDN’T IT BE WASTEFUL TO HAVE BOTH PROCESSES HAVE A COPY OF THE SAME VARIABLE?

PARENT CHILD
PROCESS PROCESS

reads SHARED reads


VARIABLE

writes
PARENT CHILD
reads
PROCESS PARENT CHILD PROCESS
reads
VARIABLE VARIABLE
PROCESS MANAGEMENT SYSTEM CALLS

WHAT WOULD HAPPEN IF A SYSTEM CALL FAILS?


System calls typically return the value -1 upon failure, and
they also edit the errno global variable to the appropriate
error value accordingly
PROCESS MANAGEMENT SYSTEM CALLS

HOW DO WE CHECK FOR ERRORS IN SYSTEM CALLS?


Use the errno global variable and the perror() system call


The errno global variable is an integer used to easily pass
error values across your program and your system

The perror() system call is a function used to interpret the


errno global variable so that the error can be seen in a
human-readable format
PROCESS MANAGEMENT SYSTEM CALLS

WHAT WOULD HAPPEN IF A PARENT TERMINATES BEFORE THE CHILD?

PARENT CHILD ZOMBIE


INIT
PROCESS PROCESS PROCESS

Will be orphaned
Adopts

ORPHAN
PROCESS
PROCESS MANAGEMENT SYSTEM CALLS

HOW TO PREVENT PREMATURE TERMINATION OF THE PARENT?


Use the wait() system call in the parent process to
guarantee that the child process will terminate before the
parent process.

It is normally the parent process’ responsibility to collect the


remains of the child process once it terminates, but the init
process (PID = 1) can handle cleanup if the parent process
does not.
QUESTIONS?
SEE YOU
THURSDAY :D

You might also like