Bug 6053 - A simple program that uses shared memory doesn't work
Summary: A simple program that uses shared memory doesn't work
Status: CLOSED NOTABUG
Alias: None
Product: ALT Linux 2.4
Classification: Archive
Component: glibc (show other bugs)
Version: 2.4
Hardware: all Linux
: P2 major
Assignee: Dmitry V. Levin
QA Contact: Andrey Cherepanov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-02-07 13:55 MSK by Andrey Fomichev
Modified: 2005-09-04 23:37 MSD (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrey Fomichev 2005-02-07 13:55:47 MSK
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 Dmitry V. Levin 2005-02-07 14:49:30 MSK
This happens due to kernel configuration parameter, CONFIG_HARDEN_SHM=y
You can control it (systemwide) via /proc/sys/kernel/shm_destroy_unused.