CS312 Lec 6
CS312 Lec 6
CS312 Lec 6
PROGRAMMING
LECTURE # 6
PROCESS CREATION-I
Course Code: CS 312
Course Instructor: Dr. Sarah Iqbal
1
AGENDA FOR TODAY
2
WHAT IS A PROCESS
3
PROCESS
A multitasking operating system such as Linux lets many programs run
at once. Each instance of a running program constitutes a process.
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
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…
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
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.
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
20
EXECVE(PATHNAME, ARGV, ENVP) SYSTEM CALL
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?
26
REASONS OF FAILURE
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
36