Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/wait.h>
- #include <string.h>
- int K0[2];
- int K1[2];
- int interrupt = 0;
- void my_pause(){
- while(!interrupt){}
- }
- void hdl(int sig){
- if(sig == SIGUSR1){
- interrupt = 1;
- }
- }
- int main(){
- signal(SIGUSR1, hdl);
- pid_t P1;
- pipe(K0);
- pipe(K1);
- P1 = fork();
- if (P1 == 0){
- printf(" Start P1\n");
- pid_t P3;
- P3 = fork();
- if (P3 == 0){
- printf(" Start P3\n");
- close(K1[0]);
- pid_t iam = getpid();
- char text[] = "First message from P3 to parent thread !"; //сообщение
- char message[44];
- sprintf(message,"%d%s",iam,text);
- write(K1[1],message,44);
- printf(" Send data from P3 to Р0\n");
- kill(getppid(),SIGUSR1);
- printf(" Send signal from P3 to P1\n");
- my_pause();
- printf(" Get signal from P2\n");
- //close(K1[0]);
- char text1[] = "Second message from P3 to parent thread!";
- sprintf(message,"%d%s",iam,text1);
- write(K1[1],message,44);
- printf(" Send data from P3 to Р0\n");
- printf(" End P3\n");
- exit(0);
- }
- else{
- close(K1[0]);
- close(K0[0]);
- my_pause();
- printf(" Get signal from P3\n");
- pid_t iam = getpid();
- char text[] = "Message from P1 thread to parent thread!";
- char message[44];
- sprintf(message,"%d%s",iam,text);
- write(K1[1],message,44);
- printf(" Send data from P1 to P0\n");
- char pid[4];
- sprintf(pid,"%d",P3);
- write(K0[1],pid,4);
- printf(" Send pid P3 to P2 by K0\n");
- int signal;
- waitpid(P3,&signal,0);
- printf(" Waited for end of P3\n");
- printf(" End P1\n");
- exit(0);
- }
- }
- else{
- pid_t P2;
- P2 = fork();
- if (P2 == 0){
- close(K1[0]);
- close(K0[1]);
- int pid;
- char pid_s[4];
- printf(" Start P2\n");
- printf(" Wait for data from K0\n");
- read(K0[0],pid_s,4);
- pid = atoi(pid_s);
- printf(" Get a pid of P3 from P1 by K0\n");
- pid_t iam = getpid();
- char text[] = "Message from P2 thread to parent thread!";
- char message[44];
- sprintf(message,"%d%s",iam,text);
- write(K1[1],message,44);
- printf(" Send data from P2 to P0\n");
- kill(pid,SIGUSR1);
- printf(" Send signal from P2 to P3\n");
- printf(" End P2\n");
- exit(0);
- }
- else{
- close(K1[1]);
- int status;
- waitpid(P1,&status,0);
- printf("Waited for end of child thread\n");
- char messages[44];
- printf("All messages from child thread:\n");
- read(K1[0],messages,44);
- write(1,messages,44);
- printf("\n");
- read(K1[0],messages,44);
- write(1,messages,44);
- printf("\n");
- read(K1[0],messages,44);
- write(1,messages,44);
- printf("\n");
- read(K1[0],messages,44);
- write(1,messages,44);
- printf("\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement