C/C++ on Linux/Unix

Moderators: Lundin
Number of threads: 314
Number of posts: 633

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
problem with shared memory Posted by rafi290 on 8 Dec 2010 at 12:20 PM
i'm trying to read a file into shared memory so children of the process will be able to read it in parallel
here is the code i'v written
i posted only the beginning. when i try to print the ouput i see no output. i use this command to print
printf("%s",shared1);
i also tried with %d and with write.
int main(int argc, char *argv[]) {

	char *shared1; // the shared memory will be attached to it
	long long int size;
	int i, fd, proc = atoi(argv[2]), num_of_byte = atoi(argv[3]), shmid, type = 2, read_test, fdfork;
	if (argc < 4) {
		perror("not enough arguments");
		exit(-1);
	}
	if (strcmp(argv[4], "signed") == 0) {
		type = 10;
	}
	fd = open(argv[1], O_RDONLY, 0600);
	if (fd == -1) {
		perror("open");
		exit(-1);
	}

	struct stat st;
	stat(argv[1], &st);
	size = st.st_size / num_of_byte;
	//create IPC memory
	if ((shmid = shmget(IPC_PRIVATE, (st.st_size + 1), 0600 | IPC_CREAT)) < 0) {
		perror("shmget error");
		exit(1);
	}
	// attach the shared memory segment to the right shared memory type

	if ((shared1 = shmat(shmid, NULL, 0)) == (char*) -1) {
		perror("shmat error");
		exit(1);
	}
	read_test = read(fd, shared1, st.st_size);
	if (read_test == -1) {
		perror("open");
		exit(-1);
	}




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.