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

  • SEARCH
  • Home
  • 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 7902273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:27:02+00:00 2026-06-03T09:27:02+00:00

I have to code a thread example for my class homework. Somehow I’m getting

  • 0

I have to code a thread example for my class homework. Somehow I’m getting a Segfault after creating my child-Process via copy() (have to use that sadly).

void thread(void);
#define CLONE_VM        0x00000100
#define ANSWER_TRUE     "t"
#define ANSWER_FALSE    "f"

static char stack[2];

int main(void) {
void** childThreadBP = NULL;
void** childThread = NULL;

int pid = 0;

puts("create stack for child process...");
void ** new_stack = (void **) malloc(128);    
//new_stack = (void **) ((char *)new_stack + 128); 

puts("create child process...");    
pid = clone(thread, new_stack, CLONE_VM, NULL);

puts("write PID to shared stack...");
stack[0] = (char) pid;

puts("child answered:");
while(1){}
if (stack[1] == ANSWER_TRUE) {
    puts("PIDs are equal.");
}
else {
    puts("PIDs are NOT equal.");
}
return EXIT_SUCCESS;
}

void thread(void) {
puts("[child]: I'm alive!");
int pidSelf;

pidSelf = getpid();
if (pidSelf == (int)stack[0]) {
    puts("[child]: dad the PID you gave me is correct!");
    stack[1] = ANSWER_TRUE;
}
else {
    puts("[child]: dad the PID you gave me is NOT correct!");
    stack[1] = ANSWER_FALSE;
}
}

Maybe you see what my mistake is… – what is wrong with the code format?!

I just need help fixing the seg fault – the rest should be ok (I think 😉 )

greetings!!

  • 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-03T09:27:03+00:00Added an answer on June 3, 2026 at 9:27 am

    You have a few issues. The first is as Martin James stated, you need a larger stack size. I used 16384 and that worked fine. Second, you need to pass in the top of the memory space, your line:

    new_stack = (void **) ((char *)new_stack + 128); 
    

    is fine, uncomment it and change it to the larger stack size:

    void ** new_stack = (void **) malloc(16384);    
    new_stack = (void **) ((char *)new_stack + 16384); 
    

    Thirdly, you have an issue with your PID storage. The stack array must be an integer, as the pid can be larger than a char variable can fit (my PID was 25689).

    That should fix it up.

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

Sidebar

Related Questions

I have this code inside thread and when I call terminate it terminates fast
I have some code where I use a thread static object in C#. [ThreadStatic]
I have a java code that looks like this: //UI thread //Some code Job
Lets say we have this code: bool KeepGoing = true; DataInThread = new Thread(new
I have the following code in my program: Thread getUsersist, getChatUsers; getUsersList = new
I have the following code in a Runnable that gets passed to a thread
I have this thread. Earlier, I was testing the code without the thread and
In my code I have three classes as follows: Forum , Forum::Thread and Forum::Post
Let's say that I have the following code that's run in one thread of
I have a thread in my application that is running code that can potentially

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.