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>
- typedef struct _thread {
- pthread_t tid;
- char my_str[1024];
- int occ;
- } thread;
- char buffer[1024];
- FILE *file;
- thread *list;
- int N, sem_m, sem_th;
- struct sembuf oper_m;
- void error_handler(char *message) {
- fprintf(stderr, "%s\n", message);
- perror("ERROR");
- exit(EXIT_FAILURE);
- }
- void SIGINT_handler(int a, siginfo_t *b, void *c) {
- int i;
- printf("\n");
- for (i = 0; i < N; i++) {
- printf("Thread %ld has %d occurrences\n",list[i].tid, list[i].occ);
- }
- }
- void *run(void *index) {
- list[(long)index].tid = pthread_self();
- list[(long)index].occ = 0;
- struct sembuf oper_t;
- int ret;
- oper_t.sem_num = (long)index;
- oper_t.sem_op = -1;
- oper_t.sem_flg = 0;
- while(1) {
- semop1: ret = semop(sem_th, &oper_t, 1);
- if (ret == -1) {
- if (errno == EINTR)
- goto semop1;
- error_handler("Semaphore operation failed");
- }
- if (strstr(list[(long)index].my_str, buffer) != NULL) {
- printf("Thread %ld has found a new occurrence\n",list[(long)index].tid);
- ret = fprintf(file, "%s\n", buffer);
- fflush(file);
- if (ret < 0) {
- error_handler("File write has failed");
- }
- list[(long)index].occ += 1;
- }
- oper_m.sem_num = 0;
- oper_m.sem_op = 1;
- oper_m.sem_flg = 0;
- semop2: ret = semop(sem_m, &oper_m, 1);
- if (ret == -1) {
- error_handler("Semaphore operation has failed");
- if(errno == EINTR)
- goto semop2;
- }
- }
- }
- int main(int argc,char *argv[]) {
- int ret;
- long i;
- pthread_t tid;
- sigset_t set;
- struct sigaction act;
- struct sembuf oper_t;
- if(argc < 3) {
- error_handler("Not valid input");
- }
- N = argc - 2;
- file = fopen(argv[1], "w+");
- if (file == NULL) {
- error_handler("Unable to open the file");
- }
- list = malloc(N*sizeof(thread*));
- sem_m = semget(IPC_PRIVATE, 1, IPC_CREAT|0666);
- if (sem_m == -1) {
- error_handler("Unable to get a semaphore");
- }
- sem_th = semget(IPC_PRIVATE, N, IPC_CREAT|0666);
- if (sem_th == -1) {
- error_handler("Unable to get a semaphore");
- }
- ret = semctl(sem_m, 0, SETVAL, N);
- if (ret == -1) {
- error_handler("semctl has failed");
- }
- for (i = 0; i < N; i++) {
- ret = semctl(sem_th, i, SETVAL, 0);
- if (ret == -1) {
- error_handler("semctl has failed");
- }
- }
- for (i = 0; i < N; i++) {
- strcpy(list[i].my_str, argv[i+2]);
- ret = pthread_create(&tid, NULL, run, (void *)i);
- if (ret != 0) {
- error_handler("Creation thread has failed");
- }
- }
- ret = sigfillset(&set);
- act.sa_sigaction = SIGINT_handler;
- act.sa_mask = set;
- act.sa_flags = 0;
- sigaction(SIGINT, &act, NULL);
- oper_m.sem_num = 0;
- oper_m.sem_op = -N;
- oper_m.sem_flg = 0;
- printf("Main thread is ready to receive strings\n");
- while(1) {
- step1: ret = semop(sem_m, &oper_m, 1);
- if (ret == -1) {
- if (errno == EINTR)
- goto step1;
- error_handler("Semaphore operation failed");
- }
- step2: ret = scanf("%s", buffer);
- if (ret == -1) {
- if (errno == EINTR)
- goto step2;
- error_handler("scanf has failed");
- }
- if (ret == 0) {
- while(getchar() != '\n') {}
- printf("Not valid input. Retry\n");
- goto step2;
- }
- oper_t.sem_op = 1;
- oper_t.sem_flg = 0;
- for (i = 0; i < N; i++) {
- oper_t.sem_num = i;
- step3: ret = semop(sem_th, &oper_t, 1);
- if (ret == -1) {
- if (errno == EINTR)
- goto step3;
- error_handler("Semaphore operation failed");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement