LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2005, 06:48 PM   #1
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Rep: Reputation: 30
c + shared memory


Ok here i have a program thats supposed to compute the fibanocci sequence with a child & parent process. Does everything it is supposed to do but when i do ./fibMem X where X > 7, it cuts off the last two numbers. So my output for ./fibMem 9 is:

Ok, Child is done!
*1123581234

What am i doing wrong? and can i have spaces between so its easier to read instead of printing everything in one line?

Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/shm.h>
#include <sys/stat.h>

#define max_sequence 10

typedef struct{
	long fib_sequence[max_sequence];
	int sequence_size;
}shared_data;

void main(int argc, char *argv[])
{
	pid_t pid;
	int status;
	int m = 0;	
	long oldnum = 1;
	long curnum = 1;
	long nextnum, i;
	int num_counter = 0;
	char* shmem;			
	int segment;
	shared_data *sdptr;
	shared_data sd;
	int temp;
	
	if(argc < 2){
		printf("Invalid Number of Arguments\n");
		exit(0);
	}
	else 
		i = atoi(argv[1]);
	
	i =i - 1;

	if(i < 2 || i > max_sequence)
	{
		printf("Invalid Number \n");
		exit(2);
	}

	sd.sequence_size = i;

	segment = shmget(IPC_PRIVATE, sizeof(sd), S_IRUSR | S_IWUSR);
	
	shmem = (char*) shmat(segment, NULL, 0);

	switch(pid = fork())
	{
		case -1:
			perror("Fork Failed \n");
			break;
		case 0:
			sprintf(shmem, "%d%c ", oldnum, ' ');
			num_counter++;
			
			while(i > 0)
			{
				shmem++;
				sprintf(shmem, "%d ", curnum);
				nextnum = curnum + oldnum;
				oldnum = curnum;
				curnum = nextnum;
				i--;
			}
			printf("\n");
			exit(1);
			
		default:
			wait(&status);
			printf("Ok, Child is done! \n");
				printf("*%s \n", shmem);
			break; 
                }
}
 
Old 03-07-2005, 07:07 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Code:
switch (pid = fork()) {
        case -1:
                perror("Fork Failed \n");
                break;
        case 0:
                sprintf(shmem, "%s %ld", shmem,oldnum);
                num_counter++;

                while (i > 0) {
                        shmem++;
                        sprintf(shmem, "%s %ld", shmem,curnum);
                        nextnum = curnum + oldnum;
                        oldnum = curnum;
                        curnum = nextnum;
                        i--;
                }
                printf("\n");
                exit(1);

        default:
                wait(&status);
                printf("Ok, Child is done! \n");
                printf("*%s \n", shmem);
                break;
}
Ok, this example should both have spaces and not have the problems with your text being overwritten.
 
Old 03-07-2005, 07:21 PM   #3
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Original Poster
Rep: Reputation: 30
Thanks jt,
That works . And i figured out why you did it the way you did by reading the man pages. Thanks again...
 
Old 03-07-2005, 07:49 PM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Np, sorry, I ment to have more of an explination... but apparently I'm doing to many things at once.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
is shared memory expandable in memory size? Thinking Programming 4 08-16-2005 09:57 AM
shared memory blackzone Programming 1 10-14-2004 11:52 AM
About Shared Memory...? aegis_shiva Programming 1 08-25-2004 05:05 AM
shared memory socket9001 Programming 4 02-06-2004 02:08 PM
setting Shared Memory ashley75 Linux - Software 0 08-22-2003 10:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration