question about share memory

This is a discussion on question about share memory within the C Programming forums, part of the General Programming Boards category; i'm trying to read a file into shared memory so children of the process will be able to read it ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    10

    question about share memory

    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.
    Code:
    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);
    	}

  2. #2
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    maybe someone?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,638
    I don't think you get how printf format specifiers work: printf(3): formatted output conversion - Linux man page.

    %s is for strings, %d is for integers. I'm guessing the first byte of your shared memory is a null or something. Write won't do you any better than printf if you're trying to go to standard out. What output do you expect? What did printf give you when using %s? what about %d?

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    it doesn't matter if i use %s or %d.
    the file i'm opening is a binary file nothing ofiical. it's an exercise i got in operating systems.
    i get nothing no output even if use > the output is empty

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    i tried to print printf(sizeof(shared1)); and it's also failed

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,638
    Quote Originally Posted by rafi View Post
    i get nothing no output even if use > the output is empty
    Do you mean the output redirect in Unix/Linux? If nothing is being printed on your screen, you are simply redirecting nothing to some file.

    Quote Originally Posted by rafi View Post
    i tried to print printf(sizeof(shared1)); and it's also failed
    This makes me wonder why you are trying something as complicated as forking and shared memory for operating systems without understanding one of the first things learned by any programming student, printf. The code you gave is the equivalent of calling "printf(4)", assuming you have a 32-bit system. You also need to read up on the sizeof operator, since it gives you the size of the variable type (char *), not how much memory is allocated/attached.

    As for printf, you need a string as the first parameter to printf. That is, you need a char * (pointing to a sequence of chars terminated by a null character). I doubt what's in your shared memory is very screen-friendly, especially if you are reading from a binary file (i.e. not text). If you have format specifiers in your string (like %d or %s), you must provide additional parameters that will "fill in" or "replace" those in the final output.

    Read the man page I sent you. Look at the tutorials here (Cprogramming.com Tutorial: Introduction to C), and look up a printf tutorial on line.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    10

    here is what i need to do

    if you have any clue i'l be very happy
    Attached Images Attached Images  

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,638
    I have many clues, and could probably whip this up in a few hours, but I refuse to do your homework for you: http://motigeva.com/bioeng2010/wp-co.../exercise2.pdf. You should really look at his/her site. There are some nice, simple examples of forking and shared memory there. The example uses strcpy to modify the contents of the shared memory, but you could easily replace that with your sorting routines.

    When done sorting, you should note that the data in shared memory is binary data. argv[3] and argv[4] tell you the format (size and signedness) of that data, not only for sorting it, but also for how to print it out. Treat that shared memory segment as an array of bytes/shorts/ints/long ints that are signed/unsigned as specified by the command line parameters, and print them using the appropriate printf format specifier according to the documentation.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    i know this website it's my course's TA.
    i put up a piece of code of my the code if you know what my problem is i'l be happy to hear.
    i also tried using read to buffer of char and then strcmp to copy from the buffer to shared memory but it didn't work

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,638
    I don't see any apparent errors in the code you provided. You seem to be checking for all error conditions properly and exiting with -1 appropriately. The only the problems I noticed were those I already mentioned regarding how you seem to be using printf, but...

    The code you provided doesn't actually cover the problem you're having with printing. I see no printf in there, thus no way to analyze what is going on. Where are you calling it? Where do you allocate the memory for storing the array you read? Where do you fork and sort? There is a lot going on that I can't see.

    i also tried using read to buffer of char and then strcmp to copy from the buffer to shared memory but it didn't work
    That's because strcmp is for comparing two strings. You were probably thinking of strcpy, which copies two strings. But strcpy wont even work for you. You have binary data, not strings. You almost certainly have a null byte somewhere in your data that would signal the end of a string, when it's not really the end of your binary data, meaning you wont copy everything. You probably need memcpy. Read the strings tutorial here so you understand what a string is and what it isn't: C Strings - C++ Tutorial - Cprogramming.com.

    Also, please read the documentation on the functions you use in the future to make sure you're using them correctly to the best of your knowledge and ability. It will help you avoid trying to copy data with a comparison function and it will save both you and I lots of time.

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    of course i ment strcpy...
    the rest of the code is longer and includes semaphores. i only attached the relevant part.
    the printf command is been called right after the last line in the code attached up
    read_test = read(fd, shared1, st.st_size);
    Code:
    	if (read_test == -1) {
    		perror("open");
    		exit(-1);
    	}
    shared1[read_test] = '\0';
    printf("%s",shared1);
    i also tried those variations
    Code:
    	for (i=0;i<4096;i++)
            printf("shared1: %s\n", *(shared1));
    there is a problem with the reading

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The PDF thing you have says you have integers. Many integers have 0 bytes in them, meaning that they will not work as strings. If your data are integers, why not print them as integers?

    (EDIT: By "0 bytes" I mean "bytes that have the value 0", not that they are 0 bytes long. Sorry.)

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,638
    Quote Originally Posted by rafi View Post
    there is a problem with the reading
    If this were the case, your if(read_test == -1) should have caught it, printed an error and caused your application to quit. It's not a reading problem, it's a problem of your comprehension of strings and printf. As tabstop and I have both noted, your binary data probably contains zero-valued bytes that signal printf to stop printing the "string" you passed it. Did you read the web pages I suggested?

    When you read successfully, your shared1 memory will effectively contain an array of ints (even though shared1 is of type char *), with the size and signed-ness as specified in argv[3] and argv[4]. You need to iterate through it as an array and print them out as integers one by one.

  14. #14
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    i used casting of pointer to do the sorting and used bubblesort.
    even when i try to print it cell by cell eg arr[i] it wont print
    i need more clues and more sleep
    good night

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer memory question
    By Edo in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2009, 03:36 AM
  2. Memory question
    By John_L in forum Tech Board
    Replies: 8
    Last Post: 06-02-2008, 11:06 PM
  3. How to LIMIT share memory in C ???
    By tritong in forum C Programming
    Replies: 5
    Last Post: 09-07-2006, 05:15 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21