System Programming
Lecture 4. Linux Tools and Introduction to Process
Instructor : Wookhee Kim
Department of Computer Science and Engineering
wookhee@konkuk.ac.kr
Some slides from Randal E. Bryant and David R.
O'Hallaron, Carnegie Mellon University
GDB Data-Centric Computing
Laboratory
• GNU Debugger (GDB) is a portable debugger
• runs on many Unix-like systems and works for many programming languages.
• Compile with –g option in GCC
• Example) gcc –o hello hello.c -g
• How to use
• $ gdb program
• Install : sudo apt install gdb
Reference: GNU Debugger - Wikipedia
System Programming - Lecture 4. Linux Tools and Introduction to Process 2
GDB - Example Data-Centric Computing
Laboratory
• What is the error?
#include <stdio.h>
int main(){
char *str;
printf("%s\n",str);
return 0;
}
System Programming - Lecture 4. Linux Tools and Introduction to Process 3
GDB – Example Data-Centric Computing
Laboratory
• run
System Programming - Lecture 4. Linux Tools and Introduction to Process 4
GDB - Example Data-Centric Computing
Laboratory
• list
System Programming - Lecture 4. Linux Tools and Introduction to Process 5
GDB - commands Data-Centric Computing
Laboratory
Command Abbreviation Description
gdb [executable] Starts gdb and loads the executable
run [cmdLineParms] r cmdLineParms Runs the loaded executable
list [point] l Lists several lines of code around/at a point. Points can be
l lineNbr line numbers, function names, or lines in a particular file,
l File:lineNbr absence of a point indicates “next few lines”
l function
break [point] b Sets a breakpoint at a point. This will stop execution at
b lineNbr this point. You can then view variables at that point or
b function perform other tasks.
continue c Run until next breakpoint or end
print variable p variableName Prints the value of a variable or the return value from a
print function p functionCall function call at the current line.
display var/func disp variableName Works just like print, except that it displays those values
disp functionCall every time you stop
watch variable wa variableName Pause execution whenever variable changes
next n Runs the next line of code, skips over functions
step s Runs to first line of code inside a function call
backtrace bt Allows you to see function call sequence that led to
where current line of code.
up Up takes you up one level
down Down takes you down one level
System Programming - Lecture 4. Linux Tools and Introduction to Process 6
quit q Quit gdb
Process Data-Centric Computing
Laboratory
• Program: a sequence or set of instructions in a programming language for
a computer to execute.
• Process: a running program
• include one or more threads
• Binary image
• Instance of virtualized memory
• Stack, register
• Thread: unit of execution in process
Reference: Computer program - Wikipedia
System Programming - Lecture 4. Linux Tools and Introduction to Process 7
Program vs. Process vs. Thread Data-Centric Computing
Laboratory
Concepts- Program vs. Process vs. Thread - Computer program - Wikipedia
System Programming - Lecture 4. Linux Tools and Introduction to Process 8
Process states Data-Centric Computing
Laboratory
Parent forks Child destroy
scheduled
Process is created
Process is ready
Process is running Exit
(Not running)
Event occurs interrupt Wait for event
Process is waiting
System Programming - Lecture 4. Linux Tools and Introduction to Process 9
Process Control Block Data-Centric Computing
Laboratory
• A data structure used by OS to store all the information for process
management
Process ID
Process State
Program Counter
Registers
Memory Info.
List of Open Files
Process Priority
I/O Status, etc…
System Programming - Lecture 4. Linux Tools and Introduction to Process 10
Virtual Address Space Data-Centric Computing
Laboratory
0xffffffff
Kernel virtual memory
0xc0000000
User stack
(created at runtime)
Memory mapped region for printf() function
shared libraries
0x40000000
Program Code and Data:
Initialized directly from
Run-time heap the contents of an
(created at runtime by malloc) executable object file
Read/write data
Loaded from the
hello executable file
Read-only code and data
0x08048000
0
Unused
System Programming - Lecture 4. Linux Tools and Introduction to Process 11
Virtual Address Space Data-Centric Computing
Laboratory
0xffffffff
Kernel virtual memory Shared Libraries: Code
0xc0000000 and data for shared
User stack libraries such as C
(created at runtime) standard library and
math library
Memory mapped region for printf() function
shared libraries
0x40000000
Run-time heap
(created at runtime by malloc)
Read/write data
Loaded from the
hello executable file
Read-only code and data
0x08048000
0
Unused
System Programming - Lecture 4. Linux Tools and Introduction to Process 12
Virtual Address Space Data-Centric Computing
Laboratory
0xffffffff
Kernel virtual memory
0xc0000000
User stack
(created at runtime)
Heap: Expands and
contracts dynamically at Memory mapped region for printf() function
run time as a result of shared libraries
calls to C standard0x40000000
library routines such as
malloc and free
Run-time heap
(created at runtime by malloc)
Read/write data
Loaded from the
hello executable file
Read-only code and data
0x08048000
0
Unused
System Programming - Lecture 4. Linux Tools and Introduction to Process 13
Virtual Address Space Data-Centric Computing
Laboratory
0xffffffff
Kernel virtual memory
0xc0000000
User stack
(created at runtime)
Stack: Top of the user’s
address space. Used to
implement function calls.
Expands and contracts Memory mapped region for printf() function
dynamically during the shared libraries
0x40000000
execution by function
calls
Run-time heap
(created at runtime by malloc)
Read/write data
Loaded from the
hello executable file
Read-only code and data
0x08048000
0
Unused
System Programming - Lecture 4. Linux Tools and Introduction to Process 14
Virtual Address Space Data-Centric Computing
Laboratory
0xffffffff Kernel is the part of the
Kernel virtual memory operating system that is
0xc0000000
User stack always resident in
(created at runtime) memory. Applications
are not allowed access
Memory mapped region for printf() function
shared libraries
0x40000000
Run-time heap
(created at runtime by malloc)
Read/write data
Loaded from the
hello executable file
Read-only code and data
0x08048000
0
Unused
System Programming - Lecture 4. Linux Tools and Introduction to Process 15
User View of Concurrent Processes Data-Centric Computing
Laboratory
Process A
Process B
Process C
Time
System Programming - Lecture 4. Linux Tools and Introduction to Process 16
Logical Control Flows Data-Centric Computing
Laboratory
Process A
Process B
Process C
Time
• Logical Control Flows
Process A
Process B
Process C
Time
System Programming - Lecture 4. Linux Tools and Introduction to Process 17
Context Switching Data-Centric Computing
Laboratory
Process A
Process C
Context Context
Switching Switching
• Control flow passes from one process to another via a context
switch
• Processes are managed by a shared chunk of OS code called the
kernel
• the kernel is not a separate process, but rather runs as part of some user
process
Time
System Programming - Lecture 4. Linux Tools and Introduction to Process 18
Obtaining Process IDs Data-Centric Computing
Laboratory
• pid_t getpid(void)
• Returns PID of current process
• pid_t getppid(void)
• Returns PID of parent process
System Programming - Lecture 4. Linux Tools and Introduction to Process 19
fork: Creating new processes Data-Centric Computing
Laboratory
• pid_t fork(void)
• Linux creates a new process as a child process of currently running process that calls
fork().
• New process : Child process
• Running process : parent process.
• Calls once Returns twice
• 0 to the child process
• child’s pid to the parent process
if (fork() == 0) {
printf(“I am child\n");
} else {
printf(“I am parent\n");
}
System Programming - Lecture 4. Linux Tools and Introduction to Process 20
errno Data-Centric Computing
Laboratory
• Number of last error
• The value is significant only when the return value of the call indicated an error
• A function that succeeds is allowed to change errno
• The value of errno is never set to zero by any system call or library function
System Programming - Lecture 4. Linux Tools and Introduction to Process 21
errno Data-Centric Computing
Laboratory
rpid = fork();
• perror() if (rpid < 0) {
• prints a system error message perror(“Fork failed:”);
return -1;
• Errors in fork() }
else if (rpid == 0) {
• EAGAIN
printf(“Child\n");
• A system-imposed limit on the }
number of processes was else {
encountered printf(“Parent\n");
• ENOMEM }
• Failed to allocate the necessary kernel structures because memory is tight
• ENOSYS
• fork() is not supported on this platform
System Programming - Lecture 4. Linux Tools and Introduction to Process 22
Process Graph Example Data-Centric Computing
Laboratory
int main()
{
pid_t pid;
int x = 1;
pid = fork(); child: x=2
if (pid == 0) { /* Child */ Child
printf exit
printf("child : x=%d\n", ++x);
exit(0); x==1 parent: x=0
Parent
} main fork printf exit
/* Parent */
printf("parent: x=%d\n", --x);
exit(0);
}
System Programming - Lecture 4. Linux Tools and Introduction to Process 23
Interpreting Process Graphs Data-Centric Computing
Laboratory
• Original graph:
child: x=2
printf exit
x==1 parent: x=0
main fork printf exit
Feasible total ordering:
• Relabled graph:
e f a b e c f d
a b c d Infeasible total ordering:
a b f c e d
System Programming - Lecture 4. Linux Tools and Introduction to Process 24
Homework2 Data-Centric Computing
Laboratory
• Due: 09/17 23:59pm
• Let’s write a code on slide 3.
• Use GDB to find a bug in the code.
• Upload the screenshot of your GDB that the bug detected.
System Programming - Lecture 4. Linux Tools and Introduction to Process 25
Online lecture Data-Centric Computing
Laboratory
• Please check the e-campus.
• Online lecture will be uploaded.
System Programming - Lecture 4. Linux Tools and Introduction to Process 26