0

Hello everyone!

I hope that someone could help with my C assignment. The main task is to demonstrate forking, piping, message queues, signals, and shared memory with an example that a rabbit female is waiting for a rabbit boy to tell her a poem, she gives a gift in return (plus other things that doesn't matter now). I started programming in C two months ago, and since it's different than C++ and Java it's giving me a lot of trouble, even to manage a string (yeah, char array).

Since I don't really know how to wait for each other to finish the current task, I tried to manage it with a status, which is in the shared memory. The status is U if the variable is "undefined", then A if the girl is waiting for a boy, B if the boy told the poem, C if the girl got the poem and then back to A. Everything goes well until the girl would have to read the B (I guess?).

I know that my code is a little bit messy, and sorry for the if statement in the female fork, I did it for debugging, it would be if - else if - else if - else, but doesn't matter in this case. :/

Here is my code:

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <time.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define MAX 1000
#define SHM_SIZE 2
#define KNRM  "\x1B[0m"
#define KRED  "\x1B[31m"
#define KYEL  "\x1B[33m"
#define KBLU  "\x1B[34m"

char *poems[MAX];
int number_of_poems;

void color_terminal(int color)
{
    switch(color)
    {
    case 1:
        printf("%s", KRED);
        break;
    case 2:
        printf("%s", KBLU);
        break;
    case 3:
        printf("%s", KYEL);
        break;
    case 4:
        printf("%s", KNRM);
    }
}

void read_poems_from_file() //not used yet
{
    number_of_poems = 0;
    FILE *fp;
    char str[MAX];
    int i = 0;
    if((fp = fopen("poems.txt", "r"))==NULL)
    {
        printf("The TXT couldn't be opened.\n");
        exit(1);
    }
    while(!feof(fp))
    {
        while(fgets(str, sizeof str, fp))
        {
            poems[i]= strdup(str);
            number_of_poems++;
            i++;
        }
    }
    /*for(i=0; i<number_of_poems; i++)
    {
        printf("%s", *(poems+i));
    }*/
    fclose(fp);
}

int generate_random(int max) //not used yet
{
    srand(time(NULL));
    int n;
    n = rand()%max;
    return(n+1);
}

int main(int argc, char* argv[])
{
    /* INIT */
    key_t key;
    int shmid;
    char* data;
    if ((key = ftok("main.c", 'R')) == -1)
    {
        perror("ftok");
        exit(1);
    }
    if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1)
    {
        perror("shmget");
        exit(1);
    }
    data = shmat(shmid, (void *)0, 0);
    if (data == (char*)(-1))
    {
        perror("shmat");
        exit(1);
    }
    strncpy(data, "u", SHM_SIZE);
    //read_poems_from_file();
    int number_of_gifts = generate_random(5);
    int communication_pipe[2];
    if (pipe(communication_pipe))
    {
        fprintf(stderr, "Pipe failed.\n");
        return EXIT_FAILURE;
    }
    /* FORK */
    pid_t rabbit_female;
    rabbit_female = fork();
    if(rabbit_female < 0)
    {
        perror("Could not fork.\n");
        exit(1);
    }
    else if(rabbit_female > 0)
    {        
        while(1)
        {
            color_terminal(1);
            if(strncmp(data, "u", 2) == 0)
            {
                printf("Waiting for rabbit boy...\n");
                strncpy(data, "a", SHM_SIZE);
                if (shmdt(data) == -1)
                {
                    perror("shmdt");
                    exit(1);
                }
                sleep(3);
            }
            else
            {
                if(strncmp(data, "b", 2) == 0)
                {
                    printf("The boy told me a poem!\n");
                    strncpy(data, "c", SHM_SIZE);
                    if (shmdt(data) == -1)
                    {
                        perror("shmdt");
                        exit(1);
                    }
                }
                else if(strncmp(data, "s", 2) == 0)
                {
                    printf("He forgot the poem.\n");
                }
                else if(strncmp(data, "b", 2) == 0)
                {
                    printf("He accepted my gift, waiting for an other boy...\n"); //not implemented yet
                    strncpy(data, "a", SHM_SIZE);
                    if (shmdt(data) == -1)
                    {
                        perror("shmdt");
                        exit(1);
                    }
                }
                else
                {
                    printf("Something went wrong. Segment: %s\n",data);
                }
            }
            color_terminal(4);
            sleep(3);
        }
    }
    else
    {
        pid_t rabbit_male;
        rabbit_male = fork();
        if(rabbit_male < 0)
        {
            perror("Could not fork.\n");
            exit(1);
        }
        else if(rabbit_male > 0)
        {            
            while(1)
            {
                sleep(2);
                color_terminal(2);                
                if(strncmp(data, "a", 2) == 0)
                {
                    printf("Boy knocking...\n");
                    printf("Telling a poem to her...\n");
                    strncpy(data, "b", SHM_SIZE);
                    if (shmdt(data) == -1)
                    {
                        perror("shmdt");
                        exit(1);
                    }
                }
                else if(strncmp(data, "c", 2) == 0)
                {
                    printf("She answered to my poem.\n");
                }
                else
                {
                    printf("Something went wrong.\n");
                }
                color_terminal(4);
                //printf("segment contains: \"%s\"\n", data);
                sleep(3);
            }
        }
        else
        {
            while(1)
            {
                //sleep(1);
                color_terminal(3);
                printf("Parent process waiting...\n"); //TO-DO implemented later
                color_terminal(4);
                //printf("segment contains: \"%s\"\n", data);
                sleep(6);
            }
        }
    }
    return 0;
}

Please help me if you can. :) Thank you in advance!

1
Contributor
1
Reply
13
Views
3 Years
Discussion Span
Last Post by blackrainbowhu
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.