Sending Struct pointers through pipes 2

This is a discussion on Sending Struct pointers through pipes 2 within the C Programming forums, part of the General Programming Boards category; Okay sorry about before it was crazy So I tried to do a simplier test. Code: #include <sys/types.h> #include <unistd.h> ...

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

    Sending Struct pointers through pipes 2

    Okay sorry about before it was crazy So I tried to do a simplier test.
    Code:
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct BUF
    {
    	 char* type;
    	 char* level;
    	int hi;
    }__attribute__ ((packed)) BUF;
    
    int main(){
    
    	pid_t pida,  parent ;
    	BUF* duh = malloc(sizeof(*duh));
    	int rv;
    	int	commpipe[2];		/* This holds the fd for the input & output of the pipe */
    	/* Setup communication pipeline first */
    	if (pipe( commpipe ) ) {
    		fprintf(stderr,"Pipe error!\n");
    		exit(1);
    	}
    	parent = getpid() ;   /* get parent of me, the parent */ 
    	printf("main parent = %08X\n", getpid() ) ; 
    	/* Fork Child #1 from "main" and check for errors */
    	pida = fork() ; 
    
    	if (pida == -1) { 
    		fprintf(stderr,"Fork error 1. Exiting.\n");  /* something went wrong */
    		exit(1);        
    	}
    	 	
    	if (pida == 0 ) { 
    		/* A zero PID indicates that this is the child process */
    		sleep(5);
    		read(commpipe[0], duh, sizeof(duh));
    		printf("%s  pipe output %s\n",duh->type,   duh->level);
    		exit(0);
    	}
    	
    	else {
    		/* A positive (non-negative) PID indicates the parent process */
    		BUF* dang = malloc(sizeof(*dang));
    		dang->type = "blood";
    		dang->level = "hard";
    
    		write(commpipe[1], dang, sizeof(dang));
    		sleep(2);
    		printf("Hello\n");
    		sleep(2);
    		printf("Goodbye\n");
    		sleep(2);
    		printf("exit\n");
    		wait(&rv);				/* Wait for child process to end */
    		fprintf(stderr,"Child exited with a %d value\n",rv);
    	}
    	return 0;
    }
    my output is
    main parent = 000055D9
    Hello
    Goodbye
    blood pipe output (null)
    exit
    Child exited with a 0 value

    so basically the duh->level equals NULL even though i assigned it to be "hard" b4 the write? So I'm not really sure how pipes send data does it not do it like parts at a time?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    read(commpipe[0], duh, sizeof(duh));
    sizeof(duh) is just the size of a pointer. You want sizeof(*duh). Same thing with your write() call.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  3. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  4. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 11:24 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21