Pranav Bhoraskar
Pranav Bhoraskar

Reputation: 135

IPC - Shared Programming Program in Linux

I am trying to create a program which can communicate between two processes naming, processor.c and receiver.c, acting as client and server, with the help of shared memory.

The first program receiver.c runs in an infinite loop receiving alpha numeric strings as input from the user one line at a time. After reading one line from the standard input, this program sends this information to the other program. The sharing of data between the two processes should take place via shared memory. The second program processor.c creates an output file vowels.out and waits for user input to be sent by the receiver program. As soon as one line is received from the receiver, it counts the number of vowels in that line and dumps the vowel count along with the original line in the vowels.out file. This program also runs in an infinite loop.

Here are the programs -

processor.c

#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <stdio.h>
#define SHM_SIZE 10240  /* make it a 10K shared memory segment */
void main()
{
    int v=0;
    char ch;
    int id_shm;
    key_t random_key;   /* Keys are used for requesting resources*/
    char *shmem, *seg;

    /* Segment named "6400", should be created by the server.  */
    random_key = 6400;

    /* Locate the segment.  Used - shmget() */
    if ((id_shm = shmget(random_key, SHM_SIZE, 0660)) < 0) {
        perror("shmget");
        exit(1);
    }
    /* Attaching segment to the data spaces.  Used - shmat() */
    if ((shmem = shmat(id_shm, NULL, 0)) == (char *) -1) {
        perror("shmat");
        exit(1);
    }

    /* Creation of File */
    FILE *op_file;
    op_file = fopen("vowels.out", "a");

    /* Now read what the server put in the memory.  */
    seg = shmem;

    while (shmem != NULL)  /* 'while' - use of entry controlled loop */
    {   
    while ((ch=fgetc(op_file))!=EOF)
    {
        if (ch=='A'||ch=='a'||ch=='E'||ch=='e'||ch=='I'||ch=='i'||ch=='O'||ch=='o'||ch=='U'||ch=='u')
        {
            v++;
        }
    }

        fprintf(op_file, "%s, %d \n", shmem, v);

          *shmem = "*";
    while(*shmem == "*")
    {
        sleep(6);
    } 
    } 
    fclose(op_file);
}

receiver.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define SHM_SIZE 10240  /* make it a 10K shared memory segment */
void main()
{
    key_t random_key;  /* Keys are used for requesting resources*/
    int id_shm;
    char *data, *shmem;


    random_key = 6400;

    /* Creation of the segment. Used - shmget()*/
    if ((id_shm = shmget(random_key, SHM_SIZE, 0660 | IPC_CREAT)) == -1) 
    {
        perror("shmget");
        exit(1);
    }

    /* Addition of segment to the data spaces.  Used - shmat() */
    if ((shmem = shmat(id_shm, NULL, 0)) == (char *) -1) 
    {
        perror("shmat");
        exit(1);
    }

    while (1)    /* 'while' - use of entry controlled loop to check on access */
    {
        scanf("%s", data);
        data = shmem;   /* Mapping of data */

    while(*shmem != "*")
    { 
        sleep(6);
    }
    }
 }

But I am receiving error specifying Segmentation Fault

Can anyone please help me with this? Is there something I am doing wrong in this program?

Thank you in advance.

Upvotes: 1

Views: 1178

Answers (1)

TonyB
TonyB

Reputation: 947

Other than several compiler warnings (*shmem = "*"; and while(*shmem == "*") ) and a couple of programming errors... You have a design issue.

Shared memory access/updates should be controlled with a semaphore to serialize access to the data and protect against partial updates.

As to your segmentation fault... It appears to be in receiver.c at scanf("%s", data);, because you are receiving input from stdin into data, which is simply a pointer to char... ... i.e. no space for the input has been allocated.

Hopefully, that should get you going...

Upvotes: 5

Related Questions