Sponsored Content
Top Forums Programming Problem with releasing semaphore lock Post 302091071 by jacques83 on Thursday 28th of September 2006 10:25:36 PM
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);
}
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
SHM_OPEN(2)						      BSD System Calls Manual						       SHM_OPEN(2)

NAME
shm_open, shm_unlink -- shared memory object operations LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <sys/types.h> #include <sys/mman.h> #include <fcntl.h> int shm_open(const char *path, int flags, mode_t mode); int shm_unlink(const char *path); DESCRIPTION
The shm_open() system call opens (or optionally creates) a POSIX shared memory object named path. The flags argument contains a subset of the flags used by open(2). An access mode of either O_RDONLY or O_RDWR must be included in flags. The optional flags O_CREAT, O_EXCL, and O_TRUNC may also be specified. If O_CREAT is specified, then a new shared memory object named path will be created if it does not exist. In this case, the shared memory object is created with mode mode subject to the process' umask value. If both the O_CREAT and O_EXCL flags are specified and a shared memory object named path already exists, then shm_open() will fail with EEXIST. Newly created objects start off with a size of zero. If an existing shared memory object is opened with O_RDWR and the O_TRUNC flag is spec- ified, then the shared memory object will be truncated to a size of zero. The size of the object can be adjusted via ftruncate(2) and queried via fstat(2). The new descriptor is set to close during execve(2) system calls; see close(2) and fcntl(2). As a FreeBSD extension, the constant SHM_ANON may be used for the path argument to shm_open(). In this case, an anonymous, unnamed shared memory object is created. Since the object has no name, it cannot be removed via a subsequent call to shm_unlink(). Instead, the shared memory object will be garbage collected when the last reference to the shared memory object is removed. The shared memory object may be shared with other processes by sharing the file descriptor via fork(2) or sendmsg(2). Attempting to open an anonymous shared memory object with O_RDONLY will fail with EINVAL. All other flags are ignored. The shm_unlink() system call removes a shared memory object named path. RETURN VALUES
If successful, shm_open() returns a non-negative integer, and shm_unlink() returns zero. Both functions return -1 on failure, and set errno to indicate the error. COMPATIBILITY
The path argument does not necessarily represent a pathname (although it does in most other implementations). Two processes opening the same path are guaranteed to access the same shared memory object if and only if path begins with a slash ('/') character. Only the O_RDONLY, O_RDWR, O_CREAT, O_EXCL, and O_TRUNC flags may be used in portable programs. The result of using open(2), read(2), or write(2) on a shared memory object, or on the descriptor returned by shm_open(), is undefined. It is also undefined whether the shared memory object itself, or its contents, persist across reboots. In FreeBSD, read(2) and write(2) on a shared memory object will fail with EOPNOTSUPP and neither shared memory objects nor their contents persist across reboots. ERRORS
The following errors are defined for shm_open(): [EINVAL] A flag other than O_RDONLY, O_RDWR, O_CREAT, O_EXCL, or O_TRUNC was included in flags. [EMFILE] The process has already reached its limit for open file descriptors. [ENFILE] The system file table is full. [EINVAL] O_RDONLY was specified while creating an anonymous shared memory object via SHM_ANON. [EFAULT] The path argument points outside the process' allocated address space. [ENAMETOOLONG] The entire pathname exceeded 1023 characters. [EINVAL] The path does not begin with a slash ('/') character. [ENOENT] O_CREAT is specified and the named shared memory object does not exist. [EEXIST] O_CREAT and O_EXCL are specified and the named shared memory object does exist. [EACCES] The required permissions (for reading or reading and writing) are denied. The following errors are defined for shm_unlink(): [EFAULT] The path argument points outside the process' allocated address space. [ENAMETOOLONG] The entire pathname exceeded 1023 characters. [ENOENT] The named shared memory object does not exist. [EACCES] The required permissions are denied. shm_unlink() requires write permission to the shared memory object. SEE ALSO
close(2), fstat(2), ftruncate(2), mmap(2), munmap(2) STANDARDS
The shm_open() and shm_unlink() functions are believed to conform to IEEE Std 1003.1b-1993 (``POSIX.1''). HISTORY
The shm_open() and shm_unlink() functions first appeared in FreeBSD 4.3. The functions were reimplemented as system calls using shared mem- ory objects directly rather than files in FreeBSD 8.0. AUTHORS
Garrett A. Wollman <wollman@FreeBSD.org> (C library support and this manual page) Matthew Dillon <dillon@FreeBSD.org> (MAP_NOSYNC) BSD
December 18, 2013 BSD
All times are GMT -4. The time now is 07:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy