Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Zmodyfikuj zadanie pierwsze, tak aby proces tworzył potomka
- i komunikował się z nim przy użyciu kolejki.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <string.h>
- #include <unistd.h>
- #include <wait.h>
- #define MESSAGE_TO_SEND "Jakas inna wiadomosc"
- #define BUFFER_SIZE 128
- struct msgbuf {
- long mtype;
- char mtext[BUFFER_SIZE];
- };
- void send_message(int id)
- {
- struct msgbuf send_buffer;
- send_buffer.mtype = 1;
- memset(send_buffer.mtext, '\0', BUFFER_SIZE-1);
- strncpy(send_buffer.mtext, MESSAGE_TO_SEND, BUFFER_SIZE-1);
- if(msgsnd(id, &send_buffer, strlen(send_buffer.mtext), 0) == -1){
- perror("msgsnd");
- exit(EXIT_FAILURE);
- }
- }
- void recv_message(int id)
- {
- struct msgbuf recv_buffer;
- memset(recv_buffer.mtext, '\0', BUFFER_SIZE-1);
- if(msgrcv(id, &recv_buffer, sizeof(recv_buffer.mtext), 1, 0) == -1){
- perror("msgrecv");
- exit(EXIT_FAILURE);
- }
- printf("Przeslana wiadomosc: %s\n", recv_buffer.mtext);
- }
- int main(void)
- {
- int id;
- pid_t pid;
- if((id = msgget(IPC_PRIVATE, 0600 | IPC_CREAT)) == -1){
- perror("msgget");
- return EXIT_FAILURE;
- }
- pid = fork();
- if(pid == -1){
- perror("fork");
- return EXIT_FAILURE;
- }
- else if(pid == 0){
- send_message(id);
- }
- else{
- wait(NULL);
- recv_message(id);
- if(msgctl(id, IPC_RMID, 0) == -1){
- perror("msgctl");
- return EXIT_FAILURE;
- }
- }
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment