473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

context switching with setjmp

hi all,
cananybody please tell me that why the control transfers to f() and
then g() and then f() and so on,when longjmp(jbuf[0],1) is called in
main.
Here is the complete code.
#include <stdio.h>
#include <setjmp.h>
#include <signal.h>

#define SECOND 1000000

char stack1[4096];
char stack2[4096];

jmp_buf jbuf[2];

f()
{
int i=0;
while(1){
printf("in f (%d)\n",i++);
usleep(SECOND);
}
}

g()
{
int i=0;
while(1){
printf("in g (%d)\n",i++);
usleep(SECOND);
}
}
setup()
{
sigsetjmp(jbuf[0],1);
jbuf[0][0].__jmpbuf[JB_SP] = (unsigned) stack1 + 4096;
jbuf[0][0].__jmpbuf[JB_PC] = (unsigned) f;

sigsetjmp(jbuf[1],1);
jbuf[1][0].__jmpbuf[JB_SP] = (unsigned) stack2 + 4096;
jbuf[1][0].__jmpbuf[JB_PC] = (unsigned) g;

}

static toggle = 0;

void dispatch(int sig)
{
puts("in dispatch");
if (sigsetjmp(jbuf[toggle],1) == 1)
return;

puts("DISPATCH");
toggle = 1 - toggle;
siglongjmp(jbuf[toggle],1);
}
main()
{
signal(SIGALRM, dispatch);
setup();
ualarm(5*SECOND, 5*SECOND);
longjmp(jbuf[0],1);
}
Nov 14 '05 #1
2 3446
On 21 Sep 2004 20:50:39 -0700, ad************@gmail.com (aditya) wrote
in comp.lang.c:
hi all,
cananybody please tell me that why the control transfers to f() and
then g() and then f() and so on,when longjmp(jbuf[0],1) is called in
main.
Not anybody here, no.
Here is the complete code.
#include <stdio.h>
#include <setjmp.h>
#include <signal.h>

#define SECOND 1000000

char stack1[4096];
char stack2[4096];

jmp_buf jbuf[2];

f()
Implicit int has been outlawed in C for five years now. All
declarators must specify a type. Wake up and smell the coffee, or
other beverage of your choice, you need to write this:

int f()

....or even better:

int f(void)
{
int i=0;
while(1){
printf("in f (%d)\n",i++);
usleep(SECOND);
Not a standard function, and there doesn't seem to be a prototype in
scope, either.
}
}

g()
{
int i=0;
while(1){
printf("in g (%d)\n",i++);
usleep(SECOND);
}
}
setup()
{
sigsetjmp(jbuf[0],1);
There is no standard C library function 'sigsetjmp', you will need to
ask in a group for the compiler/operating system combination that
provides this non-standard extension.
jbuf[0][0].__jmpbuf[JB_SP] = (unsigned) stack1 + 4096;
jbuf[0][0].__jmpbuf[JB_PC] = (unsigned) f;
The C standard does not specify the contents of the 'jmp_buf' type,
nor does it define the results of a user program trying to modify
them.
sigsetjmp(jbuf[1],1);
jbuf[1][0].__jmpbuf[JB_SP] = (unsigned) stack2 + 4096;
jbuf[1][0].__jmpbuf[JB_PC] = (unsigned) g;


[snip]

Even for the platform you are working on, it is most likely that the
modifications you are doing to those buffers is invalid. But you will
need to ask in a platform-specific group, because the C standard
doesn't define any of this.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
ad************@gmail.com (aditya) wrote:
# hi all,
# cananybody please tell me that why the control transfers to f() and
# then g() and then f() and so on,when longjmp(jbuf[0],1) is called in
# main.

# setup()
# {
# sigsetjmp(jbuf[0],1);
# jbuf[0][0].__jmpbuf[JB_SP] = (unsigned) stack1 + 4096;
# jbuf[0][0].__jmpbuf[JB_PC] = (unsigned) f;
#
# sigsetjmp(jbuf[1],1);
# jbuf[1][0].__jmpbuf[JB_SP] = (unsigned) stack2 + 4096;
# jbuf[1][0].__jmpbuf[JB_PC] = (unsigned) g;
#
# }

# main()
# {
# signal(SIGALRM, dispatch);
# setup();
# ualarm(5*SECOND, 5*SECOND);
# longjmp(jbuf[0],1);
# }

The stack frame saved in setjmp has to be still on the stack when you do the
longjmp. When you do a setjmp in a function and then return from that function,
the jmpbuf becomes undefined and the results of longjmp to it are undefined.

Mucking around with the internals of a jmpbuf value is even more undefined.

Perhaps what you really want to do is use function pointers.

f() {...}
g() {...}
typedef int (*function)();
function switcheroo[2] = {f,g};
static toggle = 0;
void dispatch(int sig)
{
puts("in dispatch");
puts("DISPATCH");
toggle = 1 - toggle;
switcheroo[toggle]();
}

--
SM Ryan http://www.rawbw.com/~wyrmwif/
GERBILS
GERBILS
GERBILS
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Peter Smithson | last post by:
Hi, I've read this page - http://devrsrc1.external.hp.com/STK/impacts/i634.html but don't understand it. Here's the text - "Non-standard usage of setjmp() and longjmp() could result in...
5
by: candy | last post by:
hi all, Consider the following C code: void funct(){ }
8
by: Zheng Da | last post by:
I wrote a simple one as follow: typedef struct __myjmp_buf { int efp; int epc; }myjmp_buf; int mysetjmp(myjmp_buf env) {
5
by: JRB | last post by:
Hi, I'm creating a small C#program that communicates through the serial port. I have a separate thread that continuously takes in data from an external device in a while loop. The problem is that...
15
by: rover8898 | last post by:
Hello all, I used setjmp() in a recent of program of mine (it is not completed, so I have not the chance to test it out yet). I am not very profocient in C coding as are some of my co-workers....
13
by: Michael M. | last post by:
In Perl, it was: ## Example: "Abc | def | ghi | jkl" ## -"Abc ghi jkl" ## Take only the text betewwn the 2nd pipe (=cut the text in the 1st pipe). $na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;...
5
by: Spiros Bousbouras | last post by:
In the following assume that a is int and env is of type jmp_buf a = setjmp(env) ; Is it legal ? It looks reasonable but it seems to violate what is mentioned under "Environmental limits" of...
28
by: Ryan Liu | last post by:
Hi, I have a client/server application, using one thread/client approach. I see very high context switch/sec. What are the ways to reduce it? Each thread sleep longer in its endless loop if...
4
by: adlloyd | last post by:
Hi all, I've got an application that's written in C++ making use of MFC (VS6). Its purpose is to process SMS messages received from a GSM modem connected via a serial port (USB connection). The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.