Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lab4_pp.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #define HAVE_STRUCT_TIMESPEC
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <semaphore.h>
- pthread_rwlock_t rwlock;
- FILE *file;
- void *entrance(void*);
- void *full_modify(void *param)
- {
- while (true) {
- pthread_rwlock_wrlock(&rwlock);
- file = fopen("file.txt", "w");
- for (int i = 0; i < 5; i++) {
- char *string = "PADDING\n";
- fputs(string, file);
- }
- fclose(file);
- pthread_rwlock_unlock(&rwlock);
- Sleep(3000);
- }
- }
- void *screen(void *param)
- {
- setbuf(stdout, NULL);
- char n;
- while (true) {
- pthread_rwlock_rdlock(&rwlock);
- file = fopen("file.txt", "r");
- while (!feof(file))
- {
- int res = fscanf(file, "%c", &n);
- if (res == EOF)
- break;
- fprintf(stdout, "%c", n);
- }
- pthread_t thread1[1];
- pthread_create(&thread1[0], NULL, entrance, NULL);
- printf("\n\n");
- fflush(stdout);
- Sleep(10);
- fclose(file);
- pthread_rwlock_unlock(&rwlock);
- Sleep(2000);
- }
- }
- void *entrance(void *param)
- {
- char n;
- setbuf(stdout, NULL);
- //while (true) {
- char count[91] = { 0 };
- pthread_rwlock_rdlock(&rwlock);
- file = fopen("file.txt", "r");
- while (!feof(file))
- {
- int res = fscanf(file, "%c", &n);
- if (res == EOF)
- break;
- count[n]++;
- }
- for (int i = 65; i < 91; i++) {
- fprintf(stdout, "%c : %d ", (char)i, count[i]);
- }
- fprintf(stdout,"\n");
- fflush(stdout);
- Sleep(10);
- fclose(file);
- pthread_rwlock_unlock(&rwlock);
- //Sleep(2000);
- //}
- return 0;
- }
- void *add(void *param)
- {
- while (true) {
- pthread_rwlock_wrlock(&rwlock);
- file = fopen("file.txt", "a");
- char string[10];
- string[8] = '\n';
- string[9] = '\0';
- for (size_t i = 0; i < 8; i++)
- {
- string[i] = 'A' + (rand() % 26);
- }
- fputs(string, file);
- fclose(file);
- pthread_rwlock_unlock(&rwlock);
- Sleep(1000);
- }
- }
- int main()
- {
- setbuf(stdout, NULL);
- pthread_rwlock_init(&rwlock, NULL);
- pthread_t thread[4] ;
- //pthread_create(&thread[0], NULL, full_modify, NULL) ;
- pthread_create(&thread[1], NULL, screen, NULL) ;
- pthread_t adders[3];
- for (int i=0; i<3; i++) pthread_create(&adders[i], NULL, add, NULL);
- //pthread_create(&thread[2], NULL, entrance, NULL);
- full_modify(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement