# include <myhead.h> struct  msgbuf { 
    long  mstype; 
    char  text[ 1024 ] ; 
} ; 
# define  SIZE  ( sizeof ( struct  msgbuf ) - sizeof ( long ) ) int  main ( int  argc,  const  char  * argv[ ] ) 
{ 
    int  pid; 
    
    key_t  key= ftok ( "/" , 'a' ) ; 
    if ( key == - 1 ) { 
        perror ( "creat key" ) ; 
        return  - 1 ; 
    } 
    printf ( "key=%d\n" , key) ; 
    
    int  msgid= msgget ( key, IPC_CREAT| 0664 ) ; 
    if ( msgid== - 1 ) { 
        perror ( "msgget" ) ; 
        return  - 1 ; 
    } 
    struct  msgbuf  msg; 
    pid= fork ( ) ; 
    if ( pid> 0 ) 
    { 
        while ( 1 ) 
        { 
            
            printf ( "请输入数据类型\n" ) ; 
            scanf ( "%ld" , & msg. mstype) ; 
            printf ( "请输入数据\n" ) ; 
            scanf ( "%s" , msg. text) ; 
            msgsnd ( msgid, & msg, SIZE, 0 ) ; 
            if ( strcmp ( msg. text, "quit" ) == 0 ) break ; 
        
        if ( msgctl ( msgid, IPC_RMID, NULL ) == - 1 ) { 
            perror ( "msgctl error" ) ; 
            return  - 1 ; 
        } 
        printf ( "删除完成\n" ) ; 
    } else  if ( pid== 0 ) 
    { 
        while ( 1 ) 
        { 
            msgrcv ( msgid, & msg, SIZE, 5 , 0 ) ; 
            printf ( "收到信息:\n" ) ; 
            printf ( "id=%ld,text=%s\n" , msg. mstype, msg. text) ; 
            if ( strcmp ( msg. text, "quit" ) == 0 ) break ; 
        } 
        exit ( 0 ) ; 
    } else { 
        perror ( "create pid" ) ; 
        return  - 1 ; 
    } 
    
    wait ( NULL ) ; 
    
    return  0 ; 
}   
# include <myhead.h> struct  msgbuf { 
    long  mstype; 
    char  text[ 1024 ] ; 
} ; 
# define  SIZE  ( sizeof ( struct  msgbuf ) - sizeof ( long ) ) int  main ( int  argc,  const  char  * argv[ ] ) 
{ 
    int  pid; 
    
    key_t  key= ftok ( "/" , 'a' ) ; 
    if ( key == - 1 ) { 
        perror ( "creat key" ) ; 
        return  - 1 ; 
    } 
    printf ( "key=%d\n" , key) ; 
    
    int  msgid= msgget ( key, IPC_CREAT| 0664 ) ; 
    if ( msgid== - 1 ) { 
        perror ( "msgget" ) ; 
        return  - 1 ; 
    } 
    struct  msgbuf  msg; 
    pid= fork ( ) ; 
    if ( pid> 0 ) 
    { 
        while ( 1 ) 
        { 
            
            printf ( "请输入数据类型\n" ) ; 
            scanf ( "%ld" , & msg. mstype) ; 
            printf ( "请输入数据\n" ) ; 
            scanf ( "%s" , msg. text) ; 
            msgsnd ( msgid, & msg, SIZE, 0 ) ; 
            if ( strcmp ( msg. text, "quit" ) == 0 ) break ; 
        } 
        
        if ( msgctl ( msgid, IPC_RMID, NULL ) == - 1 ) { 
            perror ( "msgctl error" ) ; 
            return  - 1 ; 
        } 
        printf ( "删除完成\n" ) ; 
    } else  if ( pid== 0 ) 
    { 
        while ( 1 ) 
        { 
            msgrcv ( msgid, & msg, SIZE, 10 , 0 ) ; 
            printf ( "收到信息:\n" ) ; 
            printf ( "id=%ld,text=%s\n" , msg. mstype, msg. text) ; 
            if ( strcmp ( msg. text, "quit" ) == 0 ) break ; 
        } 
        exit ( 0 ) ; 
    } else { 
        perror ( "create pid" ) ; 
        return  - 1 ; 
    } 
    wait ( NULL ) ; 
    return  0 ; 
}   
 
半双工相互通信
# include <myhead.h> void *  task1 ( void *  arg) { 
    
    if ( ( mkfifo ( "myfifo" , 0664 ) )  ==  - 1 ) { 
        perror ( "mkfile error" ) ; 
    } 
    
    int  wfd= - 1 ; 
    if ( ( wfd= open ( "myfifo" , O_WRONLY) ) == - 1 ) { 
        perror ( "open write" ) ; 
    } 
    printf ( "输入quit为退出会话\n" ) ; 
    char  wbuf[ 128 ] = "" ; 
    while ( 1 ) { 
        printf ( "请输入您要发送的信息:\n" ) ; 
        fgets ( wbuf, sizeof ( wbuf) , stdin ) ; 
        wbuf[ strlen ( wbuf) - 1 ] = 0 ; 
        write ( wfd, wbuf, sizeof ( wbuf) ) ; 
        if ( strcmp ( wbuf, "quit" ) == 0 ) 
        { 
            break ; 
        } 
    } 
    close ( wfd) ; 
    system ( "rm myfifo" ) ; 
    pthread_exit ( NULL ) ; 
} 
void *  task2 ( void *  arg) { 
    int  rfd= - 1 ; 
    if ( ( rfd= open ( "myfifo" , O_RDONLY) ) == - 1 ) { 
        perror ( "read error" ) ; 
    } 
    char  rbuf[ 128 ] = "" ; 
    while ( 1 ) { 
        memset ( rbuf, 0 , sizeof ( rbuf) ) ; 
        read ( rfd, rbuf, sizeof ( rbuf) ) ; 
        printf ( "您收到一条信息:%s\n" , rbuf) ; 
        if ( strcmp ( rbuf, "quit" ) == 0 ) { 
            break ; 
        } 
    } 
    close ( rfd) ; 
    pthread_exit ( NULL ) ; 
} 
int  main ( int  argc,  const  char  * argv[ ] ) 
{ 
    
    pthread_t  tid1, tid2; 
    int  flag; 
    while ( 1 ) 
    { 
        printf ( "------------------会话只能由发送方发起与结束--------------\n" ) ; 
        printf ( "-----------------------选择1:发送信息-------------------\n" ) ; 
        printf ( "-----------------------选择2:接收信息-------------------\n" ) ; 
        printf ( "-------------------------选择0:退出---------------------\n" ) ; 
        scanf ( "%d" , & flag) ; 
        switch ( flag) 
        { 
            case  1 : 
                
                if ( ( pthread_create ( & tid1, NULL , task1, NULL ) )  != 0 ) 
                { 
                    printf ( "tid1 create error\n" ) ; 
                    return  - 1 ; 
                } 
                pthread_join ( tid1, NULL ) ; 
                break ; 
            case  2 : 
                
                if ( ( pthread_create ( & tid2, NULL , task2, NULL ) )  != 0 ) 
                { 
                    printf ( "tid1 create error\n" ) ; 
                    return  - 1 ; 
                } 
                pthread_join ( tid2, NULL ) ; 
                break ; 
            case  0 : 
                goto  END; 
        } 
        printf ( "输入'#'字符结束,否则继续\n" ) ; 
        char  element; 
        element= getchar ( ) ; 
        if ( element ==  '#' ) break ; 
    } 
END: 
    return  0 ; 
}    
 
# include <myhead.h> int  main ( int  argc,  const  char  * argv[ ] ) 
{ 
    
    pid_t  pid =  - 1 ; 
    
    int  pipefd[ 2 ]  =  { 0 } ; 
    
    if ( pipe ( pipefd)  ==  - 1 ) 
    { 
        perror ( "pipe error" ) ; 
        return  - 1 ; 
    } 
    printf ( "pipefd[0] = %d, pipefd[1] = %d\n" ,  pipefd[ 0 ] ,  pipefd[ 1 ] ) ;  
    
    pid =  fork ( ) ; 
    if ( pid >  0 ) 
    { 
        
        
        close ( pipefd[ 0 ] ) ; 
        char  buf[ 128 ]  =  "" ; 
        while ( 1 ) 
        { 
            
            fgets ( buf,  sizeof ( buf) ,  stdin ) ; 
            buf[ strlen ( buf) - 1 ]  =  0 ;           
            write ( pipefd[ 1 ] ,  buf,  sizeof ( buf) ) ;          
            if ( strcmp ( buf,  "quit" )  ==  0 ) 
            { 
                break ; 
            } 
        } 
        
        close ( pipefd[ 1 ] ) ; 
    } else  if ( pid ==  0 ) 
    { 
        
        
        close ( pipefd[ 1 ] ) ; 
        char  rbuf[ 128 ]  =  "" ; 
        while ( 1 ) 
        { 
            bzero ( rbuf,  sizeof ( rbuf) ) ;     
            read ( pipefd[ 0 ] ,  rbuf,  sizeof ( rbuf) ) ;            
            printf ( "收到父进程消息: %s\n" ,  rbuf) ;              
            if ( strcmp ( rbuf,  "quit" )  ==  0 ) 
            { 
                break ; 
            } 
        } 
        
        close ( pipefd[ 0 ] ) ; 
        
        exit ( EXIT_SUCCESS) ; 
    } else 
    { 
        perror ( "fork error" ) ; 
        return  - 1 ; 
    } 
    
    
    wait ( NULL ) ; 
    return  0 ; 
}