UnixCG Process Programming
UnixCG Process Programming
By
Unix FCG
Version: 1.0
Date: Feb 2008
Index
• Process Environment
• Process memory layout
• Create a process
• Process exit
• Wait for child
• Exec a new program
• What are signals ?
• Generation of signals
• Disposition of signals
• Lifecycle of a signal
• Signal Handling using POSIX API
• Signals and re-entrancy
Wipro confidential 2
Process Environment
When you run the reference program with an argument of “*” i.e a.out *, what is the value
of argc, argv ?
Wipro confidential 3
Process Memory layout
Why does the size command not show the size of heap and stack ?
Explore the objdump utility for its usefulness
Wipro confidential 4
Create a process…fork
Modify the reference program given here to print the process ids within the child and parent processes.
Add global, local variables( before and after fork() ) to the program. Change the values of these variables in
the child, see how it affects the value in the parent’s copy of these variables
Wipro confidential 5
Process exit
-Modify the reference example given in the previous slide, create orphan and zombie
processes by killing them from the command line . Observe the process status in the ps
command output
- Can a process inherited by init become a zombie ?
Explore the use of atexit() to register exit handlers and see how they are called at process
exit.
Wipro confidential 6
Wait for child
Wait - example
• A parent process may use wait() or waitpid()
#include <stdio.h>
to wait for its children’s termination status
• Upon child process termination, wait returns
int main( ) with the exit status of the child process
{
• wait() blocks till any one of the children
int pid, status;
pid = fork();
terminate
if( pid == 0 ) • waitpid() has a non-blocking option, can be
{ used to wait on a particular process id
sleep(2); • Parent can examine the child process
exit(0);
}
termination status
else • Parent can register for SIGCHLD and call
{ wait() in the context of the signal handler
if( wait( &status ) == pid )
printf( “Child exited\n” );
}
}
- Modify the example code to call wait() in the context of the SIGCHLD handler. Register for the signal by
calling signal( SIGCHLD, sig_handler ), where sig_handler is the signal handler function
- Use waitpid in non-blocking mode, wait for specific child process ids;
Wipro confidential 7
Wait – more!
Try these !
- What happens to the parent and child process if the SIGCHLD is ignored explicitly
- i.e signal( SIGCHLD, SIG_IGN )
-Fork multiple children, wait on all children. Observe status of children not “waited”
upon
- We do not want the parent process to “wait”. We do not want them to become
zombies either. How do we accomplish this ? [ Clue: Init does not have its children
as zombies ]
Wipro confidential 8
Exec a new program
#include <stdio.h> • Child runs with the data, heap and stack
segments of the new executable
int main( ) • Different exec variants available – execl,
{
int pid;
execv, execle,execve, execlp, execvp
pid = fork(); • Differ in manner program arguments and
if( pid == 0 ) environment variables are passed to the child
{
printf( “Inside child\n” );
execlp( “/home/user1/a1”, “a1”,
“arg1”, “arg2”, (char *)0 ); Write you own program “a1” that prints the command
} line argument and environment variables. Run the
else example program given here and observe the output
{
printf( “Inside parent\n” )
wait ( &status );
}
}
Wipro confidential 9
What are signals ?
Wipro confidential 10
How are signals generated ?
Wipro confidential 11
Some signals….
Wipro confidential 12
Basic disposition of signal
Run the example program, send SIGINT, SIGUSR1 from command line, observe result.
Wipro confidential 13
Disposition of signal… try this
Using the example program given in the previous slide, send these signals more than
once and observe the result.
Send a different signal that is not handled, observe the default behaviour.
Fork a child process after the signal() call. Send the signals from the command line and
observe the behaviour in the parent and child
Wipro confidential 14
Lifecycle of a signal
Wipro confidential 15
Signal handling using POSIX API
Signal handling using POSIX API • Process can block a set of signals
#include <stdio.h>
from being delivered to it
• Using sigprocmask() API
main( )
{ • Operates on a set of signals
// Register signal handler represented by sigset_t
if( signal( SIGUSR1, sig_handler ) < 0 )
printf( “Error: cannot catch SIGUSR1\n” ); • Process can see the set of signals
// Setup the signal mask that are blocked and pending
sigset_t mask, pendmask;
sigemptyset( &mask );
delivery
sigaddset( &mask, SIGUSR1 ); • sigpending()
sigprocmask( SIG_BLOCK, &mask, 0 );
sleep(5);
• APIs to manipulate signal sets
if( sigpending( &pendmask ) < 0 ) • sigemptyset(), sigfillset(),
printf( “Error in pending mask );
sigismember() etc
if( sigismember( &pendmask, SIGUSR1 ) )
printf( “SIGUSR1 pending\n” );
} Run the example program, send SIGUSR1 from
command line, observe result.
void sig_handler( int signo ) Modify the program to unblock SIGUSR1 and see if
{
the signal handler is invoked.
……………………..
} In this case, observe what happens is there is no
signal handler defined?
What happens if someother signal is sent to the
process?
Wipro confidential 16
Signal handling using POSIX API…sigaction()
void sig_handler( int signo ) Run the example program, send SIGUSR1 from
{ command line, observe result.
printf( “SIGUSR1 received\n” ); Explore the different sigaction flags and try out sample
} programs with them
Wipro confidential 17
Signals and re-entrancy
Wipro confidential 18
Thank you.
Information contained and transmitted by this presentation is proprietary to Wipro Limited and is intended for use only by the individual or entity to which it is addressed,
and contains information that is privileged, confidential or exempt from disclosure under applicable law.
Wipro confidential 19