Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 08:24

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Question

Discussion on Question within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
Dwarfche's Avatar
 
elite*gold: 0
Join Date: May 2013
Posts: 1,195
Received Thanks: 38
Question

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
Dwarfche is offline  
Old 12/11/2017, 20:30   #2
 
elite*gold: 0
Join Date: May 2015
Posts: 700
Received Thanks: 444
What's "shared.h"? My Linux system has no such header file.
algernong is offline  
Old 12/11/2017, 20:51   #3
 
Dwarfche's Avatar
 
elite*gold: 0
Join Date: May 2013
Posts: 1,195
Received Thanks: 38
It is a header file, for getmem function I guess.
Dwarfche is offline  
Old 12/11/2017, 23:28   #4
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,137
Received Thanks: 573
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
warfley is offline  
Thanks
1 User
Old 12/12/2017, 21:08   #5
 
elite*gold: 0
Join Date: Feb 2009
Posts: 542
Received Thanks: 112
like this? i didnt test it

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)
maxi39 is offline  
Old 12/12/2017, 21:26   #6
 
elite*gold: 0
Join Date: May 2015
Posts: 700
Received Thanks: 444
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:
algernong is offline  
Thanks
1 User
Old 12/13/2017, 00:33   #7
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,137
Received Thanks: 573
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.
warfley is offline  
Reply


Similar Threads Similar Threads
Bot question...stupid question by the way
12/05/2007 - Conquer Online 2 - 5 Replies
ok i have this litlle problem with CoPartener..that is i cant find it...i looked where i found it before...but when i download it...BOOM...just S3DHook.dll ....evreywhere i look..the same thing...can anybody help me...help a poor noob....by the way...got weed?text2schild.php?smilienummer=1&text=WEEEE EED!' border='0' alt='WEEEEEED!' />



All times are GMT +1. The time now is 08:29.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.