Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <string.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <signal.h>
- #define MAX 10
- #define SERWER 1
- int IDqueue;
- int amount_msg;
- int end_msg = 0;
- int count_one = 0;
- int count_two = 0;
- char buf[BUFSIZ];
- typedef struct message
- {
- long msg_1;
- long msg_2;
- char text[MAX];
- } Message;
- void *msg_s()
- {
- Message msg;
- int i, j;
- char mark;
- msg.msg_1 = SERWER;
- msg.msg_2 = getpid();
- for (i = 0; i < amount_msg; i++)
- {
- j = 0;
- printf("Set message: ");
- mark = getchar();
- if (mark != '\n')
- {
- msg.text[j] = mark;
- j++;
- }
- while ((j < MAX) && ((mark = getchar()) != '\n'))
- {
- msg.text[j] = mark;
- j++;
- }
- msg.text[j] = '\0';
- printf("\n CLIENT:Sending - %s ---> %ld \n", msg.text, msg.msg_1);
- if (msgsnd(IDqueue, &msg, strlen(msg.text) + 9, IPC_NOWAIT) == -1)
- {
- perror("Message sending failed\n");
- end_msg = 1;
- pthread_exit(0);
- }
- count_one++;
- }
- end_msg = 1;
- pthread_exit(0);
- }
- void *msg_r()
- {
- Message msg;
- int i;
- msg.msg_1 = SERWER;
- msg.msg_2 = getpid();
- while (1)
- {
- if (end_msg == 1)
- {
- while (count_one != count_two)
- {
- msgrcv(IDqueue, &msg, 5000, msg.msg_2, 0);
- count_two++;
- printf("\nCLIENT: Recieve - %s addressed to %ld", msg.text, msg.msg_1);
- sprintf(buf, "%s", msg.text);
- }
- break;
- }
- if ((msgrcv(IDqueue, &msg, MAX + 9, msg.msg_2, IPC_NOWAIT)) != -1)
- {
- count_two++;
- printf("\nCLIENT: Recieve - %s addressed to %ld\n", msg.text, msg.msg_1);
- sprintf(buf, "%s", msg.text);
- }
- }
- pthread_exit(0);
- }
- int main()
- {
- int i;
- char tab[255];
- int create_thread_one, create_thread_two;
- int attach_thread_one, attach_thread_two;
- pthread_t thread_send;
- pthread_t thread_receive;
- printf("PID of process: %d\n", getpid());
- if ((IDqueue = msgget(10, IPC_CREAT | 0600)) == -1)
- {
- perror("get messages queues fail\n");
- exit(EXIT_FAILURE);
- }
- printf("ID of queue: %d\n", IDqueue);
- printf("MAX size of message = %d\n", MAX);
- printf("set amount of messages: ");
- scanf("%s", tab);
- for (i = 0; i < strlen(tab); i++)
- {
- if (tab[i] < 48 || tab[i] > 57)
- {
- printf("Bad amount of messages\n");
- exit(EXIT_FAILURE);
- }
- }
- amount_msg = atoi(tab);
- if ((create_thread_one = pthread_create(&thread_send, NULL, msg_s, NULL)) == -1)
- {
- perror("create thread one failed\n");
- exit(EXIT_FAILURE);
- }
- if ((create_thread_two = pthread_create(&thread_receive, NULL, msg_r, NULL)) == -1)
- {
- perror("create thread two failed\n");
- exit(EXIT_FAILURE);
- }
- if ((attach_thread_one = pthread_join(thread_send, NULL)) == -1)
- {
- perror("attach thread one failed\n");
- exit(EXIT_FAILURE);
- }
- if ((attach_thread_two = pthread_join(thread_receive, NULL)) == -1)
- {
- perror("attach thread two failed\n");
- exit(EXIT_FAILURE);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment