bounded buffer implementation


 
Thread Tools Search this Thread
Top Forums Programming bounded buffer implementation
# 8  
Old 11-11-2011
Is this a classroom assignment?
# 9  
Old 11-11-2011
yes it is an assignment !
# 10  
Old 11-11-2011
Then post in the homework forum using the homework forum rules as you agreed to do when you registered. That's the only way we can help you.
# 11  
Old 11-13-2011
Actually its a project not a homework assignment
# 12  
Old 11-14-2011
Hi amejoish,
nice try, now in stage1 fn send some data in buffer1 after closing the directory (like "No more files") and in stage2 fn break the loop whenever u get this line. Be careful that all ur locks are removed correctly before coming out of loop.
All the best...
# 13  
Old 11-14-2011
HI Sivasankar,

Additionally i added sleep funtion , am delayin both the threads for random no of time and its working exactly the way i want it.

Now am implementing next 2 stages
stage3: read lines from file and store in buffer
stage4: find a substr in the line and then store that line in buffer2

even in this case after introducing sleep function , the output is proper. But whats weird is am facing race conditn even after using mutexes.

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>
#include<fcntl.h>

char **buffer3,**buffer4;

sem_t empty,full;
int in=0;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;

void *stage3();
void *stage4();

int main(){
pthread_t thrd1,thrd2;

buffer3 = (char **)malloc(10);
buffer4 = (char **)malloc(10);

pthread_mutex_init(&mutex,NULL);
sem_init(&empty,0,10);
sem_init(&full,0,0);

int tid1 = pthread_create(&thrd1,NULL,&stage3,(void *)0);
	if(tid1 == -1)
	perror("cant create thread \n");
	int tid2 = pthread_create(&thrd2,NULL,&stage4,(void *)0);
    if(tid2 == -1)
    perror("cant create thread \n");
    
    pthread_join(thrd1, NULL);
     pthread_join(thrd2, NULL);
//getchar();

sem_destroy(&empty);
sem_destroy(&full);
exit(0);

}

void *stage3(){
	
	int r;
	char file[] = "sample.txt";
	char str[256];
	FILE *fd;
	fd = fopen(file,"r");
	
	while(1){
		
		r = random()%30000;
        usleep(r);
     
		sem_wait(&empty);
		pthread_mutex_lock(&mutex);
	
	    if(fgets(str,sizeof(str),fd)==NULL)
	    pthread_exit(NULL);
	    //printf("str = %s \n",str);
	         str[strlen(str)-1] = '\0';
	         buffer3[in] = str;
	         //printf("\n buffer3in[%d] = %s\n",in,buffer3[in]);
	         in = (in+1)%10;	           
	    pthread_mutex_unlock(&mutex);
	    sem_post(&full);		    	
	}
	printf("\n buffer3in[3] = %s\n",buffer3[3]);
	pthread_exit(NULL);
}

void *stage4(){
	//printf("In stage 4 \n");
	char *str2 = "asd";
	int out = 0;
	int in2 = 0;
	int r;
	
	while(1){
		r = random()%30000;
        usleep(r);
        //printf("buffer is not full \n");
        sem_wait(&full);
        pthread_mutex_lock(&mutex);
        //printf(" comapring $$ buffer3out[%d] %s $$  \n",out,buffer3[out]);    
        if(strstr(buffer3[out],str2)){
			buffer4[in2] = buffer3[out];
			printf(" \n buffer4 = %s \n",buffer4[in2]);
			//buffer3[out] = NULL;
			in2 = (in2+1)%10;
			

		}
		
		out = (out+1)%10;
		
		pthread_mutex_unlock(&mutex);
		sem_post(&empty);
	}
	
	pthread_exit(NULL);
	
}

Check the output:

Code:
ameya@ameya-Dell-System-Inspiron-N4110:~/os_concepts/Advanced_shell$ ./read

 buffer3in[0] = cat deer dog bando moron bahavar.
 comapring $$ buffer3out[0] cat deer dog bando moron bahavar. $$  

 buffer3in[1] = asdsdas
 comapring $$ buffer3out[1] asdsdas $$  

 buffer3in[2] = asdsdasbhafd

 buffer3in[3] = asdsgetg

 buffer3in[4] = adgrg
 comapring $$ buffer3out[2] adgrg $$  
 comapring $$ buffer3out[3] adgrg $$

if u see the stage 3 has inserted buffer[3](in=3) = asdgetg but the out pointer used by stage4
buffer[3] (i.e out =3) value is adgrg which is weird. Increasing the delay time for thread 1 somehow fixes the problem.

If i dont use the sleep then output is entirely messed up.

Code:
ameya@ameya-Dell-System-Inspiron-N4110:~/os_concepts/Advanced_shell$ ./read

 buffer3in[0] = cat deer dog bando moron bahavar.

 buffer3in[1] = asdsdas

 buffer3in[2] = asdsdasbhafd

 buffer3in[3] = asdsgetg

 buffer3in[4] = adgrg
 comapring $$ buffer3out[0] adgrg $$  
 comapring $$ buffer3out[1] adgrg $$  
 comapring $$ buffer3out[2] adgrg $$  
 comapring $$ buffer3out[3] adgrg $$  
 comapring $$ buffer3out[4] adgrg $$  

 buffer3in[5] = grgdfdsfds

Can you please explain this concept or how come not delaying threads messes up the output
# 14  
Old 11-14-2011
Check the pointer concepts once.
Code:
buffer3 = (char **)malloc(10);
buffer4 = (char **)malloc(10);

Here you are creating a pointer to pointer, which can hold the address of a char pointer, later you are copying the data into str
Code:
fgets(str,sizeof(str),fd)==NULL

and storing str address in respective location.
Code:
buffer3[in] = str;

So, buffer3[in] will hold the addr of str everytime and you are changing the data in it everytime.
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