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.
-gin gcc)? As you are using Linux, you can also try Valgrind to collect more information about the error.