Sponsored Content
Top Forums Programming bounded buffer implementation Post 302572894 by amejoish on Friday 11th of November 2011 03:14:15 PM
Old 11-11-2011
Code:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>
#include<sys/stat.h>
#include<dirent.h>
#include<semaphore.h>
#include<pthread.h>

char *buffer1[10];
char *buffer2[10];

int in =0;
int out =0 ;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

sem_t empty,full;
void *stage1();
void *stage2();

int main(){
	pthread_t thrd1,thrd2;
	
	sem_init(&empty, 0, 10);
	sem_init(&full,0,0);
	int tid1 = pthread_create(&thrd1,NULL,&stage1,(void *)0);
	if(tid1 == -1)
	perror("cant create thread \n");
	int tid2 = pthread_create(&thrd2,NULL,&stage2,(void *)0);
    if(tid2 == -1)
    perror("cant create thread \n");
    
     pthread_join(thrd1, NULL);
     pthread_join(thrd2, NULL);
//getchar();
pthread_exit(NULL);

}

void *stage1(){
	//printf("Inside thread 1 \n");
DIR *directory;
struct dirent *dir;
int in = 0;

directory = opendir(".");



while((dir=readdir(directory))!=NULL){
	if (strcmp(dir->d_name, ".") == 0|| strcmp(dir->d_name, "..")==0)
               continue;  
   sem_wait(&empty);
   printf("buffer is not empty \n");
   pthread_mutex_lock(&mutex);
   //printf("thread 1 got access \n");
    /* if got access enter filename into buffer */ 
   buffer1[in]=dir->d_name;
   printf(" %s in buffer 1 now \n",buffer1[in]);
   in=(in+1)%10;
   pthread_mutex_unlock(&mutex);
   sem_post(&full);//indicate consumer filename is available
      }
   
   closedir(directory);

pthread_exit(NULL);
}


void *stage2(){
	//printf("Inside thread2 also \n");
int in2 = 0;

struct stat sb;
int fsize = 4096;
	
	while(1){
	//printf("File = %s ,File size = %d \n",buffer[i],(int)sb.st_size);
			sem_wait(&full);
			printf("buffer is full goin ahead\n");
			pthread_mutex_lock(&mutex);
			//printf("thread2 got access \n");
	        if(stat(buffer1[out],&sb)==-1){
				pthread_mutex_unlock(&mutex);
			    break;
		   }	
    else if((int)sb.st_size > fsize){
				
    buffer2[in2] = buffer1[out];
    buffer1[out] = NULL;
    printf(" file = %s and size = %d \n",buffer2[in2],(int)sb.st_size);
    in = (in+1)%10;
    out = (out+1)%10;
    
   pthread_mutex_unlock(&mutex);
   printf("thread 2 unlocked mutex \n");
   sem_post(&empty);
     
}else{
	out = (out+1)%10;
	pthread_mutex_unlock(&mutex);

}
}


pthread_exit(NULL);
}

i changed the condition but i think i am missing something.

Regards,
Ameya
 

10 More Discussions You Might Find Interesting

1. Programming

Frame buffer implementation in Linux

At present, Iam working on Linux Framebuffer device console. I have a doubt sir. Please solve this. *How to display a string or a character in Frame buffer in C language? *What is the library file (is it <linux/fb.h> or other one?) used to do all I/O function manipulations like printing,... (0 Replies)
Discussion started by: chandra80
0 Replies

2. Shell Programming and Scripting

Need help on AWK implementation

Hi, I am accepting a string from user. compare this output with the awk output as below... echo "\n\n\tDay : \c" read day awk '{ if($day == $2) { if ($mon == $1) { print "Yes" }}}' syslog.txt I am getting the follwoing error awk: Field $() is not correct. The input line... (5 Replies)
Discussion started by: EmbedUX
5 Replies

3. Programming

Malloc implementation in C

Hey Guys I am trying to implement the malloc function for my OS class and I am having a little trouble with it. I would be really grateful if I could get some hints on this problem. So I am using a doubly-linked list as my data structure and I have to allocate memory for it (duh...). The... (1 Reply)
Discussion started by: Gambit_b
1 Replies

4. UNIX for Advanced & Expert Users

Malloc Implementation in C

Hey Guys Some of my friends have got together and we are trying to write a basic kernel similar to Linux. I am trying to implement the malloc function in C and I am using a doubly linked list as the primary data structure. I need to allocate memory for this link list (duh...) and I don't feel... (2 Replies)
Discussion started by: rbansal2
2 Replies

5. Linux

CAPWAP implementation

Hi I'm trying to implement CAPWAP protocol for my application.i'm able to configure my server side but i'm getting error at client(WTP) side as IOCTL error.while running the command #./WTP /mnt/cf/capwap/ : wlan2 Starting WTP... # WTP Loads... (0 Replies)
Discussion started by: ran789
0 Replies

6. UNIX for Dummies Questions & Answers

Lseek implementation

Hi everybody, i've been googling for ages now and gotten kinda desperate... The question, however, might be rather trivial for the experts: What is it exactly, i.e. physically, the POSIX function (for a file) "lseek" does? Does it trigger some kind of synchronization on disk? Is it just for the... (4 Replies)
Discussion started by: Humudituu
4 Replies

7. UNIX for Advanced & Expert Users

Ipsec implementation

How can i implement Ipsec between two machines in linux_ ubuntu? any link?? suggestion?? (0 Replies)
Discussion started by: elinaz
0 Replies

8. Homework & Coursework Questions

Bounded Buffer is hanging, tried all I can. (C++)

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: My problem is that when creating my producers and consumers, if I don't create an equal number of both, the... (12 Replies)
Discussion started by: digitalbrainiac
12 Replies

9. Programming

C: CSV implementation

I have this code from a programming book: #include <stdio.h> #include <string.h> char buf; /* input line buffer */ char* field; /* fields */ char* unquote( char* ); /* csvgetline: read and parse line, return field count */ /* sample input:... (3 Replies)
Discussion started by: totoro125
3 Replies

10. Shell Programming and Scripting

XML text bounded with tag

Could you please give your inputs on the below issue: source.xml <?xml version="1.0" encoding="UTF-16"?> <P1 > <C1 type="i"><2></C1> <V1 type="string"><6.2></V1> <D1 type="string"> <D2><1.0></D2> <D2><2.0></D2> </D1> ...................... ...................... many more... (7 Replies)
Discussion started by: unme
7 Replies
PTHREAD_MUTEX(3)					   BSD Library Functions Manual 					  PTHREAD_MUTEX(3)

NAME
pthread_mutex -- mutual exclusion primitives LIBRARY
POSIX Threads Library (libpthread, -lpthread) SYNOPSIS
#include <pthread.h> int pthread_mutex_init(pthread_mutex_t * restrict mutex, const pthread_mutexattr_t * restrict attr); int pthread_mutex_destroy(pthread_mutex_t *mutex); int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex); pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; DESCRIPTION
The pthread_mutex_init() function creates a new mutex, with attributes specified with attr. If attr is NULL, the default attributes are used. The macro PTHREAD_MUTEX_INITIALIZER can be used to initialize a mutex when the default attributes are appropriate and the mutex can be stati- cally allocated. The behavior is similar to pthread_mutex_init() with attr specified as NULL, except that no error checking is done. The pthread_mutex_destroy() function frees the resources allocated for mutex. It is possible to reinitialize a destroyed mutex, but unde- fined behavior may follow if the destroyed object is otherwise referenced. The pthread_mutex_lock() function locks mutex. If the mutex is already locked, the calling thread will block until the mutex becomes avail- able. The error conditions may vary depending on the type of the mutex; see pthread_mutexattr(3) for additional details. The pthread_mutex_trylock() function locks mutex. If the mutex is already locked, pthread_mutex_trylock() will not block waiting for the mutex, but will return an error condition. The pthread_mutex_unlock() function unlocks an acquired mutex. When operating with the default mutex type, undefined behavior follows if a thread tries to unlock a mutex that has not been locked by it, or if a thread tries to release a mutex that is already unlocked. RETURN VALUES
Upon success all described functions return zero. Otherwise, an error number will be returned to indicate the error. ERRORS
pthread_mutex_init() may fail if: [EAGAIN] The system lacks the resources to initialize another mutex. [EINVAL] The value specified by attr is invalid. [ENOMEM] The process cannot allocate enough memory to initialize another mutex. pthread_mutex_destroy() may fail if: [EBUSY] Mutex is locked by another thread. [EINVAL] The value specified by mutex is invalid. pthread_mutex_lock() may fail if: [EDEADLK] A deadlock would occur if the thread blocked waiting for mutex. [EINVAL] The value specified by mutex is invalid. pthread_mutex_trylock() may fail if: [EBUSY] Mutex is already locked. [EINVAL] The value specified by mutex is invalid. pthread_mutex_unlock() may fail if: [EINVAL] The value specified by mutex is invalid. [EPERM] The current thread does not hold a lock on mutex. SEE ALSO
pthread(3), pthread_barrier(3), pthread_cond(3), pthread_mutexattr(3), pthread_rwlock(3), pthread_spin(3) STANDARDS
These functions conform to IEEE Std 1003.1-2001 (``POSIX.1''). BSD
July 8, 2010 BSD
All times are GMT -4. The time now is 11:07 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy