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 10
- void printString();
- void setQuitFlag(int);
- void setPrintingFlag(int);
- void bindSignals();
- void init();
- void child();
- int waitForChild(pid_t);
- void onQuit(pid_t*);
- int analyzePid(pid_t);
- int addChildProcess(pid_t*);
- void removeChildProcess(pid_t*);
- /*
- * Global variables
- */
- struct sigaction endProcessSignal, printingSignal;
- int numberOfProcesses = 0;
- char textStrings[MAX_PROCESS_NUMBER][50] = {
- "<00>",
- "<11>",
- "<22>",
- "<33>",
- "<44>",
- "<55>",
- "<66>",
- "<77>",
- "<88>",
- "<99>"};
- int allowPrint = 1,
- quitFlag = 0;
- int parentPid;
- int main()
- {
- init();
- pid_t childPids[MAX_PROCESS_NUMBER];
- int currentProcessIndex = 0;
- while(1)
- {
- fflush(stdin);
- char s = getchar();
- switch(s)
- {
- case 'q':
- onQuit(childPids);
- exit(EXIT_SUCCESS);
- break;
- case '+':
- addChildProcess(childPids);
- break;
- case '-':
- removeChildProcess(childPids);
- break;
- default:
- break;
- }
- if( allowPrint && numberOfProcesses)
- {
- currentProcessIndex = currentProcessIndex >= numberOfProcesses ? 0 : currentProcessIndex;
- kill(childPids[currentProcessIndex], SIGUSR2);
- allowPrint = 0;
- currentProcessIndex++;
- }
- }
- clear();
- endwin();
- exit(EXIT_SUCCESS);
- }
- void removeChildProcess(pid_t* childPids)
- {
- if( numberOfProcesses )
- {
- kill(childPids[numberOfProcesses-1], SIGUSR1);
- waitForChild(childPids[numberOfProcesses-1]);
- --numberOfProcesses;
- }
- }
- int addChildProcess(pid_t* childPids)
- {
- if ( MAX_PROCESS_NUMBER == numberOfProcesses )
- {
- return 1;
- }
- pid_t pid = fork();
- if (analyzePid(pid))
- {
- return 1;
- }
- allowPrint = numberOfProcesses ? allowPrint : 1;
- ++numberOfProcesses;
- childPids[numberOfProcesses-1] = pid;
- usleep(10); // wait for sigaction binding in child process
- return 0;
- }
- int analyzePid(pid_t pid)
- {
- if ( !pid )
- {
- child();
- exit(EXIT_SUCCESS);
- }
- if ( pid == -1) // ERROR
- {
- printw("Error!\n");
- refresh();
- return 1;
- }
- return 0;
- }
- int getPosition()
- {
- int pos = getpid();
- pos -= parentPid;
- return pos > numberOfProcesses ? numberOfProcesses : pos;
- }
- void printString()
- {
- int position = getPosition();
- for(int i = 0; i < strlen(textStrings[position]); i++)
- {
- printw("%c", textStrings[position][i]);
- refresh();
- usleep(100000);
- }
- }
- void setQuitFlag(int arg)
- {
- quitFlag = 1;
- }
- void setPrintingFlag(int arg)
- {
- allowPrint = 1;
- }
- void bindSignals()
- {
- endProcessSignal.sa_handler = setQuitFlag;
- sigaction(SIGUSR1,&endProcessSignal,NULL);
- printingSignal.sa_handler = setPrintingFlag;
- sigaction(SIGUSR2,&printingSignal,NULL);
- }
- void init()
- {
- initscr();
- curs_set(0);
- refresh();
- bindSignals();
- }
- void child()
- {
- allowPrint = 0;
- int position = getPosition();
- while(!quitFlag)
- {
- if(allowPrint)
- {
- printString();
- allowPrint = 0;
- kill(getppid(), SIGUSR2);
- }
- }
- }
- int waitForChild(pid_t childPid)
- {
- pid_t w;
- int status;
- do
- {
- w = waitpid(childPid, &status, WUNTRACED | WCONTINUED);
- }while(!WIFEXITED(status) && !WIFSIGNALED(status));
- return status;
- }
- /*
- * Ends child processes.
- */
- void onQuit(pid_t* pidArray)
- {
- for(int i = 0; i < numberOfProcesses; i++ )
- {
- kill(pidArray[i], SIGUSR1);
- waitForChild(pidArray[i]);
- }
- clear();
- endwin();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement