- 论坛徽章:
- 0
|
...
#define PERMS (S_IRUSR | S_IWUSR)
#define MAXLINE 1024
typedef struct {
long mtype;
char mtext[1];
}mymsg_t;
int
main(void)
{
pid_t pid;
char test[MAXLINE] = "freebsd ";
int msqid1;
if ((msqid1 = msgget(IPC_PRIVATE, PERMS)) == -1)
fprintf(stderr, "Failed to create new private message queue");
if (pid = fork()){
char buf[MAXLINE];
int size;
if (msgwrite(buf, size) == -1)
fprintf(stderr, "Failed to write message.");
break;
}
&test[MAXLINE] += "OpenBSD!";
}
printf("%s \n", &text[MAXLINE]);
return 0;
}
int
msgwrite(void *buf, int len)
{
int error = 0;
mymsg_t *mymsg;
if ((mymsg = (mymsg_t *)malloc(sizeof(mymsg_t) + len - 1)) == NULL)
return -1;
memcpy(mymsg->mtext, buf, len);
mymsg->mtype = 1;
if (msgsnd(msqid1, mymsg, len, 0) == -1)
error = errno;
free(mymsg);
if (error) {
errno = error;
return -1;
}
return 0;
} |
|