Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ncurses.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- #include <string.h>
- #include <sys/wait.h>
- #define MAX_PROCESS_NUMBER 100
- void printString();
- void make_string(int num);
- void quit(int sig);
- void chgFlag(int);
- struct sigaction endProcessSignal, printingSignal;
- int num = 0;
- char string[MAX_PROCESS_NUMBER][200];
- int allowPrint = 1, killflag=0;
- int parentPid;
- void bindSignals()
- {
- endProcessSignal.sa_handler = quit;
- sigaction(SIGUSR1,&endProcessSignal,NULL);
- printingSignal.sa_handler = chgFlag;
- sigaction(SIGUSR2,&printingSignal,NULL);
- }
- void init()
- {
- initscr();
- curs_set(0);
- refresh();
- bindSignals();
- }
- void child()
- {
- allowPrint;
- int pos; // extract pos changing to separate func ?
- pos = getpid();
- make_string(num);
- pos -= parentPid; // position in list ?
- if(pos > num){
- pos = num;
- }
- while(!killflag)
- {
- if(allowPrint)
- {
- printString();
- allowPrint = 0;
- kill(getppid(), SIGUSR2); // send sigusr2 to parent ?
- }
- }
- }
- int waitForChild(pid_t childPid)
- {
- pid_t w;
- int status;
- do
- {
- w = waitpid(childPid, &status, WUNTRACED | WCONTINUED);
- }while(!WIFEXITED(status) && !WIFSIGNALED(status));
- return status;
- }
- void onQuit(pid_t* pidArray)
- {
- for(int i = 0; i < num; i++ )
- {
- kill(pidArray[i], SIGUSR1);
- waitForChild(pidArray[i]);
- }
- clear();
- endwin();
- }
- int main()
- {
- init();
- pid_t childPids[MAX_PROCESS_NUMBER], tpid;
- int currentProcessIndex = 0;
- char s;
- while(1)
- {
- fflush(stdin);
- s = getchar();
- switch(s)
- {
- case 'q':
- onQuit(childPids);
- exit(EXIT_SUCCESS);
- break;
- case '+':
- tpid = fork();
- switch(tpid)
- {
- case 0:
- child();
- exit(EXIT_SUCCESS);
- break;
- case -1:
- printw("Error!\n");
- break;
- default:
- // In parent process
- allowPrint = num ? allowPrint : 1;
- ++num;
- childPids[num-1] = tpid;
- usleep(10); // wait for sigaction binding in child process
- break;
- }
- break;
- case '-':
- if( num )
- {
- kill(childPids[num-1], SIGUSR1);
- waitForChild(childPids[num-1]);
- --num;
- }
- break;
- default:
- break;
- }
- if( allowPrint && num)
- {
- currentProcessIndex = currentProcessIndex >= num ? 0 : currentProcessIndex;
- kill(childPids[currentProcessIndex], SIGUSR2);
- allowPrint = 0;
- currentProcessIndex++;
- }
- }
- clear();
- endwin();
- exit(EXIT_SUCCESS);
- }
- void printString()
- {
- int pos = getpid();
- char c = 13;
- pos -= parentPid;
- if(pos > num){
- pos = num;
- }
- for(int i = 0; i < strlen(string[pos]); i++)
- {
- printw("%c", string[pos][i]);
- refresh();
- usleep(100000);
- }
- }
- void make_string(int num)
- {
- int pos=getpid()-parentPid;
- int m=getpid();
- if(pos>num)
- pos=num;
- char c = '0' + num;
- for (int i = 0; i < 10; ++i)
- {
- string[pos][i] = c;
- }
- }
- void quit(int sig)
- {
- killflag = 1;
- }
- void chgFlag(int a)
- {
- allowPrint = 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement