Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Napisz dwa programy (procesy niespokerwnione), które b ę d ą
- komunikowa ć si ę przy pomocy kolejki komunikatów. Do wygenerowania
- klucza u ż yj funkcji ftok().
- */
- // Program drugi - odbierajacy wiadomosc
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <string.h>
- #define FTOK_KEY 712
- #define BUFFER_SIZE 128
- struct msgbuf {
- long mtype;
- char mtext[BUFFER_SIZE];
- };
- int main(void)
- {
- int id;
- key_t key;
- struct msgbuf recv_buffer;
- if((key = ftok("/tmp", FTOK_KEY)) == -1){
- perror("ftok");
- return EXIT_FAILURE;
- }
- if((id = msgget(key, 0600 | IPC_CREAT)) == -1){
- perror("msgget");
- return EXIT_FAILURE;
- }
- recv_buffer.mtype = 1;
- memset(recv_buffer.mtext, '\0', BUFFER_SIZE);
- if(msgrcv(id, &recv_buffer, sizeof(recv_buffer.mtext), 1, 0) == -1){
- perror("msgrcv");
- return EXIT_FAILURE;
- }
- printf("Wiadomosc: %s\n", recv_buffer.mtext);
- if(msgctl(id, IPC_RMID, 0) == -1){
- perror("msgctl");
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment