System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File:...

178
System Level Programming Daniel Gruss October 6, 2020

Transcript of System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File:...

Page 1: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

System Level Programming

Daniel Gruss

October 6, 2020

Page 2: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:
Page 3: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� A program: a binary file containing code and data

� actions: write, compile, install, load

� resources: file

� A thread: an execution context

� actions: run, interrupt, stop

� resources: CPU time, stack, registers

� A process: a container for threads and memory contents of a program

� actions: create, start, terminate

� resources: threads, memory, program

1 Daniel Gruss

Page 4: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� A program: a binary file containing code and data

� actions: write, compile, install, load

� resources: file

� A thread: an execution context

� actions: run, interrupt, stop

� resources: CPU time, stack, registers

� A process: a container for threads and memory contents of a program

� actions: create, start, terminate

� resources: threads, memory, program

1 Daniel Gruss

Page 5: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� A program: a binary file containing code and data

� actions: write, compile, install, load

� resources: file

� A thread: an execution context

� actions: run, interrupt, stop

� resources: CPU time, stack, registers

� A process: a container for threads and memory contents of a program

� actions: create, start, terminate

� resources: threads, memory, program

1 Daniel Gruss

Page 6: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� A program: a binary file containing code and data

� actions: write, compile, install, load

� resources: file

� A thread: an execution context

� actions: run, interrupt, stop

� resources: CPU time, stack, registers

� A process: a container for threads and memory contents of a program

� actions: create, start, terminate

� resources: threads, memory, program

1 Daniel Gruss

Page 7: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� A program: a binary file containing code and data

� actions: write, compile, install, load

� resources: file

� A thread: an execution context

� actions: run, interrupt, stop

� resources: CPU time, stack, registers

� A process: a container for threads and memory contents of a program

� actions: create, start, terminate

� resources: threads, memory, program

1 Daniel Gruss

Page 8: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� A program: a binary file containing code and data

� actions: write, compile, install, load

� resources: file

� A thread: an execution context

� actions: run, interrupt, stop

� resources: CPU time, stack, registers

� A process: a container for threads and memory contents of a program

� actions: create, start, terminate

� resources: threads, memory, program

1 Daniel Gruss

Page 9: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� A program: a binary file containing code and data

� actions: write, compile, install, load

� resources: file

� A thread: an execution context

� actions: run, interrupt, stop

� resources: CPU time, stack, registers

� A process: a container for threads and memory contents of a program

� actions: create, start, terminate

� resources: threads, memory, program

1 Daniel Gruss

Page 10: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� A program: a binary file containing code and data

� actions: write, compile, install, load

� resources: file

� A thread: an execution context

� actions: run, interrupt, stop

� resources: CPU time, stack, registers

� A process: a container for threads and memory contents of a program

� actions: create, start, terminate

� resources: threads, memory, program

1 Daniel Gruss

Page 11: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� A program: a binary file containing code and data

� actions: write, compile, install, load

� resources: file

� A thread: an execution context

� actions: run, interrupt, stop

� resources: CPU time, stack, registers

� A process: a container for threads and memory contents of a program

� actions: create, start, terminate

� resources: threads, memory, program

1 Daniel Gruss

Page 12: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Abstractions www.tugraz.at

� Process: abstraction of a computer

� File: abstraction of a disk or a device

� Socket: abstraction of a network connection

� Window: abstraction of a display

→ Abstractions hide many details but provide the required capabilities

2 Daniel Gruss

Page 13: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Abstractions www.tugraz.at

� Process: abstraction of a computer

� File: abstraction of a disk or a device

� Socket: abstraction of a network connection

� Window: abstraction of a display

→ Abstractions hide many details but provide the required capabilities

2 Daniel Gruss

Page 14: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Abstractions www.tugraz.at

� Process: abstraction of a computer

� File: abstraction of a disk or a device

� Socket: abstraction of a network connection

� Window: abstraction of a display

→ Abstractions hide many details but provide the required capabilities

2 Daniel Gruss

Page 15: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Abstractions www.tugraz.at

� Process: abstraction of a computer

� File: abstraction of a disk or a device

� Socket: abstraction of a network connection

� Window: abstraction of a display

→ Abstractions hide many details but provide the required capabilities

2 Daniel Gruss

Page 16: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Abstractions www.tugraz.at

� Process: abstraction of a computer

� File: abstraction of a disk or a device

� Socket: abstraction of a network connection

� Window: abstraction of a display

→ Abstractions hide many details but provide the required capabilities

2 Daniel Gruss

Page 17: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Abstractions www.tugraz.at

� Process: abstraction of a computer

� File: abstraction of a disk or a device

� Socket: abstraction of a network connection

� Window: abstraction of a display

→ Abstractions hide many details but provide the required capabilities

2 Daniel Gruss

Page 18: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

CPU vs. Process www.tugraz.at

3 Daniel Gruss

Page 19: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

CPU vs. Process www.tugraz.at

4 Daniel Gruss

Page 20: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� Once a program is loaded in memory, OS can start it(s first thread) by

� setting up a stack and setting the stack pointer and

� setting the instruction pointer (of the first thread) to the programs first instruction

� Process is an instance of a program

5 Daniel Gruss

Page 21: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� Once a program is loaded in memory, OS can start it(s first thread) by

� setting up a stack and setting the stack pointer and

� setting the instruction pointer (of the first thread) to the programs first instruction

� Process is an instance of a program

5 Daniel Gruss

Page 22: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� Once a program is loaded in memory, OS can start it(s first thread) by

� setting up a stack and setting the stack pointer and

� setting the instruction pointer (of the first thread) to the programs first instruction

� Process is an instance of a program

5 Daniel Gruss

Page 23: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� Once a program is loaded in memory, OS can start it(s first thread) by

� setting up a stack and setting the stack pointer and

� setting the instruction pointer (of the first thread) to the programs first instruction

� Process is an instance of a program

5 Daniel Gruss

Page 24: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Threads www.tugraz.at

Process can have multiple threads

� same program code and data

� own stack

� own registers (including instruction pointer)

6 Daniel Gruss

Page 25: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Threads www.tugraz.at

Process can have multiple threads

� same program code and data

� own stack

� own registers (including instruction pointer)

6 Daniel Gruss

Page 26: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Threads www.tugraz.at

Process can have multiple threads

� same program code and data

� own stack

� own registers (including instruction pointer)

6 Daniel Gruss

Page 27: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Threads www.tugraz.at

Process can have multiple threads

� same program code and data

� own stack

� own registers (including instruction pointer)

6 Daniel Gruss

Page 28: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:
Page 29: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� Program: a binary file containing code and data

� a mold for a process

� Thread: an execution context

� a sequence of instructions

� if part of a process: restricted to the boundaries of a process

� Process: a container for threads and memory contents of a program

� an instance of a program

� restricted to its own boundaries and rights

7 Daniel Gruss

Page 30: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� Program: a binary file containing code and data

� a mold for a process

� Thread: an execution context

� a sequence of instructions

� if part of a process: restricted to the boundaries of a process

� Process: a container for threads and memory contents of a program

� an instance of a program

� restricted to its own boundaries and rights

7 Daniel Gruss

Page 31: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� Program: a binary file containing code and data

� a mold for a process

� Thread: an execution context

� a sequence of instructions

� if part of a process: restricted to the boundaries of a process

� Process: a container for threads and memory contents of a program

� an instance of a program

� restricted to its own boundaries and rights

7 Daniel Gruss

Page 32: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� Program: a binary file containing code and data

� a mold for a process

� Thread: an execution context

� a sequence of instructions

� if part of a process: restricted to the boundaries of a process

� Process: a container for threads and memory contents of a program

� an instance of a program

� restricted to its own boundaries and rights

7 Daniel Gruss

Page 33: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� Program: a binary file containing code and data

� a mold for a process

� Thread: an execution context

� a sequence of instructions

� if part of a process: restricted to the boundaries of a process

� Process: a container for threads and memory contents of a program

� an instance of a program

� restricted to its own boundaries and rights

7 Daniel Gruss

Page 34: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� Program: a binary file containing code and data

� a mold for a process

� Thread: an execution context

� a sequence of instructions

� if part of a process: restricted to the boundaries of a process

� Process: a container for threads and memory contents of a program

� an instance of a program

� restricted to its own boundaries and rights

7 Daniel Gruss

Page 35: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� Program: a binary file containing code and data

� a mold for a process

� Thread: an execution context

� a sequence of instructions

� if part of a process: restricted to the boundaries of a process

� Process: a container for threads and memory contents of a program

� an instance of a program

� restricted to its own boundaries and rights

7 Daniel Gruss

Page 36: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Program, Process, Thread www.tugraz.at

� Program: a binary file containing code and data

� a mold for a process

� Thread: an execution context

� a sequence of instructions

� if part of a process: restricted to the boundaries of a process

� Process: a container for threads and memory contents of a program

� an instance of a program

� restricted to its own boundaries and rights

7 Daniel Gruss

Page 37: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Resources www.tugraz.at

A process is a container.

� Process ID

� Filename

� Program file

� File descriptors

� Memory

� Accounting

� Threads

� Child processes?

8 Daniel Gruss

Page 38: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Resources www.tugraz.at

A process is a container.

� Process ID

� Filename

� Program file

� File descriptors

� Memory

� Accounting

� Threads

� Child processes?

8 Daniel Gruss

Page 39: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Resources www.tugraz.at

A process is a container.

� Process ID

� Filename

� Program file

� File descriptors

� Memory

� Accounting

� Threads

� Child processes?

8 Daniel Gruss

Page 40: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Resources www.tugraz.at

A process is a container.

� Process ID

� Filename

� Program file

� File descriptors

� Memory

� Accounting

� Threads

� Child processes?

8 Daniel Gruss

Page 41: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Resources www.tugraz.at

A process is a container.

� Process ID

� Filename

� Program file

� File descriptors

� Memory

� Accounting

� Threads

� Child processes?

8 Daniel Gruss

Page 42: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Resources www.tugraz.at

A process is a container.

� Process ID

� Filename

� Program file

� File descriptors

� Memory

� Accounting

� Threads

� Child processes?

8 Daniel Gruss

Page 43: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Resources www.tugraz.at

A process is a container.

� Process ID

� Filename

� Program file

� File descriptors

� Memory

� Accounting

� Threads

� Child processes?

8 Daniel Gruss

Page 44: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Resources www.tugraz.at

A process is a container.

� Process ID

� Filename

� Program file

� File descriptors

� Memory

� Accounting

� Threads

� Child processes?

8 Daniel Gruss

Page 45: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Thread Resources www.tugraz.at

A thread is a unit for execution.

� Thread ID

� Thread state (Running, Sleeping, . . . )

� A set of register values

� A stack

9 Daniel Gruss

Page 46: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Thread Resources www.tugraz.at

A thread is a unit for execution.

� Thread ID

� Thread state (Running, Sleeping, . . . )

� A set of register values

� A stack

9 Daniel Gruss

Page 47: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Thread Resources www.tugraz.at

A thread is a unit for execution.

� Thread ID

� Thread state (Running, Sleeping, . . . )

� A set of register values

� A stack

9 Daniel Gruss

Page 48: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Thread Resources www.tugraz.at

A thread is a unit for execution.

� Thread ID

� Thread state (Running, Sleeping, . . . )

� A set of register values

� A stack

9 Daniel Gruss

Page 49: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process and Thread Interaction www.tugraz.at

Load program, create process, . . .

� 1 initial thread

� executes the main()-function

� it’s not a “main”-thread

� process may start further threads if required (how?)

10 Daniel Gruss

Page 50: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process and Thread Interaction www.tugraz.at

Load program, create process, . . .

� 1 initial thread

� executes the main()-function

� it’s not a “main”-thread

� process may start further threads if required (how?)

10 Daniel Gruss

Page 51: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process and Thread Interaction www.tugraz.at

Load program, create process, . . .

� 1 initial thread

� executes the main()-function

� it’s not a “main”-thread

� process may start further threads if required (how?)

10 Daniel Gruss

Page 52: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process and Thread Interaction www.tugraz.at

Load program, create process, . . .

� 1 initial thread

� executes the main()-function

� it’s not a “main”-thread

� process may start further threads if required (how?)

10 Daniel Gruss

Page 53: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process and Thread Interaction www.tugraz.at

Load program, create process, . . .

� 1 initial thread

� executes the main()-function

� it’s not a “main”-thread

� process may start further threads if required (how?)

10 Daniel Gruss

Page 54: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:
Page 55: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:
Page 56: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Creation www.tugraz.at

� at boot time (kernel threads, init processes)

� at request of a user (how?)

� also: start of a scheduled batch job (cronjob, how?)

11 Daniel Gruss

Page 57: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Creation www.tugraz.at

� at boot time (kernel threads, init processes)

� at request of a user (how?)

� also: start of a scheduled batch job (cronjob, how?)

11 Daniel Gruss

Page 58: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Creation www.tugraz.at

� at boot time (kernel threads, init processes)

� at request of a user (how?)

� also: start of a scheduled batch job (cronjob, how?)

11 Daniel Gruss

Page 59: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Creation at request of a user www.tugraz.at

via Syscall!

� UNIX/Linux: fork (exact copy)

� Windows: CreateProcess (new image)

12 Daniel Gruss

Page 60: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Creation at request of a user www.tugraz.at

via Syscall!

� UNIX/Linux: fork (exact copy)

� Windows: CreateProcess (new image)

12 Daniel Gruss

Page 61: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:
Page 62: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Creation via fork (on Unix / Linux) www.tugraz.at

pid_t fork(void);

The fork() function shall create a new process. The new process (child process) shall be an

exact copy of the calling process (parent process) except as detailed below:

� unique PID

� copy of file descriptors

� semaphore state is copied

� shall be created with a single thread. If a multi-threaded process calls fork(), the new

process shall contain a replica of the calling thread and its entire address space, possibly

including the states of mutexes and other resources.

� parent and the child processes shall be capable of executing independently before either

one terminates.

� . . .

13 Daniel Gruss

Page 63: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Creation via fork (on Unix / Linux) www.tugraz.at

pid_t fork(void);

The fork() function shall create a new process. The new process (child process) shall be an

exact copy of the calling process (parent process) except as detailed below:

� unique PID

� copy of file descriptors

� semaphore state is copied

� shall be created with a single thread. If a multi-threaded process calls fork(), the new

process shall contain a replica of the calling thread and its entire address space, possibly

including the states of mutexes and other resources.

� parent and the child processes shall be capable of executing independently before either

one terminates.

� . . .

13 Daniel Gruss

Page 64: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Creation via fork (on Unix / Linux) www.tugraz.at

pid_t fork(void);

The fork() function shall create a new process. The new process (child process) shall be an

exact copy of the calling process (parent process) except as detailed below:

� unique PID

� copy of file descriptors

� semaphore state is copied

� shall be created with a single thread. If a multi-threaded process calls fork(), the new

process shall contain a replica of the calling thread and its entire address space, possibly

including the states of mutexes and other resources.

� parent and the child processes shall be capable of executing independently before either

one terminates.

� . . .

13 Daniel Gruss

Page 65: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Creation via fork (on Unix / Linux) www.tugraz.at

pid_t fork(void);

The fork() function shall create a new process. The new process (child process) shall be an

exact copy of the calling process (parent process) except as detailed below:

� unique PID

� copy of file descriptors

� semaphore state is copied

� shall be created with a single thread. If a multi-threaded process calls fork(), the new

process shall contain a replica of the calling thread and its entire address space, possibly

including the states of mutexes and other resources.

� parent and the child processes shall be capable of executing independently before either

one terminates.

� . . .

13 Daniel Gruss

Page 66: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Creation via fork (on Unix / Linux) www.tugraz.at

pid_t fork(void);

The fork() function shall create a new process. The new process (child process) shall be an

exact copy of the calling process (parent process) except as detailed below:

� unique PID

� copy of file descriptors

� semaphore state is copied

� shall be created with a single thread. If a multi-threaded process calls fork(), the new

process shall contain a replica of the calling thread and its entire address space, possibly

including the states of mutexes and other resources.

� parent and the child processes shall be capable of executing independently before either

one terminates.

� . . .

13 Daniel Gruss

Page 67: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Creation via fork (on Unix / Linux) www.tugraz.at

pid_t fork(void);

The fork() function shall create a new process. The new process (child process) shall be an

exact copy of the calling process (parent process) except as detailed below:

� unique PID

� copy of file descriptors

� semaphore state is copied

� shall be created with a single thread. If a multi-threaded process calls fork(), the new

process shall contain a replica of the calling thread and its entire address space, possibly

including the states of mutexes and other resources.

� parent and the child processes shall be capable of executing independently before either

one terminates.

� . . .

13 Daniel Gruss

Page 68: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Creation via fork (on Unix / Linux) www.tugraz.at

pid_t fork(void);

The fork() function shall create a new process. The new process (child process) shall be an

exact copy of the calling process (parent process) except as detailed below:

� unique PID

� copy of file descriptors

� semaphore state is copied

� shall be created with a single thread. If a multi-threaded process calls fork(), the new

process shall contain a replica of the calling thread and its entire address space, possibly

including the states of mutexes and other resources.

� parent and the child processes shall be capable of executing independently before either

one terminates.

� . . .

13 Daniel Gruss

Page 69: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

fork Return Value www.tugraz.at

pid_t fork(void);

Upon successful completion, fork() shall return 0 to the child process and shall return the

process ID of the child process to the parent process. Both processes shall continue to execute

from the fork() function. Otherwise, -1 shall be returned to the parent process, no child

process shall be created, and errno shall be set to indicate the error.

14 Daniel Gruss

Page 70: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Fork www.tugraz.at

pid_t child_pid;

child_pid = fork();

if (child_pid == -1) {

printf("fork failed\n");

} else if (child_pid == 0) {

printf("i’m the child\n");

} else {

printf("i’m the parent\n");

waitpid(child_pid,0,0); // wait

for child to die

}

� child does not know the parent

� parent knows the child

� parent waits for child to die (waitpid)

15 Daniel Gruss

Page 71: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Fork www.tugraz.at

pid_t child_pid;

child_pid = fork();

if (child_pid == -1) {

printf("fork failed\n");

} else if (child_pid == 0) {

printf("i’m the child\n");

} else {

printf("i’m the parent\n");

waitpid(child_pid,0,0); // wait

for child to die

}

� child does not know the parent

� parent knows the child

� parent waits for child to die (waitpid)

15 Daniel Gruss

Page 72: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Fork www.tugraz.at

pid_t child_pid;

child_pid = fork();

if (child_pid == -1) {

printf("fork failed\n");

} else if (child_pid == 0) {

printf("i’m the child\n");

} else {

printf("i’m the parent\n");

waitpid(child_pid,0,0); // wait

for child to die

}

� child does not know the parent

� parent knows the child

� parent waits for child to die (waitpid)

15 Daniel Gruss

Page 73: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:
Page 74: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

exec www.tugraz.at

int execl(const char *pathname, const char *arg, ... /* (char *) NULL */);

int execlp(const char *file, const char *arg, ... /* (char *) NULL */);

int execle(const char *pathname, const char *arg, ... /*, (char *) NULL, char *const envp[] */);

int execv(const char *pathname, char *const argv[]);

int execvp(const char *file, char *const argv[]);

int execvpe(const char *file, char *const argv[], char *const envp[]);

16 Daniel Gruss

Page 75: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

exec www.tugraz.at

int execvpe(const char *file, char *const argv[], char *const envp[]);

� replace running process by process defined by file

� pass argv

� use envp for environment variables (PATH etc.)

17 Daniel Gruss

Page 76: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

exec www.tugraz.at

int execvpe(const char *file, char *const argv[], char *const envp[]);

� replace running process by process defined by file

� pass argv

� use envp for environment variables (PATH etc.)

17 Daniel Gruss

Page 77: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

exec www.tugraz.at

int execvpe(const char *file, char *const argv[], char *const envp[]);

� replace running process by process defined by file

� pass argv

� use envp for environment variables (PATH etc.)

17 Daniel Gruss

Page 78: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

exec www.tugraz.at

int execvpe(const char *file, char *const argv[], char *const envp[]);

� replace running process by process defined by file

� pass argv

� use envp for environment variables (PATH etc.)

17 Daniel Gruss

Page 79: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Termination www.tugraz.at

� Normal exit (return value: zero)

� Error exit (return value: non-zero)

� Fatal error (e.g. segmentation fault)

� Killed by another process

18 Daniel Gruss

Page 80: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Termination www.tugraz.at

� Normal exit (return value: zero)

� Error exit (return value: non-zero)

� Fatal error (e.g. segmentation fault)

� Killed by another process

18 Daniel Gruss

Page 81: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Termination www.tugraz.at

� Normal exit (return value: zero)

� Error exit (return value: non-zero)

� Fatal error (e.g. segmentation fault)

� Killed by another process

18 Daniel Gruss

Page 82: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Termination www.tugraz.at

� Normal exit (return value: zero)

� Error exit (return value: non-zero)

� Fatal error (e.g. segmentation fault)

� Killed by another process

18 Daniel Gruss

Page 83: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Hierarchies www.tugraz.at

Some operating systems have hierarchies:

� implicit hierarchy from forking

� process groups in UNIX/Linux

� doesn’t exist in Windows

Implicit parent-child hierarchy on Unix/Linux:

� when parent dies,

all children, grand-children, grand-grand-children, . . . , die aswell

� UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.

19 Daniel Gruss

Page 84: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Hierarchies www.tugraz.at

Some operating systems have hierarchies:

� implicit hierarchy from forking

� process groups in UNIX/Linux

� doesn’t exist in Windows

Implicit parent-child hierarchy on Unix/Linux:

� when parent dies,

all children, grand-children, grand-grand-children, . . . , die aswell

� UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.

19 Daniel Gruss

Page 85: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Hierarchies www.tugraz.at

Some operating systems have hierarchies:

� implicit hierarchy from forking

� process groups in UNIX/Linux

� doesn’t exist in Windows

Implicit parent-child hierarchy on Unix/Linux:

� when parent dies,

all children, grand-children, grand-grand-children, . . . , die aswell

� UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.

19 Daniel Gruss

Page 86: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Hierarchies www.tugraz.at

Some operating systems have hierarchies:

� implicit hierarchy from forking

� process groups in UNIX/Linux

� doesn’t exist in Windows

Implicit parent-child hierarchy on Unix/Linux:

� when parent dies,

all children, grand-children, grand-grand-children, . . . , die aswell

� UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.

19 Daniel Gruss

Page 87: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Hierarchies www.tugraz.at

Some operating systems have hierarchies:

� implicit hierarchy from forking

� process groups in UNIX/Linux

� doesn’t exist in Windows

Implicit parent-child hierarchy on Unix/Linux:

� when parent dies,

all children, grand-children, grand-grand-children, . . . , die aswell

� UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.

19 Daniel Gruss

Page 88: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process Hierarchies www.tugraz.at

Some operating systems have hierarchies:

� implicit hierarchy from forking

� process groups in UNIX/Linux

� doesn’t exist in Windows

Implicit parent-child hierarchy on Unix/Linux:

� when parent dies, all children, grand-children, grand-grand-children, . . . , die aswell

� UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.

19 Daniel Gruss

Page 89: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process/Thread State www.tugraz.at

git grep TODO | sort

20 Daniel Gruss

Page 90: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process/Thread State www.tugraz.at

git grep TODO | sort

� sort has to wait for input

20 Daniel Gruss

Page 91: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process/Thread State www.tugraz.at

git grep TODO | sort

� sort has to wait for input

� what does the sort do in the meantime?

20 Daniel Gruss

Page 92: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process/Thread State www.tugraz.at

git grep TODO | sort

� sort has to wait for input

� what does the sort do in the meantime?

� loop and check (busy wait)

20 Daniel Gruss

Page 93: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process/Thread State www.tugraz.at

git grep TODO | sort

� sort has to wait for input

� what does the sort do in the meantime?

� loop and check (busy wait)

� sleep and get woken up

20 Daniel Gruss

Page 94: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process/Thread State www.tugraz.at

git grep TODO | sort

� sort has to wait for input

� what does the sort do in the meantime?

� loop and check (busy wait)

� sleep and get woken up

� blocking the process makes sense

20 Daniel Gruss

Page 95: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Process/Thread State www.tugraz.at

git grep TODO | sort

� sort has to wait for input

� what does the sort do in the meantime?

� loop and check (busy wait)

� sleep and get woken up

� blocking the process makes sense

� do we actually block the process?

20 Daniel Gruss

Page 96: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Processes vs. Threads www.tugraz.at

� Threads are more lightweight than processes

� Less independent than processes

� No protection

21 Daniel Gruss

Page 97: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Processes vs. Threads www.tugraz.at

� Threads are more lightweight than processes

� Less independent than processes

� No protection

21 Daniel Gruss

Page 98: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Processes vs. Threads www.tugraz.at

� Threads are more lightweight than processes

� Less independent than processes

� No protection

21 Daniel Gruss

Page 99: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads? www.tugraz.at

� Things should happen in parallel - even within one application

� Example: text processing

� typing

� spell checking

� formatting on screen

� automatically saving

� Some of these things may block

� wait for mouse-click / keyboard press

� wait for disk

� etc.

22 Daniel Gruss

Page 100: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads? www.tugraz.at

� Things should happen in parallel - even within one application

� Example: text processing

� typing

� spell checking

� formatting on screen

� automatically saving

� Some of these things may block

� wait for mouse-click / keyboard press

� wait for disk

� etc.

22 Daniel Gruss

Page 101: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads? www.tugraz.at

� Things should happen in parallel - even within one application

� Example: text processing

� typing

� spell checking

� formatting on screen

� automatically saving

� Some of these things may block

� wait for mouse-click / keyboard press

� wait for disk

� etc.

22 Daniel Gruss

Page 102: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads? www.tugraz.at

� Things should happen in parallel - even within one application

� Example: text processing

� typing

� spell checking

� formatting on screen

� automatically saving

� Some of these things may block

� wait for mouse-click / keyboard press

� wait for disk

� etc.

22 Daniel Gruss

Page 103: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads? www.tugraz.at

� Things should happen in parallel - even within one application

� Example: text processing

� typing

� spell checking

� formatting on screen

� automatically saving

� Some of these things may block

� wait for mouse-click / keyboard press

� wait for disk

� etc.

22 Daniel Gruss

Page 104: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads? www.tugraz.at

� Things should happen in parallel - even within one application

� Example: text processing

� typing

� spell checking

� formatting on screen

� automatically saving

� Some of these things may block

� wait for mouse-click / keyboard press

� wait for disk

� etc.

22 Daniel Gruss

Page 105: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads? www.tugraz.at

� Things should happen in parallel - even within one application

� Example: text processing

� typing

� spell checking

� formatting on screen

� automatically saving

� Some of these things may block

� wait for mouse-click / keyboard press

� wait for disk

� etc.

22 Daniel Gruss

Page 106: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads? www.tugraz.at

� Things should happen in parallel - even within one application

� Example: text processing

� typing

� spell checking

� formatting on screen

� automatically saving

� Some of these things may block

� wait for mouse-click / keyboard press

� wait for disk

� etc.

22 Daniel Gruss

Page 107: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads? www.tugraz.at

� Things should happen in parallel - even within one application

� Example: text processing

� typing

� spell checking

� formatting on screen

� automatically saving

� Some of these things may block

� wait for mouse-click / keyboard press

� wait for disk

� etc.

22 Daniel Gruss

Page 108: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads? www.tugraz.at

� Things should happen in parallel - even within one application

� Example: text processing

� typing

� spell checking

� formatting on screen

� automatically saving

� Some of these things may block

� wait for mouse-click / keyboard press

� wait for disk

� etc.

22 Daniel Gruss

Page 109: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads www.tugraz.at

� Make programming easier

� Split tasks in different blocks

� Like with processes� But they cooperate easily because of the shared address space

� Consumes less memory

� Attention: do not confuse shared memory (between processes) with shared address space

(between threads)

� Switching between threads can be faster

� No need to reconfigure memory

� May achieve better performance

23 Daniel Gruss

Page 110: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads www.tugraz.at

� Make programming easier

� Split tasks in different blocks

� Like with processes� But they cooperate easily because of the shared address space

� Consumes less memory

� Attention: do not confuse shared memory (between processes) with shared address space

(between threads)

� Switching between threads can be faster

� No need to reconfigure memory

� May achieve better performance

23 Daniel Gruss

Page 111: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads www.tugraz.at

� Make programming easier

� Split tasks in different blocks

� Like with processes

� But they cooperate easily because of the shared address space

� Consumes less memory

� Attention: do not confuse shared memory (between processes) with shared address space

(between threads)

� Switching between threads can be faster

� No need to reconfigure memory

� May achieve better performance

23 Daniel Gruss

Page 112: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads www.tugraz.at

� Make programming easier

� Split tasks in different blocks

� Like with processes� But they cooperate easily because of the shared address space

� Consumes less memory

� Attention: do not confuse shared memory (between processes) with shared address space

(between threads)

� Switching between threads can be faster

� No need to reconfigure memory

� May achieve better performance

23 Daniel Gruss

Page 113: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads www.tugraz.at

� Make programming easier

� Split tasks in different blocks

� Like with processes� But they cooperate easily because of the shared address space

� Consumes less memory

� Attention: do not confuse shared memory (between processes) with shared address space

(between threads)

� Switching between threads can be faster

� No need to reconfigure memory

� May achieve better performance

23 Daniel Gruss

Page 114: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads www.tugraz.at

� Make programming easier

� Split tasks in different blocks

� Like with processes� But they cooperate easily because of the shared address space

� Consumes less memory

� Attention: do not confuse shared memory (between processes) with shared address space

(between threads)

� Switching between threads can be faster

� No need to reconfigure memory

� May achieve better performance

23 Daniel Gruss

Page 115: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads www.tugraz.at

� Make programming easier

� Split tasks in different blocks

� Like with processes� But they cooperate easily because of the shared address space

� Consumes less memory

� Attention: do not confuse shared memory (between processes) with shared address space

(between threads)

� Switching between threads can be faster

� No need to reconfigure memory

� May achieve better performance

23 Daniel Gruss

Page 116: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads www.tugraz.at

� Make programming easier

� Split tasks in different blocks

� Like with processes� But they cooperate easily because of the shared address space

� Consumes less memory

� Attention: do not confuse shared memory (between processes) with shared address space

(between threads)

� Switching between threads can be faster

� No need to reconfigure memory

� May achieve better performance

23 Daniel Gruss

Page 117: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Why threads www.tugraz.at

� Make programming easier

� Split tasks in different blocks

� Like with processes� But they cooperate easily because of the shared address space

� Consumes less memory

� Attention: do not confuse shared memory (between processes) with shared address space

(between threads)

� Switching between threads can be faster

� No need to reconfigure memory

� May achieve better performance

23 Daniel Gruss

Page 118: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Example www.tugraz.at

24 Daniel Gruss

Page 119: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Example www.tugraz.at

while (TRUE)

{

get_next_request(&buf);

handoff_work(&buf);

}

while (TRUE)

{

wait_for_work(&buf);

look_for_page_in_cache(&buf,&page);

if (page_not_in_cache(&page))

read_page_from_disk(&buf,&page);

return_page(&page);

}

25 Daniel Gruss

Page 120: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Without Threads www.tugraz.at

Without threads,

� just one thread

� complicated program structure

� read content from disk may block process

� non-blocking read (polling!) decreases performance

26 Daniel Gruss

Page 121: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Without Threads www.tugraz.at

Without threads,

� just one thread

� complicated program structure

� read content from disk may block process

� non-blocking read (polling!) decreases performance

26 Daniel Gruss

Page 122: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Without Threads www.tugraz.at

Without threads,

� just one thread

� complicated program structure

� read content from disk may block process

� non-blocking read (polling!) decreases performance

26 Daniel Gruss

Page 123: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Without Threads www.tugraz.at

Without threads,

� just one thread

� complicated program structure

� read content from disk may block process

� non-blocking read (polling!) decreases performance

26 Daniel Gruss

Page 124: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Non-Blocking Read www.tugraz.at

while (TRUE) { // VERY simplified

get_next_event(&buf);

if (is_request_event(&buf)) {

if (page_not_in_cache(&page)) {

request_page_from_disk(&buf,&page);

save_request_in_table(&buf);

} else {

return_page(&page);

}

} else if (is_disk_event(&buf)) {

find_request_in_table(&buf);

mark_requeust_as_done(&buf);

return_page(&page);

} else if (is_...

27 Daniel Gruss

Page 125: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Non-Blocking Read www.tugraz.at

� Finite-state-machine!

� Actually simulates threads

� Better: use multithreading

28 Daniel Gruss

Page 126: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Non-Blocking Read www.tugraz.at

� Finite-state-machine!

� Actually simulates threads

� Better: use multithreading

28 Daniel Gruss

Page 127: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Non-Blocking Read www.tugraz.at

� Finite-state-machine!

� Actually simulates threads

� Better: use multithreading

28 Daniel Gruss

Page 128: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How to use multithreading? www.tugraz.at

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,

void *(*start_routine) (void *), void *arg);

29 Daniel Gruss

Page 129: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:
Page 130: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Function Pointer www.tugraz.at

� void *(*start routine) (void *)

� looks wrong...

� void* (*start routine) (void*)

� much better...

30 Daniel Gruss

Page 131: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Function Pointer www.tugraz.at

� void *(*start routine) (void *)

� looks wrong...

� void* (*start routine) (void*)

� much better...

30 Daniel Gruss

Page 132: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Function Pointer www.tugraz.at

� void *(*start routine) (void *)

� looks wrong...

� void* (*start routine) (void*)

� much better...

30 Daniel Gruss

Page 133: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Function Pointer www.tugraz.at

� void *(*start routine) (void *)

� looks wrong...

� void* (*start routine) (void*)

� much better...

30 Daniel Gruss

Page 134: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Function Pointer www.tugraz.at

� void* (*start routine) (void*)

� start routine is the function pointer name

� type: void* (*) (void*)

� (*) indicates this is a function pointer

� takes a void*

� returns a void*

31 Daniel Gruss

Page 135: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Function Pointer www.tugraz.at

� void* (*start routine) (void*)

� start routine is the function pointer name

� type: void* (*) (void*)

� (*) indicates this is a function pointer

� takes a void*

� returns a void*

31 Daniel Gruss

Page 136: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Function Pointer www.tugraz.at

� void* (*start routine) (void*)

� start routine is the function pointer name

� type: void* (*) (void*)

� (*) indicates this is a function pointer

� takes a void*

� returns a void*

31 Daniel Gruss

Page 137: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Function Pointer www.tugraz.at

� void* (*start routine) (void*)

� start routine is the function pointer name

� type: void* (*) (void*)

� (*) indicates this is a function pointer

� takes a void*

� returns a void*

31 Daniel Gruss

Page 138: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Function Pointer www.tugraz.at

� void* (*start routine) (void*)

� start routine is the function pointer name

� type: void* (*) (void*)

� (*) indicates this is a function pointer

� takes a void*

� returns a void*

31 Daniel Gruss

Page 139: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Function Pointer www.tugraz.at

� void* (*start routine) (void*)

� start routine is the function pointer name

� type: void* (*) (void*)

� (*) indicates this is a function pointer

� takes a void*

� returns a void*

31 Daniel Gruss

Page 140: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Let’s make a function pointer for main www.tugraz.at

int main(int argc, char *argv[])

� Function pointer: (*)

� +argument parenthesis:

(*)()

� +return type: int (*)()

� +first argument: int (*)(int)

� +second argument: int (*)(int, char*[])

32 Daniel Gruss

Page 141: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Let’s make a function pointer for main www.tugraz.at

int main(int argc, char *argv[])

� Function pointer: (*)

� +argument parenthesis:

(*)()

� +return type: int (*)()

� +first argument: int (*)(int)

� +second argument: int (*)(int, char*[])

32 Daniel Gruss

Page 142: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Let’s make a function pointer for main www.tugraz.at

int main(int argc, char *argv[])

� Function pointer: (*)

� +argument parenthesis:

(*)()

� +return type: int (*)()

� +first argument: int (*)(int)

� +second argument: int (*)(int, char*[])

32 Daniel Gruss

Page 143: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Let’s make a function pointer for main www.tugraz.at

int main(int argc, char *argv[])

� Function pointer: (*)

� +argument parenthesis: (*)()

� +return type:

int (*)()

� +first argument: int (*)(int)

� +second argument: int (*)(int, char*[])

32 Daniel Gruss

Page 144: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Let’s make a function pointer for main www.tugraz.at

int main(int argc, char *argv[])

� Function pointer: (*)

� +argument parenthesis: (*)()

� +return type:

int (*)()

� +first argument: int (*)(int)

� +second argument: int (*)(int, char*[])

32 Daniel Gruss

Page 145: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Let’s make a function pointer for main www.tugraz.at

int main(int argc, char *argv[])

� Function pointer: (*)

� +argument parenthesis: (*)()

� +return type: int (*)()

� +first argument:

int (*)(int)

� +second argument: int (*)(int, char*[])

32 Daniel Gruss

Page 146: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Let’s make a function pointer for main www.tugraz.at

int main(int argc, char *argv[])

� Function pointer: (*)

� +argument parenthesis: (*)()

� +return type: int (*)()

� +first argument:

int (*)(int)

� +second argument: int (*)(int, char*[])

32 Daniel Gruss

Page 147: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Let’s make a function pointer for main www.tugraz.at

int main(int argc, char *argv[])

� Function pointer: (*)

� +argument parenthesis: (*)()

� +return type: int (*)()

� +first argument: int (*)(int)

� +second argument:

int (*)(int, char*[])

32 Daniel Gruss

Page 148: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Let’s make a function pointer for main www.tugraz.at

int main(int argc, char *argv[])

� Function pointer: (*)

� +argument parenthesis: (*)()

� +return type: int (*)()

� +first argument: int (*)(int)

� +second argument:

int (*)(int, char*[])

32 Daniel Gruss

Page 149: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Let’s make a function pointer for main www.tugraz.at

int main(int argc, char *argv[])

� Function pointer: (*)

� +argument parenthesis: (*)()

� +return type: int (*)()

� +first argument: int (*)(int)

� +second argument: int (*)(int, char*[])

32 Daniel Gruss

Page 150: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Function Pointer www.tugraz.at

� void* (*start routine) (void*) = &main;?

� type doesn’t match... what now?

� cast:

void* (*start_routine) (void*) = (void* (*)(void*))&main;

Dangerous though ;)

33 Daniel Gruss

Page 151: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Function Pointer www.tugraz.at

� void* (*start routine) (void*) = &main;?

� type doesn’t match... what now?

� cast:

void* (*start_routine) (void*) = (void* (*)(void*))&main;

Dangerous though ;)

33 Daniel Gruss

Page 152: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Function Pointer www.tugraz.at

� void* (*start routine) (void*) = &main;?

� type doesn’t match... what now?

� cast:

void* (*start_routine) (void*) = (void* (*)(void*))&main;

Dangerous though ;)

33 Daniel Gruss

Page 153: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Function Pointer www.tugraz.at

� void* (*start routine) (void*) = &main;?

� type doesn’t match... what now?

� cast:

void* (*start_routine) (void*) = (void* (*)(void*))&main;

Dangerous though ;)

33 Daniel Gruss

Page 154: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Function Pointer www.tugraz.at

� void* (*start routine) (void*) = &main;?

� type doesn’t match... what now?

� cast:

void* (*start_routine) (void*) = (void* (*)(void*))&main;

Dangerous though ;)

33 Daniel Gruss

Page 155: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How to use multithreading? www.tugraz.at

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,

void *(*start_routine) (void *), void *arg);

The pthread create() function starts a new thread in the calling process. The new thread

starts execution by invoking start routine(); arg is passed as the sole argument of

start routine().

The attr argument points to a pthread attr t structure whose contents are used at

thread creation time to determine attributes for the new thread; this structure is initialized

using pthread attr init and related functions. If attr is NULL, then the thread is

created with default attributes.

Before returning, a successful call to pthread create() stores the ID of the new thread in

the buffer pointed to by thread; this identifier is used to refer to the thread in subsequent

calls to other pthreads functions.

34 Daniel Gruss

Page 156: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How to use multithreading? www.tugraz.at

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,

void *(*start_routine) (void *), void *arg);

The pthread create() function starts a new thread in the calling process. The new thread

starts execution by invoking start routine(); arg is passed as the sole argument of

start routine().

The attr argument points to a pthread attr t structure whose contents are used at

thread creation time to determine attributes for the new thread; this structure is initialized

using pthread attr init and related functions. If attr is NULL, then the thread is

created with default attributes.

Before returning, a successful call to pthread create() stores the ID of the new thread in

the buffer pointed to by thread; this identifier is used to refer to the thread in subsequent

calls to other pthreads functions.

34 Daniel Gruss

Page 157: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How to use multithreading? www.tugraz.at

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,

void *(*start_routine) (void *), void *arg);

The pthread create() function starts a new thread in the calling process. The new thread

starts execution by invoking start routine(); arg is passed as the sole argument of

start routine().

The attr argument points to a pthread attr t structure whose contents are used at

thread creation time to determine attributes for the new thread; this structure is initialized

using pthread attr init and related functions. If attr is NULL, then the thread is

created with default attributes.

Before returning, a successful call to pthread create() stores the ID of the new thread in

the buffer pointed to by thread; this identifier is used to refer to the thread in subsequent

calls to other pthreads functions.

34 Daniel Gruss

Page 158: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How to use multithreading? www.tugraz.at

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,

void *(*start_routine) (void *), void *arg);

The pthread create() function starts a new thread in the calling process. The new thread

starts execution by invoking start routine(); arg is passed as the sole argument of

start routine().

The attr argument points to a pthread attr t structure whose contents are used at

thread creation time to determine attributes for the new thread; this structure is initialized

using pthread attr init and related functions. If attr is NULL, then the thread is

created with default attributes.

Before returning, a successful call to pthread create() stores the ID of the new thread in

the buffer pointed to by thread; this identifier is used to refer to the thread in subsequent

calls to other pthreads functions.

34 Daniel Gruss

Page 159: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How to use multithreading? www.tugraz.at

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,

void *(*start_routine) (void *), void *arg);

� pthread t = thread ID

� pthread t*? call by reference

35 Daniel Gruss

Page 160: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How to use multithreading? www.tugraz.at

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,

void *(*start_routine) (void *), void *arg);

� pthread t = thread ID

� pthread t*? call by reference

35 Daniel Gruss

Page 161: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How to use multithreading? www.tugraz.at

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,

void *(*start_routine) (void *), void *arg);

� pthread t = thread ID

� pthread t*?

call by reference

35 Daniel Gruss

Page 162: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How to use multithreading? www.tugraz.at

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,

void *(*start_routine) (void *), void *arg);

� pthread t = thread ID

� pthread t*? call by reference

35 Daniel Gruss

Page 163: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How do pthreads terminate? www.tugraz.at

The new thread terminates in one of the following ways:

� It calls pthread exit, specifying an exit status value that is available to another thread

in the same process that calls pthread join.

� It returns from start routine(). This is equivalent to calling pthread exit with

the value supplied in the return statement.

� It is canceled (see pthread cancel).

� Any of the threads in the process calls exit, or the main thread performs a return from

main(). This causes the termination of all threads in the process.

36 Daniel Gruss

Page 164: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How do pthreads terminate? www.tugraz.at

The new thread terminates in one of the following ways:

� It calls pthread exit, specifying an exit status value that is available to another thread

in the same process that calls pthread join.

� It returns from start routine(). This is equivalent to calling pthread exit with

the value supplied in the return statement.

� It is canceled (see pthread cancel).

� Any of the threads in the process calls exit, or the main thread performs a return from

main(). This causes the termination of all threads in the process.

36 Daniel Gruss

Page 165: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How do pthreads terminate? www.tugraz.at

The new thread terminates in one of the following ways:

� It calls pthread exit, specifying an exit status value that is available to another thread

in the same process that calls pthread join.

� It returns from start routine(). This is equivalent to calling pthread exit with

the value supplied in the return statement.

� It is canceled (see pthread cancel).

� Any of the threads in the process calls exit, or the main thread performs a return from

main(). This causes the termination of all threads in the process.

36 Daniel Gruss

Page 166: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How do pthreads terminate? www.tugraz.at

The new thread terminates in one of the following ways:

� It calls pthread exit, specifying an exit status value that is available to another thread

in the same process that calls pthread join.

� It returns from start routine(). This is equivalent to calling pthread exit with

the value supplied in the return statement.

� It is canceled (see pthread cancel).

� Any of the threads in the process calls exit, or the main thread performs a return from

main(). This causes the termination of all threads in the process.

36 Daniel Gruss

Page 167: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How do pthreads terminate? www.tugraz.at

void pthread_exit(void *retval);

� The pthread exit() function terminates the calling thread and returns a value via

retval that (if the thread is joinable) is available to another thread in the same process

that calls pthread join.

� After the last thread in a process terminates, the process terminates as by calling exit

with an exit status of zero; [...]

37 Daniel Gruss

Page 168: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How do pthreads terminate? www.tugraz.at

void pthread_exit(void *retval);

� The pthread exit() function terminates the calling thread and returns a value via

retval that (if the thread is joinable) is available to another thread in the same process

that calls pthread join.

� After the last thread in a process terminates, the process terminates as by calling exit

with an exit status of zero; [...]

37 Daniel Gruss

Page 169: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

How do pthreads terminate? www.tugraz.at

void pthread_exit(void *retval);

� The pthread exit() function terminates the calling thread and returns a value via

retval that (if the thread is joinable) is available to another thread in the same process

that calls pthread join.

� After the last thread in a process terminates, the process terminates as by calling exit

with an exit status of zero; [...]

37 Daniel Gruss

Page 170: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Waiting for threads www.tugraz.at

int pthread_join(pthread_t thread, void **retval);

� The pthread join() function waits for the thread specified by thread to terminate.

If that thread has already terminated, then pthread join() returns immediately.

� If retval is not NULL, then pthread join() copies the exit status of the target thread

into the location pointed to by retval. If the target thread was canceled, then

PTHREAD CANCELED is placed in the location pointed to by retval.

38 Daniel Gruss

Page 171: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Waiting for threads www.tugraz.at

int pthread_join(pthread_t thread, void **retval);

� The pthread join() function waits for the thread specified by thread to terminate.

If that thread has already terminated, then pthread join() returns immediately.

� If retval is not NULL, then pthread join() copies the exit status of the target thread

into the location pointed to by retval. If the target thread was canceled, then

PTHREAD CANCELED is placed in the location pointed to by retval.

38 Daniel Gruss

Page 172: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Waiting for threads www.tugraz.at

int pthread_join(pthread_t thread, void **retval);

� The pthread join() function waits for the thread specified by thread to terminate.

If that thread has already terminated, then pthread join() returns immediately.

� If retval is not NULL, then pthread join() copies the exit status of the target thread

into the location pointed to by retval. If the target thread was canceled, then

PTHREAD CANCELED is placed in the location pointed to by retval.

38 Daniel Gruss

Page 173: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Killing threads www.tugraz.at

int pthread_cancel(pthread_t thread);

� The pthread cancel() function sends a cancellation request to the thread thread.

39 Daniel Gruss

Page 174: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Killing threads www.tugraz.at

int pthread_cancel(pthread_t thread);

� The pthread cancel() function sends a cancellation request to the thread thread.

39 Daniel Gruss

Page 175: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Take Aways www.tugraz.at

� Processes divide resources amongst themselves (except processor time)

� Threads divide processor time amongst themsevles (and a few resources)

� Sometimes processes are more appropriate, sometimes threads

40 Daniel Gruss

Page 176: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Take Aways www.tugraz.at

� Processes divide resources amongst themselves (except processor time)

� Threads divide processor time amongst themsevles (and a few resources)

� Sometimes processes are more appropriate, sometimes threads

40 Daniel Gruss

Page 177: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window:

Take Aways www.tugraz.at

� Processes divide resources amongst themselves (except processor time)

� Threads divide processor time amongst themsevles (and a few resources)

� Sometimes processes are more appropriate, sometimes threads

40 Daniel Gruss

Page 178: System Level Programming...1 Daniel Gruss Abstractions Process: abstraction of a computer File: abstraction of a disk or a device Socket: abstraction of a network connection Window: