bounded buffer implementation


 
Thread Tools Search this Thread
Top Forums Programming bounded buffer implementation
# 1  
Old 11-06-2011
bounded buffer implementation

Hi Experts,

I have a programming assignment that asks us to implement a pipegrep function. it basically has 5 stages and each stage has a thread and buffers are used between stages.

am currently implementing stage 1 . In stage 1 am suppose to read directory and store the filenames in buffer1 shared with stage2 which applies filter like filesize etc,eg: it puts the files having filesize > 4096 into buffer2.I have implemented but am facing problems , am getting desired out put sometime but most of the times it doesnt work as required.

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];

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;

directory = opendir(".");

in=0;

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^ \n",buffer1[in]);
   in=(in+1)%10;
   sem_post(&full);//indicate consumer filename is available
   pthread_mutex_unlock(&mutex);
   }
   
   closedir(directory);

pthread_exit(NULL);;
}


void *stage2(){
	//printf("Inside thread2 also \n");
int out =0 ;
int in = 0;
struct stat sb;
int fsize = 4096;
int s=0;	
	while(s != -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");
	        s=stat(buffer1[out],&sb);
	        	
    if(sb.st_size > fsize){
				
    buffer2[in] = buffer1[out];
    printf(" file = %s and size = %d \n",buffer2[in],(int)sb.st_size);
    out = (out+1)%10;
    in = (in+1)%10;
   
    sem_post(&empty);
    pthread_mutex_unlock(&mutex);     
}
else {
	out = (out+1)%10;
	pthread_mutex_unlock(&mutex);
}
}


pthread_exit(NULL);
}

O/p:
Code:
ameya@ameya-Dell-System-Inspiron-N4110:~/os_concepts/Advanced_shell$ ./listfiles
 file = advanced_shell.c~ and size = 11141 
 file = advanced_shell and size = 16693 
 file = listfiles and size = 7919 
 file = advanced_shell.c and size = 10840 

program stuck here, i have closed the directory as well

Please check and provide u r feedback.

Regards,
Ameya
# 2  
Old 11-06-2011
Issue might be with the mutex and semaphores I guess. Why don't you try conditional var in mutex? You can signal when a directory is read into the buffer which can then be processed by the stage2.

--ahamed
# 3  
Old 11-06-2011
If you answer the following question for me, I am pretty sure you will find at least the semaphore issue yourself:

What happens to the "full" semaphore when you find a file, and the file size is greater than 4096?

Last edited by jjinno; 11-06-2011 at 05:10 AM..
# 4  
Old 11-07-2011
Also, you need to check what the pointers you're putting into your buffers actually point to....
# 5  
Old 11-08-2011
Quote:
Originally Posted by jjinno
If you answer the following question for me, I am pretty sure you will find at least the semaphore issue yourself:

What happens to the "full" semaphore when you find a file, and the file size is greater than 4096?

The full semaphore increments (sem_post) when a file is found after which the stage 2 tries to lock the mutex and stats the file for checking the size.
# 6  
Old 11-08-2011
Hi amejoish,
the main issue in your code is
Code:
while(s != -1)

Here you are waiting for the next file accd to the previous stat output. Its not a correct way as after the last file stage1 thread will stop and the stage2 thread will be in wait state forever. Change the condition, then your code will work fine.
# 7  
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question