SlideShare a Scribd company logo
1 of 26
Download to read offline
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

Similar to Unix presentation.pdf

The Ring programming language version 1.6 book - Part 83 of 189
The Ring programming language version 1.6 book - Part 83 of 189The Ring programming language version 1.6 book - Part 83 of 189
The Ring programming language version 1.6 book - Part 83 of 189Mahmoud Samir Fayed
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in LinuxTushar B Kute
 
The Ring programming language version 1.5.4 book - Part 80 of 185
The Ring programming language version 1.5.4 book - Part 80 of 185The Ring programming language version 1.5.4 book - Part 80 of 185
The Ring programming language version 1.5.4 book - Part 80 of 185Mahmoud Samir Fayed
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docxeugeniadean34240
 
Raspberry pi Part 6
Raspberry pi Part 6Raspberry pi Part 6
Raspberry pi Part 6Techvilla
 
The Ring programming language version 1.5.1 book - Part 76 of 180
The Ring programming language version 1.5.1 book - Part 76 of 180The Ring programming language version 1.5.1 book - Part 76 of 180
The Ring programming language version 1.5.1 book - Part 76 of 180Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 77 of 181
The Ring programming language version 1.5.2 book - Part 77 of 181The Ring programming language version 1.5.2 book - Part 77 of 181
The Ring programming language version 1.5.2 book - Part 77 of 181Mahmoud Samir Fayed
 
한컴MDS_Virtual Target Debugging with TRACE32
한컴MDS_Virtual Target Debugging with TRACE32한컴MDS_Virtual Target Debugging with TRACE32
한컴MDS_Virtual Target Debugging with TRACE32HANCOM MDS
 
Global Interpreter Lock: Episode I - Break the Seal
Global Interpreter Lock: Episode I - Break the SealGlobal Interpreter Lock: Episode I - Break the Seal
Global Interpreter Lock: Episode I - Break the SealTzung-Bi Shih
 
Please help with the below 3 questions, the python script is at the.pdf
Please help with the below 3  questions, the python script is at the.pdfPlease help with the below 3  questions, the python script is at the.pdf
Please help with the below 3 questions, the python script is at the.pdfsupport58
 
The Ring programming language version 1.7 book - Part 86 of 196
The Ring programming language version 1.7 book - Part 86 of 196The Ring programming language version 1.7 book - Part 86 of 196
The Ring programming language version 1.7 book - Part 86 of 196Mahmoud Samir Fayed
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OSC.U
 
The Ring programming language version 1.6 book - Part 85 of 189
The Ring programming language version 1.6 book - Part 85 of 189The Ring programming language version 1.6 book - Part 85 of 189
The Ring programming language version 1.6 book - Part 85 of 189Mahmoud Samir Fayed
 
DSP_Assign_2 (Autosaved)
DSP_Assign_2 (Autosaved)DSP_Assign_2 (Autosaved)
DSP_Assign_2 (Autosaved)Joseph Chandler
 

Similar to Unix presentation.pdf (20)

Process management
Process managementProcess management
Process management
 
The Ring programming language version 1.6 book - Part 83 of 189
The Ring programming language version 1.6 book - Part 83 of 189The Ring programming language version 1.6 book - Part 83 of 189
The Ring programming language version 1.6 book - Part 83 of 189
 
Unixppt (1) (1).pptx
Unixppt (1) (1).pptxUnixppt (1) (1).pptx
Unixppt (1) (1).pptx
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in Linux
 
Ping to Pong
Ping to PongPing to Pong
Ping to Pong
 
25-MPI-OpenMP.pptx
25-MPI-OpenMP.pptx25-MPI-OpenMP.pptx
25-MPI-OpenMP.pptx
 
The Ring programming language version 1.5.4 book - Part 80 of 185
The Ring programming language version 1.5.4 book - Part 80 of 185The Ring programming language version 1.5.4 book - Part 80 of 185
The Ring programming language version 1.5.4 book - Part 80 of 185
 
Parallel computing(2)
Parallel computing(2)Parallel computing(2)
Parallel computing(2)
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
 
Raspberry pi Part 6
Raspberry pi Part 6Raspberry pi Part 6
Raspberry pi Part 6
 
The Ring programming language version 1.5.1 book - Part 76 of 180
The Ring programming language version 1.5.1 book - Part 76 of 180The Ring programming language version 1.5.1 book - Part 76 of 180
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
The Ring programming language version 1.5.2 book - Part 77 of 181The Ring programming language version 1.5.2 book - Part 77 of 181
The Ring programming language version 1.5.2 book - Part 77 of 181
 
Parallel Programming
Parallel ProgrammingParallel Programming
Parallel Programming
 
한컴MDS_Virtual Target Debugging with TRACE32
한컴MDS_Virtual Target Debugging with TRACE32한컴MDS_Virtual Target Debugging with TRACE32
한컴MDS_Virtual Target Debugging with TRACE32
 
Global Interpreter Lock: Episode I - Break the Seal
Global Interpreter Lock: Episode I - Break the SealGlobal Interpreter Lock: Episode I - Break the Seal
Global Interpreter Lock: Episode I - Break the Seal
 
Please help with the below 3 questions, the python script is at the.pdf
Please help with the below 3  questions, the python script is at the.pdfPlease help with the below 3  questions, the python script is at the.pdf
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
The Ring programming language version 1.7 book - Part 86 of 196The Ring programming language version 1.7 book - Part 86 of 196
The Ring programming language version 1.7 book - Part 86 of 196
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
 
The Ring programming language version 1.6 book - Part 85 of 189
The Ring programming language version 1.6 book - Part 85 of 189The Ring programming language version 1.6 book - Part 85 of 189
The Ring programming language version 1.6 book - Part 85 of 189
 
DSP_Assign_2 (Autosaved)
DSP_Assign_2 (Autosaved)DSP_Assign_2 (Autosaved)
DSP_Assign_2 (Autosaved)
 

Recently uploaded

OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 

Recently uploaded (20)

OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 

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; }