Write a C program that accepts 3 parameters. Each parameter indicates the quantity of product to be
produced. Each product will be produced in different production line. Ready products will be placed
in a buffer area located at the end of each production line. Packaging workers will pack the products
into boxes. Information of each production line as follow:
Production line A: product ready in 1-2 minutes, buffer capacity: 12 units
Production line B: product ready in 2-3 minutes, buffer capacity: 6 units
Production line C: product ready in 1-2 minutes, buffer capacity: 24 units
There are currently 2 packaging workers available. Each worker needs 2 minutes to pack 6 units of
product into a box. The production line will be temporary suspended if the buffer area is full of
product. The operation will continue when the worker has taken 1 unit of product from the buffer
area.
Simulate the operation of production line and the packaging workers by using threads and
appropriate semaphores. There are at least 5 threads, but you may use additional thread if it is
necessary. You have to decide how the workers select the product to pack. Assume that 1 second in
your program is equivalent to 1 minute.
Sample output:
...
Buffer A: 11
Buffer C: 4
Worker X packing B: 3
Buffer B: 8
Worker Y packing C: 2
Buffer A: 12
Production line A suspended.
...

#i attach the code here le .pls help on

Recommended Answers

All 4 Replies

It would have been a lot better to have posted the actual code in code tags to preserve the program's format, such as tabs and spaces. I won't bother reading code that isn't at least formatted a little bit.

Well I tried and....the number of errors and warning...oh my.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>

void *A(void *arg);
void *B(void *arg);
void *C(void *arg);

int main()
{
	int res, total=0;
	pthread_t b_thread, e_thread, s_thread;
	void *thread_result;

	res = sem_init(&A,B,C 0, 0);

	if(res != 0)
	{
		perror("Semaphore initialization failed");
		exit(EXIT_FAILURE);
	}

	res = pthread_create(&b_thread, A,NULL);

	if(res != 0)
	{
		perror("Thread creation failed");
		exit(EXIT_FAILURE);
	}

	res = pthread_create(&s_thread, NULL, B, NULL);
	
	if(res != 0)
	{
		perror("Thread creation failed");
		exit(EXIT_FAILURE);
	}

	res = pthread_create(&s_thread, NULL, C, NULL);

	if(res != 0)
	{
		perror("Thread creation failed");
		exit(EXIT_FAILURE);
	}

	and(getpid());

	while(total <12)
	{
		sleep(rand()%3 + 1);
		sem_post(&A);
		printf("A ready\n");
		total++;
	}

	printf("\nWaiting for thread to finish...\n");

	res = pthread_join(b_thread, &thread_result);

	if(res != 0)
	{
		perror("Thread join failed");
		exit(EXIT_FAILURE);
	}

	printf("Thread joined\n");

	res = pthread_join(s_thread, &thread_result);

	if(res != 0)
	{
		perror("Thread join failed");
		exit(EXIT_FAILURE);
	}

	printf("Thread joined\n");
	sem_destroy(&B);
	exit(EXIT_SUCCESS);
}

void *A(void *arg)
{
	int i;
	srand(time(NULL));

	for(i=0; i<7; i++)
	{
		sem_wait(&B);
		sleep(rand()%4+1);
		printf("A prepared.\n");
	}

	pthread_exit(NULL);
}

void *B(void *arg)
{
	int i;
	srand(time(NULL));

	for(i=0; i<5; i++)
	{
		sem_wait(&A);
		sleep(rand()%3+1);
		printf("B prepared.\n");
	}

	pthread_exit(NULL);
}

void *C(void *arg)
{
	int i;
	srand(time(NULL));

	for(i=0; i<5; i++)
	{
		sem_wait(&A);
		sleep(rand()%3+1);
		printf("C prepared.\n");
	}
	pthread_exit(NULL);
}

gcc test.c -Wall -ansi -pedantic -o test -lpthread

test.c: In function ‘main’:
test.c:22: error: expected %<)%> before numeric constant
test.c:22: warning: passing argument 1 of ‘sem_init’ from incompatible pointer type
/usr/include/semaphore.h:37: note: expected ‘union sem_t *’ but argument is of type ‘void * (*)(void *)’
test.c:22: warning: passing argument 2 of ‘sem_init’ makes integer from pointer without a cast
/usr/include/semaphore.h:37: note: expected ‘int’ but argument is of type ‘void * (*)(void *)’
test.c:22: warning: passing argument 3 of ‘sem_init’ makes integer from pointer without a cast
/usr/include/semaphore.h:37: note: expected ‘unsigned int’ but argument is of type ‘void * (*)(void *)’
test.c:30: warning: passing argument 2 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘const union pthread_attr_t * __restrict__’ but argument is of type ‘void * (*)(void *)’
test.c:30: error: too few arguments to function ‘pthread_create’
test.c:51: warning: implicit declaration of function ‘and’
test.c:55: warning: passing argument 1 of ‘sem_post’ from incompatible pointer type
/usr/include/semaphore.h:70: note: expected ‘union sem_t *’ but argument is of type ‘void * (*)(void *)’
test.c:79: warning: passing argument 1 of ‘sem_destroy’ from incompatible pointer type
/usr/include/semaphore.h:40: note: expected ‘union sem_t *’ but argument is of type ‘void * (*)(void *)’
test.c:19: warning: unused variable ‘e_thread’
test.c: In function ‘A’:
test.c:90: warning: passing argument 1 of ‘sem_wait’ from incompatible pointer type
/usr/include/semaphore.h:55: note: expected ‘union sem_t *’ but argument is of type ‘void * (*)(void *)’
test.c: In function ‘B’:
test.c:104: warning: passing argument 1 of ‘sem_wait’ from incompatible pointer type
/usr/include/semaphore.h:55: note: expected ‘union sem_t *’ but argument is of type ‘void * (*)(void *)’
test.c: In function ‘C’:
test.c:117: warning: passing argument 1 of ‘sem_wait’ from incompatible pointer type
/usr/include/semaphore.h:55: note: expected ‘union sem_t *’ but argument is of type ‘void * (*)(void *)’

Ohhh that much better :)

Line 22: is that the one with perror()? I don't see anything wrong with it.

Line 22: is that the one with perror()? I don't see anything wrong with it.

reading the context of the error, you can see that line 22 on his side doesn't match up with line 22 as they are numbered here. his line 22, the one with the error, is :

res = sem_init(&A,B,C 0, 0);

obviously, you are not passing in the expected argument types to sem_init. the first argument must be a pointer to a structure of type sem_t. the second an int. the third an unsigned int.

all of your parameters are void pointers. and you've got an extraneous fourth (and maybe fifth??) argument, not sure why the extra stuff is there.

you have other problems as well, but you need to fix this first, then recompile.

you should review sem_init(), as well as the rest of the <sempaphore.h> library. working through an example will help you tremendously.


.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.