UNIX Processes 1: Introduction.

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

Daemon Processes Long lived utility processes Often started at system boot and ended when system shuts down Run in the background with no controlling terminal.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
Process Description and Control
The Process Model.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes Professor Jennifer Rexford
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.
Page 1 Processes and Threads Chapter 2. Page 2 Processes The Process Model Multiprogramming of four programs Conceptual model of 4 independent, sequential.
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Introduction to Kernel
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 Control in Unix Operating Systems Hebrew University Spring 2004.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CSSE Operating Systems
CSE 451 Section 4 Project 2 Design Considerations.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
BINA RAMAMURTHY UNIVERSITY AT BUFFALO System Structure and Process Model 5/30/2013 Amrita-UB-MSES
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
Introduction to Processes CS Intoduction to Operating Systems.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
System V IPC Provides three mechanisms for InterProcess Communication (IPC) : Messages : exchange messages with any process or server. Semaphores : allow.
Creating and Executing Processes
UNIX Files File organization and a few primitives.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
System calls for Process management
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.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Operating Systems Process Creation
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
Process Management Azzam Mourad COEN 346.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Files in UNIX u UNIX deals with two different classes of files:  Special Files  Regular Files u Regular files are just ordinary data files on disk -
Introduction to Kernel
Using Processes.
Unix Process Management
UNIX PROCESSES.
Example questions… Can a shell kill itself? Can a shell within a shell kill the parent shell? What happens to background processes when you exit from.
Tarek Abdelzaher Vikram Adve Marco Caccamo
System Structure and Process Model
System Structure and Process Model
Structure of Processes
Lecture 5: Process Creation
System Structure B. Ramamurthy.
System Structure and Process Model
Advanced UNIX progamming
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
CS510 Operating System Foundations
Process Description and Control in Unix
Process Description and Control in Unix
Presentation transcript:

UNIX Processes 1: Introduction

Overview Files in UNIX Directories and Paths User vs. System Mode The UNIX Process What is a Process Representing a process States of a process Creating and managing processes fork() wait() getpid() exit() etc. Files in UNIX Directories and Paths User vs. System Mode UNIX I/O primitives A simple example The basic I/O system calls Buffered vs. un-buffered I/O 1: Introduction

UNIX Processes A program that has started is manifested in the context of a process. A process in the system is represented: Process Identification Elements Process State Information Process Control Information User Stack Private User Address Space, Programs and Data Shared Address Space 1: Introduction

Process Control Block Process Information, Process State Information, and Process Control Information constitute the PCB. All Process State Information is stored in the Process Status Word (PSW). All information needed by the OS to manage the process is contained in the PCB. A UNIX process can be in a variety of states: 1: Introduction

States of a UNIX Process User running: Process executes in user mode Kernel running: Process executes in kernel mode Ready to run in memory: process is waiting to be scheduled Asleep in memory: waiting for an event Ready to run swapped: ready to run but requires swapping in Preempted: Process is returning from kernel to user-mode but the system has scheduled another process instead Created: Process is newly created and not ready to run Zombie: Process no longer exists, but it leaves a record for its parent process to collect. See Process State Diagram!! 1: Introduction

1 2 4 3 Process State Transitions user running kernel running sys-call return interrupt/ interrupt return kernel running 2 sleep schedule 4 3 wakeup ready to run asleep 1: Introduction

Creating a new process In UNIX, a new process is created by means of the fork() - system call. The OS performs the following functions: It allocates a slot in the process table for the new process It assigns a unique ID to the new process It makes a copy of process image of the parent (except shared memory) It assigns the child process to the Ready to Run State It returns the ID of the child to the parent process, and 0 to the child. Note, the fork() call actually is called once but returns twice - namely in the parent and the child process. 1: Introduction

Fork() Pid_t fork(void) is the prototype of the fork() call. Remember that fork() returns twice in the newly created (child) process with return value 0 in the calling process (parent) with return value = pid of the new process. A negative return value (-1) indicates that the call has failed Different return values are the key for distinguishing parent process from child process! The child process is an exact copy of the parent, yet, it is a copy i.e. an identical but separate process image. 1: Introduction

A fork() Example #include <unistd.h> main() { pid_t pid /* process id */ printf(“just one process before the fork()\n”); pid = fork(); if(pid == 0) printf(“I am the child process\n”); else if(pid > 0) printf(“I am the parent process\n”); else printf(“DANGER Mr. Robinson - the fork() has failed\n”) } 1: Introduction

Basic Process Coordination The exit() call is used to terminate a process. Its prototype is: void exit(int status), where status is used as the return value of the process. exit(i) can be used to announce success and failure to the calling process. The wait() call is used to temporarily suspend the parent process until one of the child processes terminates. The prototype is: pid_t wait(int *status), where status is a pointer to an integer to which the child’s status information is being assigned. wait() will return with a pid when any one of the children terminates or with -1 when no children exist. 1: Introduction

more coordination To wait for a particular child process to terminate, we can use the waitpid() call. Prototype: pid_t waitpid(pid_t pid, int *status, int opt) Sometimes we want to get information about the process or its parent. getpid() returns the process id getppid() returns the parent’s process id getuid() returns the users id use the manual pages for more id information. 1: Introduction

Orphans and Zombies or MIAs A child process whose parent has terminated is referred to as orphan. When a child exits when its parent is not currently executing a wait(), a zombie emerges. A zombie is not really a process as it has terminated but the system retains an entry in the process table for the non-existing child process. A zombie is put to rest when the parent finally executes a wait(). When a parent terminates, orphans and zombies are adopted by the init process (prosess-id -1) of the system. 1: Introduction

Fun with Zombies! Consider the following program: #include <unistd.h> main() { pid_t pid; int num1, num2; pid = fork(); printf(“pid=%d: enter 2 numbers:”, getpid()); scanf(“%d %d”, &num1 &num2); printf(“\n pid=%d: the sum is:\n”, getpid(), num1 + num2); } 1: Introduction

What is going on?? Who is getting to write to the standard output - parent or child ?? Who is going to receive the first input ? What happens if the child process completes while the parent is still waiting for input ? What UNIX tools would you use to get some indication of what is going on ? Exercise: Go try it !! 1: Introduction

The exec() family Why is the basic fork() facility not enough for a serious system programmer? Remember, a process consists among other things of text and data segments that represent the running program! What if we could change the program code that the text-segment represents? ===> The process would of course execute a different program. 1: Introduction

notable exec properties an exec call transforms the calling process by loading a new program in its memory space. the exec does not create a new sub-process. unlike the fork there is no return from a successful exec. all types of exec calls perform in principle the same task. ----> see chapter 2.7 in the textbook for a detailed description of the various exec() formats. 1: Introduction

exec??() calls execl(): must be given the arguments as a NULL terminated list. It needs a valid pathname for the executable. execlp(): only needs the filename of the executable but an NULL terminated argument list. execv():must receive an array of arguments in addition to the path of the executable execvp():only needs the filename of the executable and the corresponding argument list 1: Introduction

exec() example: #include <unistd.h> main() { printf(“executing ls\n”); execl(“/bin/ls”, “ls”, “-l”, (char *)0); /* if execl returns, then the call has failed.... */ perror(“execl failed to run ls”); exit(1) } 1: Introduction

By Convention Consider int execl ( const char *path, const char *arg0 ... ) Here, the parameter arg0 is, by convention, the name of the program or command without any path-information. Example: execl( “/bin/ls”, “ls”, “-l”, (char *)0 ); In general, the first argument in a command-line argument list is the name of the command or program itself! 1: Introduction

An execvp() example #include <unistd.h> main() { char * const av[] = {“myecho”,”hello”,”world”, (char *) 0}; execvp(av[0], av); } where myecho is name of a program that lists all its command line arguments. 1: Introduction

ps and kill Two of the shell commands that you will be using when working with processes are ps and kill. ps reports on active processes. -A lists all processes -e includes the environment -u username, lists all processes associated with username NOTE: the options may be differ on different systems!! What if you get a listing that is too long? try ps -A | grep username 1: Introduction

kill and killall In order to communicate with an executing process from the shell, you must send a signal to that process. The kill command sends a signal to the process. You can direct the signal to a particular process by using its pid The kill command has the following format: kill [options] pid -l lists all the signals you can send -p (on some systems prints process informantion) -signal is a signal number 1: Introduction

more on kill To terminate a process you can send the HUB signal, which is signal number 9. Example: kill -9 24607 will force the process with pid 24607 to terminate. On LINUX you have the option to use killall to kill all processes that are running and are owned by you. USE THE KILL-COMMAND TO TERMINATE ANY UNWANTED BACKGROUND PROCESSES !!! 1: Introduction

Files UNIX Input/Output operations are based on the concept of files. Files are an abstraction of specific I/O devices. A very small set of system calls provide the primitives that give direct access to I/O facilities of the UNIX kernel. Most I/O operations rely on the use of these primitives. We must remember that the basic I/O primitives are system calls, executed by the kernel. What does that mean to us as programmers??? 1: Introduction

User and System Space User Space Kernel Space Program Code Library Routine fread() read() user code kernel code User Space Kernel Space 1: Introduction

Different types of files UNIX deals with two different classes of files: Special Files Regular Files Regular files are just ordinary data files on disk - something you have used all along when you studied programming! Special files are abstractions of devices. UNIX deals with devices as if they were regular files. The interface between the file system and the device is implemented through a device driver - a program that hides the details of the actual device. 1: Introduction

special files UNIX distinguishes two types of special files: Block Special Files represent a device with characteristics similar to a disk. The device driver transfers chunks or blocks of data between the operating system and the device. Character Special Files represent devices with characteristics similar to a keyboard. The device is abstracted by a stream of bytes that can only be accessed in sequential order. 1: Introduction

Access Primitives UNIX provides access to files and devices through a (very) small set of basic system calls (primitives) create() open() close() read() write() ioctl() 1: Introduction

the open() call #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *path, int flags, [mode_t mode]); char *path: is a string that contains the fully qualified filename of the file to be opened. int flags: specifies the method of access i.e. read_only, write_only read_and_write. mode_t mode: optional parameter used to set the access permissions upon file creation. 1: Introduction

read() and write() #include <unistd.h> ssize_t read(int filedes, void *buffer, size_t n); ssize_t write(int filedes, const void *buffer, size_t n); int filedes: file descriptor that has been obtained though an open() or create() call. void *buffer: pointer to an array that will hold the data that is read or holds the data to be written. size_t n: the number of bytes that are to be read or written from/to the file. 1: Introduction

A close() call Although all open files are closed by the OS upon completion of the program, it is good programming style to “clean up” after you are done with any system resource. Please make it a habit to close all files that you program has used as soon as you don’t need them anymore! #include <unistd.h> int close(int filedes); Remember, closing resources timely can improve system performance and prevent deadlocks from happening (more later) 1: Introduction

A rudimentary example: #include <fcntl.h> /* controls file attributes */ #include<unistd.h> /* defines symbolic constants */ main() { int fd; /* a file descriptor */ ssize_t nread; /* number of bytes read */ char buf[1024]; /* data buffer */ /* open the file “data” for reading */ fd = open(“data”, O_RDONLY); /* read in the data */ nread = read(fd, buf, 1024); /* close the file */ close(fd); } 1: Introduction

Buffered vs unbuffered I/O The system can execute in user mode or kernel mode! Memory is divided into user space and kernel space! What happens when we write to a file? the write call forces a context switch to the system. What?? the system copies the specified number of bytes from user space into kernel space. (into mbufs) the system wakes up the device driver to write these mbufs to the physical device (if the file-system is in synchronous mode). the system selects a new process to run. finally, control is returned to the process that executed the write call. Discuss the effects on the performance of your program! 1: Introduction

Un-buffered I/O Every read and write is executed by the kernel. Hence, every read and write will cause a context switch in order for the system routines to execute. Why do we suffer performance loss? How can we reduce the loss of performance? ==> We could try to move as much data as possible with each system call. How can we measure the performance? 1: Introduction

Buffered I/O explicit versus implicit buffering: explicit - collect as many bytes as you can before writing to file and read more than a single byte at a time. However, use the basic UNIX I/O primitives Careful !! Your program my behave differently on different systems. Here, the programmer is explicitly controlling the buffer-size implicit - use the Stream facility provided by <stdio.h> FILE *fd, fopen, fprintf, fflush, fclose, ... etc. a FILE structure contains a buffer (in user space) that is usually the size of the disk blocking factor (512 or 1024) 1: Introduction

The fcntl() system call The fcntl() system call provides some control over already open files. fcntl() can be used to execute a function on a file descriptor. The prototype is: int fcntl(int fd, int cmd, .......) where fd is the corresponding file descriptor cmd is a “pre-defined” command (integer const) .... are additional parameters that depend on what cmd is. 1: Introduction

The fcntl() system call Two important commands are: F_GETFL and F_SETFL F_GETFL is used to instruct fcntl() to return the current status flags F_SETFL instructs fcntl() to reset the file status flags Example: int i = fcntl(fd, F_GETFL); or int i = fcntl(fd, F_SETFL, 1: Introduction

Using fcntl() to change to non-blocking I/O We can use the fcntl() system call to change the blocking behavior of the read() and write(): Example: #include <fcntl.h> ….. if ( fcntl(filedes, F_SETFL, O_NONBLOCK) == -1) perror(“fcntl”) 1: Introduction

The select() call Suppose we are dealing with a server process that is supporting multiple clients concurrently. Each of the client processes communicates with the server via a pipe. Further let us assume that the clients work completely asynchronously, that is, they issue requests to the server in any order. How would you write a server that can handle this type of scenario? DISCUSS!! 1: Introduction

select() cont’d What exactly is the problem? If we are using the standard read() call, it will block until data is available in the pipe. if we start polling each of the pipes in sequence, the server may get stuck on the first pipe (first client), waiting for data. other clients may, however, issued a request that could be processed instead. The server should be able to examine each file descriptor associated with each pipe to determine if data is available. 1: Introduction

Using the select() call The prototype of select() is: int select(int nfds, fd_set *readset, fd_set *writeset, fd_set errorset, timeval *timeout ) ndfs tells select how many file descriptors are of interest readset, writeset, and errorset are bit maps (binary words) in which each bit represents a particular file descriptor. timeout tells select() whether to block and wait and if waiting is required timeout explicitly specifies how long 1: Introduction

fd_set and associated functions Dealing with bit masks in C, C++, and UNIX makes programs less portable. In addition, it is difficult to deal with individual bits. Hence, the abstraction fd_set is available along with macros (functions on bit masks). Available macros are: void FD_ZERO(fd_set *fdset) resets all the bits in fdset to 0 void FD_SET(int fd, fd_set *fdset) set the bit representing fd to 1 int FD_ISSET(int fd, fd_set *fdset) returns 1 if the fd bit is set void FD_CLR(int fd, fd_set *fdset) turn of the bit fd in fdset 1: Introduction

a short example #include ..... int fd1, fd2; fd_set readset; fd1 = open (“file1”, O_READONLY); fd2 = open (“file2”, O_READONLY); FD_ZERO(&readset); FD_SET(fd1, &readset); FD_SET(fd2, &readset); select (5, &readset, NULL, NULL, NULL) .......... 1: Introduction

more select The select() system call can be used on any file descriptor and is particularly important for network programming with sockets. One important note: when select returns it modifies the bit mask according to the state of the file descriptors. You should save a copy of your original bit mask if you execute the select() multiple times. 1: Introduction