KEMBAR78
Process (OS),GNU,Introduction to Linux oS | PPTX
LINUX
PROGRAMING
By: Sapthami Chatra
Harshith Raj
OPERATING SYSTEM
A program that acts as an intermediate between the user of the computer and the
computer hardware.
Operating system provides an environment where a user can execute their programs.
OPERATING SYSTEM SERVICES:
• File Management
• I/O Management
• Memory Management
• Device Management
• Resoure Management
• CPU Management
• Hardware Mangement
WHY LINUX?
● Open source
● Cost
● Customization
● Security
● Stability
● Performance
● Compatibility
● Software Availability
Compiling with GCC
The compilers of choice on Linux systems are all part of the GNU Compiler Collection,
usually known as GCC. GCC also include compilers for C, C++,Java, Objective-C,
Fortran, and Chill.
File name-pgm1.c
Let’s begin simple example
#include<stdio.h>
int main(){
printf(“lets begin
linux programingn”);
return 0;
}
Compile this code using gcc:
gcc –c pgm1.c –o pgm1
Output:
Compiling with GCC
Compiling multiple source file:
File name-main.c File name-add.cpp headerfile name-add.h
#include <stdio.h>
#include “add.h”
int main(int argc,char ** argv){
int i,j;
i=atoi(argv[1]);
j=atoi(argv[2]);
printf(“the addition of 2
numbers is %dn”,add(i,j);
return 0;
}
Compilation can be done as:
g++ -c main.c add.cpp –o add
#include<stdio.h>
#include “add.h”
Int add(int i,int j){
return i+j;
}
Int add(int i,int j);
Automating the Process with GNU
Make
EXECUTABLE: Object file 1, Object
file 2
<TAB> commands
Object file 1: Source file 1, Source fil
e 2
<TAB> commands
Object file 2: Source file 2
<TAB> commands
Syntax for make command:
Target : dependencies
commands
Make command:
Consider an example: File name-text_head.h
File name-int_main.c
File name-text_file.c
Using make:
Modify the file-text_file.c:
Clean the object files and executable file name
make command:
● The debugger is the program that we use to figure out why our program isn’t behaving the way we think
it should.
● We can use GDB to step through our code, set breakpoints, and examine the value of local variables.
Compiling with Debugging Information:
make CFLAGS=-g
gcc -g -c main.c
g++ -g -c reciprocal.cpp
g++ -g –-output reciprocal main.o reciprocal.o
Running GDB
You can start up gdb by typing:
% gdb add
When gdb starts up, you should see the GDB prompt:
(gdb)
GNU Debugger
● run our program inside the debugger
● (gdb) run
● (gdb) break main
Gnu Debugger
Starting program: reciprocal Program
received signal SIGSEGV, Segmentation
fault. __strtol_internal (nptr=0x0,
endptr=0x0, base=10, group=0) at
strtol.c:287 287 strtol.c: No such file or
directory. (gdb)
Breakpoint 1 at 0x804862e: file
main.c, line 8.
● (gdb) run 4 5
Starting program:add 4 5
Breakpoint 1, main (argc=2,
argv=0xbffff5e4) at main.c:8 8 i = atoi
(argv[1]);
● (gdb) next
Addition of 2 numbers is 9
● The exit code is a small integer; by convention, an exit code of zero
denotes successful execution, while nonzero exit codes indicate that
an error occurred. Some programs use different nonzero exit code
values to distinguish specific errors.
Program Exit Codes
The Environment
Syntax:
export NAME=value
Some common environment variables are:
Consider an example:
Environment variables:
Step 4: Create static library.
3
2
Static library
Step 1:Create a C file that contains
functions in your library
Step 2:Create a header file for the
library
4
Step 3: Compile library files.
1
Step 1: Create a C file with main
function
Step 2:Compile the driver
program.
Let’s create a program that uses static library
Step 3:Link the compiled driver
program to the static library.
Step 4:Run the driver program
1 2
3 4
● Compile library files: gcc –fpic -c add.c
● Create dynamic library: gcc –shared –o libbasic.so add.o
● Compile the executable file: gcc –c demo.c
● Generate the executable file by linking with the library: gcc –o demo demo.o libbasic.so
Dynamic library:
PROCESS
“Running instance of a program is
called a PROCESS.”
Process v/s Program
 A program is a passive entity, such as file containing a list of
instructions stored on a disk.
 Process is a active entity, with a program counter specifying
the next instruction to execute and a set of associated
resources.
 A program becomes a process when an executable file is
loaded into main memory.
Process - State Transition Diagram
Context switching
Creating Processes
1.Using System
 It creates a sub-process running the standard
shell.
 Hands the command to that shell for execution.
 Because the system function uses a shell to
invoke your command, it's subject to the
features and limitations of the system shell.
 The system function in the standard C library is
used to execute a command from within a
program.
 Much as if the command has been typed into a
shell
#include<stdlib.h>
int main()
{
int return_value;
return_value = system("ls -
l");
return return_value;
}
Creating Processes (cont.)
2.Using Fork and Exec
 Fork makes a child process that is an
exact copy of its parent process.
 When a program calls fork, a duplicate
process, called the child process, is
created.
 The parent process continues executing
the program from the point that fork was
called.
 The child process, too, executes the
same program from the same place.
 All the statements after the call to fork
will be executed twice, once, by the
parent process and once by the child
process
(A)
(B)
Creating Processes (cont.)
Process Scheduling
 Linux schedules the parent and child processes independently; there’s
no guarantee of which one will run first, or how long it will run before
Linux interrupts it and lets the other process (or some other process
on the system) run.
Signals
 A signal is a special message sent to a
process.
 Signals are asynchronous; when a
process receives a signal, it processes
the signal immediately, without finishing
the current function or even the current
line of code.
 There are several dozen different
signals, each with a different meaning.
 Each signal type is specified by its
signal number, but in programs, you
usually refer to a signal by its name.
Process Termination
 When a parent forks a child, the two process can
take any turn to finish themselves and in some
cases the parent may die before the child.
 In some situations, though, it is desirable for the
parent process to wait until one or more child
processes have completed.
 This can be done with the wait() family of system
calls.
 These functions allow you to wait for a process to
finish executing, enable parent process to retrieve
information about its child’s termination
THANK
YOU

Process (OS),GNU,Introduction to Linux oS

  • 1.
  • 2.
    OPERATING SYSTEM A programthat acts as an intermediate between the user of the computer and the computer hardware. Operating system provides an environment where a user can execute their programs. OPERATING SYSTEM SERVICES: • File Management • I/O Management • Memory Management • Device Management • Resoure Management • CPU Management • Hardware Mangement
  • 3.
    WHY LINUX? ● Opensource ● Cost ● Customization ● Security ● Stability ● Performance ● Compatibility ● Software Availability
  • 4.
    Compiling with GCC Thecompilers of choice on Linux systems are all part of the GNU Compiler Collection, usually known as GCC. GCC also include compilers for C, C++,Java, Objective-C, Fortran, and Chill. File name-pgm1.c Let’s begin simple example #include<stdio.h> int main(){ printf(“lets begin linux programingn”); return 0; } Compile this code using gcc: gcc –c pgm1.c –o pgm1 Output:
  • 5.
    Compiling with GCC Compilingmultiple source file: File name-main.c File name-add.cpp headerfile name-add.h #include <stdio.h> #include “add.h” int main(int argc,char ** argv){ int i,j; i=atoi(argv[1]); j=atoi(argv[2]); printf(“the addition of 2 numbers is %dn”,add(i,j); return 0; } Compilation can be done as: g++ -c main.c add.cpp –o add #include<stdio.h> #include “add.h” Int add(int i,int j){ return i+j; } Int add(int i,int j);
  • 6.
    Automating the Processwith GNU Make EXECUTABLE: Object file 1, Object file 2 <TAB> commands Object file 1: Source file 1, Source fil e 2 <TAB> commands Object file 2: Source file 2 <TAB> commands Syntax for make command: Target : dependencies commands
  • 7.
    Make command: Consider anexample: File name-text_head.h File name-int_main.c File name-text_file.c Using make:
  • 8.
    Modify the file-text_file.c: Cleanthe object files and executable file name make command:
  • 9.
    ● The debuggeris the program that we use to figure out why our program isn’t behaving the way we think it should. ● We can use GDB to step through our code, set breakpoints, and examine the value of local variables. Compiling with Debugging Information: make CFLAGS=-g gcc -g -c main.c g++ -g -c reciprocal.cpp g++ -g –-output reciprocal main.o reciprocal.o Running GDB You can start up gdb by typing: % gdb add When gdb starts up, you should see the GDB prompt: (gdb) GNU Debugger
  • 10.
    ● run ourprogram inside the debugger ● (gdb) run ● (gdb) break main Gnu Debugger Starting program: reciprocal Program received signal SIGSEGV, Segmentation fault. __strtol_internal (nptr=0x0, endptr=0x0, base=10, group=0) at strtol.c:287 287 strtol.c: No such file or directory. (gdb) Breakpoint 1 at 0x804862e: file main.c, line 8. ● (gdb) run 4 5 Starting program:add 4 5 Breakpoint 1, main (argc=2, argv=0xbffff5e4) at main.c:8 8 i = atoi (argv[1]); ● (gdb) next Addition of 2 numbers is 9
  • 11.
    ● The exitcode is a small integer; by convention, an exit code of zero denotes successful execution, while nonzero exit codes indicate that an error occurred. Some programs use different nonzero exit code values to distinguish specific errors. Program Exit Codes
  • 12.
    The Environment Syntax: export NAME=value Somecommon environment variables are:
  • 13.
  • 14.
    Step 4: Createstatic library. 3 2 Static library Step 1:Create a C file that contains functions in your library Step 2:Create a header file for the library 4 Step 3: Compile library files. 1
  • 15.
    Step 1: Createa C file with main function Step 2:Compile the driver program. Let’s create a program that uses static library Step 3:Link the compiled driver program to the static library. Step 4:Run the driver program 1 2 3 4
  • 16.
    ● Compile libraryfiles: gcc –fpic -c add.c ● Create dynamic library: gcc –shared –o libbasic.so add.o ● Compile the executable file: gcc –c demo.c ● Generate the executable file by linking with the library: gcc –o demo demo.o libbasic.so Dynamic library:
  • 17.
  • 18.
    “Running instance ofa program is called a PROCESS.”
  • 19.
    Process v/s Program A program is a passive entity, such as file containing a list of instructions stored on a disk.  Process is a active entity, with a program counter specifying the next instruction to execute and a set of associated resources.  A program becomes a process when an executable file is loaded into main memory.
  • 20.
    Process - StateTransition Diagram
  • 21.
  • 22.
    Creating Processes 1.Using System It creates a sub-process running the standard shell.  Hands the command to that shell for execution.  Because the system function uses a shell to invoke your command, it's subject to the features and limitations of the system shell.  The system function in the standard C library is used to execute a command from within a program.  Much as if the command has been typed into a shell #include<stdlib.h> int main() { int return_value; return_value = system("ls - l"); return return_value; }
  • 23.
    Creating Processes (cont.) 2.UsingFork and Exec  Fork makes a child process that is an exact copy of its parent process.  When a program calls fork, a duplicate process, called the child process, is created.  The parent process continues executing the program from the point that fork was called.  The child process, too, executes the same program from the same place.  All the statements after the call to fork will be executed twice, once, by the parent process and once by the child process (A) (B)
  • 24.
  • 25.
    Process Scheduling  Linuxschedules the parent and child processes independently; there’s no guarantee of which one will run first, or how long it will run before Linux interrupts it and lets the other process (or some other process on the system) run.
  • 26.
    Signals  A signalis a special message sent to a process.  Signals are asynchronous; when a process receives a signal, it processes the signal immediately, without finishing the current function or even the current line of code.  There are several dozen different signals, each with a different meaning.  Each signal type is specified by its signal number, but in programs, you usually refer to a signal by its name.
  • 27.
    Process Termination  Whena parent forks a child, the two process can take any turn to finish themselves and in some cases the parent may die before the child.  In some situations, though, it is desirable for the parent process to wait until one or more child processes have completed.  This can be done with the wait() family of system calls.  These functions allow you to wait for a process to finish executing, enable parent process to retrieve information about its child’s termination
  • 28.