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 pierwszy - wysylajacy wiadomosc
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <string.h>
- #define MESSAGE_TO_SEND "Wiadomosc do wyslania"
- #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 send_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;
- }
- send_buffer.mtype = 1;
- memset(send_buffer.mtext, '\0', BUFFER_SIZE);
- strncpy(send_buffer.mtext, MESSAGE_TO_SEND, BUFFER_SIZE);
- if(msgsnd(id, &send_buffer, sizeof(send_buffer.mtext), 0) == -1){
- perror("msgsnd");
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment