Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

l4_2

.c
Скачиваний:
0
Добавлен:
20.11.2024
Размер:
1.16 Кб
Скачать
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/poll.h>
#include <string.h>

sem_t* sem;
int flag = 0;
FILE* f;

int main()
{
    printf("Programm 2 started\n");

    sem = sem_open("my_sem", O_CREAT, 0664, 1);
    printf("Semaphore created\n");

    f = fopen("test.txt","a");
    if (f == NULL)
    {
        printf("Error while creating file\n");
    }else{
        printf("File created successfuly\n");
    }

    struct pollfd mypoll;
    mypoll.fd = 0;
    mypoll.events = POLLIN;

    while(flag == 0)
    {
        sem_wait(sem);
        for (int i = 0; i < 10; i++)
        {
            fprintf(f,"%d",2);
            fflush(f);
            printf("2");
            fflush(stdout);
            sleep(1);
        }
        sem_post(sem);
        sleep(1);
        if (poll(&mypoll, 1, 1000) != 0)
        {
            printf("\nKEy has been pressed\n");
            break;
        }
    }
    fclose(f);
    printf("File closed\n");

    sem_close(sem);
    printf("Semaphore closed\n");

    sem_unlink("/sem");
    printf("Semaphore deleted\n");

    return 0;
}
Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]