Process (contd.) Indranil Sen Gupta (odd section) and Mainack - - PowerPoint PPT Presentation

process contd
SMART_READER_LITE
LIVE PREVIEW

Process (contd.) Indranil Sen Gupta (odd section) and Mainack - - PowerPoint PPT Presentation

Process (contd.) Indranil Sen Gupta (odd section) and Mainack Mondal (even section) CS39002 Spring 2019-20 So far on processes What is a process? Structure of a process Process states Process control block Context switch


slide-1
SLIDE 1

Process (contd.)

Indranil Sen Gupta (odd section) and Mainack Mondal (even section)

CS39002 Spring 2019-20

slide-2
SLIDE 2

So far on processes

  • What is a process?
  • Structure of a process
  • Process states
  • Process control block
  • Context switch
slide-3
SLIDE 3

Process Representation in Linux

Represented by the C structure task_struct

pid t pid; /* process identifier */ long state; /* state of the process */ unsigned int time slice /* scheduling information */ struct task struct *parent; /* this process’s parent */ struct list head children; /* this process’s children */ struct files struct *files; /* list of open files */ struct mm_struct *mm; /* address space of this pro */

Doubly linked list

slide-4
SLIDE 4

Process scheduling

slide-5
SLIDE 5

Process scheduling

  • The process scheduler selects an available

process for execution on the CPU

  • Dispatcher: The kernel process that assigns CPU to a

process

slide-6
SLIDE 6

Recap: Process state diagram

New Ready Running Waiting Terminated Interrupt Scheduler Dispacher I/O or event wait I/O or event completion exit

slide-7
SLIDE 7

Recap: Process state diagram

New Ready Running Waiting Terminated Interrupt Scheduler Dispacher I/O or event wait I/O or event completion exit Job queue

slide-8
SLIDE 8

Recap: Process state diagram

New Ready Running Waiting Terminated Interrupt Scheduler Dispacher I/O or event wait I/O or event completion exit Job queue Ready queue

slide-9
SLIDE 9

Recap: Process state diagram

New Ready Running Waiting Terminated Interrupt Scheduler Dispacher I/O or event wait I/O or event completion exit Job queue Ready queue Device queue

slide-10
SLIDE 10

Process scheduling

  • Several scheduling queues exist in OS
  • A PCB is linked to one of the queues at any given tome
  • The PCBs in a queue are connected as a linked list
slide-11
SLIDE 11

Structure of process queues

slide-12
SLIDE 12

Structure of process queues

slide-13
SLIDE 13

Structure of process queues

slide-14
SLIDE 14

Structure of process queues

slide-15
SLIDE 15

Structure of process queues

slide-16
SLIDE 16

Characteristics of process queues

  • Each I/O device has its own device queue
  • Each event also has its own queue
  • Process scheduling can be represented as a queueing

diagram

  • Queueing diagram represents queues, resources, flows
  • We will discuss actual scheduling algorithm later
slide-17
SLIDE 17

Representation of process scheduling

slide-18
SLIDE 18

Representation of process scheduling

slide-19
SLIDE 19

Operations on processes

slide-20
SLIDE 20

Process creation

  • During execution a process may create several new processes
  • Each process has a unique process identifier (pid)
  • Other than the first process (init), all other processes are created

by fork system call

  • Parent process create children processes, which, in turn create
  • ther processes, forming a tree of processes
slide-21
SLIDE 21

Process creation (contd.)

  • Address space
  • Child duplicate of parent
  • Child has a program loaded into it
  • UNIX example
  • fork() : creates a new process
  • exec(): replace new process’s memory with new code
slide-22
SLIDE 22

Process creation (contd.)

  • Address space
  • Child duplicate of parent
  • Child has a program loaded into it
  • UNIX example
  • fork() : creates a new process
  • exec(): replace new process’s memory with new code

pid > 0 pid = 0

slide-23
SLIDE 23

Process creation example

Error condition child process parent process

slide-24
SLIDE 24

Process termination

  • A child process executes last statement
  • exit() call for deleting the process
  • return status data from child to parent via wait()
  • Deallocate the resources
slide-25
SLIDE 25

Process termination

  • A child process executes last statement
  • exit() call for deleting the process
  • return status data from child to parent via wait()
  • Deallocate the resources

Child process . . exit(2) // Exit with status code Parent process pid_t pid; Int status; . pid = wait (&status) // pid of terminated child

slide-26
SLIDE 26

Process termination: Corner cases

  • In some OS
  • All child must terminate when a process terminates
  • Cascading termination: All children, grandchildren
  • etc. must be terminated
  • OS takes care of this cascade
  • Combinations of exit() and wait()
  • If no parent is waiting then zombie process
  • If parent terminated without invoking wait then
  • rphan process
slide-27
SLIDE 27

Zombie and orphan process

  • Zombie process
  • A process that has terminated, but who parent had not

not yet called wait()

  • All processes move to this state when they terminate and

remain there until parent calls wait()

  • Entry in process table removed only after calling wait()
slide-28
SLIDE 28

Zombie and orphan process

  • Zombie process
  • A process that has terminated, but who parent had not

not yet called wait()

  • All processes move to this state when they terminate and

remain there until parent calls wait()

  • Entry in process table removed only after calling wait()
  • Orphan process
  • parent terminated without invoking wait
  • Immediately “init” process assigned as parent
  • “init” periodically invokes wait()
slide-29
SLIDE 29

Inter-process communication (IPC)

  • Processes executing concurrently in OS may be

independent or cooperating

  • Cooperating process
  • Affect or be affected by other processes, e.g., sharing data
slide-30
SLIDE 30

Inter-process communication (IPC)

  • Processes executing concurrently in OS may be

independent or cooperating

  • Cooperating process
  • Affect or be affected by other processes, e.g., sharing data
  • Can share information
  • Speed-up in computation
  • Design can be modular
slide-31
SLIDE 31

Inter-process communication (IPC)

  • Processes executing concurrently in OS may be

independent or cooperating

  • Cooperating process
  • Affect or be affected by other processes, e.g., sharing data
  • Can share information
  • Speed-up in computation
  • Design can be modular
  • Cooperating processes need IPC
  • shared memory
  • Message passing
slide-32
SLIDE 32

Inter-process communication (IPC)

  • Ways to do IPC
  • way 1: shared memory - shmget(), shmcat(), shmaddr(),

shmat(), shmdt(), shmctl()

  • way 2: message passing (pipe) - pipe(), read(), write(), close()
  • way 3: message passing (named pipe) - mkfifo(), read(),

write(), close()

  • way 4: Over network - RPC or Remote Procedure Call,

sockets

slide-33
SLIDE 33

Shared memory system

slide-34
SLIDE 34

Schematic for shared memory

process A shared memory kernel

process B

slide-35
SLIDE 35

Let’s check the function calls

char *myseg; key_t key; int shmid; key = 235; // some unique id shmid = shmget(key, 250, IPC_CREAT | 0666); myseg = shmat(shmid, NULL, 0); . . shmdt(myseg); . . shmctl(shmid, IPC_RMID, NULL);

slide-36
SLIDE 36

Let’s check the function calls

char *myseg; key_t key; int shmid; key = 235; // some unique id shmid = shmget(key, 250, IPC_CREAT | 0666); // create shared memory segment myseg = shmat(shmid, NULL, 0); . . shmdt(myseg); . . shmctl(shmid, IPC_RMID, NULL);

slide-37
SLIDE 37

Let’s check the function calls

char *myseg; key_t key; int shmid; key = 235; // some unique id shmid = shmget(key, 250, IPC_CREAT | 0666); // create shared memory segment myseg = shmat(shmid, NULL, 0); // attach the segment to the // address space of this process . . shmdt(myseg); . . shmctl(shmid, IPC_RMID, NULL);

slide-38
SLIDE 38

Let’s check the function calls

char *myseg; key_t key; int shmid; key = 235; // some unique id shmid = shmget(key, 250, IPC_CREAT | 0666); // create shared memory segment myseg = shmat(shmid, NULL, 0); // attach the segment to the // address space of this process . . shmdt(myseg); // detach the segment from the address space . . shmctl(shmid, IPC_RMID, NULL);

slide-39
SLIDE 39

Let’s check the function calls

char *myseg; key_t key; int shmid; key = 235; // some unique id shmid = shmget(key, 250, IPC_CREAT | 0666); // create shared memory segment myseg = shmat(shmid, NULL, 0); // attach the segment to the // address space of this process . . shmdt(myseg); // detach the segment from the address space . . shmctl(shmid, IPC_RMID, NULL); // mark the segment to be destroyed

slide-40
SLIDE 40

Producer consumer problem

  • A producer process produces information that is

consumed by the consumer process

  • Compiler produces assembly code consumed by assembler
  • Program produces lines to print, print spool consumes
  • The information is read/write from a buffer
slide-41
SLIDE 41

Producer consumer problem

  • A producer process produces information that is

consumed by the consumer process

  • Compiler produces assembly code consumed by assembler
  • Program produces lines to print, print spool consumes
  • The information is read/write from a buffer
  • Two variants
  • Bounded buffer
  • Unbounded buffer
slide-42
SLIDE 42

Producer consumer problem

  • A producer process produces information that is

consumed by the consumer process

  • Compiler produces assembly code consumed by assembler
  • Program produces lines to print, print spool consumes
  • The information is read/write from a buffer
  • Two variants
  • Bounded buffer
  • Unbounded buffer
  • Bounded buffer : producer waits when buffer is full,

consumer waits when buffer is empty

slide-43
SLIDE 43

Producer consumer solution with bounded buffer

6

  • Shared data: implemented as a circular array

#define BUFFER_SIZE 10 typedef struct { . . . // information to be shared } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0;

in

  • ut

6

slide-44
SLIDE 44

Key ideas

8

  • Circular buffer
  • Index in: the next position to write to
  • Index out: the next position to read from
  • To check buffer full or empty:
  • Buffer empty: in==out
  • Buffer full: in+1 % BUFFER_SIZE == out
  • Why ? There is still one slot left …
slide-45
SLIDE 45

Pseudo code

7 while (true) { /* Produce an item */ while (( (in + 1) % BUFFER_SIZE) == out) ; /* do nothing -- no free buffers */ buffer[in] = newProducedItem; in = (in + 1) % BUFFER SIZE; } while (true) { while (in == out) ; // do nothing -- nothing to consume // remove an item from the buffer itemToConsume = buffer[out];

  • ut = (out + 1) % BUFFER SIZE;

return itemToComsume; }

Producer Consumer Solution is correct, but can only use BUFFER_SIZE-1 elements

in

  • ut

7

slide-46
SLIDE 46

Better utilization of buffer space

9

  • Circular buffer
  • Suppose that we want to use all buffer space:
  • an integer count: the number of filled buffers
  • Initially, count is set to 0.
  • incremented by producer after it produces a new

buffer

  • decremented by consumer after it consumes a buffer.
slide-47
SLIDE 47

Better utilization of buffer space: Pseudo code

10

Producer

while (true) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }

Consumer

while (true) { while (count == 0) ; // do nothing nextConsumed = buffer[out];

  • ut = (out + 1) % BUFFER_SIZE;

count--; /* consume the item in nextConsumed */ }

slide-48
SLIDE 48

Message passing system

slide-49
SLIDE 49

Basics of message passing

  • Mechanism for processes to communicate and to synchronize

their actions

  • Message system – processes communicate with each other

without resorting to shared variables

  • IPC facility provides two operations:

– send(message) – receive(message)

  • The message size is either fixed or variable
slide-50
SLIDE 50

Communication model

process A message queue kernel process B m0 m1 m2 ... m3 mn

slide-51
SLIDE 51

Ways for message passing

  • Pipes
  • Named pipes
  • Covered in last class
  • Also in the assignments
slide-52
SLIDE 52

Finally for communication of two processes over network

  • Sockets API
  • Remote procedure call
slide-53
SLIDE 53

Summary

  • What is a process?
  • Structure of a process
  • Process states
  • Process control block
  • Context switch
  • Why is process scheduling necessary?
  • Ready queues, event queues, queueing diagram
  • How does two processes talk?
  • Shared memory, pipe, named pipe
slide-54
SLIDE 54