Sponsored Content
Top Forums UNIX for Advanced & Expert Users User defined stacks for threads using glibc-2.3.5 Post 302180727 by mysterious on Tuesday 1st of April 2008 02:34:00 AM
Old 04-01-2008
Thanks to your reply.

I am calling pthread_create () inside library call itself And not in main, stack is allocated (using malloc) in main and passed with size argument to library function. may this be the cause for segfault? Is it not possible to pass stack pointer, size and entry point function to thread to library function and library inturn creates thread ?

Code:
/* main */

#include <stdio.h>
#include <limits.h>
void *func1 (void *p1)
{
   pthread_setcancelstate (PTHREAD_CANCEL_ENABLE,NULL);
   pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
   printf ("\n In Thread Function1 :) \n");
   sleep (3);
   pthread_exit (NULL);
}
int main (void)
{
  size_t size = PTHREAD_STACK_MIN + 0x444000;
  void *base = malloc (size );
  test_main (func1,base+PTHREAD_STACK_MIN,size-PTHREAD_STACK_MIN);
  sleep (10);
  sleep (10);
//  free (base);
  getchar();
  return 0;
}

/* Library Function */
#include <stdio.h>
//#define __USE_XOPEN2K
//#define _XOPEN_SOURCE 600
#include <pthread.h>
#include <unistd.h>
#include <limits.h>
#include <sched.h>
#include <stdlib.h>

void CleanupHandler (void* pArg)
{
   printf ("\nI am In clean up handler \n");
   usleep (100);
}
void * a(void *);
void * func(void *p(void *))
{
   printf ("\n In Thread Function :) \n");
   pthread_cleanup_push(CleanupHandler,NULL);
   p (NULL);
   pthread_cleanup_pop(1);
   usleep (1000);
   sleep (3);
   pthread_exit (NULL);
  return NULL;
}
int test_main (void*fn(void*),void *stackbase,size_t size)
{
   pthread_attr_t tattr;
   pthread_t tid;
   int ret;
   struct sched_param stparam;
   void *stackbase1,*stackbase2;
   size_t size1,size2;
   int policy;

   /* initialized with default attributes */
   ret = pthread_attr_init(&tattr);
   if (ret != -1)
   {
#if 1
     printf ("\n Stack:size <%p:%d>\n",stackbase,size);
     /* setting the base address and size of the stack */
     stackbase2 = memset (stackbase,0,size);
     ret = pthread_attr_setstack(&tattr, stackbase,size);
     if (ret != -1)
     {
#endif
       if (pthread_attr_setschedpolicy (&tattr,SCHED_RR))
       {
          printf ("\n setschedpolicy failed\n");
          pthread_attr_destroy (&tattr);
          exit (0);
       }
       if (pthread_attr_getschedpolicy (&tattr,&policy))
       {
          printf ("\n getschedpolicy failed\n");
          pthread_attr_destroy (&tattr);
          exit (1);
       }
       printf ("\n Policy = <%s>\n",(policy==0)?"SCHED_NORMAL":(policy == 1)?"SCHED_FIFO":(policy == 2)?"SCEHD_RR":(policy==3)?"SCHED_OTHERS":"NONE");
       sched_getparam (0,&stparam);
       stparam.sched_priority = 99;
       pthread_attr_setschedparam (&tattr,&stparam);
//     pthread_attr_setdetachstate (&tattr,PTHREAD_CREATE_JOINABLE);
//     pthread_attr_setinheritsched (&tattr,PTHREAD_EXPLICIT_SCHED);

        /* address and size specified */
        ret = pthread_create(&tid, &tattr, func, (void*)fn);
        if (ret == -1)
        {
           printf ("\nThread Creation Failed\n");
           pthread_attr_destroy (&tattr);
           perror (NULL);
        }
        else
        {
           printf ("\n Thread Creation Passed\n");
           pthread_attr_getstack (&tattr, &stackbase1,&size1);
           printf ("\n Stack:size <%p:%d>\n",stackbase1,size1);
           sleep (2);
           sleep (1);
           printf ("\n AF Join \n");
           pthread_attr_destroy (&tattr);
        }
#if 1
     }
     else
     {
        printf ("\n Setstack failed\n");
     }
#endif
  }
  else
  {
     printf ("\n attrInit failed\n");
  }

  return 0;
}

Is there anything wrong with the above code ? I am not able to figure it out .

edit by bakunin: please use code-tags when posting code. It makes your program text all the more readable.

Last edited by bakunin; 04-01-2008 at 10:35 AM..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nawk user-defined function

HELP!!!! I am in an on-line shell programming class and have a question. Here is the data: Mike Harrington:(510) 548-1278:250:100:175 Christian Dobbins:(408) 538-2358:155:90:201 Susan Dalsass:(206) 654-6279:250:60:50 (There are 12 contribuors total) This database contains names, phone... (1 Reply)
Discussion started by: NewbieGirl
1 Replies

2. AIX

User defined signal 1

Hi, I am just running a incremental back-up on one of my server. But these days It abrubtly fails with below error. ========== User defined signal 1 =========== When I rerun the back-up, It completed successfully.Earlier this was not happening. Any Idea, what could be the problem... (0 Replies)
Discussion started by: nitesh_raj
0 Replies

3. UNIX for Dummies Questions & Answers

User defined service

I want to add a new IP service which executes a script on SCO OS5. I have amended /etc/services and added to port number (3333) I have amended /etc/inetd.conf and added a line for this service but I can't get it to execute my own shell script When I telnet to the IP address on port 3333 I... (1 Reply)
Discussion started by: markdrury
1 Replies

4. Shell Programming and Scripting

need help with User Defined Function

Dear Friends, I need a help regarding User defined function in shell script. My problem is as follows: my_func.sh my_funcI(){ grep 'mystring' I.dat } my_funcQ(){ grep 'mystring' Q.dat } myfuncI myfuncQ But As both the function has same function only the... (11 Replies)
Discussion started by: user_prady
11 Replies

5. Linux

User defined stacks for threads using glibc-2.3.5

Hi, I am currently working with threads using linux 2.6.11 and glibc 2.3.5 on x86. When i am trying to create thread with user defined stacks glibc gives segmentation fault. I have written a wrapper to pthread_create () and generated shared object for the same. When i am trying to create any... (0 Replies)
Discussion started by: mysterious
0 Replies

6. Programming

add more user-defined signals

Hi Is there a way to add more user-defined signals? I am currently using SIGUSR1 and SIGUSR2 - but I need another one. How can I do that? Thanks! (9 Replies)
Discussion started by: naamabm
9 Replies

7. Shell Programming and Scripting

User defined functions in awk

Hi; Is der ne to to use user defined functions for the values in awk find $1 -type f -ls | nawk '{{print "|"$3"|"$5"|"$6"|"$8"|"$9"|"$10"|"} for(i=11;i<=NF;i++){printf("%s",$i)}}' In above command i want to append some values returned by user functions on line. thnks; ajay (1 Reply)
Discussion started by: ajaypadvi
1 Replies

8. UNIX for Dummies Questions & Answers

user defined commands

Hi, i would like to create user defined commands. e,g: if an user executes , mkdircd test then a directory called test should be created and it should be cd to test. How i can create the command mkdircd with below action: mkdir $1 && cd $1. Please help me in achieving this (7 Replies)
Discussion started by: pandeesh
7 Replies

9. UNIX and Linux Applications

Install glibc-debug for glibc-2.11.3-17.31.1 on SLES 11

I have to debug a function getopt_long in glibc-2.11.3-17.31.1. For that how can I download and install its corresponding glibc-debug on SLES 11? (8 Replies)
Discussion started by: rupeshkp728
8 Replies
All times are GMT -4. The time now is 04:55 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy