Discussione Ufficiale Programmiamo con Inforge | Esercitazione 03 in C | Livello intermedio

Una Discussione Ufficiale punta a raccogliere tutte le informazioni su un argomento o un fatto di attualità, con costanti aggiornamenti da parte del creatore e dei partecipanti.
Ecco le mie!

Ciao
 

Allegati

  • ex5.txt
    3.7 KB · Visualizzazioni: 12
  • ex4.txt
    1.9 KB · Visualizzazioni: 5
  • ex3.txt
    1.7 KB · Visualizzazioni: 7
  • ex2.txt
    974 bytes · Visualizzazioni: 6
  • ex1.txt
    434 bytes · Visualizzazioni: 12
C:
#Esercizio 1


#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#define CHILD_PID 0
#define FAILED_FORK -1

int main()
{
    int pid = fork();
    if (pid == FAILED_FORK)
    {
        puts("[!] Fork call failed");
        exit(EXIT_FAILURE);
    }
    if (pid == CHILD_PID)
    {

        printf("[*] Child process id: %ld\n", (long)getpid());
    }
    else
    {
        printf("[*] Parent process id: %ld\n", (long)getpid());
    }
}

C:
#Esercizio 2

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#define CHILD_PID 0
#define FAILED_FORK -1

static void check_fork_succesfull_executed(pid_t pid);

int main()
{
    pid_t child_a, child_b;

    child_a = fork();
    check_fork_succesfull_executed(child_a);

    if (child_a == CHILD_PID)
    {
        /* Child A code */
        printf("[*] Child A process id: %ld\n", (long)getpid());
    }
    else
    {
        child_b = fork();
        check_fork_succesfull_executed(child_b);
        if (child_b == CHILD_PID)
        {
            /* Child B code */
            printf("[*] Child B process id: %ld\n", (long)getpid());
        }
        else
        {
            /* Parent Code */
            printf("[*] Parent process id: %ld\n", (long)getpid());
        }
    }
}

static void check_fork_succesfull_executed(pid_t pid)
{
    if (pid == FAILED_FORK)
    {
        puts("[!] Fork call failed");
        exit(EXIT_FAILURE);
    }
}

C:
#Esercizio 3

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define CHILD_PID 0
#define FAILED_FORK -1
#define PIPE_READ_END 0
#define PIPE_WRITE_END 1
#define FAILED_PIPE_CREATION -1

static void check_fork_succesfull_executed(pid_t pid);
static void check_pipe_succesfull_created(int pipe_code);

int main()
{
    pid_t child_a;
    int pipe_fd[2];

    // create pipe
    int pipe_creation_code = pipe(pipe_fd);
    check_pipe_succesfull_created(pipe_creation_code);

    child_a = fork();
    check_fork_succesfull_executed(child_a);

    if (child_a == CHILD_PID)
    {
        /* Child code */

        /* child is writer: close pipe read end */
        close(pipe_fd[PIPE_READ_END]);

        // write process id to pipe and exit
        char buffer[sizeof(pid_t)];
        pid_t c_pid = getpid();
        sprintf(buffer, "%d", c_pid);
        write(pipe_fd[PIPE_WRITE_END], buffer, sizeof(pid_t));
        exit(EXIT_SUCCESS);
    }
    else
    {
        /* Parent Code */

        /* parent is reader: close write end */
        close(pipe_fd[PIPE_WRITE_END]);

        // read process id from pipe and print it
        char buffer[sizeof(pid_t)];
        ssize_t read_bytes = read(pipe_fd[PIPE_READ_END], buffer, sizeof(pid_t));

        printf("[*] I'm parent process %d and i got child pid: %s\n", getpid(), buffer);
    }
    exit(EXIT_SUCCESS);
}

static void check_fork_succesfull_executed(pid_t pid)
{
    if (pid == FAILED_FORK)
    {
        puts("[!] Fork call failed");
        exit(EXIT_FAILURE);
    }
}

static void check_pipe_succesfull_created(int pipe_code)
{
    if (pipe_code == FAILED_PIPE_CREATION)
    {
        puts("[!] Pipe creation failed");
        exit(EXIT_FAILURE);
    }
}

C:
#Esercizio 4


#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#define CHILD_PID 0
#define FAILED_FORK -1
#define PIPE_READ_END 0
#define PIPE_WRITE_END 1
#define FAILED_PIPE_CREATION -1

static void check_fork_succesfull_executed(pid_t pid);
static void check_pipe_succesfull_created(int pipe_code);
static int ranged_rand(int lower, int upper);

int main()
{
    pid_t child_a;
    int pipe_fd[2];

    // create pipe
    int pipe_creation_code = pipe(pipe_fd);
    check_pipe_succesfull_created(pipe_creation_code);

    child_a = fork();
    check_fork_succesfull_executed(child_a);

    if (child_a == CHILD_PID)
    {
        /* Child code */

        /* child is writer: close pipe read end */
        close(pipe_fd[PIPE_READ_END]);

        // write process id to pipe and exit
        char buffer[sizeof(int)];
        int rand_0_200 = ranged_rand(0, 200);
        sprintf(buffer, "%d", rand_0_200);
        write(pipe_fd[PIPE_WRITE_END], buffer, sizeof(int));
        exit(EXIT_SUCCESS);
    }
    else
    {
        /* Parent Code */
        char buffer[sizeof(int)];

        /* parent is reader: close write end */
        close(pipe_fd[PIPE_WRITE_END]);

        // read process id from pipe and print it
        ssize_t read_bytes = read(pipe_fd[PIPE_READ_END], buffer, sizeof(int));

        printf("[*] I'm parent process %d and i got a random number in 0-200 by child: %s\n", getpid(), buffer);
    }
    exit(EXIT_SUCCESS);
}

static void check_fork_succesfull_executed(pid_t pid)
{
    if (pid == FAILED_FORK)
    {
        puts("[!] Fork call failed");
        exit(EXIT_FAILURE);
    }
}

static void check_pipe_succesfull_created(int pipe_code)
{
    if (pipe_code == FAILED_PIPE_CREATION)
    {
        puts("[!] Pipe creation failed");
        exit(EXIT_FAILURE);
    }
}

static int ranged_rand(int lower, int upper)
{
    srand(time(0));
    return (rand() % (upper - lower + 1)) + lower;
}

C:
#Esercizio 5


#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>

#define CHILD_PID 0
#define FAILED_FORK -1
#define PIPE_READ_END 0
#define PIPE_WRITE_END 1
#define FAILED_PIPE_CREATION -1

static void check_fork_succesfull_executed(pid_t pid);
static void check_pipe_succesfull_created(int pipe_code);
static int read_integer();

int main()
{
    pid_t child_a, child_b;
    int pipe_1_fd[2], pipe_2_fd[2];

    // open two pipes
    int pipe_1_creation_code = pipe(pipe_1_fd);
    check_pipe_succesfull_created(pipe_1_creation_code);
    int pipe_2_creation_code = pipe(pipe_2_fd);
    check_pipe_succesfull_created(pipe_2_creation_code);

    child_a = fork();
    check_fork_succesfull_executed(child_a);

    if (child_a == CHILD_PID)
    {
        /* Child A code */
        int n_0_100;
        char wrong_num_msg[] = "[!] Not a valid number in 0 100\nPlease provide one:";

        // close unused pipe ends
        close(pipe_2_fd[PIPE_WRITE_END]);
        close(pipe_2_fd[PIPE_READ_END]);
        close(pipe_1_fd[PIPE_READ_END]);

        // wait for a number
        printf("Enter a number between 0-100: ");
        do
        {
            n_0_100 = read_integer(wrong_num_msg);
            if (n_0_100 < 0 || n_0_100 > 100)
            {
                puts(wrong_num_msg);
            }
        } while (n_0_100 < 0 || n_0_100 > 100);

        // write number to pipe and exit
        char buffer[sizeof(int)];
        sprintf(buffer, "%d", n_0_100);
        write(pipe_1_fd[PIPE_WRITE_END], buffer, sizeof(int));
        exit(EXIT_SUCCESS);
    }
    else
    {
        child_b = fork();
        check_fork_succesfull_executed(child_b);
        if (child_b == CHILD_PID)
        {
            /* Child B code */

            //close unused pipes
            close(pipe_1_fd[PIPE_WRITE_END]);
            close(pipe_1_fd[PIPE_READ_END]);
            close(pipe_2_fd[PIPE_WRITE_END]);

            // read child 1 integer received from parent
            char buffer[sizeof(int)];

            ssize_t read_bytes = read(pipe_2_fd[PIPE_READ_END], buffer, sizeof(int));

            // print received buffer
            printf("I'm child two %ld and i received this number: %s\n", (long)getpid(), buffer);
            exit(EXIT_SUCCESS);
        }
        else
        {
            /* Parent Code */

            // close unused pipes
            close(pipe_1_fd[PIPE_WRITE_END]);
            close(pipe_2_fd[PIPE_READ_END]);

            // read child 1 integer from pipe
            char buffer[sizeof(int)];
            ssize_t read_bytes = read(pipe_1_fd[PIPE_READ_END], buffer, sizeof(int));
            // write it to child 2 pipe
            write(pipe_2_fd[PIPE_WRITE_END], buffer, sizeof(int));
            exit(EXIT_SUCCESS);
        }
    }
}

static void check_fork_succesfull_executed(pid_t pid)
{
    if (pid == FAILED_FORK)
    {
        puts("[!] Fork call failed");
        exit(EXIT_FAILURE);
    }
}

static void check_pipe_succesfull_created(int pipe_code)
{
    if (pipe_code == FAILED_PIPE_CREATION)
    {
        puts("[!] Pipe creation failed");
        exit(EXIT_FAILURE);
    }
}

#define READ_INT_BUF_SIZE 1024 // use 1KiB just to be sure
static int read_integer(char *wrong_int_msg)
{
    int a;
    char buf[READ_INT_BUF_SIZE];

    do
    {
        if (!fgets(buf, READ_INT_BUF_SIZE, stdin))
        {
            // reading input failed, give up:
            puts("[!] Error: cannot read input");
            return EXIT_FAILURE;
        }

        // have some input, convert it to integer:
        a = atoi(buf);
        if (a == 0)
        {
            puts(wrong_int_msg);
        }
    } while (a == 0); // repeat until we got a valid number
    return a;
}
Messaggio unito automaticamente:

Grazie per aver condiviso la soluzione!
A prima vista ben fatto direi. ;)

La prossima volta pubblicale utilizzando il tag CODE e sotto tag SPOILER, grazie. Per questa volta edito io appena riesco (tramite pc) o uno dei colleghi, se arriva prima di me.

Fatto sotto ! Aspettiamo il prossimo!