Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <errno.h>
- struct mymsgbuf {
- long mtype; /* typ wiadomości */
- int request; /* numer żądania danego działania */
- int i;
- } msg;
- struct mymsgbuf buf;
- int open_queue( key_t keyval )
- {
- int qid;
- if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1)
- return(-1);
- return(qid);
- }
- int send_message( int qid, struct mymsgbuf *qbuf )
- {
- int result, length;
- /* lenght jest rozmiarem struktury minus sizeof(mtype) */
- length = sizeof(struct mymsgbuf) - sizeof(long);
- if((result = msgsnd( qid, qbuf, length, 0)) == -1)
- return(-1);
- return(result);
- }
- int remove_queue( int qid )
- {
- if( msgctl( qid, IPC_RMID, 0) == -1)
- return(-1);
- return(0);
- }
- int read_message( int qid, long type, struct mymsgbuf *qbuf )
- {
- int result, length;
- /* lenght jest rozmiarem struktury minus sizeof(mtype) */
- length = sizeof(struct mymsgbuf) - sizeof(long);
- if((result = msgrcv( qid, qbuf, length, type, 0)) == -1)
- return(-1);
- return(result);
- }
- int main(void)
- {
- int a,s,j,d;
- int qid;
- key_t msgkey;
- printf("podaj liczbe procesow");
- scanf("%d",&a);
- for (j=0;j<a;j++)
- {
- printf("Proces (PID: %d) \n", getpid());
- //scanf("%d","%l","%d",&a,d,&s);
- }
- printf("podaj wartosc");
- scanf("%d",&d);
- printf("podaj pid do ktorego chcesz przekazac wartosc");
- scanf("%d",&s);
- /* tworzymy wartość klucza IPC */
- msgkey = ftok(".", 'm');
- /* otwieramy/tworzymy kolejkę */
- if(( qid = open_queue( msgkey)) == -1)
- {
- perror("Otwieranie_kolejki");
- exit(1);
- }
- msg.mtype = d; /* typ wiadomości musi być dodatni */
- msg.request = 1;
- msg.i = d;/* wysyana liczba */
- if((send_message( qid, &msg )) == -1)
- {
- perror("Wysylanie");
- exit(1);
- }
- /* Odbieramy */
- buf.mtype = d; /* typ wiadomości musi być dodatni */
- buf.request = 1;
- /* Odbieramy wiadomosc */
- read_message(qid, buf.mtype, &buf);
- printf("Zawartosc: %d\n", buf.i);
- remove_queue(qid);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement