shmat problem


 
Thread Tools Search this Thread
Top Forums Programming shmat problem
# 1  
Old 06-01-2007
shmat problem

The first shmat in my program cannot fetch the start address of the shared memory.

Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int main(int argc,char *argv[]) {
	int pid;
	key_t mykey;
	int shmid;
	long *shm;
	long *shm2;

	mykey=ftok("file.txt",1);
	if (mykey==-1) {
		perror("ftok error.\n");
		return 5;
	}
	shmid=shmget(mykey,4,IPC_CREAT);
	if (shmid==-1) {
		perror("shmget error.\n");
		return 5;
	}

	shm=(long *)shmat(shmid,NULL,SHM_RND);    //error occured here
	if (shm==(void *)-1) {
		perror("first shmat fail.\n");
		return 5;
	}
	(*shm)=30;

	shm2=(long *)shmat(shmid,NULL,SHM_RND);
	printf("%d\n",(*shm2));
	return 0;
}

Is there something wrong with my code?

Thanks.
# 2  
Old 06-01-2007
You did not give any access rights in shmget.

See Shared Memory Segments

Code:
    key_t key;
    int shmid;

    key = ftok("/home/beej/somefile3", 'R');
    shmid = shmget(key, 1024, 0644 | IPC_CREAT);

# 3  
Old 06-01-2007
duplicate post
# 4  
Old 06-01-2007
Quote:
Originally Posted by porter
You did not give any access rights in shmget.

See Shared Memory Segments

Code:
    key_t key;
    int shmid;

    key = ftok("/home/beej/somefile3", 'R');
    shmid = shmget(key, 1024, 0644 | IPC_CREAT);

It works.

Thanks a lot.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies

2. IP Networking

Problem with forwarding emails (SPF problem)

Hi, This is rather a question from a "user" than from a sys admin, but I think this forum is apropriate for the question. I have an adress with automatic email forwarding and for some senders (two hietherto), emails are bouncing. This has really created a lot of problems those two time so I... (0 Replies)
Discussion started by: carwe
0 Replies

3. SuSE

shmat failed due to Cannot allocate memory

Hi, My program was running for a whole night. after 12 hours i got an error message "Cannot allocate memory" during the shmat commmand. So can you please let me know what could be the reason? is there any solution? thanks in advance. Regards, Mano (5 Replies)
Discussion started by: ManoharanMani
5 Replies

4. Programming

shmat() with fixed address

Hi, I'm using Ubuntu 10.04 on a 64bit machine. In my shmat() call, I want to assign fixed memory address to shmaddr variable. I have no idea which address value to give. Some where in the net I read we can make use of sysproc info to know the user space addresses, but could not figure out how... (1 Reply)
Discussion started by: sajjar
1 Replies

5. Programming

Problem shmat in HP-UX Titanium ia64. EINVAL Error

I have a process that needs two active connections to the same zone of shared memory simultaneously. The firs conection works ok, but when i do the second call to shmat it give me error 22 (EINVAL). Only works ok the second call to shmat if i disconnect the first connection (shmdt) Steps:... (3 Replies)
Discussion started by: dairby
3 Replies

6. UNIX for Advanced & Expert Users

Problem shmat in HP-UX. EINVAL Error

I have a process that need two active connections to the same zone of shared memory. But when i do the second call to shmat it give me error 22 (EINVAL). Only works ok the second call to shmat if i disconnect the first connection (shmdt) In Sun,AIX and Digital, i donīt have this problem. ... (7 Replies)
Discussion started by: dairby
7 Replies

7. AIX

user login problem & Files listing problem.

1) when user login to the server the session got colosed. How will resolve? 2) While firing the command ls -l we are not able to see the any files in the director. but over all view the file system using the command df -g it is showing 91% used. what will be the problem? Thanks in advance. (1 Reply)
Discussion started by: pernasivam
1 Replies

8. AIX

sharemem-f-shmat

Hi, I got this error from AIX 4.3.3/db2 7: * %SHAREMEM-F-SHMAT Error getting PUTPARM shared memory address , shmid=23029 * * 3630, errno=13 Anybody who know how to fix this? Thanks in advance, itik (1 Reply)
Discussion started by: itik
1 Replies

9. Linux

shmat() Failure While Using a Large Amount of Shared Memory

Hi, I'm developing a data processing pipeline with multiple stages, with data being moved between the stages using shared memory segments. The size of the data is typically of the order of hundreds of megabytes, and there are typically a few tens of main shared memory segments each of size... (2 Replies)
Discussion started by: theicarusagenda
2 Replies

10. HP-UX

shmat() permission denied, what's this?

I have installed an application that runs correctly for root but not other users. The application generates an error that indicates users don't have permission to attach to shared memory. A daemon process creates the shared memory segment. I've checked every permission I can think of but nothing... (2 Replies)
Discussion started by: km4hr
2 Replies
Login or Register to Ask a Question

Featured Tech Videos