processes Flashcards

1
Q

When a task manager switches from executing one process to another, what is the primary purpose of saving and restoring the process context?

A

To ensure that the process can resume execution from where it left off when it is next scheduled to run

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Process management means managing the context of a process (True/False)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is a program (process slides)

A

A program is a passive set of instructions stored on a
secondary storage device, such as a disk

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is a process (process slides)

A

A process is an active execution of a program stored in
memory
– Program becomes process when loaded into memory
– Alt def: A process is an executing (i.e., running) instance of a
program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

more precise definition of process (process slides)

A

A process is the context (i.e., information and data) maintained for an executing program
– A process needs certain resources, including CPU time, memory, files, and I/O devices, to accomplish its task
* Intuitively, a process is the abstraction of a physical processor
– Exists because it is difficult for the OS to otherwise coordinate many concurrent activities, such as incoming network data, multiple users, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

process management, what activities is the OS responsible for?

A
  • *The OS is responsible for the following activities
    – Process creation and deletion
    – Process suspension and resumption
    – Provision of mechanisms for:
  • Process synchronization
  • Process communication
    – Deadlock handling
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

process management, how does the OS correctly run multiple processes concurrently?

A

– What kind of information must be kept?
– What does OS have to do to run processes correctly?
– OS must be able to distinguish among different processes
* Multiple programs may be loaded into memory at same time
* Each process is assigned a unique, non-negative integral process ID, or PID

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what does PID stand for? (processes slides)

A

The PID is a process ID number. Every process has a unique ID number, and it is allocated together with system resources such as memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the PID used for? (processes slides)

A

The system administrator uses the PID for changing the task
priority and ending tasks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how are processes logically organized?

A

Processes are organized in a hierarchical manner, just like
files and folders. So the child processes are nested under the
parent processes and so on.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is the procedure when a process ends?

A

When a process ends, it is reported back to the parent
process. Its resources are freed and the pid is removed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

where are current processes stored?

A
  • Information on current processes is stored in the /proc filesystem.
  • This filesystem consists of kernel data that changes in real time (i.e., sensing and responding to external events nearly simultaneously). It is easy to extract the information contained therein using commands such as cat.
  • Listing the contents of /proc with the ls command as follows will show numerous directories whose names consist only of numbers:
  • ls /proc | less
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the segments of memory in the process context?

A
  • When a program loaded into memory, it is organized into the following segments of memory
    – text contains the actual program code, or executable instructions
    – data contains global and static variables initialized at runtime
    – heap contains dynamic memory allocated at runtime
    – stack contains return addresses, function parameters, and variables
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what are the types of registers/register contexts?

A

– Program Counter (PC)
* Address of next instruction to be executed (may be in kernel or user memory space of this process)
– Processor Status Register
* Contains the hardware status at the time of preemption. Contents and format are hardware-dependent
– Stack Pointer (SP)
* Points to the top of the kernel or user stack, depending on the mode of operation at the time of preemption
– General-Purpose Registers
* Hardware dependent, R0, R1, R2, … eax, ebx, ecx…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what are some of the states in a process state machine?

A
  • A process executes according to the following state machine:
    – created: also “new”, initial state when created
    – waiting: also “ready”, awaiting to be scheduled for execution
    – running: actively executing instructions on the CPU
    – blocked: unable to continue without an event occurring, e.g., I/O
    – terminated: no longer running due to completion or being killed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

user level context

A

code, data, stack heap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

register context

A

R0, R1, … , PC, SP, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

system level context

A

Os resources. stuff like open files, signal related data structures, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

While a program is executing, the
process can be uniquely identified by
several elements, including:

A
  • Identifier (PID)
  • State
  • Priority
  • Program counter
  • Memory pointers
  • Context data
  • I/O status information
  • Accounting information
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

where is the process context stored?

A

– User level context is stored in memory
– Other context information is stored in a data structure called process control block (PCB)
* Contains other information that the OS needs to manage the
process
– Process status (running, waiting, etc.)
– Process priority
– …
– The OS has a process control block table
* For each process, there is one entry in the table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Process Control Block (PCB)

A

Information associated with each process (also called task control block)
* Process state
– Running, waiting, etc.
* Program counter
– Location of instruction to execute next
* 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

CPU Scheduler (short-term scheduler)

A

– Selects which processes should be executed next and allocates the CPU
– Invoked very frequently (milliseconds)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Processes can be described as either

A

– I/O-bound
* Spends more time doing I/O than computations; many short CPU bursts
– CPU-bound
* Spends more time doing computations; few very long CPU bursts

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Job Scheduler (long-term scheduler)

A

– Selects which processes to be brought into ready queue
– Invoked very infrequently (seconds, minutes)
– Controls degree of multiprogramming, the max number of processes accommodate efficiently

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Maintains scheduling queues of processes

A

– Job queue
* Set of all PCBs in the system
– Ready queue
* Set of all processes residing in main memory, ready and waiting to execute
– Device queues
* Set of processes waiting for an I/O device
– Processes migrate among the various queues

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Long- and Short-Term Schedulers

A

Ready queue
Set of all processes residing in main memory, ready and waiting to execute

Long-term scheduler
Selects which processes to be brought into ready queue

CPU Scheduler (short-term scheduler)
Selects which processes should be executed next and allocates the CPU

Device or I/O waiting queues
Set of processes waiting for an I/O device

27
Q

Context Switch

A
  • When CPU switches to another process, the OS must
    – Save the PCB of old process being swapped out
    – Select new process to be swapped in
    – Load the PCB of the new process being swapped in
  • Context-switch time is overhead
    – The system does no useful work while switching
  • Typical time about 1 microsecond
    – The more complex the OS and the PCB, the longer the context switch
  • Time dependent on hardware support
    – Some hardware provides multiple sets of registers per CPU, resulting in multiple contexts loaded at once
28
Q

Calling one of the exec() family of system calls

A

– Terminates the currently running program, and
– Replaces it with a new specified program that starts executing in its main () function

  • The process ID does not change across an exec because a new process is not created
  • exec() merely replaces the current process (its text, data, heap, and stack segments) with a brand new program from disk
29
Q

The exec() Family

A
  • There are 6 versions of the exec function, and they all do
    about the same thing:
    – Main difference is how parameters are passed
30
Q

the six exec() functions

A

int execl(const char *path, const char *arg, … );
– execl takes full path name of command and variable length of
arguments terminated by NULL
execl(“/bin/ls”, “/bin/ls”, “-r”, “-t”, “-l”, NULL);

int execlp(const char *file, const char *arg, … );
– execlp will try to find the command from $PATH, so full path to command not needed
execlp(“ls”, “ls”, “-r”, “-t”, “-l”, NULL);

int execle(const char *path, const char *arg, …,
char *const envp[] );
– execle uses an argument list and environment variables
char *env[] = {“PATH=/bin”, NULL};
execle(“child.exe”, “child”, “arg1”, “arg2”, NULL, env);

int execv(const char *path, char *const argv[]);
– execv is the equivalent of execl, except that the arguments are passed in as a NULL terminated array
char *args[] = {“/bin/ls”, “-r”, “-t”, “-l”, NULL};
execv(“/bin/ls”, args);

int execvp(const char *file, char *const argv[]);
– execvp is the equivalent of execlp, except that the arguments are passed in as a NULL terminated array
char *args[] = {“ls”, “-r”, “-t”, “-l”, NULL};
execvp(“ls”, args);

int execve(const char *filename, char *const
argv [], char *const envp[] );

31
Q

commonalities in the exec() family

A
  • All six return –1 on error, but no return on success
  • Accept either a pathname or filename argument
  • Command-line arguments are specified as separate arguments or we have to build an array of pointers to the arguments and pass the address of the array
32
Q

Process Creation

A
  • A parent process creates child processes, which create other processes, forming a tree of processes
    – Generally, processes identified and managed via a process identifier (PID)
  • Execution options
    – Parent and children execute concurrently
    – Parent waits until children terminate
33
Q

The fork() System Call

A
  • Creation of a new process accomplished using the fork() system call
    – fork() creates a child process by making an exact copy of the parent process and then starts the process concurrently
    – fork() system call is unique
  • Called once, but returns twice with the parent receiving the child’s unique PID and the child receiving 0 as the return value from fork()
    – The process that initiates the fork() becomes the parent process of the newly created child process
  • The child process inherits a copy of the parent’s memory space, but they do not share the same memory as both parent and child processes will execute in their own environments
34
Q

what the fork() system call returns

A
  • Current process splits into two processes: parent, child
    – Returns -1 if unsuccessful
    – Returns 0 in the child
    – Returns the child’s PID in the parent
35
Q

Why we use fork()

A
  • There are two uses for fork:
    – When a process wants to duplicate itself so that the parent and child can each execute different sections of code at the same time
  • This is common for network servers
    – the parent waits for a service request from a client
    – when the request arrives, the parent calls fork() and lets the child handle the request
    – parent goes back to waiting for the next service request to arrive

– When a process wants to execute a different program
* This is common for shells
* In this case, the child typically does an exec() right after it returns from the fork

36
Q

Why does fork() fail

A
  • The two main reasons for fork to fail are
    – If there are already too many processes in the system (which usually means something else is wrong)
    – If the total number of processes for this real user ID exceeds the system’s limit
    cat /proc/sys/kernel/pid_max — > Max unique PIDs the system supports
37
Q

Why Process synchronization must be
implemented?

A
  • To prevent data inconsistency among processes
  • To prevent process deadlocks
  • To prevent race conditions (i.e. when two or more operations are executed at the same time)
  • When you start a program
    – it is loaded and given an address space in main memory from your hard disk
    – Next, it is scheduled onto an available CPU
    – Next, the process instructions are executed simultaneously
38
Q

wait() and waitpid() System Calls

A
  • The wait() and waitpid() system calls force the parent process to suspend execution until the child process has completed
  • Process suspension : Whenever the processes in main memory are entered into the blocked state, the OS suspends one process by putting it in the Suspended state and transferring it to disk. The free space present in the memory is used for bringing another process.
    – waitpid() waits for a specific child process identified by its PID while wait () simply waits for the first child process to terminate (if the parent has more than one child process)
    – Both return the PID of the terminated process if successful
  • Or –1 if an error occurred (usually means no child exists to wait on)
    – Once the child process has terminated, the parent process resumes execution
39
Q

Purpose of process suspension

A
  • Swapping
  • Timing
  • Interactive User request
  • Parent process request
  • Processes are typically in Ready, Running, Blocked states.
  • But there is also a suspended state.
  • If any process which is in the Ready state does not occupy the main memory, OS swaps one of the blocked processes out into the Suspend queue.
  • When a Suspended process is ready to run it moves into Ready, Suspend queue. Thus, we have two more states which are Blocked_Suspend and Ready_Suspend.
40
Q

wait() Function

A

include <sys/types.h>

#include <sys/wait.h>
pid_t wait(int *statloc);

  • statloc argument can have two values: 1.NULL pointer i.e. (int *)0 or 2.pointer to an integer i.e. a variable which will be bound to status information about the child
  • A process that calls wait() can
    – suspend (block) if all of its children are still running
    – return immediately with the termination status of a child
    – return immediately with an error if there are no child processes.
41
Q

The fork-exec Model

A
  • Common use of the fork() and exec() system calls centers around being able to run another program in parallel without terminating the current process
    – Parent process uses fork() to create the child process, which will then use the exec() family of system calls to run the desired program while the parent waits on the child to terminate
42
Q

Process Termination

A
  • A process may be terminated
    – When it executes its last statement and asks the operating system to delete it
    – By calling the exit() system call in the <stdlib.h> library</stdlib.h>
  • A parent may terminate execution of child processes using the abort() system call if
    – Child has exceeded allocated resources
    – Task assigned to child is no longer required
    – Parent is exiting
  • Some operating systems do not allow child to continue if its parent terminates
  • When a child process terminates, a SIGCHLD signal is sent to the parent
43
Q

Zombies and Orphans

A
  • A child process whose parent has terminated is referred to as an orphan
    – Child is still executing, but parent has terminated
    – Some process is needed to query the child’s exit status
  • When a child exits and its parent is not currently waiting (i.e., executing a wait()), it becomes a zombie
    – A zombie is not really a process (since it terminated), but the system still has an entry in the process table for the non-existing child process
  • When a parent terminates, any orphans and zombies are adopted by the init process (PID 1) of the system
44
Q

What happens when you Ctrl-C a program that created several children?

A

– Typically the program and its children terminate
– Why the children? (part of the signal handler definition)

45
Q

In addition to having a unique ID, a process also belongs to a process group

A

– Several processes can be members of the same process group
– When a process forks, the child inherits its process group from its parent
– A process can change its process group to a new value by using setpgid()
– When a process execs, its process group remains the same

46
Q

Process Groups

A
  • A “process group” is a logical cluster of processes that belong to the same application or deployment unit and perform the same function across multiple hosts.
  • The distribution of signals to process groups forms the basis of job control employed by shell programs.
  • Process groups are key building blocks of most modern web-based applications.
  • A session denotes a collection of one or more process groups.
47
Q

Process Identification

A
  • pid = getpid( );
    – Returns its own process id
  • pid = getppid( );
    – Returns parent process id
  • uid = getuid( );
    – Returns real user ID of own process
  • newpg = setpgrp( );
    – Sets process group of own process to itself
  • pgid = getpgrp( );
    – Returns the process group ID of own process
  • Each process is named by a process ID number, a value of type pid_t.
  • The lifetime of a process ends when the parent process of the corresponding process waits on the process ID after the process has terminated.
  • Process IDs can also denote process groups and sessions.
  • The processes belonging to a single command are called a process group. This is so that you can operate on all of them at once.
  • You can get the process ID of a process by calling getpid. The function getppid returns the process ID of the parent of the current process (this is also known as the parent process ID). Your program should include the header files unistd.h and sys/types.h to use these functions.
48
Q

Process Data

A
  • Since a child process is a copy of the parent, it has copies of the parent’s data
  • A change to a variable in the child will not change that variable in the parent
49
Q

Inherited Data and File Descriptors

A
  • A forked child has instances of current values of the variables and open file descriptors
  • Variables
    – Passed by value (i.e., a copy)
  • Read/write pointers for a file
    – Passed by reference
50
Q

Process File Descriptors

A
  • A child and parent have copies of the file descriptors, but the R-W pointer is maintained by the system
    – The R-W pointer is shared
  • This means that a read() or write() in one process will affect the other process since the R-W pointer is changed
51
Q

The specific attributes of the child process that differ from the parent process are:

A
  • The child process has its own unique process ID.
  • The parent process ID of the child process is the process ID of its parent process.
  • The child process gets its own copies of the parent process’s open file descriptors. Subsequently changing attributes of the file descriptors in the parent process won’t affect the file descriptors in the child, and vice versa. However, the file position associated with each descriptor is shared by both processes.
  • The elapsed processor times for the child process are set to zero.
52
Q

Process Termination

A

When a process terminates for any reason—either because the program terminates, or as a result of a signal—the following things happen:
* All open file descriptors in the process are closed. Note that I/O streams are not flushed automatically when the process terminates.
* A process exit status is saved to be reported back to the parent process via wait or waitpid. If the program exited, this status includes as its low-order 8 bits the program exit status.
* Any child processes of the process being terminated are assigned a new parent process. (On most systems, including GNU, this is the init process, with process ID 1.)
* A SIGCHLD signal is sent to the parent process.
* If termination of a process causes a process group to become orphaned, and any member of that process group is stopped, then a SIGHUP signal and a SIGCONT signal are sent to each process in the group.

53
Q

Process Control Block (PCB) contains the following information related to a process

Process ID
Program Counter
Process Priority
List of open files
Bootstrap program

A

Process ID
Program Counter
Process Priority
List of open files
(all but bootstrap)

54
Q

When a process places an I/O request, it is placed in the _________________

A

I/O Queue

55
Q

include <stdio.h></stdio.h>

Considering you don’t have an overloaded machine, how many times will this code print hello :

#include <sys/types.h>
#include <unistd.h>
int main()
{
fork();
fork();
fork();
printf("hello\n");
return 0;
}</unistd.h>

A

8 times

56
Q

fork() returns a 0 on failure (t/f)

A

true

57
Q

wait() or sleep() functions can introduce synchronization between concurrently executing processes (t/f)

A

true

58
Q

Signals can be generated from these sources.

A

hardware, keyboard and C program

59
Q

When a system call is made, the CPU will run in _____________ mode

A

supervisory

60
Q

Choose the modes of opening a file

O_RDONLY
O_RDWR
O_WRONLY
O_WDR

A

O_RDONLY
O_RDWR
O_WRONLY
(all but O_WDR)

61
Q

This is a collection of memory locations that a process can access.

A

virtual address space

62
Q

In UNIX/LINUX the file descriptor associated with your monitor or screen or the standard output is ___________

A

1

63
Q

Match the different sections of a process’s user-level context with the corresponding data stored

text
data
heap
stack

A

text
source code

data
static and global variables initialized at runtime

heap
dynamic memory allocated at runtime

stack
return addresses, function parameters, variables etc

64
Q

What is stored in a register during a context switch for a register-level context in processes?

A

The program counter (PC) and some processor status flags