Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 951dba6

Browse files
Skeleton for Exercise 23-4
1 parent 19e57ed commit 951dba6

File tree

9 files changed

+152
-8
lines changed

9 files changed

+152
-8
lines changed

‎10_Time/curr_time.c‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*************************************************************************\
2+
* Copyright (C) Michael Kerrisk, 2024. *
3+
* *
4+
* This program is free software. You may use, modify, and redistribute it *
5+
* under the terms of the GNU Lesser General Public License as published *
6+
* by the Free Software Foundation, either version 3 or (at your option) *
7+
* any later version. This program is distributed without any warranty. *
8+
* See the files COPYING.lgpl-v3 and COPYING.gpl-v3 for details. *
9+
\*************************************************************************/
10+
11+
/* curr_time.c
12+
13+
Implement our currTime() function.
14+
*/
15+
#include <time.h>
16+
#include "curr_time.h" /* Declares function defined here */
17+
18+
#define BUF_SIZE 1000
19+
20+
/* Return a string containing the current time formatted according to
21+
the specification in 'format' (see strftime(3) for specifiers).
22+
If 'format' is NULL, we use "%c" as a specifier (which gives the'
23+
date and time as for ctime(3), but without the trailing newline).
24+
Returns NULL on error. */
25+
26+
char *
27+
currTime(const char *format)
28+
{
29+
static char buf[BUF_SIZE]; /* Nonreentrant */
30+
time_t t;
31+
size_t s;
32+
struct tm *tm;
33+
34+
t = time(NULL);
35+
tm = localtime(&t);
36+
if (tm == NULL)
37+
return NULL;
38+
39+
s = strftime(buf, BUF_SIZE, (format != NULL) ? format : "%c", tm);
40+
41+
return (s == 0) ? NULL : buf;
42+
}

‎10_Time/curr_time.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*************************************************************************\
2+
* Copyright (C) Michael Kerrisk, 2024. *
3+
* *
4+
* This program is free software. You may use, modify, and redistribute it *
5+
* under the terms of the GNU Lesser General Public License as published *
6+
* by the Free Software Foundation, either version 3 or (at your option) *
7+
* any later version. This program is distributed without any warranty. *
8+
* See the files COPYING.lgpl-v3 and COPYING.gpl-v3 for details. *
9+
\*************************************************************************/
10+
11+
/* curr_time.h
12+
13+
Header file for curr_time.c.
14+
*/
15+
#ifndef CURR_TIME_H
16+
#define CURR_TIME_H /* Prevent accidental double inclusion */
17+
18+
char *currTime(const char *fmt);
19+
20+
#endif

‎23_Timers_and_Sleeping/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test_3: Ex_23_3
1313
./Ex_23_3 EVP
1414

1515
test_4: ptmr_sigev_thread
16-
./ptmr_sigev_thread
16+
./ptmr_sigev_thread 60
1717

1818
# ------------------------------------------------------------------------------
1919
# Compile programs

‎23_Timers_and_Sleeping/README.md‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,22 @@ The sample output is:
3030
```text
3131
./Ex_23_2.sh
3232
Using t_nanosleep...
33-
Remaining: 0.000346101
34-
Slept for: 61.986514 secs
33+
Remaining: 0.000715167
34+
Slept for: 62.077613 secs
3535
Sleep complete
3636
Using Ex_23_2...
37-
Remaining: 0.000576292
38-
Slept for: 61.888986 secs
37+
Remaining: 9437184.000000012
38+
Slept for: 60.000036 secs
3939
Sleep complete
4040
Using t_clock_nanosleep...
41-
Interrupted... Slept: 62.007887 secs... Remaining: 0.000790663... Restarting
42-
Slept: 62.008789 secs
41+
Interrupted... Slept: 62.072626 secs... Remaining: 0.002018094... Restarting
42+
Slept: 62.074727 secs
4343
Sleep complete
4444
```
4545

46-
There is no meaningful difference between the three (3) versions. All of them overran the set interval by about two (2) seconds.
46+
There is no meaningful difference between the two (2) versions: t_nanosleep; and t_clock_nanosleep. Both of them overran the set interval by about two (2) seconds.
47+
48+
My solution was close to the set interval.
4749

4850
## Exercise 23-3
4951

‎23_Timers_and_Sleeping/curr_time.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../10_Time/curr_time.c

‎23_Timers_and_Sleeping/curr_time.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../10_Time/curr_time.h
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*************************************************************************\
2+
* Copyright (C) Michael Kerrisk, 2024. *
3+
* *
4+
* This program is free software. You may use, modify, and redistribute it *
5+
* under the terms of the GNU Lesser General Public License as published *
6+
* by the Free Software Foundation, either version 3 or (at your option) *
7+
* any later version. This program is distributed without any warranty. *
8+
* See the files COPYING.lgpl-v3 and COPYING.gpl-v3 for details. *
9+
\*************************************************************************/
10+
11+
/* itimerspec_from_str.c
12+
13+
Implement our itimerspecFromStr() function.
14+
*/
15+
#ifndef __APPLE__ /* Mac OS X doesn't define the 'itimerspec' structure
16+
(or the POSIX timer functions (timer_*()) */
17+
#include <string.h>
18+
#include <stdlib.h>
19+
#include "itimerspec_from_str.h" /* Declares function defined here */
20+
21+
/* Convert a string of the following form to an itimerspec structure:
22+
"value.sec[/value.nanosec][:interval.sec[/interval.nanosec]]".
23+
Optional components that are omitted cause 0 to be assigned to the
24+
corresponding structure fields. */
25+
26+
void
27+
itimerspecFromStr(char *str, struct itimerspec *tsp)
28+
{
29+
char *dupstr ,*cptr, *sptr;
30+
31+
dupstr = strdup(str);
32+
33+
cptr = strchr(dupstr, ':');
34+
if (cptr != NULL)
35+
*cptr = '0円';
36+
37+
sptr = strchr(dupstr, '/');
38+
if (sptr != NULL)
39+
*sptr = '0円';
40+
41+
tsp->it_value.tv_sec = atoi(dupstr);
42+
tsp->it_value.tv_nsec = (sptr != NULL) ? atoi(sptr + 1) : 0;
43+
44+
if (cptr == NULL) {
45+
tsp->it_interval.tv_sec = 0;
46+
tsp->it_interval.tv_nsec = 0;
47+
} else {
48+
sptr = strchr(cptr + 1, '/');
49+
if (sptr != NULL)
50+
*sptr = '0円';
51+
tsp->it_interval.tv_sec = atoi(cptr + 1);
52+
tsp->it_interval.tv_nsec = (sptr != NULL) ? atoi(sptr + 1) : 0;
53+
}
54+
free(dupstr);
55+
}
56+
#endif
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*************************************************************************\
2+
* Copyright (C) Michael Kerrisk, 2024. *
3+
* *
4+
* This program is free software. You may use, modify, and redistribute it *
5+
* under the terms of the GNU Lesser General Public License as published *
6+
* by the Free Software Foundation, either version 3 or (at your option) *
7+
* any later version. This program is distributed without any warranty. *
8+
* See the files COPYING.lgpl-v3 and COPYING.gpl-v3 for details. *
9+
\*************************************************************************/
10+
11+
/* itimerspec_from_str.h
12+
13+
Header file for itimerspec_from_str.c.
14+
*/
15+
#ifndef ITIMERSPEC_FROM_STR_H
16+
#define ITIMERSPEC_FROM_STR_H
17+
18+
#include <time.h>
19+
20+
void itimerspecFromStr(char *str, struct itimerspec *tsp);
21+
22+
#endif
26.8 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /