Download Time-outs using Linux

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Process management (computing) wikipedia , lookup

DNIX wikipedia , lookup

Transcript
CS 3100
Operating Systems
Spring 2002
Linux time management and timers
GETITIMER(2)
Linux Programmer's Manual
GETITIMER(2)
NAME
getitimer,
setitimer
- get or set value of an interval timer
SYNOPSIS
#include <sys/time.h>
int getitimer(int which, struct itimerval *value);
int setitimer(int which, const struct itimerval *value,
struct itimerval *ovalue);
DESCRIPTION
The system provides each process with three interval
timers, each decrementing in a distinct time domain. When
any timer expires, a signal is sent to the process, and
the timer (potentially) restarts.
ITIMER_REAL
decrements in real time, and delivers
SIGALRM upon expiration.
ITIMER_VIRTUAL decrements only when the process is executing, and
delivers SIGVTALRM upon expiration.
ITIMER_PROF
decrements both when the process executes
and when the system is executing on behalf
of the process. Coupled with ITIMER_VIR
TUAL, this timer is usually used to profile
the time spent by the application in user
and kernel space.
SIGPROF is delivered
upon expiration.
Timer values are defined by the following structures:
struct itimerval {
struct timeval it_interval; /* next value */
struct timeval it_value;
/* current value */
};
struct timeval {
long tv_sec;
/* seconds */
long tv_usec;
/* microseconds */
};
Getitimer(2) fills the structure indicated by value with
the current setting for the timer indicated by which (one
of ITIMER_REAL, ITIMER_VIRTUAL, or ITIMER_PROF). The ele
ment it_value is set to the amount of time remaining on
the timer, or zero if the timer is disabled. Similarly,
it_interval is set to the reset value. Setitimer(2) sets
the indicated timer to the value in value. If ovalue is
nonzero, the old value of the timer is stored there.
Timers decrement from it_value to zero, generate a signal,
and reset to it_interval. A timer which is set to zero
(it_value is zero or the timer expires and it_interval is
GETITIMER(2)
769824496
Linux Programmer's Manual
GETITIMER(2)
04/30/17
CS 3100
Operating Systems
Spring 2002
zero) stops.
Both tv_sec and tv_usec are significant in determining the
duration of a timer.
Timers will never expire before the requested time,
instead
expiring some short, constant time afterwards,
dependent on the system timer resolution (currently 10ms).
Upon expiration, a signal will be generated and the timer
reset. If the timer expires while the process is active
(always true for ITIMER_VIRT) the signal will be delivered
immediately when generated. Otherwise the delivery will
be offset by a small time dependent on the system loading.
RETURN VALUE
On success, zero is returned. On error, -1
and errno is set appropriately.
is
returned,
ERRORS
EFAULT value or ovalue are not valid pointers.
EINVAL which is not
ITIMER_PROF.
one of ITIMER_REAL, ITIMER_VIRT, or
CONFORMING TO
SVr4, 4.4BSD (This call first appeared in 4.2BSD).
SEE ALSO
gettimeofday(2), sigaction(2), signal(2).
BUGS
Under Linux, the generation and delivery of a signal are
distinct, and there each signal is permitted only one out
standing event. It's therefore conceivable that under
pathologically heavy loading,
ITIMER_REAL will expire
before the signal from a previous expiration has been
delivered. The second signal in such an event will be lost.
769824496
04/30/17
CS 3100
SIGNAL(2)
Operating Systems
Linux Programmer's Manual
Spring 2002
SIGNAL(2)
NAME
signal - ANSI C signal handling
SYNOPSIS
#include <signal.h>
void (*signal(int signum, void (*sighandler)(int)))(int);
DESCRIPTION
The signal() system call installs a new signal handler for
the signal with number signum. The signal handler is set
to sighandler which may be a user specified function, or
either SIG_IGN or SIG_DFL.
Upon arrival of a signal with number signum the following
happens.
If the corresponding handler is set to SIG_IGN,
then the signal is ignored. If the handler is set to
SIG_DFL,
then the default action associated to the signal
(see signal(7)) occurs. Finally, if the handler is set to
a function sighandler then first either the handler is
reset to SIG_DFL or an implementation-dependent blocking
of the signal is performed and next sighandler is called
with argument signum.
Using a signal handler function for a signal is called
"catching the signal". The signals SIGKILL and SIGSTOP
cannot be caught or ignored.
RETURN VALUE
The function signal() returns the previous
signal handler, or SIG_ERR on error.
value
of
the
CONFORMING TO
ANSI C
SEE ALSO
kill(1), kill(2), killpg(2), pause(2), raise(3), sigac
tion(2), signal(7), sigsetops(3), sigvec(2), alarm(2)
769824496
04/30/17
CS 3100
KILL(2)
Operating Systems
Spring 2002
Linux Programmer's Manual
KILL(2)
NAME
kill - send signal to a process
SYNOPSIS
#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);
DESCRIPTION
The kill system call can be used to send any signal to any
process group or process.
If pid is positive, then signal sig is sent to pid.
If pid equals 0, then sig is sent to every process in
process group of the current process.
the
If pid equals -1, then sig is sent to every process except
for the first one.
If pid is less than -1, then sig is sent to every
in the process group -pid.
process
If sig is 0, then no signal is sent, but error checking is
still performed.
RETURN VALUE
On success, zero is returned. On error, -1
and errno is set appropriately.
is
returned,
ERRORS
EINVAL An invalid signal was specified.
ESRCH
The pid or process group does not exist.
Note that
an existing process might be a zombie, a process
which already committed
termination, but has not
yet been wait()ed for.
EPERM
The process does not have permission to
send the
signal to any of the receiving processes. For a
process to have permission to send a signal to pro
cess pid
it must either have root privileges, or
the real or effective user ID of the sending pro
cess must equal the real or saved set-user-ID of
the receiving process. In the case of SIGCONT it
suffices
when the sending and receiving processes
belong to the same session.
CONFORMING TO
SVr4, SVID, POSIX.1, X/OPEN, BSD 4.3
SEE ALSO
_exit(2), exit(3), signal(2), signal(7)
769824496
04/30/17
CS 3100
CTIME(3)
Operating Systems
Spring 2002
Linux Programmer's Manual
CTIME(3)
NAME
asctime,
ctime,
gmtime,
binary date and time to ASCII
localtime,
mktime
- transform
SYNOPSIS
#include <time.h>
char *asctime(const struct tm *timeptr);
char *ctime(const time_t *timep);
struct tm *gmtime(const time_t *timep);
struct tm *localtime(const time_t *timep);
time_t mktime(struct tm *timeptr);
extern char *tzname[2];
long int timezone;
extern int daylight;
DESCRIPTION
The ctime(), gmtime() and localtime() functions all take
an argument of data type time_t which represents calendar
time. When interpreted as an absolute time value, it rep
resents
the number of seconds elapsed since 00:00:00 on
January 1, 1970, Coordinated Universal Time (UTC).
The asctime() and mktime() functions both take an argument
representing broken-down time which is a binary represen
tation separated into year, month, day, etc.
Broken-down
time is
stored
in the structure tm which is defined in
<time.h> as follows:
struct tm
{
int
int
int
int
int
int
int
int
int
};
tm_sec;
tm_min;
tm_hour;
tm_mday;
tm_mon;
tm_year;
tm_wday;
tm_yday;
tm_isdst;
/*
/*
/*
/*
/*
/*
/*
/*
/*
seconds */
minutes */
hours */
day of the month */
month */
year */
day of the week */
day in the year */
daylight saving time */
The ctime() function converts the calendar time timep into
a string of the form
"Wed Jun 30 21:49:08 1993\n"
The return value points to a statically allocated
string which might be overwritten by subsequent calls to
any of the date and time functions.
769824496
04/30/17
CS 3100
TIME(2)
Operating Systems
Linux Programmer's Manual
Spring 2002
TIME(2)
NAME
time - get time in seconds
SYNOPSIS
#include <time.h>
time_t time(time_t *t);
DESCRIPTION
time returns the time since the Epoch (00:00:00 UTC, Jan
uary 1, 1970), measured in seconds.
If t is non-NULL, the return value is also stored in the
memory pointed to by t.
RETURN VALUE
On success, the value of time in seconds since the Epoch
is returned. On error,
((time_t)-1) is returned, and
errno is set appropriately.
ERRORS
EFAULT t points outside your accessible address space.
769824496
04/30/17
CS 3100
GETTIMEOFDAY(2)
Operating Systems
Spring 2002
Linux Programmer’s Manual
GETTIMEOFDAY(2)
NAME
gettimeofday, settimeofday - get / set time
SYNOPSIS
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv , const struct timezone
*tz);
DESCRIPTION
The functions gettimeofday and settimeofday can get and set the time
as well as a timezone. The tv argument is a timeval struct, as specified in <sys/time.h>:
struct timeval {
time_t
suseconds_t
};
tv_sec;
tv_usec;
/* seconds */
/* microseconds */
and gives the number of seconds and microseconds since the Epoch
time(2)). The tz argument is a timezone :
(see
struct timezone {
int tz_minuteswest; /* minutes W of Greenwich */
int tz_dsttime;
/* type of dst correction */
};
The use of the timezone struct is obsolete; the tz_dsttime field has
never been used under Linux.
The following macros are defined to operate on a struct timeval :
#define
timerisset(tvp)\
((tvp)->tv_sec || (tvp)->tv_usec)
#define
timercmp(tvp, uvp, cmp)\
((tvp)->tv_sec cmp (uvp)->tv_sec ||\
(tvp)->tv_sec == (uvp)->tv_sec &&\
(tvp)->tv_usec cmp (uvp)->tv_usec)
#define
timerclear(tvp)\
((tvp)->tv_sec = (tvp)->tv_usec = 0)
If either tv or tz is null, the corresponding structure is not set
returned.
or
Only the super user may use settimeofday.
RETURN VALUE
gettimeofday and settimeofday return 0 for success, or -1 for failure
(in which case errno is set appropriately).
ERRORS
EPERM
settimeofday is called by someone other than the superuser.
EINVAL Timezone (or something else) is invalid.
EPERM
settimeofday is called by someone other than the superuser.
EINVAL Timezone (or something else) is invalid.
769824496
04/30/17
CS 3100
Operating Systems
Spring 2002
EFAULT One of tv or tz pointed outside your accessible address
space.
NOTE
The prototype for settimeofday and the defines for timercmp, timerisset, timerclear, timeradd, timersub are (since glibc2.2.2) only available if _BSD_SOURCE is defined (either explicitly, or implicitly, by
not defining _POSIX_SOURCE or compiling with the -ansi flag).
Traditionally, the fields of struct timeval were longs.
CONFORMING TO
SVr4, BSD 4.3. POSIX 1003.1-2001 describes gettimeofday() but not settimeofday().
SEE ALSO
date(1), adjtimex(2), time(2), ctime(3), ftime(3)
769824496
04/30/17