Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I found a C/Linux exercise in a book, and I propose a code as a solution, please feel free to correct it.

Here is the exercise :

Taking into consideration the following dependency graph, which express dependency relations between 4 tasks of a program :

enter image description here

We want to create 4 processes (S1 to S4) which will be executed in parallel in order to automate the dependency graph above. Each process Si will be based on the model 1 below :

enter image description here

Propose a solution and clearly justify it.

Here is the code :

sem_t free;
sem_t busy;
static int x1 = 1, x2 = 1, x3 = 1;

void *s1(void *z)
{
    float b = 0;
    sem_wait(&busy);
    b = (54 * 91) - 43;
    printf("  (54 * 91) - 43 = %f  #  Task 1 completed.\n\n", b);
    x1 = 0;
    sem_post(&free);
}

void *s2(void *z)
{
    float c;
    sem_wait(&busy);
    c = (30 + 71) - 63;
    printf("  (30 + 71) - 63 = %f  #  Task 2 completed.\n\n", c);
    x2 = 0;
    sem_post(&free);
}

void *s3(void *z)
{
    float d;
    sem_wait(&busy);
    d = (65 - 40) * 58;
    printf("  (65 - 40) * 58 = %f  #  Task 3 completed.\n\n", d);
    x3 = 0;
    sem_post(&free);
}

void *s4(void *z)
{
    float e;
    sem_wait(&busy);
    e = 81 - (35 * 46);
    printf("  81 - (35 * 46) = %f  #  Task 4 completed.\n\n", e);
    sem_post(&free);
}

int main(void)
{
    pthread_t tr;

    sem_init(&free, 0, 5);
    sem_init(&busy, 0, 0);

    pthread_create(&tr, NULL, s1, NULL);

    if (x1 == 0)
    {
        pthread_create(&tr, NULL, s2, NULL);
        pthread_create(&tr, NULL, s3, NULL);
    }

    if ((x1 == 0) && (x3 == 0))
    {
        pthread_create(&tr, NULL, s4, NULL);
    }

    pthread_exit(NULL);
}
share|improve this question

closed as off-topic by 200_success, rolfl, kleinfreund, syb0rg, konijn Feb 17 at 16:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After getting your code to work, you may edit this question seeking a review of your working code." – 200_success, rolfl, kleinfreund, syb0rg, konijn
If this question can be reworded to fit the rules in the help center, please edit the question.

3  
If there is a very good reason why you want to see this content deleted, please flag for moderator attention using the "Other" option. Then clearly explain why this needs to be removed. Though perhaps you only require disassociation. Deleting it like this will only end up hurting you, if you are suspended because of it. –  rene Feb 28 at 16:21

1 Answer 1

$ gcc -Wall -o cr41813 -lpthread cr41813.c 
cr41813.c:7: error: ‘free’ redeclared as different kind of symbol
cr41813.c:60: warning: return type defaults to ‘int’
cr41813.c: In function ‘main’:
cr41813.c:79: error: expected ‘)’ before ‘;’ token
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.