Thread: debugging pipes

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    debugging pipes

    Hey I had a question about pipes in programming form.

    so if i had a main()
    write(....,....., 10)

    process 2
    read(..., ...., 10)
    read(..., ..., 10)

    the code is at a halt I got because obviously u cant keep going until the the second read finally gets a second write() which i guess makes sense

    but now lets say

    so if i had a main()
    write(....,"hello", 100)

    process 2
    read(..., ...., 100)
    read(..., ..., 100)
    exit(0)

    the problem Im not sure about is that with write now sending in the pipe "hello" and the size i guess is obviously bigger then needed the program actually skips the second read and just exits out and I dont want it to exit. Y did the second read get skipped over just because the size had changed?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Are you actually checking the return results?

    Because asking for 100 bytes is not the same as getting 100 bytes.

    Buffer sizes on most read functions are a safe upper limit, not a demand.
    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
    Join Date
    Aug 2009
    Posts
    192
    Okay well i tried to print out the pointer and got segmentation error but yea basically i can't stop the process from skipping the second read call.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    Okay so i justed check the return value and its -1....... which i guess is an error but cant figure it out.

    Code:
    #include <sys/types.h>
    #include <pthread.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void * thread2(void*);
    void * thread1(void*);
    
    int	commpipe[2];		
    int	sndpipe[2];
    int rv, dv;
    int looper,i ;
    char buf [30]; 
    char* bug;
    
    #define NUM_THREADS 2
    pthread_t tid[NUM_THREADS];      /* array of thread IDs */
    
    
    
    int main(){
    
    
    
    	char * buffer = "hello my tester\n";
    
    
    	if (pipe( commpipe ) ) {
    		fprintf(stderr,"Pipe error!\n");
    		exit(1);
    	}
    	if (pipe( sndpipe ) ) {
    		fprintf(stderr,"Pipe error!\n");
    		exit(1);
    	}
    	pthread_create(&tid[1], NULL, thread1, NULL);
    	pthread_create(&tid[0], NULL, thread2, NULL);
    	write(sndpipe[1],buffer,100);
    	//write(sndpipe[1],"hello test2 ",15);
    	//write(sndpipe[1],"hello test 3",15);
    	//write(sndpipe[1],"mark as test4",15);
    	//sleep(7);
    	write(commpipe[1], "test", 5);
    		
    	sleep(2);
    	printf("Hello\n");
    	sleep(2);
    	printf("Goodbye\n");
    	sleep(2);
    	printf("exit\n");
    
    
    	for ( i = 0; i < NUM_THREADS; i++)
        		pthread_join(tid[i], NULL);
    	fprintf(stderr,"Child exited with a %d value\n",rv);
    	return 0;
    }
    
    void * thread1(void* parm){
    
    	sleep(5);
    	read(commpipe[0], buf, 5);
    	printf("%s  pipe output\n",buf);
    
    	pthread_exit(0);
    
    }
    
    void* thread2(void* parm){
    	sleep(6);
    	read(sndpipe[0], bug,100);
    	//printf("%s\n",bug);
    
    	read(sndpipe[0], bug,100);
    	//printf("%s\n",bug);
    	for(looper =0; looper < 5; looper++){
    	   sleep(1);
    	   printf("GOSH\n");
    	}
    	pthread_exit(0);
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. threads and pipes
    By kiros88 in forum C Programming
    Replies: 3
    Last Post: 08-20-2009, 06:03 PM
  2. Pipes sometimes fail
    By EVOEx in forum Linux Programming
    Replies: 2
    Last Post: 05-02-2009, 01:47 PM
  3. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 10:48 AM
  4. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM
  5. Services and Pipes
    By nickname_changed in forum Windows Programming
    Replies: 0
    Last Post: 07-16-2003, 06:46 AM