How do I pass arguments to functions with makecontext?

I would like to pass arguments to functions that get context switched into but I'm having problems...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <ucontext.h>
#include <iostream>
#include <cstdlib>
static ucontext_t main_context;
static ucontext_t func1_context;
static ucontext_t func2_context;
static ucontext_t func3_context;
#define EXIT_SUCCESS 0
using std::cout;using std::endl;
static void func1(int k)
{
    cout<<"func1: started.\n";
	cout<<"argument is: "<<k<<endl;
    cout<<"context switching into function 3.\n";
    if (swapcontext(&func1_context, &func3_context) == -1){
		cout<<"an error occured.\n";
	} 	cout<<"exiting from func1.\n";
}
static void func2(int i)
{
    cout<<"func2: started.\n";
	cout<<"argument is: "<<i<<endl;
	cout<<"context switching into function 1\n";
    if (swapcontext(&func2_context, &func1_context) == -1){
		cout<<"an error occured.\n";
	} 	cout<<"exiting from func2.\n";
}
static void func3(int p){
	cout<<"func3: started.\n";
	/*
	cout<<"context switchign into main,\n";
	if(swapcontext(&func3_context, &main_context) == -1){
		cout<<"an error occured.\n";
	}*/
	cout<<"argument is: "<<p<<endl;
	cout<<"func3 returning.\n";
}
int main(int argc, char *argv[])
{
    char func1_stack[16384];
    char func2_stack[16384];
	char func3_stack[16384];
   if (getcontext(&func1_context) == -1){
		cout<<"an error occured.\n";
   }
	//stack used by func1_context
    func1_context.uc_stack.ss_sp = func1_stack;
    func1_context.uc_stack.ss_size = sizeof(func1_stack);
    func1_context.uc_link = &func3_context;
    makecontext(&func1_context, func1, 1);	
   if (getcontext(&func2_context) == -1){
		cout<<"an error occured.\n";
	}
	//stack used by func2_context
    func2_context.uc_stack.ss_sp = func2_stack;
    func2_context.uc_stack.ss_size = sizeof(func2_stack);
    func2_context.uc_link = &func1_context;
    makecontext(&func2_context, func2, 1);
	if(getcontext(&func3_context) == -1){
		cout<<"an error occured.\n";
	}
	//stack used by func3_context
	func3_context.uc_stack.ss_sp = func3_stack;
	func3_context.uc_stack.ss_size = sizeof(func3_stack);
	func3_context.uc_link = NULL;
	makecontext(&func3_context, func3, 1);
	
	cout<<"context switching into function 2.\n";
    if (swapcontext(&main_context, &func2_context) == -1){
		cout<<"an error occured.\n";
	}
	
	cout<<"just came back from function 3.\n";
	cout<<"main: exiting.\n";
	return 0;
}


ERRORS
in function int main(int,char**)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
In function 'int main(int, char**)':
50:41: error: invalid conversion from 'void (*)(int)' to 'void (*)()' [-fpermissive]
In file included from 1:0:
/usr/include/ucontext.h:47:13: note: initializing argument 2 of 'void makecontext(ucontext_t*, void (*)(), int, ...)'
 extern void makecontext (ucontext_t *__ucp, void (*__func) (void),
             ^
58:41: error: invalid conversion from 'void (*)(int)' to 'void (*)()' [-fpermissive]
In file included from 1:0:
/usr/include/ucontext.h:47:13: note: initializing argument 2 of 'void makecontext(ucontext_t*, void (*)(), int, ...)'
 extern void makecontext (ucontext_t *__ucp, void (*__func) (void),
             ^
66:38: error: invalid conversion from 'void (*)(int)' to 'void (*)()' [-fpermissive]
In file included from 1:0:
/usr/include/ucontext.h:47:13: note: initializing argument 2 of 'void makecontext(ucontext_t*, void (*)(), int, ...)'
 extern void makecontext (ucontext_t *__ucp, void (*__func) (void),


thanks in advance
Last edited on
Which line of the program is the error?
Line 50, 58 and 66.38
Topic archived. No new replies allowed.