LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2008, 06:10 AM   #1
the_shani
LQ Newbie
 
Registered: Feb 2008
Posts: 5

Rep: Reputation: 0
ITIMER_REAL fires, but ITIMER_VIRTUAL does not fire


Hi All,

I am trying to implement a user level thread library using sigsetjmp and siglompjmp. Thread scheduling happens whenever a timer expires. I have written a sample program to test context switching when a timer expires. Below the code for the sample.

This is how the code is supposed to work:
The signal handler for SIGVTALRM, virtual timer (ITIMER_VIRTUAL) and stacks for the user level threads)(A and B) are setup. Then A starts executing. The virtual timer then goes off and B starts executing. At each timer interrupt execution switches between A and B.

The problem:
The sample works if I am using ITIMER_REAL, by works I mean the timer raises an interrupt periodically but when I am using ITIMER_VIRTUAL no interrupts are raised. Can anyone tell me why this is happening and how to fix it?

The below is working, but if I comment the 2 lines marked with "//$$$$$$" (to use SIGALRM and ITIMER_REAL) and uncomment the two lines marked with "//&&&&&&" (to use SIGVTALRM and ITIMER_VIRTUAL) the code stops working.

Thanks for your time and replies.

Note: The below code is for 32-bit machines, on 64-bit machines replace JB_SP with JB_RSP

##############start of code####################

#include <signal.h>
#include <sys/time.h>
#include <stdio.h>
#include <setjmp.h>

int flag = 0;
sigjmp_buf buf[2];

char stack[2][2048];

void handleVtAlarm (int sigNo)
{
if(0 != sigsetjmp(buf[flag], 1))
{
return;
}
else
{
flag = (flag + 1) % 2;
siglongjmp(buf[flag], 1);
}

}

void A()
{
while(1)
{
puts("A");
sleep(1);
}
}

void B()
{
while(1)
{
puts("B");
sleep(1);
}
}

void createThreads()
{
sigsetjmp(buf[0], 1);
buf[0][0].__jmpbuf[JB_PC] = (unsigned long)A;
buf[0][0].__jmpbuf[JB_SP] = (unsigned long)(&stack[0][0] + 2048);

sigsetjmp(buf[1], 1);
buf[1][0].__jmpbuf[JB_PC] = (unsigned long)B;
buf[1][0].__jmpbuf[JB_SP] = (unsigned long)(&stack[1][0] + 2048);
}


int main()
{
struct itimerval sTimerVal;
struct sigaction sSigAction;

sSigAction.sa_handler = &handleVtAlarm;
sigemptyset(&sSigAction.sa_mask);

if (0 != sigaction(SIGALRM, &sSigAction, NULL)) //$$$$$$
//if (0 != sigaction(SIGVTALRM, &sSigAction, NULL)) //&&&&&&&
{
printf("failed to set signal handler\n");
}

createThreads();

sTimerVal.it_interval.tv_sec = 2;
sTimerVal.it_interval.tv_usec = 0;
sTimerVal.it_value.tv_sec = 2;
sTimerVal.it_value.tv_usec = 0;

if (0 != setitimer(ITIMER_REAL, &sTimerVal, NULL)) //$$$$$$
//if (0 != setitimer(ITIMER_VIRTUAL, &sTimerVal, NULL)) //&&&&&&
{
printf("failed...\n");
}
else
{
printf("alarm set\n");
}

siglongjmp(buf[0], 1);
}



###############end of code#####################
 
Old 02-04-2008, 07:33 AM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Perhaps you're just not letting it run long enough? Unless the process is ACTIVELY doing work, the virtual timer is not decrementing -- so if it's just idle waiting on the signal, you will NEVER get that signal.
 
Old 02-05-2008, 01:57 AM   #3
nachiket
LQ Newbie
 
Registered: Feb 2008
Posts: 1

Rep: Reputation: 0
Hey ... I am also trying to do similar kind of thing and getting the same problem
I have tried using the least possible time value for the alarm to set, but still this is not working.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace fire fox 1.5.0.9 with fire fox2 in mepis 6.0 inspiron_Droid LinuxAnswers Discussion 0 02-21-2007 01:19 PM
LXer: Copyright fires up Linux in Australia LXer Syndicated Linux News 0 06-19-2006 12:21 AM
Slack 9.1 on laptop fires up my CPU ! C.Loko Slackware 1 11-21-2003 04:22 PM
Pls. help! Before my boss fires me! rhonneil Linux - Networking 1 09-18-2003 07:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration