Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- TEST semaphore and mutex
- Consider binary semaphore
- */
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<pthread.h>
- #include<semaphore.h>
- #include<signal.h>
- #define BUFFER_SIZE 6
- typedef int buffer_item;
- buffer_item buffer[BUFFER_SIZE];
- // global variable, all threads can acess
- int index_counter = 0;
- int mmsnot=0;
- void *thread_Insert(void *arg); // function for sending
- void *thread_Remove(void *arg); // function for receiving
- sem_t bin_sem; // semaphore
- pthread_mutex_t mutx; // mutex
- char thread1[]="Thread A";
- char thread2[]="Thread B";
- char thread3[]="Thread C";
- int main(int argc, char **argv)
- {
- pthread_t t1, t2, t3;
- void *thread_result;
- int state1, state2;
- int mem_arry[1024];
- int *ptr;
- int sze;
- state1 = pthread_mutex_init(&mutx, NULL);
- state2 = sem_init(&bin_sem, 0 ,0);
- //mutex initialization
- //semaphore initialization, first value = 0
- if(state1||state2!=0)
- puts("Error mutex & semaphore initialization!!!");
- *ptr=malloc(1024*sizeof(int));
- for(int i=0;i<1024;i++){
- mem_arry[i]=0;
- }
- // Create thread1, thread2, thread3
- // pthread_create(&t1, NULL, thread_Insert, &thread1);
- // pthread_create(&t2, NULL, thread_Remove, &thread2);
- // pthread_create(&t3, NULL, thread_Remove, &thread3);
- pthread_create(&t3, NULL, thread_MMS, NULL);
- for(i = 0; i < 4; i++)// loop to produce 4 buyer threads
- {
- pthread_t tid;
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_create(&tid, &attr,thread_user, i);
- }
- // Waiting thread to terminate
- pthread_join(t1, &thread_result);
- pthread_join(t2, &thread_result);
- pthread_join(t3, &thread_result);
- sem_destroy(&bin_sem); // destroy semaphore
- pthread_mutex_destroy(&mutx); // destroy mutex
- return 0;
- }
- // Thread increases item
- void *thread_user(int *threadnumber)
- {
- sleep(1);
- int i;
- printf("Creating Thread: %d\n", &threadnumber);
- pthread_mutex_lock(&mutx);
- mmsnot=1
- sze=64;
- sem_post(&bin_sem); // semaphore to increase
- pthread_mutex_unlock(&mutx);
- }
- // Thread decreases item
- void *thread_MMS(void *arg, int alg)
- {
- int i;
- printf("Creating Thread: %s\n", (char*)arg);
- while(1){
- sem_wait(&bin_sem); //decrease index_counter
- pthread_mutex_lock(&mutx);
- if(mmsnot==1) {
- if(alg==1){
- firstfirt(sze);
- }else if(alg==2)
- }else{
- sleep(1);
- }
- pthread_mutex_unlock(&mutx);
- }
- int firstfirt(int size){
- int opentrk=0;
- int openstart=0;
- int lpctrl=1;
- for(int i=0;i<1024;i++){
- if (mem_arry[i]==1){
- }else if (mem_arry[i]==0){
- opentrk++;
- openstart=i;
- return opentrk;
- }
- }
- while(lpctrl){
- if(size<=opentrk){
- for(int i=openstart;i<(openstart+opentrk);i++){
- mem_arry[i]=1;
- ptr=malloc(size*sizeof(int));
- return 0;
- }
- else if(size>=opentrk){
- for(int i=(openstart+opentrk);i<1024;i++){
- if (mem_arry[i]==0){
- opentrk++;
- openstart=i;
- return opentrk;
- }
- }
- }
- }
- }
- }
- int BestFit(){
- }
- int Wrostfit(){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement