Problem with releasing semaphore lock


 
Thread Tools Search this Thread
Top Forums Programming Problem with releasing semaphore lock
# 1  
Old 09-28-2006
Problem with releasing semaphore lock

Hi,

I am trying to write stuff to a shared memory using a writer, and reading the corresponding stuff using a reader. I am facing problems while releasing the lock, as a result of which I am having segmentation faults. The code is as follows...


/********** writer.c ***********/

#include <unistd.h>
#include <semaphore.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/fcntl.h>

char shm_fn[] = "my_shm";
char sem_fn[] = "my_sem";

/**** WRITER ****/

main(){
caddr_t shmptr;
unsigned int mode;
int shmdes, index;
sem_t *semdes;
int SHM_SIZE;

mode = S_IRWXU|S_IRWXG;

/* Open the shared memory object */

if ( (shmdes = shm_open(shm_fn,O_CREAT|O_RDWR|O_TRUNC, mode)) == -1 ) {
perror("shm_open failure");
exit(-1);
}

/* Preallocate a shared memory area */

SHM_SIZE = sysconf(_SC_PAGE_SIZE);

if(ftruncate(shmdes, SHM_SIZE) == -1){
perror("ftruncate failure");
exit(-1);
}

if((shmptr = mmap(0, SHM_SIZE, PROT_WRITE|PROT_READ, MAP_SHARED,
shmdes,0)) == (caddr_t) -1){
perror("mmap failure");
exit(-1);
}

/* Create a semaphore in locked state */

semdes = sem_open(sem_fn, O_CREAT, 0644, 0);

if(semdes == (void*)-1){
perror("sem_open failure");
exit(-1);
}

/* Access to the shared memory area */

for(index = 0; index < 100; index++){
printf("write %d into the shared memory shmptr[%d]\n", index*2, index);
shmptr[index]=index*2;
}

/* Release the semaphore lock */
printf("\nRelease lock\n");

sem_post(semdes);
printf("\nBefore shmptr\n");
munmap(shmptr, SHM_SIZE);

/* Close the shared memory object */

close(shmdes);
printf("\nShmdes closed\n");
/* Close the Semaphore */

sem_close(semdes);
printf("\nSemdes closed\n");

/* Delete the shared memory object */

shm_unlink(shm_fn);
}
# 2  
Old 09-29-2006
Code tags for code please. Like {code} stuff {/code} except with [ ] instead of { } . It will make it a lot more readable.

I might suggest using fprintf(stderr,"stuff") instead of printf("stuff). stdout is buffered -- you might not see all printed messages depending on where it crashes.

If sem_post is crashing, either you're giving it an invalid fd or you corrupted memory somewhere earlier on.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Linux lock problem

Hi Team, I have a requirement to access a shared resource from the user and interrupt context. What type of locking mechanism I have to use for this. Can any body give advice on this. Thanks in advance. -Shiva (4 Replies)
Discussion started by: shivakoteswarra
4 Replies

2. Red Hat

Problem with screen lock

I'm having a weird problem with a RHEL6 workstation. When the screen lock is activated manually the system will lock and the screens will go blank. Once I try to unlock the system, the monitor will just flicker and won't respond. I tried the combination CTRL, ALT, Backspace and it did not work,... (4 Replies)
Discussion started by: goose25
4 Replies

3. UNIX for Dummies Questions & Answers

Caps lock problem

hi all this s quite a foolish problem. I seem to hav pressed some keys s.t in unix, my letters are comin in caps and with caps lock on, i am getting lowercase letters. :o Pls help. Also is there any reference or manual where i can check in case such problems arrise? thanx in advance curiosity (4 Replies)
Discussion started by: curiosity
4 Replies

4. Programming

Single Semaphore problem???

Hi friends, I have this very famous Operating System book titiled, "Operating System Concepts" by Abaraham Silbertschatz 7th edition. Regarding the semaphores, here is a paragraph from the book which says, We can also use semaphores to solve various synchronization problems. For example,... (1 Reply)
Discussion started by: gabam
1 Replies

5. Programming

semaphore problem???

Hi friends, I want to print the the following sequence of numbers using sempahores. 2 3 2 3 2 3 2 3 But something seems to be wrong regarding my concept of semaphores, I am using a single function, a single semaphore, and two threads, but I am not succeeding to achieve my goal, it prints as... (1 Reply)
Discussion started by: gabam
1 Replies

6. UNIX for Advanced & Expert Users

Testing privileges -lock lockfile /var/lock/subsys/..- Permission denied

Hi all, I have to test some user priviliges. The goal is to be sure that an unauthorized user can't restart some modules (ssh, mysql etc...). I'm trying to automate it with a shell script but in same cases I got the syslog broadcast message. Is there any way to simply get a return code... (3 Replies)
Discussion started by: Dedalus
3 Replies

7. Programming

Posix Semaphore Use - Releasing all resources?

I am attempting to write a program with multiple POSIX threads. I want to ensure all threads are released at the same time. I am (trying to) use a semaphore to accomplish this. Without too much irrelevant information, I declare my semaphore with 0 of 25 resources available. Is there a way... (7 Replies)
Discussion started by: snowbarr
7 Replies

8. Solaris

/etc/lvm/lock problem

Hi, I amnot able to execute any svm command. it gives the below message and hangs: # metadetach d50 d30 metadetach: waiting on /etc/lvm/lock ^Cmetadetach: Interrupt # metaclear d50 metaclear: waiting on /etc/lvm/lock ^Cmetaclear: Interrupt Please advice. Regards, Sagar. (4 Replies)
Discussion started by: sag71155
4 Replies

9. UNIX for Advanced & Expert Users

Semaphore problem....

I'm having an issue implementing my semaphores.... The following is how I'm setting up the semaphore. First: I get the semkey (which I've wrapped in an IF statement using perror() but in an attempt to keep the code clutter free I've removed it here) semkey = ftok("./request", 'S' ) ... (1 Reply)
Discussion started by: Dreams in Blue
1 Replies

10. Red Hat

screen lock problem

hi friends i have a small problem,in my redhat enterprise linux system screen lock is not working if i click screen lock no action takes place... so is there any solution to fix this problem or any alternate method available please let me know.... waiting for replys....... thanks... (2 Replies)
Discussion started by: madhusudankh
2 Replies
Login or Register to Ask a Question