producer consumer deadlock

This is a discussion on producer consumer deadlock within the C Programming forums, part of the General Programming Boards category; For an assignment I am writing a program for the producer consumer problem. As per the assignment specs I have ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    13

    producer consumer deadlock

    For an assignment I am writing a program for the producer consumer problem.

    As per the assignment specs I have written 3 programs with a shared memory
    The shared memory contains the buffer and the semaphores in a struct.
    The parent initialises the semaphores, shared memory and pointers, runs the programs and catches the return codes
    The reader reads from a file, decrypts the data, places it into the buffer
    The writer reads from the buffer, and prints output to the screen

    Currently I have 2 readers and 1 writer.

    Everything compiles fine but I keep getting a deadlock and I am hoping someone can point out the fault in my logic

    Here is the relevant code.
    reader
    Code:
    	while (fgets(read_buffer, BUFFER_SLOT_SIZE, fp) != '\0') 
    	{		
    		/* Decrypt data. Item produced */
    		for (i = 0; i < BUFFER_SLOT_SIZE; i++) 
    		{
    			read_buffer[i] = decrypt(read_buffer[i]);
    			if (read_buffer[i] == '\n') { break; }
    		}
    		sem_wait(&smp->empty);				        /* Decrement empty count */
    		sem_wait(&smp->mutex);			                /* Enter critical region */
    		strcpy(smp->buffer[smp->head], read_buffer); 	/* Add item to buffer */
    		smp->head = (smp->head + 1) % BUFFER_SIZE;	/* Increment head pointer */
    		sem_post(&smp->mutex);			                /* Exit critical reagion */
    		sem_post(&smp->full);				                /* Increment full count */
    		nanosleep(&tv, NULL);			                       /* Sleep for random time */
    	}
    	/* Increment completed counter. Use mutex to ensure exclusion */
    	sem_wait(&smp->mutex);
    	smp->completed++;
    	sem_post(&smp->mutex);
    writer
    Code:
    	/* Continue while data in the buffer */
    	while (smp->completed < 2 && value != 0)
    	{
    		sem_wait(&smp->full);                                    /* Decrement full count */
    		sem_wait(&smp->mutex);	                        /* Enter critical region */
    		strcpy(write_buffer, smp->buffer[smp->tail); 	/* Read item from the buffer */
    		smp->tail = (smp->tail + 1) % BUFFER_SIZE;	/* Increment tail pointer */
    		sem_post(&smp->mutex);				/* Exit critical region */
    		sem_post(&smp->empty);				/* Increment empty count */
    		printf("%s", write_buffer);				/* Perform action on item */
    		nanosleep(&tv, NULL);					/* Sleep for random time */
    		sem_getvalue(&smp->empty, &value);			/* Get value of empty */
    	}
    cheers

    Strider1974

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    Since posting the above I have read through numerous webpages and a couble of different books and I still cannot see the logical fault in my code. Everyone gives the same basic outline on how to resolve this issue which I feel I have implemented, but I cannot get this work. I think I might go and beat my head against a wall...it would be more fun.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Your reader is waiting on the empty semaphore, and the writer is waiting on the full semaphore. Unless these semaphores were initialized with a value greater than zero, you have a deadlock with your initial conditions. What are the initial states of your semaphores?
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    Sorry I have may have confused the issue with the terminolgy used in the specs

    The reader reads from the file and places the data into the buffer
    The writer reads from the buffer and writes to the screen

    Full has been initialised to o
    Empty has been initialised to BUFFER_SIZE - which is 8

    The program runs, normally until the buffer has been filled and then printed to the screen before deadlocking, although sometimes it will lock before that.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    What is the mutex semaphore initialized to?
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Code:
    if (read_buffer[i] == '\n') { break; }
    .
    Its not necessary that read_buffer will always contain a '\n', should be looking for '\0' instead, especially when there are excess characters in the input stream,fgets won't read all of them.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    bithub
    The mutex semaphore has been initialised to 1
    Code:
    sem_init(&smp->full, 0, 0);
    sem_init(&smp->empty, 0, BUFFER_SIZE);
    sem_init(&smp->mutex, 0, 1);
    zalezog
    We are allowed to assume that each line of the file willl be less than 255 characters

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    One possible issue is that if empty semaphore equals zero, the writer will break out of the loop while the readers are stuck waiting on the that semaphore. Since the readers will work faster than the writers (there are 2 readers vs 1 writer), this is a very real possibility. The first thing I would check is if the writer ever exits that loop.
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    Using some print statements it seems that the readers are in fact stuck waiting for the full semaphore to be incremented.

    Isn't a semaphore supposed to release any processes waiting on it when is incremented above 0

    How can I get around this problem?

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by strider1974 View Post
    Using some print statements it seems that the readers are in fact stuck waiting for the full semaphore to be incremented.

    Isn't a semaphore supposed to release any processes waiting on it when is incremented above 0

    How can I get around this problem?
    I don't see how that's possible. Your readers never wait on the full semaphore -- they only post to it.
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    sorry

    I meant the readers(producers) are waiting on the semaphore named full

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by strider1974 View Post
    sorry

    I meant the readers(producers) are waiting on the semaphore named full
    I'm still confused. Do you mean the writer?
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. producer / consumer question
    By pheres in forum C++ Programming
    Replies: 8
    Last Post: 08-02-2007, 05:55 PM
  2. pthread producer consumer problem
    By Amit Chikorde in forum C Programming
    Replies: 1
    Last Post: 01-17-2007, 06:39 PM
  3. Consumer program
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 09-27-2006, 05:21 AM
  4. Consumer - Producer
    By MethodMan in forum C Programming
    Replies: 0
    Last Post: 04-18-2003, 07:14 PM
  5. Producer consumer problem
    By traz in forum C Programming
    Replies: 2
    Last Post: 11-08-2002, 07:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21