zadania brak pytan

zadania brak pytan
A2
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 1
0

Ocenicie moje prace? nie mam pytań tylko rozwiązania

zad1

Kopiuj
#include <iostream>
#include <pthread.h>

void* print_array(void* arg) {
    int* arr = static_cast<int*>(arg);
    for (int i = 0; i < 10; ++i) {
        std::cout << "arr[" << i << "] = " << arr[i] << std::endl;
    }
    pthread_exit(NULL);
}

int main() {
    pthread_t thread;
    int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    pthread_create(&thread, NULL, print_array, array);
    pthread_join(thread, NULL);  // poprawne zamknięcie

    return 0;
}

zad2

Kopiuj
#include <iostream>
#include <pthread.h>

int N = 0;

void* increment_thread(void* arg) {
    for (int i = 0; i < 10; ++i) N++;
    pthread_exit(NULL);
}

int main() {
    pthread_t thread;
    pthread_create(&thread, NULL, increment_thread, NULL);

    for (int i = 0; i < 10; ++i) N++;

    pthread_join(thread, NULL);
    std::cout << "Wartosc N: " << N << std::endl;

    return 0;
}

zad3

Kopiuj
#include <iostream>
#include <pthread.h>

int N = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void* increment_thread(void* arg) {
    for (int i = 0; i < 10; ++i) {
        pthread_mutex_lock(&mutex);
        N++;
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}

int main() {
    pthread_t thread;
    pthread_create(&thread, NULL, increment_thread, NULL);

    for (int i = 0; i < 10; ++i) {
        pthread_mutex_lock(&mutex);
        N++;
        pthread_mutex_unlock(&mutex);
    }

    pthread_join(thread, NULL);
    std::cout << "Bezpieczna wartosc N: " << N << std::endl;

    return 0;
}

zad4

Kopiuj
#include <iostream>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <cstring>

struct msg_buffer {
    long msg_type;
    char msg_text[100];
};

int main() {
    key_t key = ftok("progfile", 65);
    int msgid = msgget(key, 0666 | IPC_CREAT);

    msg_buffer message;
    message.msg_type = 1;

    std::cout << "Podaj komunikat: ";
    fgets(message.msg_text, sizeof(message.msg_text), stdin);
    msgsnd(msgid, &message, sizeof(message), 0);

    msgrcv(msgid, &message, sizeof(message), 1, 0);
    std::cout << "Odebrano: " << message.msg_text << std::endl;

    msgctl(msgid, IPC_RMID, NULL);
    return 0;
}

zad5

Kopiuj
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <unistd.h>

struct msg_buffer {
    long msg_type;
    char msg_text[100];
};

int main() {
    key_t key = ftok("progfile", 65);
    int msgid = msgget(key, 0666 | IPC_CREAT);

    struct msg_buffer message;
    message.msg_type = 1;

    if (fork() == 0) {
        // Proces dziecka - odbiorca
        msgrcv(msgid, &message, sizeof(message), 1, 0);
        printf("Odebrano: %s", message.msg_text);
    } else {
        // Proces rodzic - nadawca
        printf("Wpisz dane: ");
        fgets(message.msg_text, sizeof(message.msg_text), stdin);
        msgsnd(msgid, &message, sizeof(message), 0);
        wait(NULL);
        msgctl(msgid, IPC_RMID, NULL);
    }

    return 0;
}

Zadanie 6.1.2 Współdzielona pamięć bez synchronizacji

Kopiuj
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>

int main() {
    const char* name = "/shared_mem";
    int shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
    ftruncate(shm_fd, 1024);
    char* ptr = (char*)mmap(0, 1024, PROT_WRITE, MAP_SHARED, shm_fd, 0);

    while (1) {
        strcpy(ptr, "CIEPLO");
        sleep(1);
        strcpy(ptr, "ZIMNO");
        sleep(1);
    }

    munmap(ptr, 1024);
    close(shm_fd);
    shm_unlink(name);
    return 0;
}

Program 2

Kopiuj
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>

int main() {
    const char* name = "/shared_mem";
    int shm_fd = shm_open(name, O_RDWR, 0666);
    char* ptr = (char*)mmap(0, 1024, PROT_READ, MAP_SHARED, shm_fd, 0);

    while (1) {
        if (strcmp(ptr, "CIEPLO") == 0 || strcmp(ptr, "ZIMNO") == 0) {
            printf("Odczyt: %s\n", ptr);
        } else {
            printf("Błąd: niepoprawna wartość: %s\n", ptr);
        }
        sleep(1);
    }

    munmap(ptr, 1024);
    close(shm_fd);
    return 0;
}

Zadanie 6.3

Kopiuj
struct SharedData {
    char text[64];
};

#include <stdio.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>

struct SharedData {
    char text[64];
};

int main() {
    const char* shm_name = "/shared_struct";
    const char* sem_name = "/sem_shared";

    int shm_fd = shm_open(shm_name, O_CREAT | O_RDWR, 0666);
    ftruncate(shm_fd, sizeof(struct SharedData));
    struct SharedData* data = (struct SharedData*)mmap(0, sizeof(struct SharedData), PROT_WRITE, MAP_SHARED, shm_fd, 0);

    sem_t* sem = sem_open(sem_name, O_CREAT, 0666, 1);

    while (1) {
        sem_wait(sem);
        strcpy(data->text, "CIEPLO");
        sleep(1);
        strcpy(data->text, "ZIMNO");
        sleep(1);
        sem_post(sem);
    }

    munmap(data, sizeof(struct SharedData));
    close(shm_fd);
    shm_unlink(shm_name);
    sem_close(sem);
    sem_unlink(sem_name);

    return 0;
}
Kopiuj
#include <stdio.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>

struct SharedData {
    char text[64];
};

int main() {
    const char* shm_name = "/shared_struct";
    const char* sem_name = "/sem_shared";

    int shm_fd = shm_open(shm_name, O_RDWR, 0666);
    struct SharedData* data = (struct SharedData*)mmap(0, sizeof(struct SharedData), PROT_READ, MAP_SHARED, shm_fd, 0);

    sem_t* sem = sem_open(sem_name, 0);

    while (1) {
        sem_wait(sem);
        if (strcmp(data->text, "CIEPLO") == 0 || strcmp(data->text, "ZIMNO") == 0) {
            printf("Odczyt: %s\n", data->text);
        } else {
            printf("Błąd: Nieprawidłowy tekst: %s\n", data->text);
        }
        sem_post(sem);
        sleep(1);
    }

    munmap(data, sizeof(struct SharedData));
    close(shm_fd);
    sem_close(sem);

    return 0;
}
cerrato
  • Rejestracja: dni
  • Ostatnio: dni
  • Lokalizacja: Poznań
  • Postów: 9012
3

Dziwne to Twoje zapytanie. W sensie - zrobiłeś zadania i prosisz o ich ocenę, ale nie masz do nich pytań?

Pomijając, że wygląda to mocno podejrzanie (pewnie masz jakieś zadanie na zaliczenie, skądś je spisałeś i chcesz przedstawić jako swoje, ale boisz się, że wykładowca Cię zapyta - a nic nie rozumiesz z tego, co wkleiłeś powyzej), ale tak poza tym - ciężko jest zrecenzować coś, jak nie wiemy dokładnie, co dany kod ma robić. OK, można wyłapać błędy składniowe, pokazać jak coś uprościć itp, ale co do samej oceny działania - skoro nie wiemy, jakie wytyczne mają zostać spełnione, to ciężko ocenić, czy program robi to, co ma robić.

CZ
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 2541
4

Z szybkich ocen to zdecyduj się czy piszesz w c++ czy w c, bo mieszasz języki. Przynajmniej na poczatku. No i proste zmienne wewal w atomica. Od standardu C11 masz #include <stdatomic.h>

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.