Lecture 3 - Processes Flashcards
Can we associate a FIFO to more than two processes?
Yes.
Process
A program in execution; process execution must progress in sequential fashion
Program becomes process when executable file loaded into memory.
One program can have multiple processes.
5 parts of process
- Text section (program code)
- Current activity (program counter), processor registers
- Stack containing temporary data (function parameters, return addresses, local variables)
- Data section: global variables (initialized and unitialized)
- Heap: memory dynamically allocated during run time
5 states of process
- New: The process is being created
- Running: Instructions are being executed
- Waiting: The process is waiting for some event to occur
- Ready: The process is waiting to be assigned to a processor
- Terminated: The process has finished execution
Process Control Block (PCB)
Hold information associated with each process:
■Process state –running, waiting, etc
■Program counter –location of instruction to next execute
■CPU registers –contents of all process-centric registers
■CPU scheduling information-priorities, scheduling queue pointers
■Memory-management information –memory allocated to the process
■Accounting information –CPU used, clock time elapsed since start, time limits
■I/O status information –I/O devices allocated to process, list of open files
Threads
Ability to run multiple processes within one program by having multiple program counters executing multiple locations at once. Need to store thread details in PCB.
C structure to represent Process in Linux
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 process */
Main benefit of Process Scheduling
Maximize CPU use, quickly switch processes onto CPU core
Role of Process Scheduler
Process scheduler selects among available processes for next execution on CPU core
2 Scheduling Queues of processes
- Ready queue –set of all processes residing in main memory, ready and waiting to execute
- Wait queues –set of processes waiting for an event (i.e. I/O)
CPU Context switch
When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process
Context of a process
Current state of process, represented in PCB
What is context-switch overhead?
When the cpu switches between contexts, it does no useful work. The more complex the OS and the PCB, the longer the context switch.
Why does context switch depend on hardware?
We could have cpus with multiple sets of registers, which allows multiople contexts to be loaded at once. Faster.
Main difference between multitasking in IOS vs Android?
IOS is more limited on the background processes it can run, and has one main foreground process.
Android has both, but background processes uses a service to perform tasks, which can run even if background is suspended. The service has no ui and small memory use, which allows it to do more.
2 main process operations
- Creation
2. Termination
process identifier (pid)
id assigned to a created process
How does a tree of processes occur?
Parent creates children, which in turn can create other children
3 Resource sharing options between Parent and Child process
- Parent and children share all resources
- Children share subset of parent’s resources
- Parent and child share no resources
2 Execution options between Parent and Child process
- Parent and child can run concurrently
2. Parent waits for child to terminate
Relationship between child and parent address space
Child duplicate of parent virtual address space, and changes in one don’t affect the other.
fork() system call
creates child process (pid = 0) from parent process (pid > 0)
exec() system call
replace the process’memory space with a new program
wait() system call
The parent process may wait for termination of a child process by using the wait()system call. The call returns status information and the pid of the terminated process
pid = wait(&status);
What happens when a process terminate?
Asks the operating system to delete it using the exit() system call.
● Returns status data from child to parent (via wait())
● Process’resources are deallocated by operating system
abort() function
+ 3 reasons to use it
Parent may terminate the execution of children processes using the abort() system call:
- Child has exceeded allocated resources.
- Task assigned to child is no longer required.
- The parent is exiting and the operating systems does not allow a child to continue if its parent terminates.
Cascading (process) termination
Some operating systems do not allow child to exists if its parent has terminated. If a process terminates, then all its children must also be terminated.
zombie process
If no parent waiting (did not invoke wait()) process is a zombie
orphan process
If parent terminated without invoking wait(), process is an orphan
Android Process Importance Hierarchy
Foreground process Visible process Service process Background process Empty process
Problem with running web browser as single process
If one web site causes trouble, entire browser can hang or crash
Cooperating process
+ 4 reasons
Can affect or be affected by other processes, including sharing data: ●Information sharing ●Computation speedup ●Modularity ●Convenience
Interprocess communication (IPC) \+ 2 Models
Method by which processes cooperate:
- Shared memory (same memory block)
- Memory passing (through message queue)
Independent Process
Cannot affect or be affected by the execution of another process
unbounded-buffer
Places no practical limit on the size of the buffer between producer and consumer
bounded-buffer
Assumes that there is a fixed buffer size between producer and consumer
Shared Memory (Interprocess Communication) \+ Major issue
An area of memory shared among the processes that wish to communicate. The communication is under the control of the users processes not the operating system.
Major issues is to provide mechanism that will allow the user processes to synchronize their actions when they access shared memory.
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 functions for Message passing
●send(message)
●receive(message)
Communication link
Connection between process P and Q in message passing
Is the size of a message that the link can accommodate fixed or variable?
either fixed or varible
Implementation of communication link (Message Passing):
3 Physical
3 Logical
Physical
• Shared memory
• Hardware bus
• Network
Logical
• Direct or indirect
• Synchronous or asynchronous
• Automatic or explicit buffering
Direct Communication
+ 4 Properties
Processes must name each other explicitly:
●send(P, message) –send a message to process P
●receive(Q, message) –receive a message from process Q
Properties
●Links are established automatically
●A link is associated with exactly one pair of communicating processes
●Between each pair there exists exactly one link
●The link may be unidirectional, but is usually bi-directional
Indirect Communication
+ 4 properties
Messages are directed and received from mailboxes (also referred to as ports)
●Each mailbox has a unique id
●Processes can communicate only if they share a mailbox
Properties of communication link
●Link established only if processes share a common mailbox
●A link may be associated with many processes
●Each pair of processes may share several communication links
●Link may be unidirectional or bi-directional
3 Indirect Communication Operations
+ Send and receive function arguments
- create a new mailbox (port)
- send and receive messages through mailbox
- destroy a mailbox
Primitives are defined as:
send(A, message) –send a message to mailbox A
receive(A, message) –receive a message from mailbox
Blocking (Message Passing)
+ 2 functions
Blocking is considered synchronous
●Blocking send –the sender is blocked until the message is received
●Blocking receive –the receiver is blocked until a message is available
Non-blocking (Message Passing)
+ 2 functions
Non-blocking is considered asynchronous
●Non-blocking send–the sender sends the message and continue
●Non-blocking receive–the receiver receives:
●A valid message, or
●Null message
Rendezvous (Message Passing Synchronization)
If both send and receive are blocking, we have a rendezvous
Buffering
+ 3 Implementation methods
- Zero capacity –no messages are queued on a link.
Sender must wait for receiver (rendezvous) - Bounded capacity –finite length of n messages.
Sender must wait if link full - Unbounded capacity –infinite length.
Sender never waits
Function to create Shared Memory in POSIX
shm_fd = shm_open(name, O CREAT | O RDWR, 0666);
Set the size of the object in POSIX Shared Memory
ftruncate(shm_fd, 4096);
mmap()
Use mmap() to memory-map a file pointer to the shared memory object. Reading and writing to shared memory is done by using the pointer returned by mmap().
Pipes
Acts as a conduit allowing two processes to communicate
Ordinary pipes
Cannot be accessed from outside the process that created it. Typically, a parent process creates a pipe and uses it to communicate with a child process that it created.
■ Producer writes to one end (the write-end of the pipe)
■Consumer reads from the other end (the read-endof the pipe)
■Ordinary pipes are therefore unidirectional
Windows calls these anonymous pipes
Named pipes
Named Pipes are more powerful than ordinary pipes
■Communication is bidirectional
■No parent-child relationship is necessary between the communicating processes
■Several processes can use the named pipe for communication
■Provided on both UNIX and Windows systems