19
CS143a Discussion Week 3 Andrew Chio

CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

CS143a Discussion Week 3Andrew Chio

Page 2: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Today’s Plan

• Going over a few things for HW1 (5 minutes)• Synchronous I/O vs Asynchronous I/O

• Other questions on HW1?

• Processes, Programs, and Threads (30 minutes)

• Code – Multithreading in C (5 minutes)

• Quiz Practice (5 minutes)

Page 3: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Synchronous I/O vs Asynchronous I/O (1)

• Scenario: User Process requests I/O

• Once I/O is started, two courses of action are possible:• Synchronous I/O

• wait instruction idles CPU until I/O is complete

• no simultaneous I/O processing, at most one outstanding I/O request at a time

• Asynchronous I/O• After I/O is initiated, control returns to the user program, without waiting for I/O

completion

• Keep track of many I/O requests at the same time:• Device Status table – holds type, address and state for each device

• OS indexes into I/O device table to determine device status and modify table entry to includeinterrupt

Page 4: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Synchronous I/O vs Asynchronous I/O (2)

Page 5: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Other questions for HW1?

• Two versions of the homework:• Only difference is on Question 1ci, 1ciii

• Poll via Chat: Do you need an extension? (Due: April 18th) • Can’t guarantee, but can ask if students would like one

Page 6: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Processes and Programsmain ()

{

…;

}

A() {

}

main ()

{

…;

}

A() {

}

Pro

gramP

rocess

• Process = Program in execution (sequentially)• Information stored in Process Control Block (PCB)

• More next slide

• Memory: text, data, heap, stack

• Other resources (e.g., opened files)

• Security attributes

Page 7: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Process Control Block

• Contains information associated with each process• Process State - e.g. new, ready, running etc.

• Process Number – Process ID

• Program Counter - address of next instruction to be executed

• CPU registers - general purpose registers, stack pointer etc.

• CPU scheduling information - process priority, pointer

• Memory Management information - base/limit information

• Accounting information - time limits, I/O status information

Process Control

Block

7

Page 8: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Process State

new admitted

interrupt

I/O or

event

completion

Scheduler

dispatchI/O or

event wait

exit

readyrunning

terminated

waiting

Process is being created, may have some resources

already

Process is admitted to system, has all

resources, ready to compete for CPU Process is now running

(assigned by scheduler)

Process exits when done.

Waiting on I/O or event (trigger for some other process to go from ready to running)

Event or I/O complete: go back

to ready (CPU might not be ready

yet)

Maybe process is not done, but interrupt

forces it to stop

Page 9: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Context Switching

Page 10: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Questions so far?

• Next: Process schedulers and profiles, Threads

Page 11: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Schedulers

• Long term scheduler / job scheduler• Selects which processes should be brought into the ready queue

• Invoked infrequently (seconds, minutes), may be slow

• Controls degree of multiprogramming

• Short term scheduler / CPU scheduler• Selects which process should execute next; allocates CPU

• Invoked very frequently (milliseconds) – must be very fast

• Medium Term scheduler• Swaps out process temporarily

• Balances load for better throughput

Page 12: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Process Profiles

• I/O bound Process: spends more time in I/O, has short CPU bursts• CPU is underutilized

• CPU bound Process: spends more time doing computations, has few long CPU bursts • I/O is underutilized

• The right job mix:• Long term scheduler: admits job to keep load balanced between I/O and CPU

bound processes

• Medium term scheduler: ensures the right mix by sometimes swapping out jobs and resuming them later

Page 13: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Linux Process Creation and Threads

• Process Creation• fork and exec used to create a new process and load a new program to run

• Processes do not share resources well• High context switching overhead, must copy all of memory space

• Threads: (What if we could share?) • Basic unit of CPU utilization

• Consists of program counter, register set, stack space

• Shares with peer threads: text, data, OS resources (e.g., open files, signals)

• Collectively called a task

Page 14: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Processes and Threads

Page 15: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Questions?

• Next: Types of Threads

Page 16: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Types of Threads

• Kernel Threads• Supported by kernel

• Thread can run/block independently

• A process may have several threads waiting on different things

• Cons? A bit expensive• Must schedule in kernel space

• Examples: Windows XP/2000, Linux, UNIX, Solaris, …

• Or, can have hybrid approach using both

• User Threads• Supported above the kernel

• Via library calls• Thread management done by user-level

threads library• User Program provides scheduler and

thread package• Mapping to kernel threads: Many to

one, one to one, many to many

• Pros: Cheap, Fast, Stay in User Mode• Cons: If kernel is single threaded,

syscall from a thread can block entire task

• Examples: POSIX pthreads, Win32 threads, Java threads

Page 17: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Multi-everything that you should know

• Multiprocessing: Multiple CPUs

• Multiprogramming: Multiple processes running concurrently

• Multithreading: Multiple threads per process

Page 18: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Code Demos: POSIX pthreads and OpenMP

• single.c: Just a double for loop, printing “Hello” 10x3 times• Compile with gcc –std=c99 single.c –o single

• pthreads_demo.c: 10 threads to each print “Hello” 3 times• Compile with gcc –std=c99 pthread_demo.c –o pthreads –lpthread

• openmp_demo.c: 10 threads, but pragmas do all the heavy lifting• Compile with gcc –std=c99 openmp_demo.c –o openmp -fopenmp

Page 19: CS143a Discussion Week 3ics143/discussions/cs143a-disc3.pdf · Linux Process Creation and Threads •Process Creation •fork and exec used to create a new process and load a new

Quiz Practice

• Go to Gradescope > Quiz Practice to take the practice quiz

• Any Questions otherwise?