Question
Dears, Please advice output file not generated for this code #include #include #include #include #include #include #include #include /*This implementation uses POSIX threads to create
Dears, Please advice output file not generated for this code
#include
/*This implementation uses POSIX threads to create three threads for the Alpha.exe, Counter.exe and Re.exe processes.*/
#define MAX_LEN 20 #define RE_EXEC_DELAY 1
pthread_mutex_t mutex; int counter = 0; char last_alpha_char; int last_counter;
void sig_handler(int sig) { printf("test"); pthread_mutex_lock(&mutex); FILE *fp = fopen("output.txt", "a"); if (!fp) perror("fopen");
if (fp == NULL) { printf("Error opening file! "); exit(1); } fprintf(fp, "Counter: %d Alpha: %c ", last_counter, last_alpha_char); fclose(fp); pthread_mutex_unlock(&mutex); }
void *alpha_func(void *arg) { while (1) { sleep(1); char random_char = 'A' + (rand() % 26); last_alpha_char = random_char; printf("%c ", random_char); } }
void *counter_func(void *arg) { while (1) { sleep(1); counter++; last_counter = counter; printf("%d ", counter); } }
void *re_func(void *arg) { while (1) { sleep(RE_EXEC_DELAY * 60); printf("RESTART "); } }
int main(int argc, char *argv[]) { signal(SIGTERM, sig_handler); /*Abnormal termination of the program, such as a call to abort.*/ signal(SIGINT, sig_handler); /*Receipt of an interactive attention signal.*/ srand(time(NULL));
pthread_t alpha, counter, re; pthread_create(&alpha, NULL, alpha_func, NULL); pthread_create(&counter, NULL, counter_func, NULL); pthread_create(&re, NULL, re_func, NULL);
pthread_join(alpha, NULL); pthread_join(counter, NULL); pthread_join(re, NULL);
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started