Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Zmodyfikuj programy z zadania trzeciego tak, aby zbada ć jak si ę b ę dzie
- zachowywa ł a funkcja msgrcv(), w zale ż no ś ci od tego, czy otrzyma flag ę
- IPC_NOWAIT, czy nie.
- */
- // 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);
- for(int i = 0; i < 1000; i++){
- 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