Questions tagged [ucontext]

The header defines mcontext_t and the associated functions getcontet, setcontext, swapcontext etc. in POSIX. Often used for implementing co-routines.

64 questions
2
votes
1 answer

Why is my function not running and getting an invalid memory error?

I'm trying to simulate threads swapcontext() and whatnot but I have some issues: The callback function doesn't run I get a memory error from free(): free(): invalid size Aborted How do I wait until all the threads are done? here's my…
Coder
  • 43
  • 5
2
votes
1 answer

Making yield function with timer in C

I want to write a code to switch between threads every 10 microseconds. But the problem is in the yield function. I get an interrupt while running the timer handler. So it doesn't finish properly. This is the code I have for initializing the…
2
votes
1 answer

`nanosleep()` async-signal safety on Linux

I need to use the nanosleep function in my user-space threads library to achieve waits of the approximately desired amount, as it can save the remaining time in case of an interrupt by async signals. I use SIGALRM to switch threads preemptively;…
jnbrq -Canberk Sönmez
  • 1,790
  • 1
  • 17
  • 29
2
votes
1 answer

How do I inform the compiler that `getcontext` can return multiple times?

getcontext can return multiple times. For example, I have sketched a C program analogous to the one demostrated here: #include #include #include #include struct communication { const ucontext_t…
fghzxm
  • 1,185
  • 7
  • 19
2
votes
1 answer

Why does srand(time(NULL)) gives me segmentation fault on main?

Need some help here. I want to understand what's happening in this code. I'm trying to generate random numbers as tickets to the TCP_t struct created inside ccreate function. The problem is, everytime I executed this code WITHOUT the…
2
votes
4 answers

Implementing a user level thread library - return value from makecontext

I've seen a few questions on user thread libraries, but none that seem to answer my question. I am able to create threads, run them, cancel them, and exit from them. What I cannot do for some reason is get a thread to return data. When I…
Carl
  • 83
  • 6
2
votes
1 answer

Why does getcontext and setjmp save different registers in glibc-x86-64

Here are their source codes: https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=sysdeps/unix/sysv/linux/x86_64/getcontext.S;hb=HEAD https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=sysdeps/x86_64/setjmp.S;hb=HEAD As you can see, getcontext…
tohava
  • 5,344
  • 1
  • 25
  • 47
2
votes
2 answers

setcontext and makecontext to call a generic function pointer

In another question I had the problem to port the code: unsigned long stack[] = { 1, 23, 33, 43 }; /* save all the registers and the stack pointer */ unsigned long esp; asm __volatile__ ( "pusha" ); asm __volatile__ ( "mov %%esp, %0" :"=m"…
Simone Margaritelli
  • 4,584
  • 10
  • 45
  • 70
2
votes
2 answers

Implementing swapcontext in terms of getcontext and setcontext

int swapcontext(ucontext_t *oucp, ucontext_t *ucp); int getcontext(ucontext_t *ucp); int setcontext(const ucontext_t *ucp); If my understanding is correct, swapcontext is equivalent to first calling getcontext on oucp and then calling setcontext on…
John Smith
  • 43
  • 4
2
votes
3 answers

getcontext und setcontext won't work in functions

i am trying to copy the context of a thread including the stack to create a checkpoint, which i can restore later on. For that reason i tried to move the call of getcontext and setcontext into a function which also saves the stack, but that wont…
sternstauner
  • 33
  • 1
  • 3
1
vote
1 answer

How do I pass arguments to makecontext()'s start routine?

I'd like to pass 3 arguments to thread_routine_handler like this: makecontext(&th->context, thread_routine_handler, 3, th, start_routine, arg); but I don't know how to read them properly. It returns the warning: warning: passing argument 2 of…
Jack
  • 16,276
  • 55
  • 159
  • 284
1
vote
1 answer

Ucontext gets blocked signal after swapcontext

I am blocking a SIGINT in ucontext A, for example, say I hit ^C (SIGINT) while it is running, nothing happens as expected. In context B, there is a SIGINT handler. When I swapcontext(A, B) then ucontext B immediately executes the signal handler.…
beans
  • 31
  • 5
1
vote
2 answers

Address boundary error when invoke swapcontext()

I've written a simple program, use ucontext library. However, a signal SIGSEGV (address boundary error) occurred. The running env is MacOS. I do not know what's wrong I made? Updated Here: Version 2 As @Jeremy suggest, we could use static on…
Jacky1205
  • 3,273
  • 3
  • 22
  • 44
1
vote
1 answer

How to call ucontext.h getcontext from inside a function

I am trying to call getcontext into another function (instead of calling it directly into main) in order to copy the thread's stack and restore it later. This code should print repeatedly, but it won't work once the function that called getcontext…
TheLinuxGK
  • 113
  • 3
1
vote
0 answers

How can I check if a context has completed and proceeded to the uc_link?

I'm setting up a scheduler in C and using setcontext and swapcontext a lot. I'm setting context to a function and swapping around to other functions using a timer, but I don't know how to check if the function has completed and has proceeded onto…
Steve
  • 88
  • 8