Processes and Threads - cs.anu.edu.au

14
Systems, Networks, and Concurrency COMP2310/6310 Processes and Threads Operating system support Process states Unix processes

Transcript of Processes and Threads - cs.anu.edu.au

Page 1: Processes and Threads - cs.anu.edu.au

Systems, Networks, and Concurrency COMP2310/6310

Processes and Threads

Operating system support

Process states

Unix processes

Page 2: Processes and Threads - cs.anu.edu.au

Systems, Networks, and Concurrency COMP2310/6310

One CPU per control flow

• Specialized configurations e.g.:

– Distributed µcontrollers

– Physical process control systems:

1 CPU per task, connected via a

bus-system

• Process management

(scheduling) not required

• Must coordinate access to

shared memory

6

Processes and Threads C03

Page 3: Processes and Threads - cs.anu.edu.au

Systems, Networks, and Concurrency COMP2310/6310

One CPU for all control flows

• OS: emulate one CPU for every

control flow:

Multi-tasking operating system

stack

– Process management

(scheduling) required

– Must coordinate access to shared

memory

– Support for memory protection

essential

7

Processes and Threads C03

Page 4: Processes and Threads - cs.anu.edu.au

Systems, Networks, and Concurrency COMP2310/6310

Processes

8

Processes and Threads C03

• Process ::= address space +

control flow(s)

• Kernel has full knowledge

about all processes

– state

– requirements

– currently held resources

Page 5: Processes and Threads - cs.anu.edu.au

Systems, Networks, and Concurrency COMP2310/6310

Threads

9

Processes and Threads C03

• Threads (individual control

flows) can be handled:

– Inside the OS:

• Kernel scheduling

• Thread performs I/O

– Outside the OS:

• User-level scheduling

• Parent process performs I/O

Page 6: Processes and Threads - cs.anu.edu.au

Systems, Networks, and Concurrency COMP2310/6310

SMP

10

Processes and Threads C03

Symmetric Multi-Processing

• All CPUs share the same

physical address space

(and access to resources)

• Any process / thread can

be executed on any

available CPU

Page 7: Processes and Threads - cs.anu.edu.au

Systems, Networks, and Concurrency COMP2310/6310

Processes or Threads ?

11

Processes and Threads C03

Specific definition of threads depends on operating system and context:

• Threads ≡ a group of processes, which share some resources

(process hierarchy)

• Due to the overlap in resources, the attributes attached to threads are

less than for ‘first-class-citizen-processes’

• Thread switching and inter-thread communication can be more

efficient than switching on process level

• Scheduling of threads depends on the actual thread implementations:

– user-level control-flows (no kernel support)

– kernel-level control-flows, handled as processes with some restrictions

Page 8: Processes and Threads - cs.anu.edu.au

Systems, Networks, and Concurrency COMP2310/6310

Process Control Blocks

12

Processes and Threads C03

• Process Id

• Process state:

{created, ready, executing, blocked,

suspended, …}

• Scheduling attributes:

Priorities, deadlines, consumed CPU-time, …

• CPU state:

Saved/restored information on context

switches (including program counter, stack

pointer, …)

Page 9: Processes and Threads - cs.anu.edu.au

Systems, Networks, and Concurrency COMP2310/6310

Process Control Blocks

13

Processes and Threads C03

• …

• Memory attributes / privileges:

memory base, limits, shared areas, …

• Allocated resources / privileges:

open and requested devices and files, …

PCBs are commonly enqueued at a certain

state or condition (awaiting access or change

in state)

Page 10: Processes and Threads - cs.anu.edu.au

Systems, Networks, and Concurrency COMP2310/6310

Process States

14

Processes and Threads C03

• created:

task is ready to run, but not yet

considered by any dispatcher

(waiting for admission)

• ready:

ready to run (waiting for a CPU)

• running:

holding a CPU and executing

• blocked: not ready to run

(waiting for a resource)

Page 11: Processes and Threads - cs.anu.edu.au

Systems, Networks, and Concurrency COMP2310/6310

Process States

15

Processes and Threads C03

• suspended states: swapped out

of main memory

(non-time-critical processes)

waiting for main memory space

(or other resources)

• dispatching and suspending can

now be independent modules

Page 12: Processes and Threads - cs.anu.edu.au

Systems, Networks, and Concurrency COMP2310/6310

Process States

16

Processes and Threads C03

Page 13: Processes and Threads - cs.anu.edu.au

Systems, Networks, and Concurrency COMP2310/6310

Unix Processes

17

Processes and Threads C03

• In UNIX systems tasks are

created by ‘cloning’

pid = fork();

– duplicates the current process

– returns 0 to the newly created

process (the ‘child’ process)

– returns the process id of child

process to creating process (the

‘parent’ process)

– or returns -1 as C-style indication

of failure

if (fork() == 0) {

// child process

exec("path_to_exe", args);

exit(0); // terminate

} else {

// parent process

pid = wait();

/* wait for termination of

one child process */

}

parent child

fork

waitexec

Page 14: Processes and Threads - cs.anu.edu.au

Systems, Networks, and Concurrency COMP2310/6310

Communication Between Unix Processes

18

Processes and Threads C03

int pipe_fd[2], c, rc;

if (pipe(pipe_fd) == -1) {

perror("no pipe"); exit(1);

}

if (fork() == 0) { // child/receiver

close(pipe_fd[1]);

while((rc = read(pipe_fd[0], &c,

1)) > 0) putchar(c);

if (rc == -1) {

perror("pipe broken");

close(pipe_fd[0]); exit(1);

}

close(pipe_fd[0]);

}

else { // parent/sender

close(pipe_fd[0]);

while ((c = getchar()) > 0) {

if (write(pipe_fd[1], &c, 1)== -1)

{

perror("pipe broken");

close(pipe_fd[1]); exit(1);

}

}

close(pipe_fd[1]);

wait(NULL);

}parent pipe

fork

close

childpipe

readwrite

close