SlideShare a Scribd company logo
Unix Programming
Module 5
BY:
S Guru Prasad -1BY20CS154
Saeed Ahmed Alojily -1BY20CS159
Sai Harshit B -1BY20CS162
Vigneshwaran P S -1BY20CS215
Contents
➔ Setjmp and Longjmp
➔ Sigsetjmp and Siglongjmp
➔ Kill
➔ Alarm
➔ Interval Timers
➔ Posix 1b Timers
These are APIs that are used to create
functions we use in higher level
languages to make code simpler
1. Setjmp and longjmp
● setjmp and longjmp are a pair of C function facilitating cross-procedure transfer of control. Typically
they are used to allow resumption of execution at a known good point after an error.
● Both take as first argument a buffer, which is used to hold the machine state at the jump destination.
When setjmp is called it populates that buffer with the current location state (which includes stack and
frame pointers and the return address for the call to setjmp, and returns zero.
● longjmp takes a buffer previously populated by setjmp. It also takes a (non-zero) second argument,
which will ultimately be the result of the function call. longjmp restores the machine state from the
buffer. It then jumps to the return address it has just restored, passing its second argument as the
result. That return address is the return address from the original call to setjmp, so the effect will be as
if setjmp has just returned with a non-zero argument.
1. Setjmp and longjmp
setjmp and longjmp are typically used in a top level function in the following way.
1. Setjmp and
longjmp
During normal processing if an error is found, the state held in buf can be used to
return control back to the top level using longjmp.
The program will behave as though the original call to setjmp had just returned with
result 1.
2. Sigsetjmp and siglongjmp
● The sigsetjmp and siglongimp APIs have similar functions as their corresponding
setjmp and longjmp APIs.
● The sigsetjmp and siglongjmp APIs are defined in POSIX 1 and on most UNIX systems that
support signal mask.
● The function prototypes of the APIs are:
#include <setjmp.h>
int sigsetjmp (sigjmpbuf env, int save_sigmask );
int siglongimp (sigjmpbuf env, int ret_val ):
2. Sigsetjmp and siglongjmp
● The sigsetjmp and siglongimp are created to support signal mask processing
Specifically, it is implementation dependent on whether a process signal mask is
saved and restored when it invokes the setjmp and longjmp APIs respectively.
● The sigsetjmp API behaves similarly to the setjmp API, except that it has a second
argument, save_sigmask, which allows user to specify whether a calling process
signal mask should be saved to the provided env argument.
● If the save_sigmask argument is nonzero, the caller's signal mask is saved, else
signal mask is not saved.
● The siglongjmp API does all operations as the longimp API, but it also restores a
calling process signal mask if the mask was saved in its env argument.
2. Sigsetjmp and siglongjmp
● The ret_val argument specifies the return value of the corresponding sigsetjmp API
when called by siglongjmp API. Its value should be nonzero number, and if it is zero
the siglongjmp API will reset it to 1.
2. Sigsetjmp and siglongjmp
#include <iostream.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <setjmp.h>
sigjmp_buf env;
void callme(int sig_num)
{
cout<< “catch signal:” <<sig_num <<endl;
siglongjmp(env,2);
}
int main()
{
sigset_t sigmask;
struct sigaction action,old_action;
sigemptyset(&sigmask);
if(sigaddset(&sigmask,SIGTERM)==1)||sigprocmask(SIG_SETM
ASK,&sigmask,0)==-1)
perror(“set signal mask”);
sigemptyset(&action.sa_mask);
sigaddset(&action.sa_mask,SIGSEGV);
action.sa_handler=(void(*)())callme;
action.sa_flags=0;
if(sigaction(SIGINT,&action,&old_action)==-1)
perror(“sigaction”);
if(sigsetjmp(env,1)!=0)
{
cerr<<”return from signal interruption”;
return 0;
}
else
cerr<<”return from first time sigsetjmp is called”;
pause();
}
The kill API can be used by a process to send a signal to a related process.
➔ This is a simple means of IPC or control
➔ The sender and recipient processes must be related such that either sender
process real or effective user ID matches that of the recipient process, or the
sender has su privileges
➔ For example, a parent and child process can send signals to each other via
the kill API.
➔ The kill API is defined in most UNIX system and is a POSIX.1 standard.
3.Kill
The function prototype is as
● The sig_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:
#include <signal.h>
int kill ( pid_t pid, int signal_num);
Pid value Effects on the Kill API
A Positive Value pid is a process ID. Sends Signal to that process
0 Sends signal to all processes whose process group ID is the same as the calling
process.
-1 Sends signal to all processes whose real user ID is the same as the effective user ID of
the calling process
Negative value Sends signal to all processes whose process group ID matches the absolute value of pid
3.Kill
● The return value of kill is zero if it succeeds or -1 if it fails.
● The following C program illustrates the implementation of the UNIX kill Command.
#include<iostream.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<signal.h>
int main(int argc,char** argv)
{
int pid, sig = SIGTERM;
if(argc==3)
{
if(sscanf(argv[1],”%d”,&sig)!=1)
{
cerr<<”invalid number:” << argv[1] << endl;
return -1;
}
argv++,argc--;
}
while(--argc>0)
if(sscanf(*++argv, “%d”, &pid)==1)
{
if(kill(pid,sig)==-1)
perror(“kill”);
}
else
cerr<<”invalid pid:” << argv[0] <<endl;
return 0;
}
3.Kill
$ kill - < signal_num > < pid >
4. Alarm
An API that can be used a process to request the
kernel for an alarm, like setting an alarm clock.
➔ What is it for?
To send the SIGALARM signal after a certain
number of real clock seconds.
➔ Note
Alarm api is a POSIX.1 standard
Syntax:
#include<signal.h>
unsigned int alarm( unsigned int timeInterval);
The arguement timeInterval denotes the number
of CPU seconds elapse time.
4. Alarm
➔ Example:
#include<signal.h>
#include<stdio.h>
#include<unistd.h>
Void wakeup() {} ;
unsigned int sleep(unsigned int timer)
{
struct sigaction action;
action.sa_handler=wakeup;
action.sa_flags=0;
sigemptyset(&action.sa_mask);
if(sigaction(SIGALRM, &action, 0)==-1)
{
perror("sigaction");return -1;
}
(void)alarm(timer);
(void)pause();
return 0;
}
➔ Explanation:
The sleep function defined in the code
suspends calling a process for the
specified number of seconds (timer
variable). The process will be awakened by
either the elapsed time exceeding the
timer or when process is interrupted by a
signal.
The sleep function sets up a signal handler
for the SIGALRM, calls the API to request
the kernel to send the SIGALRM signal after
the interval and and finally suspends its
execution using the pause system call.
The wakeup signal handler function is
called when the SIGALRM signal is sent to
the process. When it returns, the pause
system is aborted and calling process will
return from sleep function.
5. Interval Timers
#include<signal.h>
#include<stdio.h>
#include<unistd.h>
#define INTERVAL 5
void call(int sig_no)
{alarm(INTERVAL);/*Do scheduled actions*/}
int main()
{
struct sigaction action;
sigemptyset(&action.sa_mask);
action.sa_handler=(void(*)())call;
action.sa_flags=SA_RESTART;
if(sigaction(SIGALRM, &action, 0)==-1)
{perror("sigaction");return -1; }
if(alarm(INTERVAL)==-1)
{perror("alarm");}
else while(1)
{/*Do normal Operations*/}
return 0;
}
➔ Explanation:
As seen earlier, we set up the call function
handling function using sigaction for
SIGALRM.
The program then invokes the alarm API to
call the call function to do the scheduled
actions, and then carry on with its usual
routine.
5. Interval Timers
In addition to alarm API, UNIX also has setitimer
API which provides features in addition to alarm API
1. setitimer resolution time is in microseconds,
while in alarm it is in seconds.
2. The alarm API can be used to set up one real
time clock per timer per process, but
setitimer allows defining upto 3 different
types of timers per process, a real time
clock timer, a timer based on user time
spent on process, and timer based on total
user and system time spent on process.
The setitimer API is not available in POSIX as there
is another set of timers specified under POSIX.1b.
There is another related API called getitimer that
users can use to query the timer values set by
setitimer API.
5. Interval Timers
Syntax: (function prototypes)
#include<sys/time.h>
/*setitimer:*/
int setitimer(int which, const struct itimeval *val, struct itimerval *old);
/*getitimer*/
int getitimer(int which, struct itimeval *old);
Here:
which = to specify which timer to process
Possible argument values:
- ITIMER_REAL: Real time clock. Generates SIGALRM signal.
- ITIMER_VIRTUAL: User time spent on process. Generates
SIGVTALRM signal.
- ITIMER_PROF: Total user and system time spent on process.
Generates SIGPROF signal.
5. Interval Timers
Syntax: (struct itimeval)
struct itimeval
{
struct timeval it_interval; /*timer interval*/
struct timeval it_value; /*current value*/
};
Note: setitimer and getitimer return 0 if they succeed and -1 if they fail.
They are not inherited by child processes, but are retained when a process
exec’s a new program.
The next slide shows the code on the first slide of the topic, but using
setitimer API instead of alarm API.
5. Interval Timers
#include<signal.h>
#include<stdio.h>
#include<unistd.h>
#define INTERVAL 5
void call(int sig_no)
{/*Do scheduled actions*/}
int main()
{ struct itimerval val;
struct sigaction action;
sigemptyset(&action.sa_mask);
action.sa_handler=(void(*)())call;
action.sa_flags=SA_RESTART;
if(sigaction(SIGALRM, &action, 0)==-1)
{perror("sigaction");return -1; }
val.it_interval.tv_sec= INTERVAL;
val.it_interval.tv_usec=0;
val.it_value.tv_sec=INTERVAL;
val.it_value.tv_usec=0;
if(setitimer(ITIMER_REAL, &val, 0)==-1)
{perror("alarm");}
else while(1)
{/*Do normal Operations*/}
return 0;
}
➔ Explanation:
This code uses setitimer instead of alarm
API
6. Posix 1b Timers
POSIX.1b defines a set of APIs for interval timer
manipulation. The POSIX.1b timers are more
flexible and powerful than UNIX timers.
What makes POSIX.1b timers better?
1. Users may define multiple independent
timers per system clock
2. The timer resolution is in nanoseconds
3. Users may specify, on a per timer basis, the
signal to be raised when a timer expires.
4. The timer interval may be specified as an
either absolute or relative time.
6. Posix.1b Timers
There is a limit on how many POSIX timers can
be created per process. This limit is set by the
TIMER_MAX constant, as defined in the
<limits.h> header.
POSIX timers are not inherited by child
processes, but are retained across the exec
system call, just like in interval timers.
Unlike UNIX timers, POSIX timers do not use
the SIGALRM signal when they expire, it can
be used in the same program with sleep API.
POSIX timers have 5 different functions:
Create, set, get time, get overrun and delete.
They are described in the following slide
6. POSIX.1b Timers
Syntax: (function prototypes)
There are a few functions under POSIX timers, and their prototypes are as follows:-
int timer_create(clockid_t clock, struct sigevent* spec, timer_t* timer_hdrp);
Used to dynamically create a timer and returns its handler. The clock argument value
may be CLOCK_REALTIME for creating a real time clock timer. Other values for the
argument are system dependent.
int timer_settime(timer_t timer_hdr, int flag, struct itimrspec* val, struct itimrspec* old);
int timer_gettime(timer_t timer_hdr, struct itimrspec* old);
int timer_getoverrun(timer_t timer_hdr);
int timer_delete(timer_t timer_hdr);
6. POSIX.1b Timers
Syntax:
There are a few structures and union, listed as follows:-
struct sigevent
{
int sigev_notify;
int sigev_signo;
union sigval sigev_value;
};
union sigval
{
int sival
void *sival_ptr;
};
6. POSIX.1b Timers
Syntax:
There are a few structures and union, listed as follows:-
struct itimerspec
{
struct timespec it_interval;
struct timespec it_value;
};
struct timespec
{
time_t tv_sec;
long tv_nsec;
};
6. POSIX.1b Timers
#include<stdio.h>
#include<unistd.h>
#include<time.h>
#include<signal.h>
#define fuse 12
void call (int signo, siginfo_t* evp, void*
ucontext)
{
time_t tim=time(0);
printf("Timer has rung!");
}
int main
{
struct sigaction sigv;
struct sigevent sigx;
struct itimerspec val;
struct tm do_time;
timer_t t_id;
sigemptyset( &sigv.sa_mask);
sigv.sa_flags = SA_SIGINFO;
sigv.sa_sigaction = call;
if (sigaction(SIGUSR1, &sigv, 0) == -1)
{perror("sigaction");return(1);}
sigx.sigev_notify = SIGEV_SIGNAL;
sigx.sigev_signo = SIGUSR1;
sigx.sigev_value.sival_int = fuse;
if(timer_create(CLOCK_REALTIME, &sigx,
&t_id)==-1)
{perror("timer_create");return(2);}
val.it_value.tv_sec = 60;
val.it_value.tv_nsec = 0;
val.it_interval.tv_sec = 120;
val.it_interval.tv_nsec = 0;
if(timer_settime(t_id, 0, &val, 0) == -1)
{perror("timer_settime");return(3);}
/*Do something and wait for timer to expire*/
pause();
if(timer_delete(t_id) == -1)
{ perror("timer_delete");return(4); }
return 0;
}
Thank You

More Related Content

PPT
SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES
PDF
Usp notes unit6-8
PDF
Timers in Unix/Linux
PDF
Lab report 201001067_201001104
PDF
Lab report 201001067_201001104
PDF
Lab report 201001067_201001104
DOCX
AOS Chapter 6 for internal.docx
ODP
Introduction to MPI
SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES
Usp notes unit6-8
Timers in Unix/Linux
Lab report 201001067_201001104
Lab report 201001067_201001104
Lab report 201001067_201001104
AOS Chapter 6 for internal.docx
Introduction to MPI

Similar to Unix presentation.pdf (20)

PPTX
Process management
PDF
The Ring programming language version 1.6 book - Part 83 of 189
PPTX
Unixppt (1) (1).pptx
PPT
Traffic Signal Synchronization Operating Systems
PDF
Signal Handling in Linux
PDF
Ping to Pong
PPTX
25-MPI-OpenMP.pptx
PDF
The Ring programming language version 1.5.4 book - Part 80 of 185
PPT
Parallel computing(2)
DOCX
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
PPTX
Raspberry pi Part 6
PDF
The Ring programming language version 1.5.1 book - Part 76 of 180
PDF
The Ring programming language version 1.5.2 book - Part 77 of 181
PDF
Parallel Programming
PDF
한컴MDS_Virtual Target Debugging with TRACE32
PDF
Global Interpreter Lock: Episode I - Break the Seal
PDF
Please help with the below 3 questions, the python script is at the.pdf
PDF
The Ring programming language version 1.7 book - Part 86 of 196
PPT
Ch7 OS
 
PDF
The Ring programming language version 1.6 book - Part 85 of 189
Process management
The Ring programming language version 1.6 book - Part 83 of 189
Unixppt (1) (1).pptx
Traffic Signal Synchronization Operating Systems
Signal Handling in Linux
Ping to Pong
25-MPI-OpenMP.pptx
The Ring programming language version 1.5.4 book - Part 80 of 185
Parallel computing(2)
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
Raspberry pi Part 6
The Ring programming language version 1.5.1 book - Part 76 of 180
The Ring programming language version 1.5.2 book - Part 77 of 181
Parallel Programming
한컴MDS_Virtual Target Debugging with TRACE32
Global Interpreter Lock: Episode I - Break the Seal
Please help with the below 3 questions, the python script is at the.pdf
The Ring programming language version 1.7 book - Part 86 of 196
Ch7 OS
 
The Ring programming language version 1.6 book - Part 85 of 189
Ad

Recently uploaded (20)

PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Construction Project Organization Group 2.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
Mechanical Engineering MATERIALS Selection
DOCX
573137875-Attendance-Management-System-original
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Welding lecture in detail for understanding
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
OOP with Java - Java Introduction (Basics)
Construction Project Organization Group 2.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Model Code of Practice - Construction Work - 21102022 .pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
bas. eng. economics group 4 presentation 1.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Mechanical Engineering MATERIALS Selection
573137875-Attendance-Management-System-original
CH1 Production IntroductoryConcepts.pptx
Welding lecture in detail for understanding
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Ad

Unix presentation.pdf

  • 1. Unix Programming Module 5 BY: S Guru Prasad -1BY20CS154 Saeed Ahmed Alojily -1BY20CS159 Sai Harshit B -1BY20CS162 Vigneshwaran P S -1BY20CS215
  • 2. Contents ➔ Setjmp and Longjmp ➔ Sigsetjmp and Siglongjmp ➔ Kill ➔ Alarm ➔ Interval Timers ➔ Posix 1b Timers These are APIs that are used to create functions we use in higher level languages to make code simpler
  • 3. 1. Setjmp and longjmp ● setjmp and longjmp are a pair of C function facilitating cross-procedure transfer of control. Typically they are used to allow resumption of execution at a known good point after an error. ● Both take as first argument a buffer, which is used to hold the machine state at the jump destination. When setjmp is called it populates that buffer with the current location state (which includes stack and frame pointers and the return address for the call to setjmp, and returns zero. ● longjmp takes a buffer previously populated by setjmp. It also takes a (non-zero) second argument, which will ultimately be the result of the function call. longjmp restores the machine state from the buffer. It then jumps to the return address it has just restored, passing its second argument as the result. That return address is the return address from the original call to setjmp, so the effect will be as if setjmp has just returned with a non-zero argument.
  • 4. 1. Setjmp and longjmp setjmp and longjmp are typically used in a top level function in the following way.
  • 5. 1. Setjmp and longjmp During normal processing if an error is found, the state held in buf can be used to return control back to the top level using longjmp. The program will behave as though the original call to setjmp had just returned with result 1.
  • 6. 2. Sigsetjmp and siglongjmp ● The sigsetjmp and siglongimp APIs have similar functions as their corresponding setjmp and longjmp APIs. ● The sigsetjmp and siglongjmp APIs are defined in POSIX 1 and on most UNIX systems that support signal mask. ● The function prototypes of the APIs are: #include <setjmp.h> int sigsetjmp (sigjmpbuf env, int save_sigmask ); int siglongimp (sigjmpbuf env, int ret_val ):
  • 7. 2. Sigsetjmp and siglongjmp ● The sigsetjmp and siglongimp are created to support signal mask processing Specifically, it is implementation dependent on whether a process signal mask is saved and restored when it invokes the setjmp and longjmp APIs respectively. ● The sigsetjmp API behaves similarly to the setjmp API, except that it has a second argument, save_sigmask, which allows user to specify whether a calling process signal mask should be saved to the provided env argument. ● If the save_sigmask argument is nonzero, the caller's signal mask is saved, else signal mask is not saved. ● The siglongjmp API does all operations as the longimp API, but it also restores a calling process signal mask if the mask was saved in its env argument.
  • 8. 2. Sigsetjmp and siglongjmp ● The ret_val argument specifies the return value of the corresponding sigsetjmp API when called by siglongjmp API. Its value should be nonzero number, and if it is zero the siglongjmp API will reset it to 1.
  • 9. 2. Sigsetjmp and siglongjmp #include <iostream.h> #include <stdio.h> #include <unistd.h> #include <signal.h> #include <setjmp.h> sigjmp_buf env; void callme(int sig_num) { cout<< “catch signal:” <<sig_num <<endl; siglongjmp(env,2); } int main() { sigset_t sigmask; struct sigaction action,old_action; sigemptyset(&sigmask); if(sigaddset(&sigmask,SIGTERM)==1)||sigprocmask(SIG_SETM ASK,&sigmask,0)==-1) perror(“set signal mask”); sigemptyset(&action.sa_mask); sigaddset(&action.sa_mask,SIGSEGV); action.sa_handler=(void(*)())callme; action.sa_flags=0; if(sigaction(SIGINT,&action,&old_action)==-1) perror(“sigaction”); if(sigsetjmp(env,1)!=0) { cerr<<”return from signal interruption”; return 0; } else cerr<<”return from first time sigsetjmp is called”; pause(); }
  • 10. The kill API can be used by a process to send a signal to a related process. ➔ This is a simple means of IPC or control ➔ The sender and recipient processes must be related such that either sender process real or effective user ID matches that of the recipient process, or the sender has su privileges ➔ For example, a parent and child process can send signals to each other via the kill API. ➔ The kill API is defined in most UNIX system and is a POSIX.1 standard. 3.Kill
  • 11. The function prototype is as ● The sig_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: #include <signal.h> int kill ( pid_t pid, int signal_num); Pid value Effects on the Kill API A Positive Value pid is a process ID. Sends Signal to that process 0 Sends signal to all processes whose process group ID is the same as the calling process. -1 Sends signal to all processes whose real user ID is the same as the effective user ID of the calling process Negative value Sends signal to all processes whose process group ID matches the absolute value of pid 3.Kill
  • 12. ● The return value of kill is zero if it succeeds or -1 if it fails. ● The following C program illustrates the implementation of the UNIX kill Command. #include<iostream.h> #include<stdio.h> #include<unistd.h> #include<string.h> #include<signal.h> int main(int argc,char** argv) { int pid, sig = SIGTERM; if(argc==3) { if(sscanf(argv[1],”%d”,&sig)!=1) { cerr<<”invalid number:” << argv[1] << endl; return -1; } argv++,argc--; } while(--argc>0) if(sscanf(*++argv, “%d”, &pid)==1) { if(kill(pid,sig)==-1) perror(“kill”); } else cerr<<”invalid pid:” << argv[0] <<endl; return 0; } 3.Kill $ kill - < signal_num > < pid >
  • 13. 4. Alarm An API that can be used a process to request the kernel for an alarm, like setting an alarm clock. ➔ What is it for? To send the SIGALARM signal after a certain number of real clock seconds. ➔ Note Alarm api is a POSIX.1 standard Syntax: #include<signal.h> unsigned int alarm( unsigned int timeInterval); The arguement timeInterval denotes the number of CPU seconds elapse time.
  • 14. 4. Alarm ➔ Example: #include<signal.h> #include<stdio.h> #include<unistd.h> Void wakeup() {} ; unsigned int sleep(unsigned int timer) { struct sigaction action; action.sa_handler=wakeup; action.sa_flags=0; sigemptyset(&action.sa_mask); if(sigaction(SIGALRM, &action, 0)==-1) { perror("sigaction");return -1; } (void)alarm(timer); (void)pause(); return 0; } ➔ Explanation: The sleep function defined in the code suspends calling a process for the specified number of seconds (timer variable). The process will be awakened by either the elapsed time exceeding the timer or when process is interrupted by a signal. The sleep function sets up a signal handler for the SIGALRM, calls the API to request the kernel to send the SIGALRM signal after the interval and and finally suspends its execution using the pause system call. The wakeup signal handler function is called when the SIGALRM signal is sent to the process. When it returns, the pause system is aborted and calling process will return from sleep function.
  • 15. 5. Interval Timers #include<signal.h> #include<stdio.h> #include<unistd.h> #define INTERVAL 5 void call(int sig_no) {alarm(INTERVAL);/*Do scheduled actions*/} int main() { struct sigaction action; sigemptyset(&action.sa_mask); action.sa_handler=(void(*)())call; action.sa_flags=SA_RESTART; if(sigaction(SIGALRM, &action, 0)==-1) {perror("sigaction");return -1; } if(alarm(INTERVAL)==-1) {perror("alarm");} else while(1) {/*Do normal Operations*/} return 0; } ➔ Explanation: As seen earlier, we set up the call function handling function using sigaction for SIGALRM. The program then invokes the alarm API to call the call function to do the scheduled actions, and then carry on with its usual routine.
  • 16. 5. Interval Timers In addition to alarm API, UNIX also has setitimer API which provides features in addition to alarm API 1. setitimer resolution time is in microseconds, while in alarm it is in seconds. 2. The alarm API can be used to set up one real time clock per timer per process, but setitimer allows defining upto 3 different types of timers per process, a real time clock timer, a timer based on user time spent on process, and timer based on total user and system time spent on process. The setitimer API is not available in POSIX as there is another set of timers specified under POSIX.1b. There is another related API called getitimer that users can use to query the timer values set by setitimer API.
  • 17. 5. Interval Timers Syntax: (function prototypes) #include<sys/time.h> /*setitimer:*/ int setitimer(int which, const struct itimeval *val, struct itimerval *old); /*getitimer*/ int getitimer(int which, struct itimeval *old); Here: which = to specify which timer to process Possible argument values: - ITIMER_REAL: Real time clock. Generates SIGALRM signal. - ITIMER_VIRTUAL: User time spent on process. Generates SIGVTALRM signal. - ITIMER_PROF: Total user and system time spent on process. Generates SIGPROF signal.
  • 18. 5. Interval Timers Syntax: (struct itimeval) struct itimeval { struct timeval it_interval; /*timer interval*/ struct timeval it_value; /*current value*/ }; Note: setitimer and getitimer return 0 if they succeed and -1 if they fail. They are not inherited by child processes, but are retained when a process exec’s a new program. The next slide shows the code on the first slide of the topic, but using setitimer API instead of alarm API.
  • 19. 5. Interval Timers #include<signal.h> #include<stdio.h> #include<unistd.h> #define INTERVAL 5 void call(int sig_no) {/*Do scheduled actions*/} int main() { struct itimerval val; struct sigaction action; sigemptyset(&action.sa_mask); action.sa_handler=(void(*)())call; action.sa_flags=SA_RESTART; if(sigaction(SIGALRM, &action, 0)==-1) {perror("sigaction");return -1; } val.it_interval.tv_sec= INTERVAL; val.it_interval.tv_usec=0; val.it_value.tv_sec=INTERVAL; val.it_value.tv_usec=0; if(setitimer(ITIMER_REAL, &val, 0)==-1) {perror("alarm");} else while(1) {/*Do normal Operations*/} return 0; } ➔ Explanation: This code uses setitimer instead of alarm API
  • 20. 6. Posix 1b Timers POSIX.1b defines a set of APIs for interval timer manipulation. The POSIX.1b timers are more flexible and powerful than UNIX timers. What makes POSIX.1b timers better? 1. Users may define multiple independent timers per system clock 2. The timer resolution is in nanoseconds 3. Users may specify, on a per timer basis, the signal to be raised when a timer expires. 4. The timer interval may be specified as an either absolute or relative time.
  • 21. 6. Posix.1b Timers There is a limit on how many POSIX timers can be created per process. This limit is set by the TIMER_MAX constant, as defined in the <limits.h> header. POSIX timers are not inherited by child processes, but are retained across the exec system call, just like in interval timers. Unlike UNIX timers, POSIX timers do not use the SIGALRM signal when they expire, it can be used in the same program with sleep API. POSIX timers have 5 different functions: Create, set, get time, get overrun and delete. They are described in the following slide
  • 22. 6. POSIX.1b Timers Syntax: (function prototypes) There are a few functions under POSIX timers, and their prototypes are as follows:- int timer_create(clockid_t clock, struct sigevent* spec, timer_t* timer_hdrp); Used to dynamically create a timer and returns its handler. The clock argument value may be CLOCK_REALTIME for creating a real time clock timer. Other values for the argument are system dependent. int timer_settime(timer_t timer_hdr, int flag, struct itimrspec* val, struct itimrspec* old); int timer_gettime(timer_t timer_hdr, struct itimrspec* old); int timer_getoverrun(timer_t timer_hdr); int timer_delete(timer_t timer_hdr);
  • 23. 6. POSIX.1b Timers Syntax: There are a few structures and union, listed as follows:- struct sigevent { int sigev_notify; int sigev_signo; union sigval sigev_value; }; union sigval { int sival void *sival_ptr; };
  • 24. 6. POSIX.1b Timers Syntax: There are a few structures and union, listed as follows:- struct itimerspec { struct timespec it_interval; struct timespec it_value; }; struct timespec { time_t tv_sec; long tv_nsec; };
  • 25. 6. POSIX.1b Timers #include<stdio.h> #include<unistd.h> #include<time.h> #include<signal.h> #define fuse 12 void call (int signo, siginfo_t* evp, void* ucontext) { time_t tim=time(0); printf("Timer has rung!"); } int main { struct sigaction sigv; struct sigevent sigx; struct itimerspec val; struct tm do_time; timer_t t_id; sigemptyset( &sigv.sa_mask); sigv.sa_flags = SA_SIGINFO; sigv.sa_sigaction = call; if (sigaction(SIGUSR1, &sigv, 0) == -1) {perror("sigaction");return(1);} sigx.sigev_notify = SIGEV_SIGNAL; sigx.sigev_signo = SIGUSR1; sigx.sigev_value.sival_int = fuse; if(timer_create(CLOCK_REALTIME, &sigx, &t_id)==-1) {perror("timer_create");return(2);} val.it_value.tv_sec = 60; val.it_value.tv_nsec = 0; val.it_interval.tv_sec = 120; val.it_interval.tv_nsec = 0; if(timer_settime(t_id, 0, &val, 0) == -1) {perror("timer_settime");return(3);} /*Do something and wait for timer to expire*/ pause(); if(timer_delete(t_id) == -1) { perror("timer_delete");return(4); } return 0; }