|
|
发表于 2006-4-2 10:35:09
|
显示全部楼层
是你的wait搞错了,改一下,变成下面这个样子:
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- void
- quit (char *err)
- {
- printf("error : %d\n",errno);
- perror (err);
- exit (EXIT_FAILURE);
- }
- struct msgbuf
- {
- long mtype;
- char msg_text[100];
- };
- int
- main (int arg, char *arc[])
- {
- int ret;
- struct msgbuf msg = {1,{0}};;
- int msqid;
- msg.mtype = getpid ();
- strcpy (msg.msg_text, "I send the message!\0");
- while (arg != 2)
- {
- printf ("the number of arguments should be two\n");
- quit ("arg number");
- }
- int key = atoi (arc[1]);
- if ((msqid = msgget (key, 0)) < 0)
- {
- printf ("keyd don't exists\n", key);
- msqid = msgget (key, IPC_CREAT | 0666);
- if (msqid < 0)
- quit ("msgget create");
- printf ("msg queue create successfully\n");
- }
- system ("ipcs -q");
- int len = strlen (msg.msg_text);
- int pid;
- if((pid=fork())<0)
- quit("fork");
- if(pid==0)
- {
- ret = msgsnd (msqid, &msg, len, 0);
- if (ret < 0)
- quit ("msgsnd");
- exit(0);
- }
- else
- {
- int status;
- waitpid(pid,&status,0);
- ret = msgrcv (msqid, &msg, len, 0, 0);
- if (ret < 0)
- quit ("msgrcv");
- printf ("message contents %s\n", msg.msg_text);
- printf ("message type=%d\n", msg.mtype);
- }
- system ("ipcs -q");
- msgctl (msqid, IPC_RMID, NULL);
- exit (EXIT_SUCCESS);
- }
复制代码 |
|