include <semaphore.h>#include <pthread.h>#include <stdlib.h>#include <stdio.h>#include <unistd.h>// I'm using g++ to compile this program in Ubuntu 9.04. I use the command ./a.out to run it. /*The program simulates many producers and consumers.Producers are many and put products in a joint stack. Consumers are also many and take from this same stack. Only the product number goes into the stack, nothing else. Each producer makes a product number by random from 0..PRODUCTS-1 and puts into the stack. It also raises the appropriate element in array "production" by 1.It also raises the production_total with 1. Each consumer looks at the number at the top of each stack.It raises the counter by 1 over what has been "consumed" by that production number by raising the element in the array "consumption". It also raises consumption_total by 1. After all the threads have finished all numbers must match.The array production must look like the array consumption and production_total must look like consumption_total. The stack must start and end in 0. So that can happen I need to synchronize producers and consumers with semaphors and mutex so they will not interrupt each other. How can I make the threads end? Where should I put the semaphors and mutex?*/ #define PRODUCERS 20#define CONSUMERS 20#define PRODUCTS 25#define STACKMAX 100 // Producers produce products with a product number between 0..PRODUCTS // array production og consumption store numbers for production and consumption// for each product.int production[PRODUCTS];int production_total;int consumption[PRODUCTS];int consumption_total; int stacksize; // stack can be 0..STACKSIZE-1, I need to use semaphore to watch out for highs and lows...int stack[STACKMAX]; // This is how I learnt how to create a semaphoresem_t semaphore; //This is how a semaphore is usually used://sem_wait(&semaphore);//sem_post(&semaphore); //This is how I learnt how to create a mutex - could need morepthread_mutex_t mutex;//This is how they're suppoes to be used//pthread_mutex_lock(&mutex);//pthread_mutex_unlock(&mutex); #define True 1#define False 0 // If quitting = True then we're stopping and all threads should exitint quitting; void* producer(void *arg){ int product_number; printf("I'm a producer\n"); return NULL; // This needs to be removed so the code will run // I need to change this code! Here I need at least one semaphore and probably one mutex. // The if.then.else used here is not the most suitable solution. while(!quitting) { if(stacksize<STACKMAX) { product_number=rand()%PRODUCTS; // Some number on between 0..PRODUCTS // Put on stack stack[stacksize++]=product_number; // and record it.. production[product_number]++; production_total++; } else { printf("The stack is full!\n"); } } return NULL;} void* consumer(void *arg){ int product_number; printf("I'm a consumer!\n"); return NULL; // This return should delete // I need to change this code! Here I need at least one semaphore and probably one mutex. // The if.then.else used here is not the most suitable solution. while(!quitting) { if(stacksize>0) { product_number=stack[--stacksize]; consumption[product_number]++; consumption_total++; } else { printf("The stack is empty!\n"); } } return NULL;} int main(int argc, char **argv){ pthread_t consumer_threads[CONSUMERS]; pthread_t producer_threads[PRODUCERS]; // Initialize the semaphore with a value of 1. // Note the second argument: passing zero denotes // that the semaphore is shared between threads (and // not processes). if(sem_init(&semaphore, 0, 1)) { printf("Could not initialize a semaphore\n"); return -1; } // Initialize the mutex if(pthread_mutex_init(&mutex, NULL)) { printf("Unable to initialize a mutex\n"); return -1; } // Null random generator srand(0); // Null all counters stacksize=0; consumption_total=0; production_total=0; for(int i=0;i<PRODUCTS;i++) { consumption[i]=0; production[i]=0; } // Consumers can look at this global variable to see if producers have stopped producing // and it's safe to quit quitting=False; // Print initial values printf("stacksize (should be 0) = %i\n",stacksize); printf("production_total = %i\n",production_total); printf("consumption_total = %i (needs to match production)\n",consumption_total); printf("Production array broken down:\n"); for(int i=0;i<PRODUCTS;i++) { printf("%i ",production[i]); } printf("\n"); printf("Consumption array broken down (needs to match production array):\n"); for(int i=0;i<PRODUCTS;i++) { printf("%i ",consumption[i]); } printf("\n"); // Start the producer for(int i = 0; i < PRODUCERS; ++i) { if(pthread_create(&producer_threads[i], NULL, &producer, NULL)) { printf("Could not create thread %d\n", i); return -1; } } // Start the consumer int thread_count=0; for(int i = 0; i < CONSUMERS; ++i) { if(pthread_create(&consumer_threads[i], NULL, &consumer, NULL)) { printf("Could not create thread %d\n", i); return -1; } } // The main program sleeps for a while // then puts the global variable qutting = True , by that all consumers and producers //should stop producing. sleep(3); quitting=True; printf("Now the main program wants to quit...\n"); for(int i = 0; i < CONSUMERS; ++i) { if(pthread_join(consumer_threads[i], NULL)) { printf("Could not join thread %d\n", i); return -1; } } for(int i = 0; i < PRODUCERS; ++i) { if(pthread_join(producer_threads[i], NULL)) { printf("Could not join thread %d\n", i); return -1; } } printf("All threads are dead\n"); sem_destroy(&semaphore); pthread_mutex_destroy(&mutex); printf("All over.. now everything should match\n"); printf("stacksize (should be 0) = %i\n",stacksize); printf("production_total = %i\n",production_total); printf("consumption_total = %i (needs to match production)\n",consumption_total); printf("Production array broken down:\n"); for(int i=0;i<PRODUCTS;i++) { printf("%i ",production[i]); } printf("\n"); printf("Consumption array broken down (needs to match production array):\n"); for(int i=0;i<PRODUCTS;i++) { printf("%i ",consumption[i]); } printf("\n"); return 0;}#include <<strong class="highlight">semaphore</strong>.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
// I'm using g++ to compile this program in Ubuntu 9.04. I use the command ./a.out to run it.
/*
The program simulates many producers and consumers.
Producers are many and put products in a joint stack. Consumers are also many and take from this same stack.
Only the product number goes into the stack, nothing else.
Each producer makes a product number by random from 0..PRODUCTS-1 and puts into the stack. It also raises
the appropriate element in array "production" by 1.
It also raises the production_total with 1.
Each consumer looks at the number at the top of each stack.
It raises the counter by 1 over what has been
"consumed" by that production number by raising the element in the array "consumption". It also raises
consumption_total by 1.
After all the threads have finished all numbers must match.
The array production must look like the array
consumption and production_total must look like consumption_total.
The stack must start and end in 0.
So that can happen I need to synchronize p
没有合适的资源?快使用搜索试试~ 我知道了~
os.zip_C Builder_zip
共21个文件
c:14个
txt:4个
exe:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 170 浏览量
2022-09-22
20:37:01
上传
评论
收藏 23KB ZIP 举报
温馨提示
forking the proceeses
资源推荐
资源详情
资源评论
收起资源包目录
os.zip (21个子文件)
os lab 4
l3q3.c 188B
l3q1.c 706B
l3q2.c 344B
l3q5.c 414B
lab31.c 727B
xyz
COPY.BAK 845B
lab52a.exe 15KB
thread
lab4_q2.c 588B
lab4_q3.c 2KB
mutex1.c 1KB
semaphore.txt 5KB
producer_cons.txt 14KB
OPTIMAL.C 856B
COPY.C 909B
deadlockcreate3.c 2KB
lab52a_fcfs.c 694B
labtestb7.c 1KB
bankers.txt 4KB
sem.c 4KB
lab52b.exe 16KB
input.txt 7B
共 21 条
- 1
资源评论
邓凌佳
- 粉丝: 65
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功