Question

12/11/2017 19:39 Dwarfche#1
I'd like to ask if anyone can help me with the following task:

Make a C-program that creates two parallel processes, communicating through shared memory with size 1 symbol.

The first process should read (scanf) string(should use char array I guess) and then pass each symbol, one by one, to the second process, which writes the symbol in a file.

-It should work on Linux and must use "shared.h" + getmem(...) function to access the shared memory

I have no idea how to start it. Any help is appreciated. Thanks
12/11/2017 20:30 algernong#2
What's "shared.h"? My Linux system has no such header file.
12/11/2017 20:51 Dwarfche#3
It is a header file, for getmem function I guess.
12/11/2017 23:28 warfley#4
I've never heard about anything like a shared.h header or getmem function in posix C. But usually what you are trying to accomplish is done like this:
1. Create shared memory space and allocate a pointer to it
2. Initialize your required datastructure (a char queue in your case) in this memory
3. Fork
3.1. On child process read input and write data into the Q
3.2. On parent process read data from Q
12/12/2017 21:08 maxi39#5
like this? i didnt test it:D

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

int main() {  
char* sharedMem = (char*) malloc(sizeof(char));
*sharedMem = '\0'; 
int fork_pid;
if( (fork_pid = fork() ) == 0 ){
     while(1){
if(*sharedMem != '\0') {
   
printf("%c",*sharedMem);
*sharedMem = '\0';
}}

}else {
    while(1){
scanf("%c",sharedMem);
}}
}
dont use while(1) :D
12/12/2017 21:26 algernong#6
No, that won't work. When you call fork(), you create a new process with its own heap, thus sharedMem is a different memory location for parent and child. If the child writes something to sharedMem, the parent process won't be able to access it (or vice versa).

You need a special syscall for IPC through shared memory; on Linux, that's mmap or shmget. One of the first Google results for how to use them gives an example: [Only registered and activated users can see links. Click Here To Register...]
12/13/2017 00:33 warfley#7
Do you need to do that for any kind of homework or so? Maybe you got a library for that tasks, with shared.h being the wraper. I would take a look on your resources and check if there is this shared.h and maybe some documentation. If no doc is available just take a look what functions are avalable. I'd guess you got some special functions to use, as you mentiond getmem.