#include #include #include #include #include #include #include #define MAX 256 int CTRL=0, SEND=0, RECV = 0; typedef unsigned short int ushort; typedef struct { long type; char data[MAX]; } message; //mark=main //kwds=ftok, msgget,msgsnd, msgrecv, mesgctl //capt=Envoyer un message int main(int argc, char* argv[] ) { int opt, type; char *optlist ="cd:r:s:"; key_t key; message msg; int file; while ( ( opt = getopt( argc, argv , optlist) ) > 0 ) switch( opt ){ case 'r': RECV = 1; type = atoi( optarg ); break; case 's': SEND = 1; msg . type = atoi( optarg ); break; case 'c': CTRL = 1; break; case 'd': strncpy( msg . data, optarg, MAX ); ; break; default : printf("\nusage %s : %s", argv[0], optlist); exit ( 1 ); } if ( ( key = ftok( argv[0], 0 )) == -1 ) perror("ftok"), exit( 1 ); if ( ( file = msgget( key, IPC_CREAT | 0600 ) ) == -1 ) perror("msget"), exit( 1 ); if ( SEND ) { if ( msgsnd( file, ( void*) & msg, MAX , 0 ) < 0 ) perror("msgsnd"), exit(1); } if ( RECV ) { if ( msgrcv( file, ( void*) & msg, MAX , type, 0 ) >= 0 ) printf("type=%ld data=%s", msg . type, msg . data); else perror("msgrecv"), exit(1); } if ( CTRL ) msgctl( file, IPC_RMID, NULL); printf("\n"); return 0; }