Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef Posix_compile
- #include <unistd.h>
- #include <errno.h>
- #include <signal.h>
- #include <pthread.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/mman.h>
- #include <sys/sem.h>
- #include <semaphore.h>
- #include <fcntl.h>
- #else
- #include <windows.h>
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define SIZE 4096
- void error_handler(char *message) {
- fprintf(stderr, "%s\n", message);
- perror("ERROR");
- exit(EXIT_FAILURE);
- }
- int N;
- pid_t child[64];
- int indx = 0;
- char *buffer[1024];
- void SIGINT_handler_main(int a, siginfo_t *b, void *c) {
- int i;
- for(i = 0; i < N; i++) {
- kill(child[i], SIGINT);
- }
- }
- void SIGINT_handler_child(int a, siginfo_t *b, void *c) {
- int i;
- printf("Strings of process %d:\n",getpid());
- for (i = 0; i < indx; i++) {
- printf("%s ",buffer[i]);
- }
- printf("\n");
- }
- int main(int argc, char** argv){
- N = argc-1;
- pid_t pid;
- void *shm;
- int sem, i, ret;
- sigset_t set;
- struct sembuf oper;
- struct sigaction act;
- if (argc < 2) {
- error_handler("Not valid input");
- }
- shm = mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, 0, 0);
- if (shm == NULL) {
- error_handler("mmap has failed");
- }
- sem = semget(IPC_PRIVATE, N + 1, IPC_CREAT|0666);
- if (sem == -1) {
- error_handler("semget has failed");
- }
- for (i = 0; i < N; i++) {
- ret = semctl(sem, i, SETVAL, 0);
- if (ret == -1) {
- error_handler("semctl has failed");
- }
- }
- ret = semctl(sem, N, SETVAL, N);
- if (ret == -1) {
- error_handler("semctl has failed");
- }
- for (i = 0; i < N; i++) {
- pid = fork();
- if (pid == -1) {
- error_handler("fork has failed");
- }
- if (pid == 0) {
- char my_str[1024];
- int my_index = i;
- if (strcpy(my_str, argv[my_index + 1]) == NULL) {
- error_handler("strcpy has failed");
- }
- ret = sigfillset(&set);
- if (ret == -1) {
- error_handler("sigfillset has failed");
- }
- act.sa_sigaction = SIGINT_handler_child;
- act.sa_mask = set;
- act.sa_flags = 0;
- ret = sigaction(SIGINT, &act, NULL);
- if (ret == -1) {
- error_handler("sigaction has failed");
- }
- while (1) {
- oper.sem_num = my_index;
- oper.sem_op = -1;
- oper.sem_flg = 0;
- step1: ret = semop(sem, &oper, 1);
- if (ret == -1) {
- if (errno == EINTR) goto step1;
- error_handler("semop has failed");
- }
- if (strstr(my_str,(char*)shm) != NULL) {
- step2: buffer[indx] = malloc(strlen((char*)shm)+1);
- if (buffer[indx] == NULL) {
- if (errno == EINTR) goto step2;
- error_handler("malloc has failed");
- }
- if (strcpy(buffer[indx], (char *)shm) == NULL) {
- error_handler("strcpy has failed");
- }
- indx ++;
- }
- oper.sem_num = N;
- oper.sem_op = 1;
- oper.sem_flg = 0;
- step3: ret = semop(sem, &oper, 1);
- if (ret == -1) {
- if (errno == EINTR) goto step3;
- error_handler("semop has failed");
- }
- }
- }
- child[i] = pid;
- }
- ret = sigfillset(&set);
- if (ret == -1) {
- error_handler("sigfillset has failed");
- }
- act.sa_sigaction = SIGINT_handler_main;
- act.sa_mask = set;
- act.sa_flags = 0;
- ret = sigaction(SIGINT, &act, NULL);
- if (ret == -1) {
- error_handler("sigaction has failed");
- }
- printf("Main process is ready to receive strings\n");
- while (1) {
- oper.sem_num = N;
- oper.sem_op = -N;
- oper.sem_flg = 0;
- stepA: ret = semop(sem, &oper, 1);
- if (ret == -1) {
- if (errno == EINTR) goto stepA;
- error_handler("semop has failed");
- }
- stepB: ret = scanf("%s", (char*)shm);
- if (ret == -1) {
- if (errno == EINTR) goto stepB;
- error_handler("scanf has failed");
- }
- for(i = 0; i < N; i++) {
- oper.sem_num = i;
- oper.sem_op = 1;
- oper.sem_flg = 0;
- stepC: ret = semop(sem, &oper, 1);
- if (ret == -1) {
- if (errno == EINTR) goto stepC;
- error_handler("semop has failed");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement