Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <unistd.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <signal.h>
- #include <ctype.h>
- #include <fcntl.h>
- #include<string.h>
- #include<sys/wait.h>
- char strin[100];
- int pipep1p2[2];
- int pipep2p3[2];
- pid_t proces1;
- pid_t proces2;
- pid_t proces3;
- int qid1;
- int qid2;
- int qid3;
- key_t msgkey1;
- key_t msgkey2;
- key_t msgkey3;
- struct mymsgbuf
- {
- long type;
- int signb;
- }msg;
- int send_message( int qid, struct mymsgbuf *qbuf )
- {
- int result, length;
- /* The length is essentially the size of the structure minus sizeof(mtype) */
- length = sizeof(struct mymsgbuf) - sizeof(long);
- if((result = msgsnd( qid, qbuf, length, 0)) == -1)
- {
- return(-1);
- }
- return(result);
- }
- int read_message( int qid, long type, struct mymsgbuf *qbuf )
- {
- int result, length;
- /* The length is essentially the size of the structure minus sizeof(mtype) */
- length = sizeof(struct mymsgbuf) - sizeof(long);
- if((result = msgrcv( qid, qbuf, length, type, 0)) == -1)
- {
- return(-1);
- }
- return(result);
- }
- int open_queue( key_t keyval )
- {
- int qid;
- if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1)
- {
- return(-1);
- }
- return(qid);
- }
- void sig_handlerp1(int signo)
- {
- if (signo == 12)
- {
- printf("P1 id :%d,received SIGUSR2 (nb: %d)\n",getpid(),signo);
- if(( qid1 = open_queue( msgkey1)) == -1)
- {
- perror("open_queue");
- exit(1);
- }
- if((read_message( qid1, 1 , &msg )) == -1)
- {
- perror("send_message");
- exit(1);
- }
- }
- kill(proces2,12);
- sleep(10);
- }
- void sig_handlerp2(int signo)
- {
- if (signo == 12)
- {
- printf("P2 id :%d,received SIGUSR2 (nb: %d)\n",getpid(),signo);
- if(( qid2 = open_queue( msgkey1)) == -1)
- {
- perror("open_queue");
- exit(1);
- }
- if((read_message( qid2, 1 , &msg )) == -1)
- {
- perror("send_message");
- exit(1);
- }
- }
- kill(proces3,12);
- sleep(10);
- }
- void sig_handlerp3(int signo)
- {
- if (!(signo == 12))
- {
- printf("P3 id :%d,received (nb: %d)\n",getpid(),signo);
- printf("parent P3: %d",getppid());
- kill(getppid(),signo);
- sleep(10);
- }
- else printf("P3 (%d) received signal nb: %d\n",getpid(),signo);
- }
- void sig_handlerpm(int signo)
- {
- printf("PM (%d) received signal nb: %d\n",getpid(),signo);
- msg.type = 1;
- printf("flag1");
- msg.signb = signo;
- printf("flag2");
- if((send_message( qid1, &msg )) == -1)
- {
- printf("send_message");
- exit(1);
- }
- printf("flag3");
- if((send_message( qid2, &msg )) == -1)
- {
- printf("send_message");
- exit(1);
- }
- printf("flag4");
- if((send_message( qid3, &msg )) == -1)
- {
- printf("send_message");
- exit(1);
- }
- printf("flag5");
- kill(proces1,12);
- printf("flag6");
- sleep(10);
- }
- pid_t wrap(void (*process_callback)(void))
- {
- printf("creating process\n");
- pid_t pid = fork();
- switch(pid) {
- case -1:
- printf("Blad funkcji fork.");
- exit(1);
- case 0:
- process_callback();
- return 0;
- default:
- return pid;
- }
- }
- void p1()
- {
- int i;
- for(i=1;i<65;i++)
- {
- signal(i, sig_handlerp1);
- }
- char buffer[32];
- char fileName[50];
- size_t bufsize = 32;
- char *b = buffer;
- int choice;
- close(pipep1p2[0]);
- printf("P1 id: %d parent id: %d\n",getpid(),getppid());
- while(1)
- {
- sleep(1);
- printf("\nType 1: reading from stdin\n 2: reading from file\n");
- scanf("%d",&choice);
- if(choice == 1)
- {
- printf("Type input:\n");
- getline(&b,&bufsize,stdin);
- getline(&b,&bufsize,stdin);
- write(pipep1p2[1],b,32);
- choice = 0;
- }
- if(choice == 2)
- {
- printf("Type file name:\n");
- scanf("%s",fileName);
- FILE* file = fopen(fileName,"r");
- while(fgets(buffer,sizeof(buffer),file))
- {
- write(pipep1p2[1],buffer,32);
- }
- fclose(file);
- choice = 0;
- }
- }
- }
- void p2()
- {
- int i;
- for(i=1;i<65;i++)
- {
- signal(i, sig_handlerp2);
- }
- int* length;
- char c;
- printf("P2 id: %d parent id: %d\n",getpid(),getppid());
- close(pipep1p2[1]);
- close(pipep2p3[0]);
- while(1)
- {
- *length = 0;
- read(pipep1p2[0],strin,32);
- for(i=0;i<strlen(strin);i++)
- {
- c=strin[i];
- if(isspace(c)==0)(*length)++;
- }
- write(pipep2p3[1],length,4);
- }
- }
- void p3()
- {
- int i;
- for(i=1;i<65;i++)
- {
- signal(i, sig_handlerp3);
- }
- int* lengthp3;
- printf("P3 id: %d parent id: %d\n",getpid(),getppid());
- close(pipep2p3[1]);
- while(1)
- {
- read(pipep2p3[0],lengthp3,4);
- printf("string length P3: %d\n",*lengthp3);
- }
- }
- int main()
- {
- printf("PM id: %d\n",getpid());
- int i;
- for(i=1;i<65;i++)
- {
- signal(i, sig_handlerpm);
- }
- /* Generate our IPC key value */
- msgkey1 = ftok(".", 'a');
- msgkey2 = ftok(".", 'b');
- msgkey3 = ftok(".", 'c');
- /* Open/create the queue */
- if(( qid1 = open_queue( msgkey1)) == -1) {
- perror("open_queue");
- exit(1);
- }
- if(( qid2 = open_queue( msgkey2)) == -1) {
- perror("open_queue");
- exit(1);
- }
- if(( qid3 = open_queue( msgkey3)) == -1) {
- perror("open_queue");
- exit(1);
- }
- /* Load up the message with arbitrary test data
- msg.mtype = 1;
- msg.request = 1;
- msg.salary = 1000.00;
- Bombs away!
- if((send_message( qid, &msg )) == -1) {
- perror("send_message");
- exit(1);
- }*/
- pipe(pipep1p2);
- pipe(pipep2p3);
- pid_t proces1 = wrap(p1);
- if(proces1==0){printf("P1: ended %d\n",getpid());}
- if(proces1!=0)
- {
- pid_t proces2 = wrap(p2);
- if(proces2==0)printf("P2: ended %d\n",getpid());
- if(proces2!=0)
- {
- pid_t proces3 = wrap(p3);
- if(proces3==0)printf("P3: ended %d\n",getpid());
- if(proces3!=0)
- {
- wait(NULL);
- wait(NULL);
- wait(NULL);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement