forked from getiot/linux-c
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
all: | ||
gcc sleep_example.c -o sleep_example | ||
gcc simple_threading_timer.c -o simple_threading_timer -lrt | ||
gcc signal_interrupt_timer.c -o signal_interrupt_timer -lrt | ||
|
||
clean: | ||
rm sleep_example | ||
rm simple_threading_timer | ||
rm signal_interrupt_timer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
#include <signal.h> | ||
#include <unistd.h> | ||
#include <string.h> | ||
#include <errno.h> | ||
|
||
#define UNUSED(x) (void)(x) | ||
|
||
pid_t gettid(void); | ||
|
||
struct t_eventData { | ||
int myData; | ||
}; | ||
|
||
static void handler(int sig, siginfo_t *si, void *uc) | ||
{ | ||
UNUSED(sig); | ||
UNUSED(uc); | ||
struct t_eventData *data = (struct t_eventData *) si->_sifields._rt.si_sigval.sival_ptr; | ||
printf("[%lu] Timer fired %d - thread-id: %d\n", time(NULL), ++data->myData, gettid()); | ||
} | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
int res = 0; | ||
timer_t timerId = 0; | ||
|
||
struct sigevent sev = { 0 }; | ||
struct t_eventData eventData = { .myData = 0 }; | ||
|
||
/* specifies the action when receiving a signal */ | ||
struct sigaction sa = { 0 }; | ||
|
||
/* specify start delay and interval */ | ||
struct itimerspec its = { | ||
.it_value.tv_sec = 1, | ||
.it_value.tv_nsec = 0, | ||
.it_interval.tv_sec = 1, | ||
.it_interval.tv_nsec = 0 | ||
}; | ||
|
||
printf("Signal Interrupt Timer - thread-id: %d\n", gettid()); | ||
|
||
sev.sigev_notify = SIGEV_SIGNAL; // Linux-specific | ||
sev.sigev_signo = SIGRTMIN; | ||
sev.sigev_value.sival_ptr = &eventData; | ||
|
||
/* create timer */ | ||
res = timer_create(CLOCK_REALTIME, &sev, &timerId); | ||
|
||
if ( res != 0){ | ||
fprintf(stderr, "Error timer_create: %s\n", strerror(errno)); | ||
exit(-1); | ||
} | ||
|
||
/* specifz signal and handler */ | ||
sa.sa_flags = SA_SIGINFO; | ||
sa.sa_sigaction = handler; | ||
|
||
/* Initialize signal */ | ||
sigemptyset(&sa.sa_mask); | ||
|
||
printf("Establishing handler for signal %d\n", SIGRTMIN); | ||
|
||
/* Register signal handler */ | ||
if (sigaction(SIGRTMIN, &sa, NULL) == -1){ | ||
fprintf(stderr, "Error sigaction: %s\n", strerror(errno)); | ||
exit(-1); | ||
} | ||
|
||
/* start timer */ | ||
res = timer_settime(timerId, 0, &its, NULL); | ||
|
||
if ( res != 0){ | ||
fprintf(stderr, "Error timer_settime: %s\n", strerror(errno)); | ||
exit(-1); | ||
} | ||
|
||
printf("Press ENTER to Exit\n"); | ||
while(getchar()!='\n'){} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
#include <signal.h> | ||
#include <unistd.h> | ||
#include <string.h> | ||
#include <errno.h> | ||
|
||
pid_t gettid(void); | ||
|
||
struct t_eventData{ | ||
int myData; | ||
}; | ||
|
||
void expired(union sigval timer_data) | ||
{ | ||
struct t_eventData *data = timer_data.sival_ptr; | ||
printf("[%lu] Timer fired %d - thread-id: %d\n", time(NULL), ++data->myData, gettid()); | ||
} | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
int res = 0; | ||
timer_t timerId = 0; | ||
|
||
/* sigevent specifies behaviour on expiration */ | ||
struct sigevent sev = { 0 }; | ||
struct t_eventData eventData = { .myData = 0 }; | ||
|
||
/* specify start delay and interval | ||
* it_value and it_interval must not be zero */ | ||
|
||
struct itimerspec its = { | ||
.it_value.tv_sec = 1, | ||
.it_value.tv_nsec = 0, | ||
.it_interval.tv_sec = 1, | ||
.it_interval.tv_nsec = 0 | ||
}; | ||
|
||
printf("Simple Threading Timer - thread-id: %d\n", gettid()); | ||
|
||
sev.sigev_notify = SIGEV_THREAD; | ||
sev.sigev_notify_function = &expired; | ||
sev.sigev_value.sival_ptr = &eventData; | ||
|
||
/* create timer */ | ||
res = timer_create(CLOCK_REALTIME, &sev, &timerId); | ||
|
||
if (res != 0) { | ||
fprintf(stderr, "Error timer_create: %s\n", strerror(errno)); | ||
exit(-1); | ||
} | ||
|
||
/* start timer */ | ||
res = timer_settime(timerId, 0, &its, NULL); | ||
|
||
if (res != 0) { | ||
fprintf(stderr, "Error timer_settime: %s\n", strerror(errno)); | ||
exit(-1); | ||
} | ||
|
||
printf("Press ETNER Key to Exit\n"); | ||
while(getchar()!='\n'){} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <stdio.h> | ||
#include <signal.h> | ||
#include <unistd.h> | ||
#include <string.h> | ||
#include <time.h> | ||
|
||
static char msg[] = "I received a msg."; | ||
|
||
void show_msg(int signo) | ||
{ | ||
printf("[%lu] %s\n", time(NULL), msg); | ||
} | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
struct sigaction act; | ||
union sigval tsval; | ||
act.sa_handler = show_msg; | ||
act.sa_flags = 0; | ||
|
||
sigemptyset(&act.sa_mask); | ||
sigaction(50, &act, NULL); | ||
|
||
printf("Start\n"); | ||
|
||
while (1) { | ||
sleep(1); | ||
|
||
/*向主进程发送信号,实际上是自己给自己发信号*/ | ||
sigqueue(getpid(), 50, tsval); | ||
} | ||
|
||
return 0; | ||
} |