CS5
CS5
CS5
Accept the default action of the signal, which for most signals will
terminate the process.
Ignore the signal. The signal will be discarded and it has no affect
whatsoever on the recipient process.
#include <signal.h>
int sigprocmask(int cmd, const sigset_t *new_mask, sigset_t *old_mask);
Returns: 0 if OK, -1 on error
• The new_mask argument defines a set of signals
to be set or reset in a calling process signal
mask.
• The sigfillset API sets all the signal flags in the sigmask argument.
• [ all the above functions return 0 if OK, -1 on error ]
#include<iostream.h>
#include<stdio.h>
#include<signal.h>
int main()
{
sigset_t sigmask;
sigemptyset(&sigmask);
if(sigpending(&sigmask)==-1)
perror(“sigpending”)
else
cout << “SIGTERM signal is:” << (sigismember(&sigmask,SIGTERM) ? “Set” :
“No Set”) << endl;
};
SIGACTION
• Improved form of signal () where you can
specify additional signals to be blocked when
the API is handling a signal.
• The sigaction API prototype is:
#include<signal.h>
int sigaction(int signal_num, struct sigaction* action, struct
sigaction* old_action);
Returns: 0 if OK, -1 on error
• The struct sigaction data type is defined in the
<signal.h> header as:
struct sigaction
{
void (*sa_handler)(int);
sigset_t sa_mask;
int sa_flag;
}
The following program illustrates the uses
of sigaction:
#include<iostream.h>
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
void callme(int sig_num)
{
cout<<”catch signal:”<<sig_num<<endl;
}
int main(int argc, char* argv[])
{
sigset_t sigmask;
struct sigaction action,old_action;
sigemptyset(&sigmask);
if(sigaddset(&sigmask,SIGTERM)==-1 || sigprocmask(SIG_SETMASK,&sigmask,0)==-1)
perror(“set signal mask”);
sigemptyset(&action.sa_mask);
sigaddset(&action.sa_mask,SIGSEGV);
action.sa_handler=callme;
action.sa_flags=0;
if(sigaction(SIGINT,&action,&old_action)==-1)
perror(“sigaction”);
pause(); cout<<argv[0]<<”exists\n”;
return 0;
}
KILL
• A process can send a signal to a related
process via the kill API.
• This is a simple means of inter-process
communication or control.
• The function prototype of the API is:
#include<signal.h>
int kill(pid_t pid, int signal_num);
Returns: 0 on success, -1 on failure.
• The signal_num argument is the integer value of a signal to be
sent to one or more processes designated by pid.
• The possible values of pid and its use by the kill API are:
• These accounting records are typically a small amount of binary data with the
name of the command, the amount of CPU time used, the user ID and group ID,
the starting time, and so on.
• The accounting records are written to the specified file, which is usually
/var/account/acct. Accounting is turned off by executing accton without any
arguments.
• The data required for the accounting record, such as CPU times
and number of characters transferred, is kept by the kernel in the
process table and initialized whenever a new process is created, as
in the child after a fork.
• This means that the order of the records in the accounting file
corresponds to the termination order of the processes, not the
order in which they were started.
• A new record is initialized by the kernel for the child after a fork,
not when a new program is executed.
The structure of the accounting records is defined in the
header <sys/acct.h> and looks something like
Values for ac_flag from accounting record