Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve.

Similar presentations


Presentation on theme: "Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve."— Presentation transcript:

1 Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve

2 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)

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

4 Copyright ©: Nahrstedt, Angrave, Abdelzaher 4 Linux: 5 State Process Model Add states for creating and deleting process Add transitions Timeout, Dispatch, Event Occurs

5 Copyright ©: 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.

6 Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 Process Execution and Context Switching

7 Copyright ©: 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 int main (void) { printf(“I am process %ld\n”, (long)getpid()); printf(“My parent id is %ld\n”, (long)getppid()); return 0; }

8 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 process

9 Copyright ©: Nahrstedt, Angrave, Abdelzaher 9 Creating a Process in Unix Example 2 #include 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?

10 Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 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; Chain Fan

11 Copyright ©: 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

12 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)

13 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()

14 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 equal

15 Copyright ©: 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 errnoCause ECHILDCaller has no unwaited-for children EINTRFunction was interrupted by signal EINVALOptions parameter of waitpid was invalid

16 Copyright ©: Nahrstedt, Angrave, Abdelzaher 16 Waiting for a child to finish (Try “man –s 2 wait”) #include pid_t childpid; childpid = wait(NULL); if (childpid != -1) printf(“waited for child with pid %ld\n”, childpid);


Download ppt "Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve."

Similar presentations


Ads by Google