c - Struggling to comunicate between processes with signals -


im trying create game in c under linux terminal.

i need create tetris game consists of 2 c files, 1 c file create execute file (a.out) , other create (draw.out). first program create child process , execute other.

i need send signals other program, found dificult.

the source code :

the first file-

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <stdbool.h> #include <termios.h> #include <signal.h>  char getch();  int main() {     int fd[2],pid;     char *args[] = { "./draw.out", null },tav;     pipe(fd);     pid=fork();     if(pid==0)     {         execve("draw.out", args, null);      }     else     {          while(true)             kill(0,sigusr2);     }      return 1;     //getchar();  }  char getch() {         char buf = 0;         struct termios old = {0};         if (tcgetattr(0, &old) < 0)                 perror("tcsetattr()");         old.c_lflag &= ~icanon;         old.c_lflag &= ~echo;         old.c_cc[vmin] = 1;         old.c_cc[vtime] = 0;         if (tcsetattr(0, tcsanow, &old) < 0)                 perror("tcsetattr icanon");         if (read(0, &buf, 1) < 0)                 perror ("read()");         old.c_lflag |= icanon;         old.c_lflag |= echo;         if (tcsetattr(0, tcsadrain, &old) < 0)                 perror ("tcsetattr ~icanon");         return (buf); } 

the second file-

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <stdbool.h> #include <signal.h>   typedef struct {     int x;     int y; }point;  typedef struct {     point dots[3]; }tool;  void drawboard(int array[][20]); void initboard(int array[][20]); tool retrievetool(); bool changelocation(int array[][20],tool* tool); void my_handler(int signum);  int main() {     bool nexttool=true;     tool temp=retrievetool();     int gameboard[20][20];     signal(sigusr2, my_handler);     initboard(gameboard);     changelocation(gameboard,&temp);     drawboard(gameboard);     while(true)     {         signal(sigusr2, my_handler);         sleep(1);         system("clear");         if(!changelocation(gameboard,&temp))             temp=retrievetool();         drawboard(gameboard);     }     return 1;     //getchar();  }  void initboard(int array[][20]) {     bool islast=false;     int i=0,j=0;     for(i=0;i<20;i++)     {         if(i==19)             islast=true;         for(j=0;j<20;j++)         {             if((j==0)||(j==19)||(islast))                 array[i][j]=1;             else                 array[i][j]=0;         }     } }  void drawboard(int symbols[][20]) {     int i=0,j=0;     for(i=0;i<20;i++)     {         for(j=0;j<20;j++)             if(symbols[i][j]==1)                 printf("*");             else                 if(symbols[i][j]==2)                     printf("-");                 else                     printf(" ");         printf("\n");     } }  tool retrievetool() {     tool temp;     int startx=0,starty=8,i=0;     for(i=0;i<3;i++)     {         temp.dots[i].x=startx;         temp.dots[i].y=starty;         starty++;     }     return temp; }  bool changelocation(int array[][20],tool* tool) {     int i=0;     for(i=0;i<3;i++)     {         if(array[tool->dots[i].x+1][tool->dots[i].y]!=0)             return false;     }     i=0;     for(i=0;i<3;i++)     {         array[tool->dots[i].x][tool->dots[i].y]=0;         if((tool->dots[i].x+1)==19)             tool->dots[i].x=-1;         tool->dots[i].x++;         array[tool->dots[i].x][tool->dots[i].y]=2;     }     return true; }  void my_handler(int signum) {     if (signum == sigusr2)     {         printf("received sigusr1!\n");     } } 

the draw.out output file of second file.

i created signal handeler in second file, program still don't recieve signal, doing wrong?

thank you, asaf.

this fragment:

while(true) kill(0,sigusr2); 

has no sense. kill should used process id of reveiver of sigusr2 (the child process in case, identified pid). note infinite loop sending signals child process not want.

in child process, have error in print statement @ signal handler:

printf("received sigusr1!\n"); 

should be

printf("received sigusr2!\n"); 

depending on os version, have reinstall signal handler once gets called

note: synchronizing processes, not threads


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -