int main (void) { int error; int i; pthread_t tid[NUMTHREADS]; int val; if (initshared(0)) { perror("Could not initialize shared variable"); return 1; } getshared(&val); printf("Shared variable initialized to %d\n", val); for (i = 0; i < NUMTHREADS; i++) if (error = pthread_create(tid+i, NULL, increment, NULL)) fprintf(stderr, "Failed to create thread: %s\n", strerror(error)); printf("Number of threads created: %d\n", NUMTHREADS); for (i = 0; i < NUMTHREADS; i++) if (error = pthread_join(tid[i], NULL)) fprintf(stderr, "Failed to join thread: %s\n", strerror(error)); printf("All threads done\n"); getshared(&val); printf("Shared variable now has value %d\n", val); return 0; }
int main(int argc, char *argv[]) { struct sigaction act; int key; sigset_t mask, oldmask; if (argc != 2) { fprintf(stderr, "Usage: %s key\n", argv[0]); return 1; } key = atoi(argv[1]); if (initshared(key) == -1) { perror("Failed to initialize shared memory"); return 1; } if ((sigfillset(&mask) == -1) || (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1)) { perror("Failed to block signals to set up handlers"); return 1; } printf("This is process %ld waiting for SIGUSR1 (%d)\n", (long)getpid(), SIGUSR1); act.sa_handler = showit; act.sa_flags = 0; if ((sigemptyset(&act.sa_mask) == -1) || (sigaction(SIGUSR1, &act, NULL) == -1)) { perror("Failed to set up signal handler"); return 1; } if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1) { perror("Failed to unblock signals"); return 1; } for ( ; ; ) pause(); }