Thread: Need advice on thread communication

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048

    Need advice on thread communication

    Currently I have this:
    Code:
    bool gasp_thread_signal(
    	char const *src, int ext_pipes[2], int int_pipes[2], int sig
    )
    {
    	int ret = SIGSTOP;
    	struct pollfd pfd = {0};
    	node_t n = 0;
    	pfd.fd = int_pipes[0];
    	pfd.events = POLLIN;
    	write( ext_pipes[0], &sig, sizeof(int) );
    	fprintf( stderr, "%s thread is now waiting\n", src );
    	while ( ret != SIGCONT )
    	{
    		while ( poll( &pfd, 1, CLOCKS_PER_SEC ) == 0 );
    		read( int_pipes[0], &ret, sizeof(int) );
    		if ( n == UCHAR_MAX ) {
    			fputs( "Poll limit reached, aborting attempt\n", stderr );
    			return 0;
    		}
    		++n;
    	}
    	fprintf( stderr, "%s thread is now running\n", src );
    	return 1;
    }
    
    bool gasp_thread_should_die(
    	char const *src, int ext_pipes[2], int int_pipes[2]
    )
    {
    	int ret = SIGCONT;
    	struct pollfd pfd = {0};
    	pfd.fd = int_pipes[0];
    	pfd.events = POLLIN;
    	if ( poll( &pfd, 1, 10 ) )
    	{
    		read( int_pipes[0], &ret, sizeof(int) );
    		switch ( ret )
    		{
    		case SIGKILL:
    			return 1;
    		case SIGSTOP:
    			return !gasp_thread_signal(
    				src, ext_pipes, int_pipes, SIGCONT );
    		}
    	}
    	return 0;
    }
    Since it's not quite working I must be doing something wrong, any ideas?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,176
    > write( ext_pipes[0], &sig, sizeof(int) );
    Why is this ext_pipes where everything else is int_pipes?

    > while ( poll( &pfd, 1, CLOCKS_PER_SEC ) == 0 );
    CLOCKS_PER_SEC is just simply the wrong value to use here. CLOCKS_PER_SEC is the scale factor for the clock() function. It has nothing to do with milliseconds.

    Create a minimal example of two threads where one of them should die, making use of those functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Quote Originally Posted by Salem View Post
    > write( ext_pipes[0], &sig, sizeof(int) );
    Why is this ext_pipes where everything else is int_pipes?

    > while ( poll( &pfd, 1, CLOCKS_PER_SEC ) == 0 );
    CLOCKS_PER_SEC is just simply the wrong value to use here. CLOCKS_PER_SEC is the scale factor for the clock() function. It has nothing to do with milliseconds.

    Create a minimal example of two threads where one of them should die, making use of those functions.
    3 things, 1st an answer to your question about the ext_pipes and int_pipes, the ext_pipes is for talking to another thread while the int_pipes is for hearing the response from said thread, the functions calling will determine which is which, in this case the main thread passes scan_pipes to ext_pipes and main_pipes to int_pipes while the scan thread does the reverse, it passes main_pipes to ext_pipes and scan_pipes to int_pipes, I'm unfamiliar with thread polling so I thought to take the safest option of using separate pipes for different purposes, if you know of a better way I'm all eyes

    2nd thing, poll() takes a wait time, I figured the number for clocks a second would be reasonable, again if you know better I'm all eyes

    final thing, this isn't meant to kill the thread normally, that supposed to be for when the user wants to cancel the scan either because they entered the wrong parameters or they're closing the window or just plain let the last reference to the lua object go

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Got round to making that short file you requested, tried to stop the thread midway to read from array but it either never stops or the response is never heard
    Attached Files Attached Files

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,176
    Even the tiniest bit of debugging effort on your part would have found this.
    Code:
    $ gcc -g foo.c -pthread
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) b gasp_thread_signal
    Breakpoint 1 at 0x400a0d: file foo.c, line 13.
    (gdb) run
    Starting program: ./a.out 
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
    [New Thread 0x7ffff77ef700 (LWP 6284)]
    
    Thread 1 "a.out" hit Breakpoint 1, gasp_thread_signal (src=0x401003 "Main", ext_pipes=0x7fffffffde40, int_pipes=0x7fffffffde38, sig=19) at foo.c:13
    13	{
    (gdb) n
    14		int ret = SIGSTOP;
    (gdb) 
    15		struct pollfd pfd = {0};
    (gdb) 
    16		size_t n = 0;
    (gdb) 
    17		pfd.fd = int_pipes[0];
    (gdb) 
    18		pfd.events = POLLIN;
    (gdb) 
    19		int r = write( ext_pipes[0], &sig, sizeof(int) );
    (gdb) 
    20		fprintf( stderr, "%s thread is now waiting\n", src );
    (gdb) p r
    $1 = -1
    (gdb) p ext_pipes[0]
    $2 = 5
    Quote Originally Posted by rtfm
    pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication. The array pipefd is used to return two file descriptors referring to the ends of the pipe.
    pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe. Data written to the write end of the pipe is buffered by the kernel until it is read from the read end
    of the pipe. For further details, see pipe(7).
    You're writing to the WRONG end of the pipe.

    Try adding something like
    #define PIPE_RD 0
    #define PIPE_WR 1

    and use those constants in place of all the [0] and [1] you have.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Quote Originally Posted by Salem View Post
    Even the tiniest bit of debugging effort on your part would have found this.
    Code:
    $ gcc -g foo.c -pthread
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) b gasp_thread_signal
    Breakpoint 1 at 0x400a0d: file foo.c, line 13.
    (gdb) run
    Starting program: ./a.out 
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
    [New Thread 0x7ffff77ef700 (LWP 6284)]
    
    Thread 1 "a.out" hit Breakpoint 1, gasp_thread_signal (src=0x401003 "Main", ext_pipes=0x7fffffffde40, int_pipes=0x7fffffffde38, sig=19) at foo.c:13
    13	{
    (gdb) n
    14		int ret = SIGSTOP;
    (gdb) 
    15		struct pollfd pfd = {0};
    (gdb) 
    16		size_t n = 0;
    (gdb) 
    17		pfd.fd = int_pipes[0];
    (gdb) 
    18		pfd.events = POLLIN;
    (gdb) 
    19		int r = write( ext_pipes[0], &sig, sizeof(int) );
    (gdb) 
    20		fprintf( stderr, "%s thread is now waiting\n", src );
    (gdb) p r
    $1 = -1
    (gdb) p ext_pipes[0]
    $2 = 5


    You're writing to the WRONG end of the pipe.

    Try adding something like
    #define PIPE_RD 0
    #define PIPE_WR 1

    and use those constants in place of all the [0] and [1] you have.
    Yeah I noticed that last night and fixed it but that didn't resolve my main problem so I figured it wasn't worth mentioning, I have managed to fix that main problem just now although with some API change, anyways I've attached the updated file both for my benefit and anyone else who happens to read through this thread (as always consider it MIT Licensed if you need to reference one)
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need advice regarding inter-process communication.
    By Absurd in forum C Programming
    Replies: 7
    Last Post: 04-27-2015, 05:12 PM
  2. Advice on inter-process communication
    By erupter in forum C Programming
    Replies: 8
    Last Post: 08-10-2012, 01:31 PM
  3. Replies: 3
    Last Post: 11-20-2011, 12:01 AM
  4. Best solution for inter-thread communication?
    By IceDane in forum Linux Programming
    Replies: 7
    Last Post: 05-25-2009, 05:15 AM
  5. Replies: 2
    Last Post: 04-12-2004, 01:37 AM

Tags for this Thread