Reputation: 1955
I am trying to get in a fork()
scenario the child process to create a fibonnaci sequence and store it in a struc
as shared_data
. I need to use the exact struct from the assignment but I've never used it like this so I am assuming that the errors I have in this code are related to that...
26: error: expected identifier (before . token)
39: error: expected identifier (before . token)
44: error: expected expression before shared_data
45: error: expected expression before shared_data
I figured I am using this wrong but what would be the proper way? if the struct was like this it would work:
struct cards{
int a;
int b;
int c;
} cards={0,0,0};
then I can use cards.a=3;
and so forth and so on... this is the first time I work with this kind of shared memory structure and while researching and looking around stack overflow as well, people ask questions that are similar but they just confuse me more. what would be the proper way of doing what I did below?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* for fork */
#include <sys/types.h> /* for pid_t */
#include <sys/wait.h> /* for wait */
#define MAX_SEQUENCE 10
typedef struct{
long fib_sequence[MAX_SEQUENCE];
int sequence_size;
} shared_data;
int fibonacci(int n)
{
int first = 0;
int second = 1;
int total, i;
for (i=0;i<=n;i++)
{
printf("%d\n", first);
shared_data.fib_sequence[i] = first;
total = first + second;
first = second;
second = total;
}
return 0;
}
int main(int argc, char *argv[])
{
int j;
/*Spawn a child to run the program.*/
pid_t pid=fork();
if (pid==0) { /* child process */
shared_data.sequence_size = argc;
fibonacci(argc);
}
else { /* pid!=0; parent process */
waitpid(pid,0,0); /* wait for child to exit */
for (j=0; j<=shared_data.sequence_size; j++)
printf("%d\n",shared_data.fib_sequence[j]);
}
return 0;
}
Upvotes: 0
Views: 3014
Reputation: 15788
You are using shared_data as a variable, however you declared it as a datatype.
typedef struct{
long fib_sequence[MAX_SEQUENCE];
int sequence_size;
} shared_data;
You should change it to:
struct{
long fib_sequence[MAX_SEQUENCE];
int sequence_size;
} shared_data;
Alternatively you could create a global variable using the datatpye:
typedef struct{
long fib_sequence[MAX_SEQUENCE];
int sequence_size;
} shared_data;
shared_data sequence;
int fibonacci(int n)
{
int first = 0;
int second = 1;
int total, i;
for (i=0;i<=n;i++)
{
printf("%d\n", first);
sequence.fib_sequence[i] = first;
total = first + second;
first = second;
second = total;
}
return 0;
}
int main(int argc, char *argv[])
{
int j;
/*Spawn a child to run the program.*/
pid_t pid=fork();
if (pid==0) { /* child process */
sequence.sequence_size = argc;
fibonacci(argc);
}
else { /* pid!=0; parent process */
waitpid(pid,0,0); /* wait for child to exit */
for (j=0; j<=sequence.sequence_size; j++)
printf("%d\n",sequence.fib_sequence[j]);
}
return 0;
}
As a side note, the above does not address your incorrect use of argc. argc is only a count of how many arguments were passed to your program. you are probably wanting to translate one of the arguments into an int:
shared_data.sequence_size = (atol(argc) > 10) ? 10 : atol(argc);
fibonacci(shared_data.sequence_size);
Hope this helps.
Upvotes: 1
Reputation: 58637
For sharing memory strictly between a parent and child process, don't bother with the System V shared memory segments (shmget, shmat, ...).
Use mmap to make an MAP_ANONYMOUS map, which is marked MAP_SHARED. When you fork, that map is not copied between parent and child, but shared.
The advantage of this is: 1) API simplicity: just one function to call, get a pointer, and you're done. 2) automatic clean up: when both processes go away, the memory goes away. No need for a sysadmin to go in there and do an "ipcrm" to collect garbage shared memory segments.
Upvotes: 2
Reputation: 6679
fork() creates a child process with an exact copy of the memory of the parent. So the child has new memory, not shared memory. Each process has it's own memory.
You can observe this with a simple program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
char buffer[128] = "I am the parent.";
int main(int argc, char *argv[]) {
pid_t pid = fork();
if (0 == pid) {
strcpy(buffer, "I am the child.");
} else { /* parent process */
waitpid(pid,0,0);
printf("%s", buffer);
}
return 0;
}
The result is "I am the parent.". Had the memory been shared, you would expect the buffer to have been overwritten by the child process. That did happen, but in a separate process space which simply went away when the child died.
Upvotes: 1
Reputation: 1131
It looks like you have a typo in this line of code:
for (j=0; j<=shared+data.sequence_size; j++)
shared+data
should be shared_data
Upvotes: 2