Find the answer to your Linux question:
Results 1 to 2 of 2
Here I had included my code which implements signal handling and threading. In this the thread is sending signal to the process which created it....can we do this way? and ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    1

    Problem with signal handler


    Here I had included my code which implements signal handling and
    threading.

    In this the thread is sending signal to the process which created
    it....can we do this way?
    and the process is already blocked for input of a variable, meanwhile
    this signal is delivered to it so the handler is executed. But
    surprisingly after the execution of handler the process is not again
    blocking on the ip but continuing with the already stored value of the
    variable.

    Is my code wrong or is this wrong way of Doing or the kernel

    doesn't support this type of coding?

    Sorry for the inconvenience caused.

    I hope to see a reply if you find time.

    Thanks & Regards
    Sathish Pillim

    --






    #include <stdio.h>
    #include <unistd.h>
    #include <signal.h>

    #include <stdlib.h>
    #include <pthread.h>
    #include <sys/types.h>
    #include <linux/unistd.h>

    _syscall0(pid_t,gettid)
    pid_t gettid(void);

    pid_t pid;

    void Signaling(int sig)
    {
    printf("Now in the Signal handler\n");
    printf("How r u\n");
    printf("Come to my room\n");
    }

    void *thread_function();

    int main()
    {

    struct sigaction act;
    pid_t pid;
    int i=1,iResult;
    pthread_t a_thread;
    pthread_attr_t thread_attr;

    act.sa_handler=Signaling;
    sigemptyset(&act.sa_mask);
    act.sa_flags=0;

    sigaction(SIGUSR1,&act,NULL);
    iResult=pthread_attr_init(&thread_attr);
    if(iResult!=0){
    perror("Attribute creation failed\n");
    exit(1);
    }


    iResult=pthread_attr_setdetachstate(&thread_attr,P THREAD_CREATE_DETACHED);

    if(iResult!=0){
    perror("setting attributions failed\n");
    exit(2);
    }


    pid=gettid();

    while(1){
    printf("Now in the process..after while\n");
    printf("Enter i value");
    scanf("%d",&i);

    /*The process is actually blocking here on sacnf and thread is
    sending a signal
    to the process so the signal handler is executed but the kernel is not
    resuming from the
    previous state that is blocked on sacnf but it is wrongly resuming
    with the previous
    value of 'i' without waiting on scanf, am I wrong in coding or there
    is another approach
    or is it a bug in the Kernel */

    scanf("%d",&i);/*I included this because when it is resumed after the
    handler
    the process can block here for i/p without going on executing
    with the previous value otherwise it is not needed*/

    printf("%d\n######\n$$$$$$$$\n",i);
    iResult=pthread_create(&a_thread,&thread_attr,(voi d
    *)thread_function,NULL);
    printf("Now after thread creation\n");
    printf("How r u\n");
    printf("***********************\n");
    printf("end of while\n");
    }
    return 0;
    }


    void *thread_function()
    {
    printf("Now in thread\n");
    sleep(4);
    kill(pid,SIGUSR1);
    pthread_exit(NULL);/*Though I had set it in detach state the threads
    are remaining
    without getting cleaned up.i.e Zombie state*/
    }

  2. #2
    Just Joined!
    Join Date
    Aug 2008
    Posts
    1
    I am facing exactly the same problem... please someone help us out

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •