Slide 1 COMP 3438 System Programming UNIX Processes UNIX Processes (Chapters 2 & 3)

Slides:



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

15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
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.
Processes. Process ID (pid) Synopsis #include pid_t getpid(void) – returns the pid of the currently running process. pid_t getppid(void) – returns the.
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.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Phones OFF Please Processes Parminder Singh Kang Home:
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,
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,
Creating and Executing Processes
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
System calls for Process management
Operating Systems Processes 1.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
Process Management CS3320 Spring Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides.
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.
Process Management Azzam Mourad COEN 346.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
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.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 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” –
Using Processes.
Unix Process Management
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
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
Processes in Unix, Linux, and Windows
Fork and Exec Unix Model
Processes in Unix, Linux, and Windows
System Structure and Process Model
CGS 3763 Operating Systems Concepts Spring 2013
Process Creation Process Termination
Lecture 6: Multiprogramming and Context Switching
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
CS510 Operating System Foundations
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

Slide 1 COMP 3438 System Programming UNIX Processes UNIX Processes (Chapters 2 & 3)

Slide 2 COMP 3438 System Programming UNIX Processes Overview of the Subject (COMP 3438) Overview of Unix Sys. Prog. Process/File (HW #1) Overview of Device Driver Development Character Device Driver Development (HW #2) Introduction to Block Device Driver Overview of Complier Design Lexical Analysis (HW #3) Syntax Analysis (HW #4) Part I: Unix System Programming (Device Driver Development) Part II: Compiler Design Course Organization (This Lecture is in red)

Slide 3 COMP 3438 System Programming UNIX Processes UNIX processes What is a process? What does a process look like in the system? When is a process created? By whom? How is a process created? In how many ways? When does a process stop? Can we wait for a process to die? What is a process called if it never die?

Slide 4 COMP 3438 System Programming UNIX Processes UNIX processes A process is an instance of a program in execution (the execution of the program has started but has not yet terminated). process is dynamic while program is static; A process is the basic unit for competing the resources. In particular, it is the basic active entity to CPU scheduler. When does a program become a process? The program is read into memory; A unique process ID is assigned, Appropriate information is added to the OS kernel structure, and Necessary resources to run the program code is allocated; Have a thread of execution – stream of instructions and a program counter.

Slide 5 COMP 3438 System Programming UNIX Processes Process image Each process has been allocated an image (also called address space, or execution environment), which contains the information needed to run the program: its current status, the program's data and stack, its program counter, and other register values; Sample layout of a program image in memory – This view is abstract!

Slide 6 COMP 3438 System Programming UNIX Processes Process characteristics A process has an unique ID, a thread of control, and some private data On each computer there is a hierarchy of processes related by parent-child links The process that executes the request for creation of a process is called the parent of that process, and the created process is called the child. A child process inherits all the properties of its parent when it is created

Slide 7 COMP 3438 System Programming UNIX Processes Process ID Process ID – integer PID Parent process ID – an integer PPID User process ID – an integer UID In UNIX, each user has a unique user ID. Each process is associated with a particular user called the owner of the process, which executes the program. The owner has certain privileges with respect to the process. Use getpid, getppid and getuid to determine the ID of the child, the parent, and the owner process.

Slide 8 COMP 3438 System Programming UNIX Processes Process ID: example The following program prints out its PID, its parent PID and its owner’s user ID. /* Example */ #include void main(void) { printf("Process ID: %ld\n",(long)getpid()); printf("Parent process ID: %ld\n", (long)getppid()); printf("Owner user ID: %ld\n", (long)getuid()); } Note: under POSIX, the type pid_t of the values returned by these system calls may be either an int or a long.

Slide 9 COMP 3438 System Programming UNIX Processes Process ID: using ps ps is the short for process status. “ps” lists your current processes PID TTY TIME CMD 1755 pts/1 00:00:17 bash 1981 pts/1 00:00:00 ps “ps –a ” lists more processes, including ones being run by other users and at other terminals (but not include the shells) “ps –l ” prints longer, more information lines, including UID, PID, PPID, process status, etc.

Slide 10 COMP 3438 System Programming UNIX Processes Process creation using fork() A traditional method for achieving concurrent execution in UNIX is for a process to create new processes using the fork() system call. The new process receives a copy of the contents of its parent’s address space - UNIX copies the parent’s memory image to child. UNIX kernel does the following: Allocates a new chunk of memory and kernel data structure Copies the original process into the new process Adds the new process to the set of “Ready” processes Returns control back to both processes

Slide 11 COMP 3438 System Programming UNIX Processes Example of fork() /* forkdemo1.c – Page 263*/ #include main() { int ret_from_fork, mypid; mypid = getpid(); printf(“Before: my pid is %d\n”, mypid); ret_from_fork = fork(); sleep(1); printf(“After: my pid is %d, fork() said %d\n”, getpid(), ret_from_fork); } Output (a possible one): $ cc forkdemo1.c –o fordemo1 $./forkdemo1 Before: my pid is 4170 After: my pid is 4170, fork() said 4171 $After: my pid is 4171, fork() said 0

Slide 12 COMP 3438 System Programming UNIX Processes Process creation using fork() Both parent and child continue execution at the instruction after the fork. But creation of two completely identical processes would not be very useful – we want parent and child execute different code. But how can the parent and child distinguish themselves?

Slide 13 COMP 3438 System Programming UNIX Processes Values returned by fork() The value returned by fork() allows the parent and the child to execute different part of the code. The fork returns 0 to the child and returns the child’s PID to the parent: /* After the fork, the parent and child output their process IDs*/ #include if ((childpid = fork()) == 0) { fprintf(stderr, "I am the child, ID = %ld\n", (long)getpid()); /* child code goes here */ } else if (childpid > 0) { fprintf(stderr, "I am the parent, ID = %ld\n", long)getpid()); /* parent code goes here */ }

Slide 14 COMP 3438 System Programming UNIX Processes FORK() example-1: creates a chain of n processes /* Example */ #include int i; int n; pid_t childpid; for (i = 1; i < n; ++i) if (childpid = fork() ) break; /* parent breaks out; child continues */ fprintf(stderr,"This is process %ld with parent %ld\n", (long)getpid(),(long)getppid());

Slide 15 COMP 3438 System Programming UNIX Processes FORK() example-2 creates a fan of n processes /* Example */ #include int i; int n; pid_t childpid; for (i = 1; i < n; ++i) if ((childpid = fork() ) <= 0) break; /* child breaks out, parent continues */ fprintf(stderr, "This is process %ld with parent %ld\n", (long)getpid(), (long)getppid()); 4 123

Slide 16 COMP 3438 System Programming UNIX Processes The wait system call After fork, both parent and child proceed independently. If a parent wants to wait until the child finishes, it executes wait or waitpid. pid_t wait(int *stat); In general, the wait system call causes the caller process to pause until a child terminates or stops or until the caller receives a signal returns right away if the process has no children or if a child has already terminated or stopped but has not yet been waited for. What happen if the parent terminates first without waiting for its children? (the children are still running) - Orphan The stat is a pointer to an integer variable that stores the exit status of the child.

Slide 17 COMP 3438 System Programming UNIX Processes Value returned by wait() If wait returns because a child terminated, the return value is positive and is the PID of that child; Otherwise, wait returns -1 and sets errno. errno value = ECHILD indicates that there were no unwaited-for child processes; errno value = EINTR indicates that the call was interrupted by a signal.

Slide 18 COMP 3438 System Programming UNIX Processes WAIT() example creates a process chain Only one forked process is a child of the original process. /* Example */ #include int i; int n; pid_t childpid; int status; pid_t waitreturn; for (i = 1; i < n; ++i) if (childpid = fork()) break; while(childpid != (waitreturn = wait(&status))) if ((waitreturn == -1) && (errno != EINTR)) break; fprintf(stderr, "I am process %ld, my parent is %ld\n", (long)getpid(), (long)getppid());  How are the output messages ordered?

Slide 19 COMP 3438 System Programming UNIX Processes The exec system call The fork system call creates a copy of the calling process. However, many applications require the child process to execute code different from the parent’s. The exec() family of system calls provides a facility for overlaying the calling process with a new executable module exec loads a new executable into the process image, copies arguments into the process, and calls main(argc, argv). If successful, exec never returns; the calling process is completely overlaid by the new program and is started from its beginning The traditional way to use the fork-exec combination is to have the child execute the new program while the parent continues to execute the original code

Slide 20 COMP 3438 System Programming UNIX Processes EXEC() example creates a process to run ls -l /* Example */ #include void main(void) { pid_t childpid; int status; if ((childpid = fork()) == -1) { perror("Error in the fork"); exit(1);} else if (childpid == 0) {/*child code*/ if (execl("/usr/bin/ls", "ls","-l", NULL) < 0) {perror("Exec of ls failed"); exit(1);} } else if (childpid != wait(&status)) /* parent code */ perror("A signal occurred before child exited"); exit(0); } When can a fork call fail?

Slide 21 COMP 3438 System Programming UNIX Processes Variations of exec system call Six variations of the exec system call, which can be distinguished by a)the way command-line arguments and environment are passed, and b)whether a pathname has to be given for the executable. The execl calls ( execl, execlp, and execle ) pass the command-line arguments as a list of pointers The execv calls ( execv, execvp, execve ) pass the command-line arguments in an argument array They can also be classified according to (b) In all calls, in addition to the pathname, name of the executable must also be provided, either as a separate parameter or as the first element of the argument array.

Slide 22 COMP 3438 System Programming UNIX Processes EXEC() example creates a process to run ls -l /* Example */ #include void main(void) { pid_t childpid; int status; char *arglist[3]; arglist[0] = “ls”; arglist[1] = “-l”; arglist[2] = 0; /* null pointer*/ if ((childpid = fork()) == -1) { perror("Error in the fork"); exit(1);} else if (childpid == 0) {/*child code*/ if (execvp("ls", arglist) < 0) {perror("Exec of ls failed"); exit(1);} } else if (childpid != wait(&status)) /* parent code */ perror("A signal occurred before child exited"); exit(0); }

Slide 23 COMP 3438 System Programming UNIX Processes Variations of exec system call All of these functions eventually make a call to execve – the real system call. The exec family tree

Slide 24 COMP 3438 System Programming UNIX Processes Process termination Upon termination of a process, the OS de-allocates the resources held by the process, updates the appropriate statistics, and notifies other processes: Canceling pending timers and signals, Releasing virtual memory spaces, Releasing locks, closing open files Notifying the parent in response to a wait system call What happened if the parent of the terminating process is not currently executing a wait()? - Zombie A process can terminate either normally or abnormally.

Slide 25 COMP 3438 System Programming UNIX Processes Normal process termination A normal termination occurs if there was a return from main, an implicit return from main, a call to the C function exit, or a call to the _exit system call. exit calls user-defined exit handlers and may provide additional cleanup before it invokes the _ exit system call. exit takes a single, integer argument, called exit status, which will be made available to the parent process (which may be waiting). By convention, a zero means success, some non-zero value means something has gone wrong.

Slide 26 COMP 3438 System Programming UNIX Processes Abnormal process termination A process can terminate abnormally by calling abort, causing the SIGABRT signal to be sent to the calling process, or processing a signal that causes termination. A code dump may be produced. User-installed exit handlers will not be called upon abnormal termination.

Slide 27 COMP 3438 System Programming UNIX Processes Background processes Recall that the shell is a command interpreter which prompts for commands, reads the commands from standard input, forks children to execute the commands, and waits for the children to finish. A user can terminate execution of a command by ctrl-c. Most shells interpret a command line ending with & as one that should be executed by a background process When a shell creates a background process, it does not wait for the process to complete before issuing a prompt and accepting additional commands A ctrl-c does not terminate a background process. e.g., compare ls -l and ls -l &

Slide 28 COMP 3438 System Programming UNIX Processes Daemons A daemon is a background process that normally runs indefinitely (have a infinite loop). Unix relies on many daemon processes to perform routine tasks pageout daemon handling paging, in.rlogind handling remote login requests, the Web server daemon receiving http connection requests ftp daemon, mail daemon,... etc. How does a daemon process work? e.g. an notification system : The following program, called simplebiff, notifies the user (called "oshacker") of pending mail by sending a ctrl-g character to standard error.

Slide 29 COMP 3438 System Programming UNIX Processes Daemons example  The following command starts simplebiff simplebiff & /* Example */ #include #define MAILFILE "/var/mail/oshacker" #define SLEEPTIME 10 void main(void) { int mailfd; for( ; ; ) { if ((mailfd = open(MAILFILE, O_RDONLY)) != -1) { fprintf(stderr,"%s", "\007"); close(mailfd); } sleep(SLEEPTIME); }