DOC PREVIEW
U of I CS 241 - Processes

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 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 8 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 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Copyright ©: Nahrstedt, Angrave, Abdelzaher 1 Processes Tarek Abdelzaher Vikram Adve Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 Users, Programs, Processes !! Users have accounts on the system !! Users launch programs !! Many users may launch same program !! One user may launch many instances of the same program !! Processes: !! an executing program (the unit of work of a modern computer)Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 Process States !! Possible process states !! Running (occupy CPU) !! Blocked !! Ready (does not occupy CPU) !! Other states: suspended, terminated !! Question: in a single processor machine, how many process can be in running state? Copyright ©: Nahrstedt, Angrave, Abdelzaher 4 Linux: 5 State Process Model !! Add states for creating and deleting process !! Add transitions Timeout, Dispatch, Event OccursCopyright ©: Nahrstedt, Angrave, Abdelzaher 5 How to List all Processes? !! On Windows: run Windows task manager !! Hit Control+ALT+delete !! Click on the “processes” tab !! On UNIX !! $ ps –e !! Try “man ps” to understand more about its usage. Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 Process Execution and Context SwitchingCopyright ©: Nahrstedt, Angrave, Abdelzaher 7 Process Identification !! UNIX identifies processes via a unique Process ID !! Each process also knows its 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) !! Example 1 #include <stdio.h> #include <unistd.h> int main (void) { printf(“I am process %ld\n”, (long)getpid()); printf(“My parent id is %ld\n”, (long)getppid()); return 0; } Copyright ©: Nahrstedt, Angrave, Abdelzaher 8 Creating a Process – fork() !! fork() creates a duplicate of the calling process: !! Both processes continue with return from fork() !! Child gets exact copy of code, stack, file descriptors, heap, globals, and program counter !! Child gets new pid, ppid, time; no signals, file locks, … !! fork() returns !! -1 if fork fails !! 0 in child process !! child’s PID in parent processCopyright ©: Nahrstedt, Angrave, Abdelzaher 9 Creating a Process in Unix Example 2 #include <stdio.h> #include <unistd.h> int main(void) { pid_t x; x = fork(); if (x == 0) printf(“In child: fork() returned %ld\n”, (long) x); else printf(“In parent: fork() returned %ld\n", (long) x); } What does this print? Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 Chain and Fan Child Child Parent Parent Child 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; Chain FanCopyright ©: Nahrstedt, Angrave, Abdelzaher 11 Process Operations: Creation !! A new process needs resources such as CPU, memory, file descriptors, I/O devices !! Child can get resources from OS or from parent !! Child address space is duplicate of parent process !! Child process is given a subset of parent resources !! Prevents too many processes from overloading system !! Execution possibilities are !! Parent continues concurrently with child !! Parent waits until child has terminated Copyright ©: Nahrstedt, Angrave, Abdelzaher 12 Process Termination !! Normal exit (voluntary) !! End of main() !! exit(0) !! Error exit (voluntary) !! exit(2) or abort() !! Fatal error (involuntary) !! Divide by 0, seg fault, exceeded resources !! Killed by another process (involuntary) !! Signal: kill(procID)Copyright ©: Nahrstedt, Angrave, Abdelzaher 13 Process Operations: Termination !! When a process terminates: !! Open files are flushed and closed !! Tmp files are deleted !! Child’s resources are de-allocated !! File descriptors, memory, semaphores, file locks, … !! Parent process is notified via a signal !! Exit status is available to parent via wait() Copyright ©: Nahrstedt, Angrave, Abdelzaher 14 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 equalCopyright ©: Nahrstedt, Angrave, Abdelzaher 15 wait(), waitpid() System Calls !!wait() causes parent process to wait (block) until some child finishes !!wait() returns child’s pid and exit status to parent !!waitpid() waits for a specific child errno Cause ECHILD Caller has no unwaited-for children EINTR Function was interrupted by signal EINVAL Options parameter of waitpid was invalid Copyright ©: Nahrstedt, Angrave, Abdelzaher 16 Waiting for a child to finish (Try “man –s 2 wait”) #include <errno.h> #include <sys/wait.h> pid_t childpid; childpid = wait(NULL); if (childpid != -1) printf(“waited for child with pid %ld\n”,


View Full Document

U of I CS 241 - Processes

Documents in this Course
Process

Process

28 pages

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 Processes
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 Processes 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 Processes 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?