Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <errno.h>
- #include <semaphore.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <time.h>
- #define SIZEMAP 1
- static void handler(int signal) {
- switch (signal) {
- case SIGUSR1:
- default:
- ;
- }
- }
- static void handler_install() {
- struct sigaction action;
- action.sa_handler = handler;
- action.sa_flags = 0;
- sigemptyset(&action.sa_mask);
- sigaction(SIGUSR1, &action, NULL);
- }
- void timespec_add(struct timespec * tv_a, const struct timespec * tv_b) {
- tv_a->tv_sec += tv_b->tv_sec;
- tv_a->tv_nsec += tv_b->tv_nsec;
- if (tv_a->tv_nsec > 999999999) {
- tv_a->tv_sec += 1;
- tv_a->tv_nsec -= 999999999;
- }
- }
- void timespec_minus(struct timespec * tv_a, const struct timespec * tv_b) {
- tv_a->tv_sec -= tv_b->tv_sec;
- tv_a->tv_nsec -= tv_b->tv_nsec;
- if (tv_a->tv_nsec < 0) {
- tv_a->tv_sec -= 1;
- tv_a->tv_nsec += 1000 * 1000 * 1000;
- }
- }
- int main() {
- int shm_fd;
- struct timespec* desc_map;
- struct timespec* t_temp;
- struct timespec* t_final;
- int i;
- shm_fd = shm_open("shm_testfork", O_RDWR | O_CREAT, 0666);
- if (!shm_fd) {
- fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno));
- exit(1);
- }
- if (ftruncate(shm_fd, (SIZEMAP * sizeof(struct timespec))) != 0) {
- fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno));
- exit(1);
- }
- handler_install();
- desc_map = malloc(sizeof(struct timespec));
- t_temp = malloc(sizeof(struct timespec));
- t_final = malloc(sizeof(struct timespec));
- t_final->tv_sec = 0;
- t_final->tv_nsec = 0;
- for (i = 0; i < 1000; i++) {
- clock_gettime(CLOCK_REALTIME, t_temp);
- if (fork() == 0) {
- pause();
- timespec_minus(t_temp, desc_map);
- timespec_add(t_final, t_temp);
- }
- else {
- clock_gettime(CLOCK_REALTIME, desc_map);
- kill(getppid(), SIGUSR1);
- kill(getpid(), SIGKILL);
- }
- }
- free(desc_map);
- free(t_temp);
- free(t_final);
- shm_unlink("shm_testfork");
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement