CS312 Lec 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

SYSTEM

PROGRAMMING
LECTURE # 6
PROCESS CREATION-I
Course Code: CS 312
Course Instructor: Dr. Sarah Iqbal

1
AGENDA FOR TODAY

 Overview of Processes in Linux


 The /proc directory
 Accessing Process Identifications
 Modifying Process Identifications
 Overview of fork(), exit(), wait(), and execve()
 Race Condition after fork()
 Waiting for a Process

2
WHAT IS A PROCESS

 The UNIX standards, specifically IEEE Std 1003.1, 2004


Edition, defines a process as “an address space with
one or more threads executing within that address
space, and the required system resources for those
threads.”
A Process is an entity that can be assigned to and
executed by a Processor.

3
PROCESS
 A multitasking operating system such as Linux lets many programs run
at once. Each instance of a running program constitutes a process.

 As a multiuser system, Linux allows many users to access the system at


the same time. Each user can run many programs, or even many
instances of the same program, at the same time. The system itself runs
other programs to manage system resources and control user access.

 a program or process — that is running consists of program code, data,


variables (occupying system memory), open files (file descriptors), and
an environment. Typically, a Linux system will share code and system
libraries among processes so that there’s only one copy of the code in
memory at any one time.

4
PROCESS STRUCTURE

PCB-task struct

struct task_struct

5
PS COMMAND

 ps -- process status
 processeslives in user space and we can explore the
contents of user space using this command

 ps -a -- to view the running processes


 ps -l –to view the different attributes of the process

6
VIEWING PROCESSES
 top – top command monitors the CPU usage in real time and refreshes its output
after few seconds. This top command is getting its information from proc directory
 /proc - window to the running linux kernel
 echo $$ -- to see the pid of the current shell (can enter in this directory to view
status). On a shell you can get the PID of the shell in the environment variable in $$
and the parent ID in environment variable PPID
 The parent of any process can be found by looking at /proc/PID/stat file. (See man
page of proc for details)
 The swapper or scheduler is a system process having a PID of 0. It manages
memory allocation for processes, swaps processes from run state to Ready Queue
or other and may be to disk. No program file for swapper in /proc/ directory
 The init, now systemd is a user process having a PID of 1. It is invoked by the kernel
at the end of the booting process.
 7
Page daemon now kthreadd is a system process having a PID of 2. It support the
paging of virtual memory system
PS COMMAND…

 Each process is allocated a unique number, called a process identifier


or PID.
 The program code that will be executed by the grep command is
stored in a disk file. Normally, a Linux process can’t write to the memory
area used to hold the program code, so the code is loaded into
memory as read-only (this area can’t be written to, it can safely be
shared).
 The system libraries can also be shared. Thus, there need be only one
copy of printf, for example, in memory, even if many running programs 8
call it.
PROCESS STRUCTURE

 A process has its own stack space, used for local variables in functions
and for controlling function calls and returns. It also has its own
environment space, containing environment variables that may be
established solely for this process to use

 A process must also maintain its own program counter, a record of


where it has gotten to in its execution, which is the execution thread.

 On many Linux systems, and some UNIX systems, there is a special set of
“files” in a directory called /proc. These are special in that rather than
being true files they allow you to “look inside” processes while they are
running as if they were files in directories.

9
PROCESS TABLE
 The Linux process table is like a data structure describing all of the
processes that are currently loaded, for example, their PID, status, and
command string, the sort of information output by ps.

 The operating system manages processes using their PIDs, and they are
used as an index into the process table.

 The table is of limited size, so the number of processes a system will


support is limited.

 Early UNIX systems were limited to 256 processes. More modern


implementations have relaxed this restriction considerably and may be
limited only by the memory available to construct a process table entry
10
VIEWING PROCESSES

 This shows information about many processes.


 The TTY column shows which terminal the process was started from.
 TIME gives the CPU time used so far, and the CMD column shows the
command used to start the process.

 By default, the ps program shows only processes that maintain a


connection with a terminal, a console, a serial line, or a pseudo
terminal. Other processes run without needing to communicate with a11
user on a terminal.
SYSTEM PROCESSES

12
PROCESS SCHEDULING
 The status indicator shows only that the program is ready to run, not
necessarily that it’s actually running. On a single-processor
computer, only one process can run at a time, while others wait
their turn. These turns, known as time slices, are quite short and give
the impression that programs are running at the same time
 The Linux kernel uses a process scheduler to decide which process
will receive the next time slice. It does this using the process priority.
Processes with a high priority get to run more often, whereas others,
such as low-priority background tasks, run less frequently.
 WithLinux, processes can’t overrun their allocated time slice. They
are preemptively multitasked so that they are suspended and
resumed without their cooperation.
13
PROCESS SCHEDULING…
 In a multitasking system such as Linux where several programs are likely to
be competing for the same resource, programs that perform short bursts
of work and pause for input are considered better behaved than those
that hog the processor by continually calculating some value or
continually querying the system to see if new input is available.
 Well-behaved programs are termed nice programs, and in a sense this
“niceness” can be measured. The operating system determines the
priority of a process based on a “nice” value, which defaults to 0, and on
the behavior of the program.
 Programs that run for long periods without pausing generally get lower
priorities.
 Programs that pause while, for example, waiting for input, get rewarded.
This helps keep a program that interacts with the user responsive; while it is
waiting for some input from the user, the system increases its priority, so
that when it’s ready to resume, it has a high priority.
14
STARTING NEW PROCESS

15
STARTING NEW PROCESS

16
OVERVIEW OF
FORK(), EXIT(), WAIT(),
AND EXECVE()

17
FORK() SYSTEM CALL

 Thefork() system call allows one process, the parent, to


create a new process, the child.
 This
is done by making the new child process an (almost)
exact duplicate of the parent: the child obtains copies of
the parent’s stack, data, heap, and text segments.

 Theterm fork derives from the fact that we can envisage


the parent process as dividing to yield two copies of itself.
18
EXIT(STATUS) LIBRARY FUNCTION

 The exit(status) library function terminates a process,


making all resources (memory, open file descriptors, and
so on) used by the process available for subsequent
reallocation by the kernel.
 Thestatus argument is an integer that determines the
termination status for the process.
 Using the wait() system call, the parent can retrieve this
status.
19
WAIT(&STATUS) SYSTEM CALL

 The wait(&status) system call has two purposes.


 First, if a child of this process has not yet terminated by
calling exit(), then wait() suspends execution of the
process until one of its children has terminated.
 Second, the termination status of the child is returned in
the status argument of wait().

20
EXECVE(PATHNAME, ARGV, ENVP) SYSTEM CALL

 The execve(pathname, argv, envp) system call loads a


new program (pathname, with argument list argv, and
environment list envp) into a process’s memory.
 Theexisting program text is discarded, and the stack,
data, and heap segments are freshly created for the new
program.
 This
operation is often referred to as execing a new
program.

21
22
PROCESS CREATION

23
FORK()

 The fork() system call allows one process, the parent, to create a new process, the child
 It is a system call which is called once but return twice, once in the parent and once in the
child. To the parent process it returns PID of child process and to the child process it returns
zero. After the call returns both parent and child processes continues their execution
concurrently from the next line of code
 The child process is a clone of the parent and obtains copies of the parent’s stack, data,
heap,and text segments
 PIDs are allocated sequentially to the new child processes, so effectively unique (but do
wrap up after a very long time)
 On success, the return value to the child process is 0 and the return value to the parent process
is PID of the child
 24
On failure, a -1 will be returned in the parent context, no child process created, and errno will be
set appropriately
PROCESS
CREATION

25
WHY FORK() IS CALLED ONCE BUT RETURN TWICE?

Return value to the child process is 0, because 0 is


the ID of swapper process only and a child
process can always call getppid() to obtain its
parent's ID
Returnvalue to the parent process is PID of child,
because a process can have more than one child

26
REASONS OF FAILURE

 Three main reasons of failure:


 EAGAIN: A system-imposed limit on the number of processes
was encountered. There are number of factors that can
trigger this, e.g., Number of processes under one user has
reached, Kernel limit on total number of processes has
reached
 ENOMEM: Failed to allocate the necessary kernel structures
because memory is tight
 ERESTARTNOINTR: System call interrupted by a signal and will
be restarted
27
28
REASONS FOR PROCESS CREATION

 When a process wants to duplicate itself, so that parent & child


each can execute different sections of code concurrently.
Example: Consider a network server; parent waits for a service
request from a client. When the request arrives, parent calls fork
& let the child handle the request. Parent goes back to listen for
the next request
 When a process wants to execute a different program. This is
common for command shells where the child does an exec()
right after it returns from the fork()
29
RACE CONDITION AFTER A FORK()
 After a fork() it is indeterminate which process – the parent or
child will execute. On a multiprocessor system they may both get
simultaneous access to the CPU. In most of the cases, the parent
will execute first. The two options are:
➔ Parent first after fork()
➔ Child first after fork()
 In Linux 2.6.32, since parent's state is already active in the CPU
and its memory management information is already cached,
therefore, running the parent first should result in better
performance
30
ATTRIBUTES INHERITED AFTER FORK()

 Real, effective and saved UIDs / GIDs


 Open file descriptors (PPFDT)
 Environment variables
 Present working directory
 Nice value
 File mode creation mask (umask)
 Signal mask and signal disposition
 Attached shared memory segments
31
DIFFERENCE BETWEEN PARENT & CHILD AFTER FORK()

 Child has different PID and PPID


 Return value from fork
 Child time for CPU usage are reset to 0
 Filelocks held by the parent (using fcntl()) are not inherited by
the child
 Set of pending Alarms in the parent are cleared in the child
 Set of pending Signals in the parent are cleared in the child

32
WAITING FOR A PROCESS

 When you start a child process with fork, it takes on a life of its own
and runs independently.
 Sometimes, you would like to find out when a child process has
finished. You can arrange for the parent process to wait until the
child finishes before continuing by calling wait.
 The wait system call causes a parent process to pause until one of
its child processes is stopped. The call returns the PID of the child
process. This will normally be a child process that has terminated.
 The status information allows the parent process to determine the
33
exit status of the child process, that is, the value returned from main
or passed to exit.
34
35
READING & REFERENCES

 BeginningLinux® Programming 4th Edition By Neil Matthew


& Richard Stones
Chapter 11: Processes and Signals
 The Linux Programming Interface by Michael KerrisK
Chapter 24: PROCESS CREATION
Section: 24.1, 24.2, 24.4

36

You might also like