Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

operating system LINUX help

Status
Not open for further replies.

funjoke

Member level 3
Joined
Feb 19, 2009
Messages
58
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
1,772
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.
...


#here is the code i attach with ...pls help on

#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);
}
//create the thread that prepares A
res = pthread_create(&b_thread, A,NULL);
if(res != 0){
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
//create the thread that prepares B
res = pthread_create(&s_thread, NULL, B, NULL);
if(res != 0){
perror("Thread creation failed");
exit(EXIT_FAILURE);
}


//create the thread that prepares C
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);
}
 

microKernel

Full Member level 3
Joined
Jul 15, 2005
Messages
171
Helped
34
Reputation
68
Reaction score
12
Trophy points
1,298
Location
NE
Activity points
2,307
A few points to rectify your implementation.


1) Pass the production value as input to the threads while creating them. (Arg 4 in pthread_create)

2) Create worker threads as well that will package.

3) You are waiting for A to finish before B works. This is useless. Where is multithreaded then?
All the three production threads should be running in parallel as well as the two packaging threads. You can put thread_join near end of main just to make sure that threads do their work before you free resources and exit. Although this is not initially needed but must be done in a rightly design system

4) If you are not restricted from using other options for communicating between threads, then you may use FIFOs as the buffers for each production thread else
4b) a counter against each production line, whereby productionline will increment it, while the worker will decrement that (protected through semaphore) can be used for simulation. I'll recommend using fifos so that you learn more about multithreaded systems.

5) To make sure that the worker threads don't often get units from two lines, while 3rd gets halted, you can decide which one to pick based upon the ratio of number of units in a buffer and its capacity weighted by its speed. Higher ratio will mean, this line should be packed first.

6) Since packs are in units of 6, so you shouldn't pick a line if it has very less number of units in buffer.

7) You should make a couple of assumptions
a) Packs are for 6 units but those units can't be from different production lines (Else theoretically problem will be simplified much :) but in a real environment that won't be the case usually).
b) Input parameters to the programs should be multiples of 6 (for each production line)
c) Wroker should not sit empty
d) Production lines would start at the same time but anyone can finish first. (implied assumption)
 

funjoke

Member level 3
Joined
Feb 19, 2009
Messages
58
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
1,772
i have worked out the code
but it got some error
can help on /


CODE:

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

void *productA(void *arg);
void *productB(void *arg);
void *productC(void *arg);
void *workerX(void *arg);
void *workerY(void *arg);
sem_t pack,ok;
int pA=0;
int pLA=0;
int totalA=0;
int packA=0;
int countA=0;
sem_t mA;

int pB=0;
int pLB=0;
int totalB=0;
int packB=0;
int countB=0;
sem_t mB;

int pC=0;
int pLC=0;
int totalC=0;
int packC=0;
int countC=0;
sem_t mC;

char list[99];
int push=0;
int pop=0;
int finish=0;

int main(){
int res;
pthread_t a_thread, b_thread, c_thread ,x_thread, y_thread;
void *thread_result;

printf("please set the quantity of product A to be produced:");
scanf("%d",&pLA);
printf("please set the quantity of product B to be produced:");
scanf("%d",&pLB);
printf("please set the quantity of product C to be produced:");
scanf("%d",&pLC);
res = pthread_create(&a_thread, NULL, productA, NULL);
if(res != 0){
perror("Thread creation failed");
exit(EXIT_FAILURE);
}

res = pthread_create(&b_thread, NULL, productB, NULL);
if(res != 0){
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
res = pthread_create(&c_thread, NULL, productC, NULL);
if(res != 0){
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
res = pthread_create(&x_thread, NULL, workerX, NULL);
if(res != 0){
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
res = pthread_create(&y_thread, NULL, workerY, NULL);
if(res != 0){
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
while(totalA<pLA||totalB<pLB||totalC<pLC){
sem_wait(&ok);
if(list[push]!='A'&&list[push]!='B'&&list[push]!='C')
{
if(countA>=6){
countA=countA-6;
list[push++]='A';
sem_post(&pack);
}
if(list[push]!='A'&&list[push]!='B'&&list[push]!='C')
if(countB>=6){
countB=countB-6;
list[push++]='B';
sem_post(&pack);
}
if(list[push]!='A'&&list[push]!='B'&&list[push]!='C')
if(countC>=6){
countC=countC-6;
list[push++]='C';
sem_post(&pack);
}
}


}

res = pthread_join(a_thread, &thread_result);
if(res != 0){
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined\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(c_thread, &thread_result);
if(res != 0){
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined\n");

res = pthread_join(x_thread, &thread_result);
if(res != 0){
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined\n");
res = pthread_join(y_thread, &thread_result);
if(res != 0){
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined\n");
sem_destroy(&pack);
sem_destroy(&ok);
sem_destroy(&mA);
sem_destroy(&mB);
sem_destroy(&mC);

exit(EXIT_SUCCESS);

}
void *productA(void *arg){

int total=0;
srand(time(NULL));
while(totalA <pLA){
sleep(rand()%2 + 1);

pA++;
printf("Buffer A:%d\n",pA);
totalA++;
countA++;
sem_post(&ok);
if (pA>=12){
printf("Production line A suspended\n");
sem_wait(&mA);
}
}


}
void *productB(void *arg){
int total=0;
srand(time(NULL));
while(totalB <pLB){
sleep(rand()%2 + 2);

pB++;
printf("Buffer B:%d\n",pB);
totalB++;
countB++;
sem_post(&ok);
if (pB>=6){
printf("Production line B suspended\n");
sem_wait(&mB);
}
}
}
void *productC(void *arg){
int total=0;
srand(time(NULL));
while(totalC <pLC){
sleep(rand()%2 + 1);

pC++;
printf("Buffer C:%d\n",pC);
totalC++;
countC++;
sem_post(&ok);
if (pC>=24){
printf("Production line C suspended\n");
sem_wait(&mC);
}
}
}
void *workerX(void *arg){

int j;
int packk;
srand(time(NULL));
while(packA<pLA/6||packB<pLB/6||packC<pLC/6){
sleep(2);
sem_wait(&pack);
if(finish)
break;
j=pop++;

if(list[j]=='A'){
packk=++packA;

if(pA==12)
sem_post(&mA);
pA=pA-6;}
else
if(list[j]=='B'){
packk=++packB;
if(pB==6)
sem_post(&mB);
pB=pB-6;
}
else
if(list[j]=='C'){

packk=++packC;
if(pC==24)
sem_post(&mC);
pC=pC-6;}
printf("Worker X packing %c:%d\n",list[j],packk);
list[j]='0';
}
finish=1;
sem_post(&pack);
pthread_exit(NULL);
}
void *workerY(void *arg){

int j;
int packk;
srand(time(NULL));
while(packA<pLA/6||packB<pLB/6||packC<pLC/6){
sleep(2);
sem_wait(&pack);
if(finish)
break;
j=pop++;

if(list[j]=='A'){
packk=++packA;

if(pA==12)
sem_post(&mA);
pA=pA-6;}
else
if(list[j]=='B'){
packk=++packB;

if(pB==6)
sem_post(&mB);
pB=pB-6;}
else
if(list[j]=='C'){

packk=++packC;
if(pC==24)
sem_post(&mC);
pC=pC-6;}
printf("Worker Y packing %c:%d\n",list[j],packk);
list[j]='0';
}
finish=1;
sem_post(&pack);
pthread_exit(NULL);
}
 

microKernel

Full Member level 3
Joined
Jul 15, 2005
Messages
171
Helped
34
Reputation
68
Reaction score
12
Trophy points
1,298
Location
NE
Activity points
2,307
What errors did you face?

[i] I am pleased to see that you are puttin effort into it [/i]
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top