You are on page 1of 25

Process Concept

Process Concept
■ An operating system executes a variety of programs:
● Batch system – jobs(or process)
● Time-shared systems – user programs or tasks

■ Process – a program in execution; process execution must progress in


sequential fashion

■ Multiple parts in a process address space


● The program code, also called text section
● Stack containing temporary data
• Function parameters, return addresses, local variables
● Data segment containing global variables
● Heap containing memory dynamically allocated during run time
Process Concept
■ Program is passive entity stored on disk (executable file), process is active
● Program becomes process when executable file loaded into memory

■ Execution of program started via GUI mouse clicks, command line entry of its
name, etc

■ One program can be several processes


● Consider multiple users executing the same program
Process in Memory
Process State
• The state of a process indicates its status at a particular time
• Most operating systems use the following process states:
– New: being created
– Running: instructions are being executed
– Blocked: waiting for an event such as I/O
– Ready: waiting to be assigned to a processor
– Done: finished execution (terminated)
normal or abnormal
runni termination
new selected done
to run ng

I/O request
process
created quantum
expired block
ready
ed
I/O complete
Process State (continued)
• While a program is undergoing the transformation into an active process, it is
said to be in the new state

• When the transformation completes, the operating system puts the process in
a queue of processes that are ready to run (the ready state)

• Eventually the process scheduler selects a process to run; when the process
is actually executing on the CPU, it is in the running state

6
Process State (continued)

• A process in the blocked state is waiting for an event and is not eligible to be
picked for execution
– A process can voluntarily move to the blocked state by executing a call
such as sleep()
– More commonly, a process moves to the blocked state when it performs
an I/O request via a system call

• A context switch is the act of removing one process from the running state
and replacing it with another
– The process context is the information that the operating system needs
about the process and its environment to restart it after a context switch
– It includes such things as the executable code, stack, registers, program
counter, and the memory used for static and dynamic variables

7
Process Control Block (PCB)
Information associated with each process (also
called task control block)

■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
CPU Switch From Process to
Process
ps Utility
• The ps utility displays information about processes currently handled by the
OS

• The ps utility is executed at a UNIX shell prompt; by default it displays


information about processes for the current user. (Options offer other output
forms.)

• Column headings
UID – user ID STIME – starting time of the process
PID – process ID TTY – controlling terminal
PPID – parent process ID TIME – cumulative execution time
C – (obsolete) CMD – command name

10
ps Utility
• Example output produced by ps -ef

UID PID PPID C STIME TTY TIME CMD


root 0 0 0 May 20 ? 0:02 sched
root 1 0 0 May 20 ? 2:38 /etc/init –
root 2 0 0 May 20 ? 0:00 pageout
root 3 0 1 May 20 ? 173:15 fsflush
root 433 1 0 May 20 console 0:00 /usr/bin/login
root 13259 436 0 13:54:49 ? 0:00 /usr/lib/ssh/sshd
jjt107 13603 13261 0 13:55:29 pts/3 0:00 ps –ef
root 2017 2398 0 May 22 pts/5 0:00 sh
root 4210 5144 0 May 20 pts/1 0:00 sh
dan 28527 28500 0 May 22 pts/6 0:00 –sh
jjt107 13261 13259 0 13:54:59 pts/3 0:00 –csh
root 28499 436 0 May 22 ? 0:00 /usr/lib/ssh/sshd
root 3110 436 0 May 25 ? 0:00 /usr/lib/ssh/sshd
root 11090 436 0 May 23 ? 0:00 /usr/lib/ssh/sshd

11
Creating a Process
UNIX Process Creation
• A process creates another process by calling the fork() function
– The calling process is called the parent and the created process is
called the child
– The fork function copies the parent's memory image so that the new
process receives a copy of the address space of the parent
– Both processes continue at the instruction directly after the statement
containing the fork() call (executing in their respective memory
images)

#include <unistd.h>

pid_t fork(void);

• The return value from the fork() function is used to determine which
process is the parent and which is the child; the child gets the value 0 while
the parent gets the child's process ID
– When the fork() call fails, it returns –1 and sets errno (a child is not
created)
Fork Example (Simple)
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main(void)
{
pid_t childPID;

childPID = fork();
if (childPID == -1)
{
perror("Fork failed");
return 1;
} // End if
else if (childPID == 0) // This is the child
{
printf("My PID is %ld; I am the child\n", getpid());
} // End else if
else // This is the parent
{
printf("My PID is %ld; I am the parent of %ld\n", getpid(),
childPID);
} // End else
return 0;
} // End main
3.4 The wait() Function
The wait() Function
• When a process creates a child, both parent and child proceed with
execution from the point of the fork

• The parent process can run the wait() or waitpid() functions to block its
execution until the child process finishes

• The wait() function causes the caller (i.e., the parent) to suspend
execution until a child's status becomes available or until the caller receives
a signal
– A process status most commonly becomes available after process
termination

#include <sys/wait.h>

pid_t wait(int *status_location);

• It takes one parameter, a pointer to the location for returning the status of the
process. The function returns either the process ID that terminated or –1
(and sets errno)

16
The wait() Function
• If a child process terminates and its parent does not wait for it, it becomes a
zombie
– Zombies stay in the system(process table) until they are waited for
– I.e the process that has completed execution but still has an entry in the
process table

• If a parent terminates without waiting for a child, the child becomes an


orphan and is adopted by a special system process
– Traditionally, this process is called init and has process ID of 1; it
periodically waits for children, so eventually orphaned zombies are
removed
– i.e process is still executing, but whose parent has died

17
Overlaying a Child Process
The exec Family of Functions
• The fork() function creates a copy of the calling process, but many
applications require the child process to execute code that is different
than that of the parent

• The exec family of functions provides a facility of overlaying the


process image of the calling process with a new image
– Usually the parent continues running the same code after the fork
() call, while the child process runs the new program (by means of
an exec function call)
The exec Family of Functions
• There are six variations of the exec function
– Each differ in the way command-line arguments and the
environment are passed
– They also differ in whether a full pathname must be given for the
executable

• All exec functions returns –1 and set errno if unsuccessful

• The execl, execlp, and execle functions pass the command-line


arguments in an explicit list and are useful if the programmer knows the
number of command line arguments at compile time

• The execv, execvp, and execve functions pass the command-line


arguments in an argument array
execl() Example
#include <stdio.h>
#include <stdlib.h> This program creates a child process that
#include <unistd.h>
#include <sys/wait.h> runs the ls utility using the "-l" option. It
displays a long listing of the contents of the
int main (void)
{
current working
pid_t childpid; directory
childpid = fork();
if (childpid == -1)
{
perror("Fork failed");
return 1;
} // End if

if (childpid == 0)
• "/bin/ls" const char *path
{ • "ls" const char *arg0
execl("/bin/ls", "ls", "-l", NULL);
perror("Child failed when running execl");
• "-1" const char *arg1
return 1; • NULL NULL pointer
} // End if

if (childpid != wait(NULL))
{
perror("Parent failed to wait due to signal or error");
return 1;
} // End if

return 0;
} // End main
exec Functions (Behind the scenes)
• Each of the exec functions copies a new executable into the process
image
• The program text, variables, stack and heap are overwritten
• The new process inherits the environment (i.e., the environment
variables and values) unless the original process called execle() or
execve()
• Files that are open at the time of an exec call are usually still open
afterward
• Many attributes are inherited by a process after an exec call; below are
some of them
– process ID
– parent process ID
– current working directory
– time used so far
– resource limits
– controlling terminal
execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", NULL );

execlp("ls", "ls", "-r", "-t", "-l",NULL );

char *args[] = {"/bin/ls", "-r", "-t", "-l", NULL};


execv("/bin/ls", args)

char *args[] = {"ls", "-r", "-t", "-l", NULL};


execvp("ls", args);
exec System Call Functionality

Pass Current
Library Call Search PATH
Argument List Environment
Name automatic?
Variables
execl list yes no
execv array yes no
execle list no no
execve array no no
execlp list yes yes
execvp array yes yes
Things to remember about exec*

• this system call simply replaces the current process with a new
program -- the pid does not change
• the exec() is issued by the calling process and what is exec'ed is
referred to as the new program -- not the new process since no new
process is created
• it is important to realize that control is not passed back to the calling
process unless an error occurred with the exec() call
• in the case of an error, the exec() returns a value back to the calling
process
• if no error occurs, the calling process is lost

You might also like