82 lines
1.9 KiB
C
82 lines
1.9 KiB
C
#include <unistd.h>
|
|
#include <stdio.h>
|
|
#include <sys/uio.h>
|
|
#include <sys/ipc.h>
|
|
#include <sys/msg.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <fcntl.h>
|
|
|
|
const char *main_path = "/tmp/serweripcmain";
|
|
|
|
typedef struct DanePid
|
|
{
|
|
long mtype; /* Message type. */
|
|
pid_t pid; /* Message text. */
|
|
} dane_pid;
|
|
|
|
typedef struct DaneDouble
|
|
{
|
|
long mtype; /* Message type. */
|
|
double n; /* Message text. */
|
|
} dane_double;
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
key_t key;
|
|
ssize_t bytes;
|
|
if (access(main_path, F_OK) == -1)
|
|
{
|
|
perror("file inaccessible");
|
|
return -1;
|
|
}
|
|
if ((key = ftok(main_path, 1)) == -1)
|
|
{
|
|
perror("main ftok error");
|
|
return -1;
|
|
}
|
|
int msg_id;
|
|
if ((msg_id = msgget(key, 0600)) == -1)
|
|
{
|
|
perror("msgget error");
|
|
return -1;
|
|
}
|
|
dane_pid pid;
|
|
pid.pid = getpid();
|
|
pid.mtype = 1;
|
|
dane_double test_dane;
|
|
test_dane.n = 12;
|
|
test_dane.mtype = 2;
|
|
if (msgsnd(msg_id, (const void *)&pid, sizeof(pid_t), 0) == -1)
|
|
{
|
|
perror("pid error");
|
|
return -1;
|
|
}
|
|
printf("Sent %d bytes pid: %d\n", sizeof(pid_t), pid.pid);
|
|
key_t key2;
|
|
if ((key2 = ftok(main_path, pid.pid)) == -1)
|
|
{
|
|
perror("data ftok error");
|
|
return -1;
|
|
}
|
|
int msg_id_2;
|
|
if ((msg_id_2 = msgget(key2, 0600 | IPC_CREAT | IPC_EXCL)) == -1)
|
|
{
|
|
perror("data msgget error");
|
|
return -1;
|
|
}
|
|
printf("Opened queue with id %x\n", msg_id_2);
|
|
if (msgsnd(msg_id_2, (const void *)&test_dane, sizeof(double), 0) == -1)
|
|
{
|
|
perror("data send error");
|
|
return -1;
|
|
}
|
|
printf("Sent %d bytes data: %f\n", sizeof(double), test_dane.n);
|
|
if ((bytes = msgrcv(msg_id_2, (void *)&test_dane, sizeof(double), 3, 0)) == -1)
|
|
{
|
|
perror("msgrcv error");
|
|
msgctl(msg_id, IPC_RMID, NULL);
|
|
return -1;
|
|
}
|
|
printf("Child received %d bytes and value %f\n", bytes, test_dane.n);
|
|
} |