From: Simone Piccardi Date: Thu, 13 May 2010 12:46:19 +0000 (+0000) Subject: Materiale rimasto indietro e prove con i timer POSIX X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=commitdiff_plain;h=b028573fbe90da6c49b6de39e5e21495bf7b9767 Materiale rimasto indietro e prove con i timer POSIX --- diff --git a/signal.tex b/signal.tex index 536cf98..a147ae6 100644 --- a/signal.tex +++ b/signal.tex @@ -2995,13 +2995,17 @@ associato al \textit{process time} di un processo, la funzione \item[\errcode{ENOSYS}] non c'è il supporto per ottenere l'orologio relativo al \textit{process time} di un altro processo, e \param{pid} non corrisponde al processo corrente. - \item[\errcode{EPERM}] - \item[\errcode{ENOENT}] non c'è modo di avere un + \item[\errcode{EPERM}] l'indirizzo di \param{res} non è valido. + \item[\errcode{ENOENT}] non c'è modo di avere un tempo affidabile. \item[\errcode{ESRCH}] non esiste il processo \param{pid}. \end{errlist} } \end{functions} +La funzione ritorna l'identificativo di un orologio di sistema associato ad un +processo + + % TODO trattare gli orologi ad alta definizione e le funzioni POSIX per gli % stessi cioè: diff --git a/sources/chechmcheck.c b/sources/chechmcheck.c new file mode 100644 index 0000000..227180a --- /dev/null +++ b/sources/chechmcheck.c @@ -0,0 +1,94 @@ +/* checkmcheck.c + * + * Copyright (C) 2009 Simone Piccardi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/**************************************************************** + * + * Program checkmcheck: + * Some sample tests on mcheck + * + * Author: Simone Piccardi + * Jun. 2009 + * + * Usage: errcode -h give all info's + * + ****************************************************************/ +/* + * Include needed headers + */ +#include /* error definitions and routines */ +#include /* C standard library */ +#include /* unix standard library */ +#include /* standard I/O library */ +#include /* C strings library */ +#include /* Malloc checking library */ + + +void myabort(enum mcheck_status status); +void printcode(enum mcheck_status status); + +int main(int argc, char *argv[]) +{ +/* + * Variables definition + */ + int ret; + void * ptr; + /* + */ + ret = mcheck(myabort); + ptr = malloc(1000); + printcode(mprobe(ptr)); + free(ptr); + printcode(mprobe(ptr)); + free(ptr); + printcode(mprobe(ptr)); + + /* normal exit */ + return 0; +} + +void printcode(status) +{ + switch (status) { + case MCHECK_DISABLED: + printf("Status code = MCHECK_DISABLED\n"); + return; + case MCHECK_OK: + printf("Status code = MCHECK_OK\n"); + return; + case MCHECK_HEAD: + printf("Status code = MCHECK_HEAD\n"); + return; + case MCHECK_TAIL: + printf("Status code = MCHECK_TAIL\n"); + return; + case MCHECK_FREE: + printf("Status code = MCHECK_FREE\n"); + return; + default: + return; + } +} +/* + * routine to print code and abort + */ +void myabort(enum mcheck_status status) +{ + printcode(status); + abort(); +} diff --git a/sources/mygetclock.c b/sources/mygetclock.c new file mode 100644 index 0000000..ffbce51 --- /dev/null +++ b/sources/mygetclock.c @@ -0,0 +1,107 @@ +/* mygetclock.c + * + * Copyright (C) 2010 Simone Piccardi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/***************************************************************************** + * + * File mygetclock.c: An example host command + * + * Author: S. Piccardi May. 2010 + * + *****************************************************************************/ +#include /* C standard library */ +#include /* standard I/O library */ +#include /* unix standard library */ +#include /* time library */ + + +/* + * Program mygetclock + */ +/* Help printing routine */ +void usage(void); + +int main(int argc, char *argv[]) +{ +/* + * Variables definition + */ + int i; + clockid_t clockid; + pid_t pid; + struct timespec time; + /* + * Input section: decode command line parameters + * Use getopt function + */ + opterr = 0; /* don't want writing to stderr */ + while ( (i = getopt(argc, argv, "h")) != -1) { + switch (i) { + /* + * Handling options + */ + case 'h': /* help option */ + printf("Wrong -h option use\n"); + usage(); + return -1; + break; + case '?': /* unrecognized options */ + printf("Unrecognized options -%c\n",optopt); + usage(); + default: /* should not reached */ + usage(); + } + } + /* *********************************************************** + * + * Options processing completed + * + * Main code beginning + * + * ***********************************************************/ + if ((argc - optind) != 1) { + printf("Wrong number of arguments %d\n", argc - optind); + usage(); + } + + pid = atoi(argv[optind]); + if (pid > 0) { + if (clock_getcpuclockid(pid, &clockid) != 0) { + perror("Cannot get clockid"); + exit(1); + } + } else { + clockid = CLOCK_PROCESS_CPUTIME_ID; + } + if (clock_gettime(clockid, &time) != 0) { + perror("Cannot get time"); + exit(1); + } else { + printf("Tempo %s", ctime(&time.tv_sec)); + } + exit(0); +} +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program mygetclock: prende un orologio \n"); + printf("Usage:\n"); + printf(" mygetclock [-h] PID \n"); + printf(" -h print this help\n"); + exit(1); +} diff --git a/sources/splicecp2.c b/sources/splicecp2.c new file mode 100644 index 0000000..4f1a756 --- /dev/null +++ b/sources/splicecp2.c @@ -0,0 +1,153 @@ +/* splicecp.c + * + * Copyright (C) 2007 Simone Piccardi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/**************************************************************** + * + * Program splicecp.c: + * A sample program that copy a file using splice + * + * Author: Simone Piccardi + * Aug. 2007 + * + ****************************************************************/ +/* + * Include needed headers + */ +#define _GNU_SOURCE +#include /* file control functions */ +#include /* unix standard library */ +#include /* C standard library */ +#include /* error definitions and routines */ +#include /* standard I/O library */ +#include /* C strings library */ +#include /* file characteristics constants and functions */ +#include /* primitive system data types */ + +#include "macros.h" + +/* + * Function and globals definitions + */ +void usage(void); /* Help printing routine */ + +/* + * Main program + */ +int main(int argc, char *argv[]) +{ + /* + * Variables definition + */ + int i; + int size = 4096; + int pipefd[2]; + int in_fd, out_fd; + int nread, nwrite; + /* + * Input section: decode command line parameters + * Use getopt function + */ + opterr = 0; /* don't want writing to stderr */ + while ( (i = getopt(argc, argv, "hs:")) != -1) { + switch (i) { + /* + * Handling options + */ + case 'h': /* help option */ + printf("Wrong -h option use\n"); + usage(); + return -1; + break; + case 's': /* take wait time for childen */ + size = strtol(optarg, NULL, 10); /* convert input */ + break; + case '?': /* unrecognized options */ + printf("Unrecognized options -%c\n", optopt); + usage(); + default: /* should not reached */ + usage(); + } + } + /* + * Main body + */ + if ((argc - optind) != 2) { /* There must two argument */ + printf("Wrong number of arguments %d\n", argc - optind); + usage(); + } + /* open pipe, input and output file */ + if (pipe(pipefd) == -1) { + perror("Cannot create buffer pipe"); + exit(EXIT_FAILURE); + } + in_fd = open(argv[optind], O_RDONLY); + if (in_fd < 0) { + printf("Input error %s on %s\n", strerror(errno), argv[optind]); + exit(EXIT_FAILURE); + } + out_fd = open(argv[optind+1], O_CREAT|O_RDWR|O_TRUNC, 0644); + if (out_fd < 0) { + printf("Cannot open %s, error %s\n", argv[optind+1], strerror(errno)); + exit(EXIT_FAILURE); + } + /* copy loop */ + debug("Size %d\n", size); + while (1) { + nread = splice(in_fd, NULL, pipefd[1], NULL, size, 0); + debug("read %d bytes\n", nread); + if (nread == 0) break; + if (nread < 0) { + if (errno == EINTR) { + continue; + } else { + perror("read error"); + exit(EXIT_FAILURE); + } + } + do { + nwrite = splice(pipefd[0], NULL, out_fd, NULL, nread, 0); + debug("write %d bytes\n", nwrite); + if (nwrite == 0) continue; + if (nwrite < 0) { + if (errno == EINTR) + continue; + else { + perror("write error"); + exit(EXIT_FAILURE); + } + } + nread -= nwrite; + debug("left %d bytes\n", nread); + } while (nread); + } + return EXIT_SUCCESS; +} + + + +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program splicecp: copy two file using splice syscall\n"); + printf("Usage:\n"); + printf(" splicecp [-h] [-s N] filesrc filedst \n"); + printf(" -h print this help\n"); + printf(" -s N set a buffer size of N bytes \n"); + exit(1); +}