Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
EEE 13
Process scheduling
Timeouts
network protocols
User input
Event management
POSIX Time
POSIX specifcation
Epoch
POSIX Extensions
POSIX:XSI Extension
microseconds resolution
POSIX:TMR
nanoseconds resolution
Notes on time
time_t
32-bit =>
(2038)
Time Diference
NAME
difftime - calculate time difference
RETURN
diftime() sample
#include <stdio.h>
#include <time.h>
int main (void)
{
time_t t0;
int
i = 7;
int
y;
t0 = time(NULL);
while (i > 0) {
i--;
y = 0xFFFFFFF;
while (y > 0)
y--;
}
printf("Executed in %lf seconds.\n", diftime(time(NULL), t0));
return 0;
}
struct tm
struct tm
char
time_t
mktime(struct tm *tm);
9
struct tm
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
/*
/*
/*
/*
/*
/*
/*
/*
/*
seconds */
minutes */
hours */
day of the month */
month */
year */
day of the week */
day in the year */
daylight saving time */
10
gmtime()
localtime()
11
localtime()
#include <stdio.h>
#include <time.h>
int main (void)
{
struct tm
time_t
*current;
t = time(NULL);
current = localtime(&t);
printf ("%d days before Xmas\n",
359 - current->tm_yday);
return 0;
}
12
asctime()
mktime()
13
asctime()
#include <stdio.h>
#include <time.h>
int main (void)
{
time_t
t0;
t0 = time (NULL);
printf ("%s", asctime(localtime(&t0)));
/* Mon Aug 4 13:20:25 2008 */
}
return 0;
14
ctime()
Equivalent to asctime(localtime(t))
15
ctime()
#include <stdio.h>
#include <time.h>
int main (void)
{
time_t
t0;
t0 = time (NULL);
printf ("%s",ctime(&t0));
/* Mon Aug 4 13:20:25 2008 */
}
return 0;
16
Time functions
asctime
localtime
struct tm
seconds
time
string
gmtime
mktime
ctime
seconds
string
17
Thread-safe Versions
NAME
asctime_r, ctime_r, gmtime_r, localtime_r
transform date and time to broken-down
time or ASCII without using static
variables
SYNOPSIS
#include <time.h>
char *asctime_r(const struct tm *tm,char *buf);
char *ctime_r(const time_t *timep, char *buf);
struct tm *gmtime_r(const time_t *timep, struct
tm *result);
struct tm *localtime_r(const time_t *timep,
struct tm *result);
18
19
struct timeval
struct timeval {
long tv_sec;
long tv_usec;
/* seconds */
/* microseconds */
};
20
struct timezone
struct timezone {
int
tz_minuteswest;
/* minutes W of Greenwich */
int tz_dsttime;
/* type of dst correction */
};
21
gettimeofday()
#include <stdio.h>
#include <sys/time.h>
int main (void)
{
int i, y;
struct timeval t0,t1;
gettimeofday(&t0,0);
i = 3;
while (i > 0) {
i--;
y = 0xFFFFFFF;
while(y > 0)
y--;
}
gettimeofday(&t1,0);
printf ("%lu usecs elapsed \n",
1000000*(t1.tv_sec-t0.tv_sec)
+ t1.tv_usec-t0.tv_usec);
return 0;
}
22
Real-time Clocks
clock_id = CLOCK_REALTIME
#include <time.h>
struct timespec
struct timespec{
time_t tv_sec;
/*Seconds. */
24
gettime()
#include <stdio.h>
#include <time.h>
int main (void)
{
int i = 0;
struct timespec t0,t1;
clock_gettime(CLOCK_REALTIME, &t0);
i = 0xFFFFFF;
while (i > 0)
i--;
clock_gettime(CLOCK_REALTIME, &t1);
printf ("%lu nsecs elapsed \n",
1000000000*(t1.tv_sec-t0.tv_sec)
+t1.tv_nsec-t0.tv_nsec);
return 0;
25
Delaying Functions
NAME
sleep
sleep for the specified number of
seconds
SYNOPSIS
#include <unistd.h>
unsigned int sleep(unsigned int seconds);
26
sleep() sample
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main (void)
{
time_t t0;
t0 = time(NULL);
27
Delaying Functions
NAME
nanosleep
pause execution for a specified time
SYNOPSIS
#include <time.h>
int nanosleep(const struct timespec *req,
struct timespec *rem);
e.g. on error
28
Function Pointers
29
Simplify coding
30
Syntax
Declaration:
<return type> (* variable) (parameters);
Examples:
int (* funcptr)(int a, int b);
int (* funcptr)(int, int);
int (* mypointer)(int a, int b);
31
#include <stdio.h>
int test1(int k)
{
return k;
}
int test2(int k)
{
return 2*k;
}
int main(void)
{
int (*funcptr)(int a);
/* Strict form */
funcptr = &test1;
printf ("%d\n", (*funcptr)(10));
/* 10 */
/* Short form */
funcptr = test2;
printf ("%d\n", funcptr(10));
/* 20 */
return 0;
}
/* Function pointer */
32
33
#include <stdio.h>
int getsum(int k, int l)
{
return k+l;
}
int getproduct(int k, int l)
{
return k*l;
}
/* 3rd parameter is function pointer */
void processnumbers (int num1, int num2, int (*funcptr)(int, int))
{
printf("%d\n", funcptr(num1, num2));
}
int main(void)
{
processnumbers(10, 20, getsum);
processnumbers(10, 20, getproduct);
return 0;
}
/* 30 */
/* 200 */
34
#include <stdio.h>
typedef foat (*funcptr)(foat, foat); /* funcptr is now a data type */
foat getsum(foat k, foat l)
{
return k+l;
}
foat getproduct(foat k, foat l)
{
return k*l;
}
int main(void)
{
funcptr a;
a = getsum;
printf("%f\n", a(6, 2));
/* 8.00 */
a = getproduct;
printf("%f\n", a(6, 2));
/* 12.00 */
return 0;
}
35
#include <stdio.h>
typedef foat (*funcptr)(foat, foat);
36
Signal
37
Signal
38
Signal Illustration
Process
Register
signal handler
Kernel calls
signal handler
when even occurs
Runs in
parallel
39
Signal Names
SIGABRT
SIGALRM
SIGBUS
SIGFPE
SIGCHLD
SIGHUP
SIGINT
SIGIO
SIGSEGV
SIGURG
signal()
NAME
signal - ANSI C signal handling
SYNOPSIS
#include <signal.h>
sighandler_t signal(int signum,
sighandler_t handler);
43
signal() Syntax
signum
handler
SIG_IGN
SIG_DFL
signum if success
SIG_ERR if error
Note:
45
Related Functions
pause()
46
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void sig_usr(int signo){
if(signo==SIGINT)
printf("Got SIGINT\n");
else
printf("Unknown signal number\n");
}
int main(void)
{
if( signal(SIGINT, sig_usr) == SIG_ERR ) {
printf("Error creating SIG_INT\n");
return 1;
}
while(1)
pause();
return 0;
} /* Run program and use CTRL+C */
47
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void sig_usr(int signo){
if(signo==SIGUSR1)
printf("Got SIGUSR1\n");
else
printf("Unknown signal number\n");
}
int main(void)
{
if( signal(SIGUSR1, sig_usr) == SIG_ERR ) {
printf("Error creating SIGUSR1\n");
return 1;
}
while(1)
pause();
return 0;
} /* Run program and use kill -USR1 to signal */
48
sigaction()
NAME
sigaction
POSIX signal handling function.
SYNOPSIS
#include <signal.h>
int sigaction(int signum,
const struct sigaction *act,
struct sigaction *oldact)
sigaction()
signum
act
oldact
struct sigaction
struct sigaction {
void (*sa_handler)(int);
void (*sa_sigaction)(int , siginfo_t *, void *);
sigset_t
sa_mask;
int
sa_fags;
void (*sa_restorer)(void);
};
sa_handler
Same as in signal()
sa_mask
sa_restorer is obsolete/unused
51
siginfo_t
siginfo_t {
int
si_signo; /* Signal number */
int
si_errno; /* An errno value */
int
si_code; /* Signal code */
pid_t si_pid; /* Sending process ID */
uid_t si_uid; /* Real user ID of sending process */
int
si_status; /* Exit value or signal */
clock_t si_utime; /* User time consumed */
clock_t si_stime; /* System time consumed */
sigval_t si_value; /* Signal value */
int
si_int; /* POSIX.1b signal */
void * si_ptr; /* POSIX.1b signal */
void * si_addr; /* Memory location which caused fault */
int
si_band; /* Band event */
int
si_fd;
/* File descriptor */
};
52
success
-1
error
53
siemptyset()
NAME
sigemptyset
POSIX signal set operation.
SYNOPSIS
#include <signal.h>
int sigemptyset(sigset_t *set);
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void sig_usr(int signo){
if(signo==SIGINT)
printf("Got SIGINT\n");
else
printf("Unknown signal number\n");
}
int main(void)
{
struct sigaction sa;
sa.sa_handler = sig_usr;
sigemptyset(&sa.sa_mask);
sa.sa_fags = 0;
if(sigaction(SIGINT, &sa, NULL) == -1) {
printf("Error creating SIG_INT\n");
return 1;
}
while(1)
pause();
return 0;
}
/* Run program and use CTRL+C */
55
Related Functions
kill()
raise()
56
kill()
#include
#include
#include
#include
<sys/types.h>
<unistd.h>
<signal.h>
<stdio.h>
int main(void)
{
pid_t
id; /* Holds PID of current child */
int
number;
id = fork();
if (id == 0) {
childprocess("Enter a number");
return 0;
}
/* Parent process */
while(1) {
scanf("%d", &number);
if (number == 123)
break;
printf("Try again\n");
}
kill(id, SIGUSR1); /* terminate repeating message */
printf("Finally!\n");
return 0;
}
/* main */
58
Related Functions
alarm()
59
alarm()
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void sig_alarm(int signo)
{
if(signo==SIGALRM) {
printf("Got SIGALRM\n");
alarm(10);
printf("Will generate alarm in 10 secs\n");
} else {
printf("Unknown signal number\n");
}
}
60
alarm()
int main(void)
{
struct sigaction act;
act.sa_handler = sig_alarm;
act.sa_fags = 0;
sigemptyset(&act.sa_mask);
if (sigaction (SIGALRM, &act, 0) == -1) {
perror (NULL);
return 1;
}
alarm (10);
printf ("Will generate alarm in 10 secs\n");
while(1)
pause();
return 0;
}
61
Interval Timers
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);
62
struct itimerval
struct itimerval {
struct timeval it_interval; /* next value */
struct timeval it_value; /* current value */
};
63
which parameter
ITIMER_REAL
ITIMER_VIRTUAL
ITIMER_PROF
Interval Timers
getitimer()
ITIMER_REAL,
ITIMER_PROF
ITIMER_VIRTUAL, or
setitimer()
Interval Timers
#include<stdio.h>
#include<signal.h>
#include<time.h>
#include<unistd.h>
#include<sys/time.h>
void myalarm(int signum)
{
if (signum == SIGALRM ) {
printf("Alarm\n");
signal ( SIGALRM, myalarm);
}
}
66
Interval Timers
int main(void)
{
struct itimerval timer;
67
End
68