6 /* open/read/write/close */
9 /* Signals handling.. */
14 /** Named pipe used to communicate with process2 */
15 char * read_pipe = "/tmp/process2pipe";
17 /** Descriptor of input pipe */
22 * Shared memory variables
25 * Memory key for processes. Must be same between all processes to properly
30 * Id of the shared memory
35 * Message shared by processes. Contains array of process IDs
41 struct message * processes = NULL;
45 * Handler for signals.
47 void sig_handler(int signo)
49 fprintf(stderr, "[%s] Received %s!\n", "process3", strsignal(signo));
50 if (signo == SIGUSR1) {
52 else if (signo == SIGTERM) {
54 fprintf(stderr, "[%s] > Releasing resources\n", "process3");
55 close(read_descriptor);
57 fprintf(stderr, "[%s] Process %d, PID: %d\n", "process3", i, processes->pids[i]);
61 else if (signo == SIGTSTP) {
62 fprintf(stderr, "[%s] > Closing pipe\n", "process3");
63 close(read_descriptor);
66 else if (signo == SIGCONT) {
67 fprintf(stderr, "[%s] > Opening pipe\n", "process3");
68 read_descriptor = open(read_pipe, O_RDONLY);
73 * Program grabs data (calculated number of characters) from process2 and prints
74 * grabbed data to the standard output.
77 /** Buffer used for storing data from input pipe */
80 /** Stores number of bytes read from input pipe in current iteration */
83 fprintf(stderr, "[%s] Init!\n", "process3");
86 * Register signals handled by process
88 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
89 fprintf(stderr, "can't catch SIGUSR1\n");
91 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
92 fprintf(stderr, "can't catch SIGTERM\n");
94 if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
95 fprintf(stderr, "can't catch SIGTSTP\n");
97 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
98 fprintf(stderr, "can't catch SIGCONT\n");
102 * Register memory to share with other processes, and pass current
103 * process id to the array.
105 shmid = shmget(shmkey, sizeof(struct message), IPC_CREAT | 0666);
107 processes = (struct message *)shmat(shmid, NULL, 0);
108 processes->pids[2] = getpid();
110 /* Reading from process2 */
111 read_descriptor = open(read_pipe, O_RDONLY);
114 /* Read data from input pipe */
115 count = read(read_descriptor, &buffer, sizeof(int));
117 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process3", count);
120 fprintf(stderr, "[%s] Process2 send: %d\n", "process3", buffer);
127 /* Release resources in normal program flow exit. */
128 close(read_descriptor);