DOC PREVIEW
U of I CS 241 - Process

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 241 Spring 2007System Programming 01/30/07CS241 © 2006-2007 LA, RHC and YZ, All Rights Reserved1ProcessLawrence AngraveLecture 6Content of This LectureGoals:Start the basic system conceptThings covered in this lectureProcessAdministrativeThis weekSMP1 due Monday at 9am.Lecture quiz sometime this week on course contentCompass self Assessment Quiz 2MP QuizSMP1 - experiment with shellsImplement a shell using fork and exec, waitpidUse it as a workbench to experiment with processes and process controlbackground processesenvironment pathsignals and process controlsub-shells & recursionreasons for built-in shell commandsExample 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 the shell?What would happen if this program did not use the fork function, but just used execv directly?And now the slides….Users, Programs, ProcessesUsers have accounts on the systemUsers launch programsMany users may launch same programOne user may launch many instances of the same programProcesses: an executing programAnalogyProgram: steps for attending the lectureStep1: walk to Siebel Center BuildingStep2: enter 1404 Lecture RoomStep3: find a seatStep4: listen and take notes Process: attending the lectureActionYou are all in the middle of a processProcessesA process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended, scheduled and terminatedProcess is a unit of work in a modern computerProcess Types: OS processes executing system code programUser processes executing user code program Processes are executed concurrently with CPU multiplexing among themProcess States Possible process statesRunning (occupy CPU)BlockedReady (does not occupy CPU)Other states: suspended, terminatedTransitions between statesQuestion: in a single processor machine, how many process can be in running state?1 CPU can run Multiple ProcessesMultiple CPUs can run Multiple Processes13000program counter of CPU2QuestionsUsing “working on a MP” as an exampleWhat corresponds to “running”?What corresponds to “blocked”?What corresponds to “ready”?Why not the following transitions?Ready to blockedBlocked to runningLinux 5 State Process Model (S:118)Add states for creating and deleting processAdd transitions Timeout, Dispatch, Event OccursEvents are:I/OSynchronizationWindows Task ManagerUnix Example: psProcess Identification (RR: pp 60)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 <stdio.h>#include <unistd.h>int main (void) {printf(“I am process %ld\n”, (long)getpid());printf(“My parent id %ld\n”, (long)getppid());return 0; }Creating a Process - ForkCreating a process and executing a program are two different things in UNIXFork 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 failsChild’s PID if parent processChild gets new program counter, stack, file descriptors, heap, globals, pid!Creating a Process in Unix#include <stdio.h>#include <unistd.h>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 <stdio.h>#include <unistd.h>#include <sys/types.h> 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 Examplechildpid = 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 (RR P69)ChildChildParentParentChild 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;ChainFanProcess Operations (Creation)When creating a process, we need resources such as CPU, memory files, I/O devicesProcess can get resources from the OS or from the parent processChild process is restricted to a subset of parent resources Prevents many processes from overloading systemExecution possibilities are Parent continues concurrently with childParent waits until child has terminatedAddress space possibilities are:Child process is duplicate of parent processChild process has a new program loaded into itProcess TerminationNormal exit (voluntary)End of main()Error exit (voluntary)exit(2)Fatal error (involuntary)Divide by 0, core dump / seg faultKilled by another process (involuntary)Kill procID, end taskProcess Operations (Termination)When a process finishes last statement, it automatically asks OS to delete itChild process may return output to parent process, and all child’s resources are de-allocated. Other termination possibilitiesAbort by parent process invokedChild has exceeded its usage of some resourcesTask assigned to child is no longer requiredParent is exiting and OS does not allow child to continue without parentProcess HierarchiesParent creates a child process, a child process can create its own processesForms a hierarchyUNIX calls this a "process group"Windows has no concept of process hierarchyall processes are created equalwait() Functionwait function allows parent process to wait (block) until child finisheswait function causes the caller to suspend execution until child’s status is availablewaitpid function allows a parent to wait for a particular childOptions parameter of waitpid was invalidEINVAL Function was interrupted by signal EINTRCaller has no unwaited-for childredECHILDcauseerrnoWaiting for a child to finish – C Manual#include <errno.h>#include <sys/wait.h>pid_t childpid; childpid = wait(NULL);if (childpid != -1) printf(“waited for child with pid %ld\n”, childpid);Summary Read R&R : pp: 60-83, pp. 812-814Read S: Chapter 3:108-118, 153-156SMP1Week 2 Self


View Full Document

U of I CS 241 - Process

Documents in this Course
Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download Process
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Process and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Process 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?