Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,

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

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Processes & Threads Today Next Time Process concept Process model
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
Introduction to Processes
The Process Model.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Unix Processes.
1 CS 333 Introduction to Operating Systems Class 2 – OS-Related Hardware & Software The Process Concept Jonathan Walpole Computer Science Portland State.
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.
CSSE Operating Systems
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Unix Processes operating systems. The Process ID Unix identifies each process with a unique integer called a process ID. The process that executes the.
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.
BINA RAMAMURTHY UNIVERSITY AT BUFFALO System Structure and Process Model 5/30/2013 Amrita-UB-MSES
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve.
Operating Systems Chapter 2
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
Creating and Executing Processes
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.
8-Sep Operating Systems Yasir Kiani. 8-Sep Agenda for Today Review of previous lecture Process scheduling concepts Process creation and termination.
Processes – Part I Processes – Part I. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Review on OSs Upon brief introduction of OSs,
System calls for Process management
Operating Systems Processes 1.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
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 Dr. Yingwu Zhu. Process Concept Process – a program in execution – What is not a process? -- program on a disk - a process is an active object,
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
Operating Systems Process Creation
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
Correct C Programs. The C compiler cc [filename.c] Options include: -o [output file name] -lm to include the maths library -E to get output from preprocessor.
System calls for Process management Process creation, termination, waiting.
1 Exceptional Control Flow Ⅱ. 2 Outline Kernel Mode and User Mode Process context switches Three States of Processes Context Switch System Calls and Error.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Slide 1 COMP 3438 System Programming UNIX Processes UNIX Processes (Chapters 2 & 3)
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.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Chapter 3: Processes.
Topic 3 (Textbook - Chapter 3) Processes
Unix Process Management
Chapter 3: Process Concept
Chapter 3: Processes.
Processes A process is a running program.
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
Chapter 3: Processes.
System Structure and Process Model
Processes in Unix, Linux, and Windows
CGS 3763 Operating Systems Concepts Spring 2013
Lecture 5: Process Creation
System Structure B. Ramamurthy.
Processes in Unix, Linux, and Windows
System Structure and Process Model
Operating Systems Lecture 6.
Process Creation Process Termination
Lecture 6: Multiprogramming and Context Switching
Processes in Unix and Windows
CS510 Operating System Foundations
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Chapter 3 Process Description and Control
Presentation transcript:

Process

Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended, scheduled and terminated Process Types: OS processes executing system code program User processes executing user code program Processes are executed concurrently with CPU multiplexing among them

Process States Possible process states Running (occupy CPU) Blocked Ready (does not occupy CPU) Other states: suspended, terminated Why not the following transitions? Ready to blocked Blocked to running

1 CPU can run Multiple Processes Multiple CPUs can run Multiple Processes program counter of CPU2

Linux 5 State Process Model Add states for creating and deleting process Add transitions Timeout, Dispatch, Event Occurs Events are: I/O Synchronization

Process Identification UNIX identifies processes via unique value Process ID Each process has also parent process ID since each process is created from a parent process. Root process is the ‘init’ process ‘getpid’ and ‘getppid’ – functions to return process ID (PID) and parent process ID (PPID) #include int main (void) { printf(“I am process %ld\n”, (long)getpid()); printf(“My parent id %ld\n”, (long)getppid()); return 0; }

Creating a Process - Fork Creating a process and executing a program are two different things in UNIX Fork duplicates a process so that instead on one process you get two--- But the code being executed doesn’t change!!! Fork returns 0 if child -1 if fork fails Child’s PID if parent process Child gets new program counter, stack, file descriptors, heap, globals, pid!

Creating a Process in Unix #include int main(void) { int x; x = 0; fork(); x = 1; printf("I am process %ld and my x is %d\n", (long)getpid(), x); return 0; } What does this print?

UNIX Example #include int main(void) { pid_t parentpid; pid_t childpid; if ((childpid = fork()) == -1) { perror(can’t create a new process); exit(1); } else if (childpid == 0) {/* child process executes */ printf(“child: childpid = %d, parentpid = %d \n”, getpid(), getppid()); exit(0); } else { /*parent process executes */ printf(“parent: childpid = %d, parentpid = %d \n”, childpid, getpid()); exit(0); }

UNIX Example childpid = fork() if (childpid == 0) { printf(“child: childpid = %d, parentpid = %d \n”, getpid(), getppid()); exit(0); } else { printf(“parent: childpid = %d, parentpid = %d \n”, childpid, getpid()); exit(0); }

Chain and Fan Child Parent Child … … pid_t childpid = 0; for (i=1;i<n;i++) if (childpid = fork()) break; pid_t childpid = 0; for (i=1;i<n;i++) if ((childpid = fork()) <=0) break; ChainFan

Process Operations (Creation) When creating a process, we need resources such as CPU, memory files, I/O devices Process can get resources from the OS or from the parent process Child process is restricted to a subset of parent resources  Prevents many processes from overloading system Execution possibilities are  Parent continues concurrently with child  Parent waits until child has terminated Address space possibilities are:  Child process is duplicate of parent process  Child process has a new program loaded into it

Process Termination Normal exit (voluntary) End of main() Error exit (voluntary) exit(2) Fatal error (involuntary) Divide by 0, core dump / seg fault Killed by another process (involuntary) Kill procID, end task

Process Operations (Termination) When a process finishes last statement, it automatically asks OS to delete it Child process may return output to parent process, and all child’s resources are de- allocated. Other termination possibilities Abort by parent process invoked  Child has exceeded its usage of some resources  Task assigned to child is no longer required  Parent is exiting and OS does not allow child to continue without parent

Process Hierarchies Parent creates a child process, a child process can create its own processes Forms a hierarchy UNIX calls this a "process group" Windows has no concept of process hierarchy all processes are created equal

wait() Function  wait function allows parent process to wait (block) until child finishes  wait function causes the caller to suspend execution until child’s status is available  waitpid function allows a parent to wait for a particular child errnocause ECHILDCaller has no unwaited-for children EINTRFunction was interrupted by signal EINVALOptions parameter of waitpid was invalid

Waiting for a child to finish – C Manual #include pid_t childpid; childpid = wait(NULL); if (childpid != -1) printf(“waited for child with pid %ld\n”, childpid);

Waiting for a child to finish: RR:72 #include pid_t r_wait(int *stat_loc) { int retval; while (((retval = wait(stat_loc)) == -1) && (errno == EINTR)) ; return retval; } Restarts wait if interrupted by a signal Part of Restart library in RR, Appendix B

Scheduling vs Dispatcher? Scheduler decides which process to run next Dispatcher gives control to the next process selected by the scheduler Switching process context Switching to user mode Jumping to the proper location in the user program to restart that program

Wait for all children that have finished pid_t childpid; while (childpid = waitpid(-1, NULL, WNOHANG)) { if ((childpid == -1) && (errno != EINTR)) break; } // keep waiting Wait for any child of current process Waitpid returns 0 to report there are possible unwaited-for children but their status is not available Restart waitpid if function interrupted by signal or successfully waited for child

Dead children may become zombies When a process terminates but its parent does not wait for it? Zombie (pid entry remains in system process table) Zombies unlike orphans do not consume many resources. Professional code installs signal handler for signal SIGCHLD … which issues a wait() call

What happens When the parent process terminates first? “Child is orphaned” Child is “re-parented” to init process Child continues to consume resources init process (id=1) waits for children

Status Values for wait(int *stat_loc) The following macros are available for checking the return status of a child: #include WIFEXITED(int stat_val) WEXITSTATUS(int stat_val) WIFSIGNALED(int stat_val) WTERMSIG(int stat_val) WIFSTOPPED(int stat_val) WSTOPSIG(int stat_val) They are used in pairs. If WIFEXITED returns true, the child executed normally and the return status (at most 8 bits) can be gotten with WEXITSTATUS.

Simple example (without checking EINTR) int status; childpid = wait(&status); if (childpid == -1) perror("Failed to wait for child"); else if (WIFEXITED(status) && 0==WEXITSTATUS(status)) printf(“Child terminated normally”);

Typical process creation code (RR:74) #include int main (void) { pid_t childpid; /* set up signal handlers here... */ childpid = fork(); if (childpid == -1) { perror("Failed to fork");return 1;} if (childpid == 0) fprintf(stderr, "I am child %ld\n", (long)getpid()); else if (wait(NULL) != childpid) fprintf(stderr, "A signal must have interrupted the wait!\n"); else fprintf(stderr, "I am parent %ld with child %ld\n", (long)getpid(), (long)childpid); return 0; }

Results from running program 6 Possible forms of output!!! Fork fails (1) Parent catches signal after child executes fprintf but before child returns (2,3) Parent catches signal after child terminates and wait returns successfully(2,4) Parent catches signal between time child terminates and wait returns (2,3) or (2,4) Parent catches signal before child executes fprintf and parent fprintf happens first (3,2) Parent catches signal before child executes fprintf and child executes fprintf first (2,3)

exec() Function Exec function allows child process to execute code that is different from that of parent Exec family of functions provides a facility for overlaying the process image of the calling process with a new image. Exec functions return -1 and sets errno if unsuccessful

Exec Example #include int main (void) { pid_t childpid; childpid = fork(); if (childpid == -1) { perror("Failed to fork"); return 1; } if (childpid == 0) { /*child code*/ execl(“/bin/ls”, “ls”, “-l”, NULL); perror(“Child failed to exec ls”); return 1; } if (wait(NULL) != childpid) { /* parent code */ perror(“Parent failed to wait due to signal or error”); return 1; } return 0; } Argv[0] is by convention program name so need ls in parameters The array of pointers must be terminated by a NULL pointer.

Background Processes and Daemons Shell – command interpreter creates background processes if line ends with & When shell creates a background process, it does not wait for the process to complete before issuing a prompt and accepting another command Ctrl-C does not terminate background process Daemon is a background process that normally runs indefinitely

Daemons Not connected to a terminal (tty) Create logs in /var/log/ Parent process “forks, disconnects tty and dies” so they are orphans Event driven Examples: ftpd, sshd, pageout daemon, httpd,mysqld Special privileges & access rights to hardware Execute as root or with a specific daemon userid A target for buffer overflow/Denial of Service attacks