DCN 3013: Network Programming - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

DCN 3013: Network Programming

Description:

The process ID is 21697. The parent process ID is 21693 ... trap/catch the signal; execute signal handler. allow the signal default action to occur ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 26
Provided by: notesU
Category:

less

Transcript and Presenter's Notes

Title: DCN 3013: Network Programming


1
DCN 3013 Network Programming
  • Chapter 2 Process and Signal
  • by
  • Muhammed Ramiza Ramli
  • KUKTEM

2
Overview
  • Process Attributes
  • Viewing process ID
  • Creating Process
  • in shell
  • in C program
  • Signal
  • Sending Signal
  • in shell
  • in C program
  • Catching Signal

3
Process Attributes
  • What is a process?
  • Instance of an executing program
  • Linux's basic scheduling unit
  • Why process?
  • Kernel uses process to control access to the CPU
    and other system resources
  • Which programs run on the CPU?
  • How long?
  • What characteristics?

4
Process Identifiers
  • Attribute of the process is it's process ID
    (PID)?
  • It's parent process is with parent process ID
    (PPID)?
  • All processes trace their lineage back to the
    process with PID 1 - init

5
Viewing Process ID
  • In Command prompt
  • Invoking ps command on console will displays the
    processes that are running on the system.
  • By default you will the output as an example
    below
  • ps
  • PID TTY TIME CMP
  • 21693 pts/0 000000 bash
  • 21694 pts/0 000000 ps

6
Get process ID
  • In C programming language, getpid() system
    call will return the PID of the current process
  • Defined in ltunistd.hgt
  • Obtain the PID of the invoking process by
    returning pid_t typedef as defined in
    ltsys/types.hgt
  • getppid() system call will return it's parent
    process ID (PPID)?
  • Both system call will return -1 on failure.

7
Sample Program
  • include ltstdio.hgt
  • include ltunistd.hgt
  • include ltsys/types.hgt
  • int main()?
  • pid_t pid, ppid
  • printf(The process ID is d\n, (int)pid)
  • printf(The parent process ID is d\n, (int)
    ppid)
  • return 0

8
Output
  • Compile the program with gcc then run the program
  • ./pid
  • The process ID is 21697
  • The parent process ID is 21693
  • The output may be different depend on the PID
    assign to the process

9
Creating Process
  • Using the system() Function
  • Defined in ltstdlib.hgt
  • include ltstdlib.hgt
  • int system(const char sting)
  • Using fork() system call
  • include ltsys/types.hgt
  • include ltunistd.hgt
  • pid_t fork (void)
  • And any variation of exec system call

10
system
  • Standard C library provides an easy way to
    execute a command within a program
  • As easy as typing the command in a shell
  • Sample code
  • include ltstdlid.hgt
  • int main()?
  • int retval
  • retval system(ls -l)
  • return retval

11
fork
  • Fork duplicate the process to create another
    child process same as its process.
  • Child process will inherit all the variable and
    value from the parent process as it's a clone
    from the parent process

12
fork Sample
  • include ltsys/types.hgt
  • include ltunistd.hgt
  • include ltstdio.hgt
  • int main()?
  • pid_t my_child
  • printf(Creatin a chlid process!\n)
  • my_child fork()
  • printf(Both will print this\n)
  • return 0

13
Signal
  • Signals are mechanism for communicating with and
    manipulating process in Linux
  • special message sent to a process
  • asynchronous
  • Every signal has a name beginning with SIG such
    as SIGTERM and SIGHUP
  • corresponding to integer constants called signal
    number, defined in ltsignal.hgt

14
Signal
  • When a process receive a signal, it can do one of
    the three things with it
  • ignore the signal
  • trap/catch the signal execute signal handler
  • allow the signal default action to occur

15
Sending Signal
  • There many way to send and receive signal.
  • Signal can be send from
  • kernel
  • another process
  • user input
  • We only control signal from a process to another
    process and signal from user input
  • user input kill command
  • another process kill function

16
alarm() system call
  • Only one alarm at a time
  • alarm () function set a timer in the calling
    process
  • defined in ltunistd.hgt
  • include ltunistd.hgt
  • unsigned int alarm (unsigned int soconds)?
  • return 0 if no other alarm was set, or the number
    of seconds remaining in a previous scheduled
    alarm
  • Setting seconds to 0 cancels any previously set
    alarm

17
alarm sample signal from kernel
  • include ltstdio.hgt
  • include ltunistd.hgt
  • int main()?
  • int x 0
  • if(alarm(3) 0)
  • while(1)
  • printf(Just loop d\n,x)
  • sleep(1)
  • return 0

18
Output
  • This program suppose to have an infinite loop.
  • But after 5 second, it will receive a signal
    SIGALRM from kernel
  • then terminate the process as the output below
  • ./sample
  • Just loop 1
  • Just loop 2
  • Just loop 3
  • Alarm Clock

19
Signal from user
  • User can send a signal to the process by pressing
    a know code to the process
  • Sample
  • Ctrl-C break
  • Ctrl-Z stop
  • Any program running can be detached from shell by
    putting '' at the end of the program name
  • eg ./loop
  • With background process, user cannot pressing any
    signall key.
  • solution Using kill command

20
kill sending signal
  • although kill is meant to kill a process, it can
    be used to send a signal to a process
  • The command kill sends the specified signal to
    the specified process or process group.
  • syntax
  • kill -s signal -p -a -- pid
  • eg kill -s TERM 8194
  • If no signal is specified, the TERM signal is
    sent.
  • else
  • kill -n pid n9 kill, n8 .....

21
sample
  • write a program with infinite loop
  • then send a signal with kill command
  • eg to kill a process use -9
  • kill -9 ltpidgt

22
kill function
  • a process can send a signal to other process by
    using kill() system call.
  • defined in ltsignal.hgt
  • include ltsys/types.hgt
  • include ltsignal.hgt
  • int kill(pid_t pid, int sig)
  • Return
  • 0 success
  • -1 failure

23
kill sample
  • include ltsys/types.hgt
  • include ltunistd.hgt
  • include ltsignal.hgt
  • int main()?
  • pid_t child
  • child fork()
  • if(child 0)
  • while(1)
  • printf(Child Process\n)
  • sleep(1)
  • else
  • sleep(3)
  • kill(child, SIGTERM)
  • return 0

24
Catching Signal
  • Each process can decide how to respond to all
    signal except SIGSTOP and SIGKILL
  • cannot be caught or ignored
  • Catching a signal actually waiting a signal
  • check periodically whether a signal has occurred
    and reacts accordingly
  • Sig name SIGALRM were send after alarm function
    call to set the alarm timer
  • By default a process will terminate their process
    after receive a signal SIGALRM

25
Signal Handler
  • A signal's default action is the desired
    behavior
  • We can alter that behavior to perform
    additional tasks
  • define a custom signal handler to override the
    default action
  • signall system call
  • include ltsignal.hgt
  • void (signal(int signum, void (sighandler)
    (int))) (int)
Write a Comment
User Comments (0)
About PowerShow.com