Fix the following C program so it compiles and runs include
Fix the following C program so it compiles and runs.
#include stdio.h
#include unistd.h
#include stdlib.h
#include string.h
#include pthread.h
#include semaphore.h
void * thread_function(void *arg);
sem_f bin_sem;
#define party_size 1024
/ definition function /
char party_area[party_size];
int main()
{
int result;
pthread_f a_thread;
void *thread_result;
result= sem_init(&bin_sem,0,0);
{
if (result != 0)
{
perror(\" the partier waits for the pledge\");
exit(exit_failure);
}
printf(\" the drink is not filled with keg\");
if(result != 0)
{
perror(\" the partier wake sup the pledge\");
exit(exit_success);
}
printf(\" the drink is filled with the new keg\");
sem_wait (&bin_sem);
}
pthread_exit(null);
}
Problems when compiling
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘bin_sem’
In function ‘main’:
error: ‘pthread_f’ undeclared (first use in this function)
error: (Each undeclared identifier is reported only once
error: for each function it appears in.)
error: expected ‘;’ before ‘a_thread’
error: ‘bin_sem’ undeclared (first use in this function)
error: ‘exit_failure’ undeclared (first use in this function)
error: ‘exit_success’ undeclared (first use in this function)
error: ‘null’ undeclared (first use in this function)
Solution
Here is the modified version for you:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
void * thread_function(void *arg);
sem_t bin_sem;
#define party_size 1024
// definition function
char party_area[party_size];
int main()
{
int result;
pthread_t a_thread;
void *thread_result;
result= sem_init(&bin_sem,0,0);
{
if (result != 0)
{
perror(\" the partier waits for the pledge\");
exit(EXIT_FAILURE);
}
printf(\" the drink is not filled with keg\");
if(result != 0)
{
perror(\" the partier wake sup the pledge\");
exit(EXIT_SUCCESS);
}
printf(\" the drink is filled with the new keg\");
sem_wait (&bin_sem);
}
pthread_exit(NULL);
}

