Bug 6053 - A simple program that uses shared memory doesn't work
: A simple program that uses shared memory doesn't work
Status: CLOSED NOTABUG
: ALT Linux 2.4
(All bugs in ALT Linux 2.4/glibc)
: 2.4
: all Linux
: P2 major
Assigned To:
:
:
:
:
:
  Show dependency tree
 
Reported: 2005-02-07 13:55 by
Modified: 2005-09-04 23:37 (History)


Attachments


Note

You need to log in before you can comment on or make changes to this bug.


Description From 2005-02-07 13:55:47
The simple C/C++ program doesn't work. But it must. I have no problem running 
this program on Red Hat 9.0. 
The program just calls 4 system calls sequentially: shmget, shmat, shmdt and 
shmctl. Another words, I create shared memory, attach to it, detach from it 
and, finally, destroy it. The text of the program is below

//================================================================
#include <stdio.h>
#include <sys/shm.h>

typedef int UShMem;

#define RIGHTS		0666

int main()
{
	UShMem id;

	id = shmget(1590, 8, IPC_CREAT | IPC_EXCL | RIGHTS);

	if(id == -1)
	{
		perror("shmget");
		return 1;
	}

	void *res = NULL;
	if ((int)(res = shmat(id, NULL, 0)) == -1)
	{
            perror("shmat");
    	    return 1;
	}

	if(shmdt(res) < 0)
	{
		perror("shmdt");
		return 1;
	}

	if(shmctl(id, IPC_RMID, NULL) < 0)
	{
		perror("shmctl");
		return 1;
	}

	printf("All done\n");

	return 0;
}
//================================================================

The call to shmctl produces error. If you comment call to shmdt, then shmctl 
works fine. The program works fine on Red Hat 9.0 and it _must_ work on any 
POSIX platform as far as I understand...

Best regards,
Andrey Fomichev

Steps to Reproduce:
1. Cut out the program code from the message and place it to main.cpp
2. Type "g++ main.cpp"
3. Run "./a.out". See the result.
Actual Results:  
shmctl: Invalid argument

Expected Results:  
All done
------- Comment #1 From 2005-02-07 14:49:30 -------
This happens due to kernel configuration parameter, CONFIG_HARDEN_SHM=y
You can control it (systemwide) via /proc/sys/kernel/shm_destroy_unused.