Processes and Pipes - PowerPoint PPT Presentation

About This Presentation
Title:

Processes and Pipes

Description:

I saw his markings, manoeuvred myself behind him and shot him down. ... Horst Rippert, German fighter pilot who claims to have shot down Antoine de Saint Exupery ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 29
Provided by: andrew203
Category:
Tags: horst | pipes | processes

less

Transcript and Presenter's Notes

Title: Processes and Pipes


1
Processes and Pipes
2
  • "He was below me. I saw his markings, manoeuvred
    myself behind him and shot him down. If I had
    known it was Saint-Exupery, I would never have
    shot him down. I loved his books. I knew he was a
    French pilot, but he was probably my favourite
    author at the time.
  • - Horst Rippert, German fighter pilot who claims
    to have shot down Antoine de Saint Exupery
  • The one thing, however, that most people think
    about when they think of UNIX is the power of the
    command line interface and the elegance of the
    pipe and filter model. While many people have
    added to this interface, contributing tools and
    programs over the years, one name stands out as a
    significant contributor, one who originally wrote
    some of the most basic and timeless tools for
    this system.
  • Doug McIlroy helped develop the concept of pipes
    and stream processing. In order to demonstrate
    the concept of stream programming, he wrote the
    original UNIX version of such tools as sort(1),
    spell(1), diff(1), join(1), graph(1), speak(1),
    and tr(1), among others
  • - text of the USENIX STUG award to Doug McIlroy

3
Goals of Todays Lecture
  • Creating a new process
  • Fork process creates a new child process
  • Wait parent waits for child process to complete
  • Exec child starts running a new program
  • System combines fork, wait, and exec all in one
  • Communication between processes
  • Pipe between two processes
  • Redirecting stdin and stdout
  • Initial background for the shell assignment
  • Software that provides interface for the user
  • Primarily used to launch other programs

4
Creating a New Process
5
Program vs. Process
  • Program
  • Executable code
  • No dynamic state
  • Process
  • An instance of a program in execution
  • With its own control flow (illusion of a
    processor)
  • private address space (illusion of memory)
  • State including code, data, stack, registers,
    instruction pointer, open file descriptors,
  • Either running, waiting, or ready
  • Can run multiple instances of the same program
  • Each as its own process, with its own process ID

6
Life Cycle of a Process
  • Running instructions are being executed
  • Waiting waiting for some event (e.g., I/O
    finish)
  • Ready ready to be assigned to a processor

Create
Ready
Running
Termination
Waiting
7
Many Processes Running Concurrently
  • Multiple processes sharing the CPU
  • Processor switches context between the two
  • When process blocks waiting for operation to
    complete
  • When process finishing using its share of the CPU
  • But, how do multiple processes start running
  • How are they invoked in the first place?

CPU
CPU
I/O
CPU
I/O
I/O
1
CPU
CPU
I/O
CPU
I/O
I/O
2
8
Why Start a New Process?
  • Run a new program
  • E.g., shell executing a program entered at
    command line
  • Or, even running an entire pipeline of commands
  • Such as wc l sort uniq -c sort nr
  • Run a new thread of control for the same program
  • E.g. a Web server handling a new Web request
  • While continuing to allow more requests to arrive
  • Essentially time sharing the computer
  • Underlying mechanism
  • A process runs fork to create a child process
  • (Optionally) child process does exec of a new
    program

9
Fork System Call
  • Create a new process
  • Child process inherits state from parent process
  • Parent and child have separate copies of that
    state
  • Parent and child share access to any open files

pid fork() if (pid ! 0) / in parent /
else / in child /
parent
child
10
Fork System Call
  • Fork is called once
  • But returns twice, once in each process
  • Telling which process is which
  • Parent fork() returns the childs process ID
  • Child fork() returns a 0

pid fork() if (pid ! 0) / in parent /
else / in child /
11
Example What Output?
int main() pid_t pid int x 1 pid
fork() if (pid ! 0) printf(parent
x d\n, --x) exit(0) else
printf(child x d\n, x) exit(0)

12
Fork
13
Wait
14
Executing a New Program
  • Fork copies the state of the parent process
  • Child continues running the parent program
  • with a copy of the process memory and registers
  • Need a way to invoke a new program
  • In the context of the newly-created child process
  • Example

null-terminated list of arguments(to become
argv)
program
execlp(ls, ls, -l, NULL) fprintf(stderr,
exec failed\n) exit(1)
15
Combining Fork() and Exec()
16
System
17
Communication Between Processes
18
Communication Between Processes
different machines
same machine
19
Interprocess Communication
  • Pipes
  • Processes on the same machine
  • One process spawns the other
  • Used mostly for a pipeline of filters
  • Sockets
  • Processes on any machines
  • Processes created independently
  • Used for client/server communication (e.g., Web)

20
Pipes
21
Creating a Pipe
22
Pipe Example
child
parent
23
Dup
a.out lt foo
24
Dup2
25
Pipes and Stdio
child
parent
26
Pipes and Exec
child
27
A Unix Shell!
28
Conclusion
  • System calls
  • An interface to the operating system
  • To perform operations on behalf of a user process
  • System calls for creating processes
  • Fork process creates a new child process
  • Wait parent waits for child process to complete
  • Exec child starts running a new program
  • System combines fork, wait, and exec all in one
  • System calls for inter-process communication
  • Pipe create a pipe with a write end and a read
    end
  • Open/close to open or close a file
  • Dup2 to duplicate a file descriptor
Write a Comment
User Comments (0)
About PowerShow.com