Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8226743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:52:51+00:00 2026-06-07T15:52:51+00:00

I use sigsetjmp/siglongjmp to change the progame stack. This is the demo: #include <stdio.h>

  • 0

I use sigsetjmp/siglongjmp to change the progame stack. This is the demo:

#include <stdio.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#define POOLSIZE 4096
int active = 0;
int total = 0;

struct thread
{
    int tid;
    sigjmp_buf env;
    char buf[4096];
    int state;
    ssize_t size;
};

struct thread *thread_pool = 0L;
char* anchor_beg = 0L;
char* anchor_end = 0L;
void(*new_thread)(int) = 0L;


void sig_call(int sig)
{
    char anchor;
    anchor_end = &anchor;
    if(sigsetjmp(thread_pool[active].env, 0) == 0)
    {
        thread_pool[active].size = anchor_beg - anchor_end;
        memcpy(thread_pool[active].buf, anchor_end, thread_pool[active].size);
        siglongjmp(thread_pool[0].env, 1);
    }
    else
    {
        memcpy(anchor_beg - thread_pool[active].size, thread_pool[active].buf, thread_pool[active].size);
    }
}

void thread_new(void(*pfn)(int))
{
    alarm(0);
    new_thread = pfn;
    thread_pool[0].state = 2;
//    printf("create new thread:%d\n", total + 1);
    raise(SIGUSR1);
}

void test(int thread)
{
    int i = 0; 
    for(;i != 1000000; i++)
    {
    }
}

void thread_main(int thread)
{
    int i = 0;
    for(i = 0; i < 4000; i++)
        thread_new(test);
}

void call(void(*pfn)(int))
{
    active = ++ total;
    thread_pool[active].tid = active;
    thread_pool[active].state = 1;
    ualarm(500, 0);
    pfn(active);
    thread_pool[active].state = 0;
}

void dispatcher()
{
    thread_pool = (struct thread*)malloc(sizeof(struct thread) * POOLSIZE);
    char anchor;
    anchor_beg = &anchor;
    thread_pool[0].tid = 0;
    thread_pool[0].state = 1;
    if(sigsetjmp(thread_pool[0].env, 0) == 0)
    {
        signal(SIGUSR1, sig_call);
        signal(SIGALRM, sig_call);

        call(thread_main);
    }
    else if(thread_pool[0].state == -1)
    {
        return;
    }
    else if(thread_pool[0].state == 2)
    {
        thread_pool[0].state = 1;
        call(new_thread);
    }

    while(1)
    {
        int i, alive = 0;
        for(i = 1; i <= total; i++)
        {
            if(thread_pool[i].state == 1)
            {
                alive ++;
                ualarm(500, 0);
                active = thread_pool[i].tid;
                siglongjmp(thread_pool[i].env, 1);
            }
        }
        if(alive == 0)
            return;
    } 

}


int main()
{
    dispatcher();
}

Is there any problem here? And when i want to call some third party interface, and maybe it is a block I/O there, can i do something to change another context to execute? and How?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:52:55+00:00Added an answer on June 7, 2026 at 3:52 pm

    Unfortunately, what you’re trying to do doesn’t work, because (per the setjmp manual):

     The longjmp() routines may not be called after the routine which called
     the setjmp() routines returns.
    

    This is because the setjmp/longjmp family of functions (including the sig variants) do not preserve the entire contents of the process stack.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

use strict; use Time::HiRes qw[gettimeofday tv_interval]; my $start_index = int(rand(50))+100;#this value is arbitrary for
use this code, in the Preferences activity, to know when the reset preference has
I use C++ Builder 2010 and I could'nt figure out how to change keyboard
Use trap to capture signals like this: i=-1;while((++i<33)); do trap echo $i >> log.txt
use level1\level2\level3; Can someone explain with a simple demo ?
use DBI(); What is causing this insert to fail to err 1, the SELECT
Use case is this: I want to unit test (in browser, QUnit or something
Use $this to refer to the current object. Use self to refer to the
Use Case Show a photo uploaded by the user in a square box with
use C#,want to upload excel file on google doc. bellow syntax use to upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.