I am working on a code with multiple number of threads and I want to print the time it took for me to complete task I assigned the i-th thread to do. Meaning I want to print the time each thread took to be done with the doSomeThing function
int main(int argc, char *argv[]){
// ...
i=0;
while (i < NumberOfThreads){
check = pthread_create(&(id_arr[i]), NULL, &doSomeThing, &data);
i++;
}
// ...
}
void* doSomeThing(void *arg){
// ...
}
if I add gettimeofday(&thread_start, NULL) before the pthread_create and then add gettimeofday(&thread_end, NULL) after the pthread_create, will I be actually measuring the time each thread took or just the time the main took? And if I put the gettimeofday inside the doSomething function wouldn't they create race-conditions?
If you have any idea on how to measure the time per thread please let me know, thank you.
You can certainly use gettimeofday inside the thread function itself. Using local (stack) variables is completely thread-safe - every thread runs on its own stack (by definition).
void* doSomeThing(void *arg){
struct timeval t0, t1, dt;
gettimeofday(&t0, NULL);
// do work
gettimeofday(&t1, NULL);
timersub(&t1, &t0, &dt);
fprintf(stderr, "doSomeThing (thread %ld) took %d.%06d sec\n",
(long)pthread_self(), dt.tv_sec, dt.tv_usec);
}
If you put the same code around pthread_create(), you would only see the amount of time it took for the thread to be created, not executed. If pthread_create blocked until the thread completed, there would be no point in ever using threads!
gettimeofday() measures elapsed time. If you want to measure CPU time, try this:
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stdint.h>
uint64_t time_used ( ) {
struct rusage ru;
struct timeval t;
getrusage(RUSAGE_THREAD,&ru);
t = ru.ru_utime;
return (uint64_t) t.tv_sec*1000 + t.tv_usec/1000;
}
...
uint64_t t1, t2;
t1 = time_used();
... do some work ...
t2 = time_used();
printf("used %d milliseconds\n",(unsigned)(t2-t1));
You will have to do that inside the thread. This is an example. Search time_used
Jonathon Reinhart uses timersub(), which simplifies things. I merge that in here:
void time_used ( struct timeval *t ) {
struct rusage ru;
getrusage(RUSAGE_THREAD,&ru);
*t = ru.ru_utime;
}
...
struct timeval t0, t1, dt;
time_used(&t0);
... do some work ...
time_used(&t1);
timersub(&t1, &t0, &dt);
printf("used %d.%06d seconds\n", dt.tv_sec, dt.tv_usec);
Related
I have created two array of threads using POSIX thread.There are two thread functions student and teacher(I have not shown them here). My sample program is given below. I want to make a time limit(say 10 sec) after which the main thread will automatically exit no matter if the corresponding threads have completed or not. How will I do that?
Sample code fragment:
int main(void)
{
pthread_t thread1[25];
pthread_t thread2[6];
int i;
int id1[25]; //for students
int id2[6]; //for teachers
for(i=0;i<25;i++)
{
id1[i]=i;
id2[i]=i;
pthread_create(&thread1[i],NULL,student,(void*)&id1[i] );
if(i<6)
{
pthread_create(&thread2[i],NULL,teacher,(void*)&id2[i]);
}
}
for (i=0;i<25;i++)
{
pthread_join(thread1[i],NULL);
if(i<6)
{
pthread_join(thread2[i],NULL);
}
}
return 0;
}
What additional things will I have to add to the above code to terminate the main thread after a certain time? (say: 10 seconds)
what you need is pthread timed join. See the snippet below
struct timespec
{
time_t tv_sec; /* sec */
long tv_nsec; /* nsec */
};
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
{
printf("ERROR\n");
}
ts.tv_sec += 10; //10 seconds
int st = pthread_timedjoin_np(thread, NULL, &ts); //only wait for 10 seconds
if (st != 0)
{
printf("ERROR\n");
}
For additional info refer the man page http://man7.org/linux/man-pages/man3/pthread_tryjoin_np.3.html
If you just want the whole process to be terminated after 10 seconds of waiting time, you just have to replace the whole for-loop with your pthread_join calls by a suitable sleep function. You could use nanosleep, clock_nanosleep, thrd_sleep or just
sleep(10);
After that your main function would go out of scope and terminate the process.
Beware, all these functions are sensible to signals that arrive in the middle.
One way to do this is to create another thread which will sleep for 10 seconds, then call exit() (which will terminate the entire process):
void *watchdog(void *arg)
{
sigset_t all_sigs;
/* Block all signals in this thread, so that we do not have to
* worry about the sleep() being interrupted. */
sigfillset(&all_sigs);
sigprocmask(SIG_BLOCK, &all_sigs, NULL);
sleep(10);
exit(0);
return NULL; /* not reached */
}
Create this thread from the main thread after creating the other threads, and detach it:
pthread_create(&watchdog_thread, NULL, watchdog, NULL);
pthread_detach(watchdog_thread);
Now your process will end either when the main thread finishes after joining the other threads, or when the watchdog thread calls exit(), whichever happens first.
Hi I am writing a C program to interface a serial device which gives data at regular intervals, i need to look for the inputs at the serial port at regular intervals. this can be done by a ' read' function . but i dont know how to call it frequently at fixed time intervals ?
This sort of behavior short-circuits the lovely machinery built in to most OSes to do just this, failing that something like cron would seem to be a lovely option. Failing all of that (if you're just looking for a quick hacky option) busy wait is not super awesome, the system isn't bright enough to hyperthread around that so your program winds up eating up a core doing nothing for the duration of your program, so while it's largely a matter of taste, I'm a nanosleep man myself.
on nix/nux systems:
#include <time.h>
int main(void)
{
struct timespec sleepytime;
sleepytime.tv_sec = seconds_you_want_to_sleep
sleepytime.tv_nsec = nanoseconds_you_want_to_sleep
while( !done)
{
nanosleep(&sleepytime, NULL);
//do your stuff here
}
return 0;
}
if you're worried about getting interrupted, the second parameter should be another timespec struct, in which will be stored the amount of time remaining, check if == 0,
then keep on trucking.
in windows apparently it is a little easier.
#include <windows.h>
int main(void)
{
while( !done)
{
Sleep(milliseconds_you_want_to_sleep);
//do your stuff here
}
return 0;
}
Unfortunately I don't run windows so I haven't been able to test the second code sample.
If you really need to read at regular intervals ( and not just poll for data to be available ) , you can do something like this :
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
void timer_handler (int signum)
{
static int count = 0;
printf ("timer expired %d times\n", ++count);
}
int main ()
{
struct sigaction sa;
struct itimerval timer;
/* Install timer_handler as the signal handler for SIGVTALRM. */
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &timer_handler;
sigaction (SIGVTALRM, &sa, NULL);
/* Configure the timer to expire after 250 msec... */
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = 250000;
/* ... and every 250 msec after that. */
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 250000;
/* Start a virtual timer. It counts down whenever this process is
executing. */
setitimer (ITIMER_REAL, &timer, NULL);
/* Do busy work. */
while (1);
}
I copied this from http://www.informit.com/articles/article.aspx?p=23618&seqNum=14 and changed the timer type, effectively you are setting up an interval timer and handling the signal when the timer runs out.
I have made an application that monitors an interface and returns a packets per second reading, how ever when executed it runs fine for about 30 seconds till I open a YouTube page to get the counter running a little high. A couple of seconds later the application freezes and does nothing. This happens in irregular intervals so Im guessing its something with the counting, theres the code, its written in C.
#include <stdio.h>
#include <pcap.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <pthread.h>
void callThreads(u_char *useless, const struct pcap_pkthdr* pkthdr, const u_char* packet);
void *packetcalc(void *threadid);
static struct timespec time1, time2;
static int t = 0, i = 0;
static long rc;
static pthread_t threads[1];
int main(int argc, char *argv[]){
pcap_t* descr;
char errbuf[PCAP_ERRBUF_SIZE];
descr = pcap_open_live("eth0", BUFSIZ, 1, -1, errbuf);
if(descr == NULL){
printf("Error: pcap_open_live()\n");
return 1;
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
pcap_loop(descr, 0, callThreads, NULL);
return 0;
}
void callThreads(u_char *useless, const struct pcap_pkthdr* pkthdr, const u_char* packet){
if(i >= 2147483647){
//In case i gets full from counting too many packets
i = 0;
time1.tv_sec = 0;
}
++i;
rc = pthread_create(&threads[t], NULL, packetcalc, (void *) t);
}
void *packetcalc(void *threadid){
static int j;
static int temp = 0;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
if(temp != time1.tv_sec){
j = (i / time1.tv_sec);
printf("Packets: %d/sec\t(%d)\t(%d)\n", j, i, (int)time1.tv_sec);
temp = time1.tv_sec;
}
pthread_exit(NULL);
}
Edit: Could it also be that I'm running this code in a VM that only has 1 CPU allocated to it due to the multithreading?
You are creating a thread per packet, which is a horrible idea. It should be enough to just print whatever counter you need right out of the callback function you give to pcap_loop(3).
There are several problems with your code. First, you create the threads using the default thread attributes, which means that they are created as joinable threads, i.e. you must call pthread_join() later or the thread control structures would remain lingering around. Hence there is a memory leak in your code. May be you should check the return value from pthread_create in order to detect when an error occurs, e.g. the system was not able to create new threads and your packet counting routine has stopped being invoked. You can also create new threads in detached state using the following code:
pthread_attr_t attr;
pthread_attribute_init(&attr);
pthread_attribute_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&threadid, &attr, packetcalc, (void *) t);
pthread_attribute_destroy(&attr);
Detached threads do not need to be joined later. They release all resources upon thread exit.
Second, you threads use some global variables as if they are private when in reality they are shared. This includes the global time1, as well as the local j and temp, which are declared static and hence are shared among the threads.
Note that creating threads is an expensive operation. While your code waits for pthread_create to finish, new packets may arrive and fill up the circular buffer, used by libpcap, hence you might lose some packets. As a matter of fact, using one thread per packet is a very bad idea. Instead use only two threads - one that runs the pcap loop and one that periodically counts the number of packets and calculates and prints the packet rate.
In my application, i need to calculate each thread's execution time [ literally time it has taken from starting of the pthread and its execution termination]. Termination can be of type 'pthread_exit' or explicit cancellation. In the following code, i have used pthread specfic data to retain the starting time of each thread, and hence i could find the total time. Do you guys think the following approach makes sense? IF NOT, input is really appreciated!!!. For testing purpose, thread cancels by itself after some period of sleep.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
typedef struct _pTime
{
time_t stime;
}pTime;
pthread_key_t kstime;
void cancelRoutine (void * arg)
{
pTime etime, *btime;
time (&(etime.stime));
printf (" Inside cancelRoutine ...tid: %l \n", pthread_self());
btime = (pTime *) pthread_getspecific (kstime);
printf ("Time taken : %lf ", difftime (etime.stime, btime->stime));
}
void * tfunction ( void * arg)
{
int waitTime = (int) arg;
printf ("\n Wait Time is %ud ", waitTime);
pTime *start;
start = (pTime *) malloc (sizeof (pTime));
time (&(start->stime));
pthread_setspecific (kstime, start);
pthread_cleanup_push (cancelRoutine, NULL);
printf (" Invoking the thread \n");
/* Doing Certain Work here */
sleep (waitTime);
pthread_cancel ( pthread_self());
sleep(waitTime);
pthread_cleanup_pop (NULL);
}
int main ( int argc, char **argv)
{
pthread_t tid[2];
int toBeSpend=10, i;
pthread_key_create(&kstime, NULL);
for (i=0; i<2; i++)
pthread_create (&tid[i], NULL, tfunction, (void *)(toBeSpend*(i+1)));
sleep (3);
for(i=0; i<2; i++)
pthread_join (tid[i], NULL);
}
It seems OK to me (though I'm not fond of specific thread variables, I rather use thread signal handlers to catch the "cancellation", though your code seems kinda nifty)
One thing you should improve is the calling of time (&(start->stime));
That should be the first thing that the thread function should do (on a local var first, and then copy it to the malloc'ed pTime) since you're calling system calls before that (takes a lot of time, relativity, and can also block).
also, you might want to condier using a profiling tools, such as gprof.
Can someone help me to complete my code with a function that can check the result of a timer "check_timer" and another one that reset this timer if it had expired "reset_timer"?
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <sys/time.h>
#define GLOBAL_TIMER 8 * 60 * 60
typedef struct protocol_type {
int protocol_number;
char *protocol_name;
pthread_t thread_timer_id;
} protocol_type_t;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
/* call back function - inform the user the time has expired */
void timeout_call_back(pthread_t thread_id)
{
printf("Welcome thread %ld, your time is up ===\n", pthread_self());
// doing some other stuff
}
/* Go to sleep for a period of seconds */
static void* start_timer(void *args)
{
/* function pointer */
void (*finish_function)(pthread_t);
int seconds = *((int*) args);
finish_function = timeout_call_back;
struct timeval now;
struct timespec timeout;
pthread_mutex_lock(&mut);
printf("thread ID : %ld, are waiting for %d seconds to to expire\n", pthread_self(), seconds);
gettimeofday(&now, NULL);
timeout.tv_sec = now.tv_sec + seconds;
timeout.tv_nsec = now.tv_usec * 1000;
pthread_cond_timedwait(&cond, &mut, &timeout);
(*finish_function)(pthread_self());
pthread_mutex_unlock(&mut);
pthread_exit(NULL);
}
// This function is in MT environnement and is running inside a daemon
int received_buffer_parser(char *received_buffer) {
pthread_mutex_t mut_main = PTHREAD_MUTEX_INITIALIZER;
protocol_type_t *my_protocol;
// Identify protocol type
my_protocol = protocol_identifier(received_buffer);
// Check if i received it in the last 8 hours in safe
pthread_mutex_lock(&mut_main);
if (check_timer(my_protocol->thread_id) has expired) { // I want to write this function
// I want to reset thread timer
launch_new_timer(my_protocol->thread_id)
}
else {
// doing some stuff
// dump data to the disk
}
pthread_mutex_unlock(&mut_main);
return 0;
}
int launch_new_timer(pthread_t thread_id)
{
int rc;
int seconds = GLOBAL_TIMER;
rc = pthread_create(&thread_id, NULL, start_timer, (void *) &seconds);
if(rc)
printf("Failed to create thread1\n");
pthread_join(thread_id, NULL);
return 0;
}
Clarification
I clarify here the real context of my code:
I receive from the network some types of different protocols packets(ICMP, SSH, RIP, OSPF, BGP...), and i want to:
identify every type of packets, let say with : identify_protocol(char *received_buffer), I got this function, it's ok.
Check if i receive this type of protocols in the last 8 hours (THE TIMER OF EACH PROTOCOL TYPE EXPIRE AFTER 8 HOURS), two choices:
a. if so, I dump the result data into a specific file on the disk.
b. no, I didn't receive this type in the last 8 HOURS i create a new thread (in my code i simplify, with thread1, thread2 and thread3, this threads are 3 threads used to be a timers for three protocols types) - i start a new timer with : start_timer(void *args) function do this job.
My main question is how to be able to check the result of my timers in a safe manner and then decide i reset the timer or not.
I design the finish_function at the beginning to help me to check when the timer has expired.
Feel free to give me a better design for best performances for my program.
My system is Linux.
To check for timers that merely expire, you don't need to use threads and synchronization at all. Simply keep global variables indicating the start time of the timer. So
when the timer starts, set a global variable (one per protocol) to gettimeofday()
when you want to check whether the timer has expired for a protocol, see whether gettimeofday()-starttime(protocol) is <8h
If you want to be notified on timer expiry, I recommend to use alarm(2). It only has second resolution, but that should be good enough for 8h timeouts. Whenever a timer is set, cancelled, or reset, compute the minimum timeout of any of the timers, then call alarm with that timeout. In the signal handler, perform the processing that you want to do on reception of timeout. Alternatively, do nothing in the signal handler, and just trust that any pending system call will be interrupted, and check all timers for expiry on EINTR.
Edit: alarm works like this
#include <unistd.h>
#include <signal.h>
void timeout(int ignored)
{
printf("timed out\n");
}
void main()
{
signal(SIGALRM, timeout);
alarm(10);
pause();
}