Hi everyone. I am new to Cboard. This is my first post.
I am practicing Linux Programming. I am doing an Addressbook program in C using IPC. This program does Insert, Display, Delete and Modify.
My task is to create two applications.
All the selections for insert, delete, modify & delete will be done in application1.
For Insertion:
application1 has to take the input data(like name, address & phone#) and send them to application2. Application2 must receive the data and save them to a file.
For Deletion: Application2 must be doing the deletion part
For Display: Application2 sends data stored in file to Application1 for displaying them
For Modify: Application2 does the modification part.
I am using System() to create a new process and using Shared memory for IPC.
I am successful in sending the data to second process and saving them to a file. I can also display the contents in the file. Can anyone please help in how to delete and modify the data.
This is my code what i have done upto now. Thanks in Advance.
address.c
Code:
#include<stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <errno.h>
typedef struct
{
int select;
char lastname[25];
char firstname[25];
char address[25];
char phonenumber[25];
}addressbook;
#define SIZE 3
addressbook a[SIZE]={0,0};
void insert(void);
void display(void);
void delete(void);
void modify(void);
char *shared_memory;
int i;
int main()
{
int select;
int segment_id;
//char* shared_memory;
int segment_size;
key_t shm_key;
const int shared_segment_size = 0x6500;
shm_key = ftok("/home/madan/programs/shm_tok",'X');
if(shm_key < 0)
{
printf("failed to create the key %s\n",strerror(errno));
}
/* Allocate a shared memory segment. */
segment_id = shmget (shm_key, shared_segment_size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
if(segment_id < 0)
{
printf("error geting the segment id %s\n",strerror(errno));
}
printf("segment ID:%d\n", segment_id);
/* Attach the shared memory segment. */
shared_memory = (char*) shmat (segment_id, 0, 0);
printf ("shared memory attached at address %p\n", shared_memory);
do
{
printf("Address book: \n Press\n 1: Insert \n 2: Display \n 3: Delete \n 4: Modify\n Enter:");
scanf("%d", &select);
switch(select)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
delete();
break;
case 4:
modify();
break;
default:
printf("wrong selection\n");
break;
}
if((select > 4) || (select < 1))
break;
}while(1);
/* Detach the shared memory segment. */
shmdt (shared_memory);
/* Deallocate the shared memory segment.*/
shmctl (segment_id, IPC_RMID, 0);
return 0;
}
void insert()
{
for(i=0; i<SIZE; i++)
{
a[i].select=1;
printf("enter details\n");
printf("enter lastname of person %d:\n", i);
scanf("%s", a[i].lastname);
printf("enter firstname of person %d:\n", i);
scanf("%s", a[i].firstname);
printf("enter address of person %d:\n", i);
scanf("%s", a[i].address);
printf("enter phone number of person %d:\n", i);
scanf("%s", a[i].phonenumber);
memcpy(shared_memory, &a[i], sizeof(addressbook));
system("./address-insert");
}
}
void display()
{
//for(i=0; i<SIZE; i++)
// {
a[i].select=2;
// printf("enter last name of the person to display the details:\n");
// scanf("%s", a[i].lastname);
memcpy(shared_memory, &a, sizeof(addressbook));
system("./address-insert");
memcpy(&a, shared_memory, sizeof(addressbook));
for(i=0; i<SIZE; i++)
{
printf("lastname of person %d: %s\n", i,a[i].lastname);
printf("firstname of person %d: %s\n", i, a[i].firstname);
printf("address of person %d: %s\n", i, a[i].address);
printf("phone number of person %d: %s\n", i, a[i].phonenumber);
}
}
void delete()
{
a[i].select=3;
memcpy(shared_memory, &a, sizeof(addressbook));
system("./address-insert");
}
void modify()
{
a[i].select=4;
memcpy(shared_memory, &a, sizeof(addressbook));
system("./address-insert");
}
-------------------------------------------------------------------------------------------------------------------------------------
address-insert.c
Code:
#include <arpa/inet.h>
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
typedef struct
{
int select;
char lastname[25];
char firstname[25];
char address[25];
char phonenumber[25];
} addressbook;
#define SIZE 3
addressbook a[SIZE]={0,0};
void insert(void);
void display(void);
void delete(void);
void modify(void);
char* shared_memory;
FILE *fp;
int i;
char *name;
int main ()
{
int segment_id;
int segment_size;
key_t shm_key;
shm_key = ftok("/home/madan/programs/shm_tok",'X');
const int shared_segment_size = 0x6500;
/* Allocate a shared memory segment. */
segment_id = shmget (shm_key, shared_segment_size, S_IRUSR | S_IWUSR);
if(segment_id < 0)
{
printf("error:[%s]",strerror(errno));
}
printf("segment id %d\n",segment_id);
/* Attach the shared memory segment. */
shared_memory = (char*) shmat (segment_id, 0, 0);
if(shared_memory == NULL)
{
printf("failed to attach the shared memory %s",strerror(errno));
}
printf ("shared memory2 attached at address %p\n", shared_memory);
memcpy(&a[i], shared_memory, sizeof(addressbook));
printf("select=%d\n", a[i].select);
switch(a[i].select)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
delete();
break;
case 4:
modify();
break;
default:
printf("wrong selection\n");
break;
}
/* Detach the shared memory segment. */
shmdt (shared_memory);
return 0;
}
void insert()
{
/* To insert data*/
fp = fopen("addressbook.dat","a+");
fwrite(&a[i], sizeof(addressbook), 1, fp);
fclose(fp);
}
void display()
{
/* To delete data */
fp = fopen("addressbook.dat", "r");
for(i=0; i<SIZE; i++)
{
fread(&a, sizeof(addressbook), 1, fp);
//memcpy(shared_memory, &a, sizeof(addressbook));
}
fclose(fp);
}
void delete()
{
/*To display data */
printf("enter last name of the person to delete:\n");
scanf("%s", name);
printf("contact to be deleted is: %s", name);
fp = fopen("addressbook.dat", "a+");
for(i=0; i<SIZE; i++)
{
fread(&a[i], sizeof(addressbook), 1, fp);
if(a[i].lastname == name)
{
printf("found\n");
}
else
printf("not found\n");
}
fclose(fp);
//memcpy(shared_memory, &a, sizeof(addressbook));
}
void modify()
{
/*To modify data */
printf("enter last name of the person to modify the details:\n");
scanf("%s", a[i].lastname);
memcpy(shared_memory, &a, sizeof(addressbook));
}
The program is running with no errors. insert() and display() are working. Can someone help me the best way to send select variable to second application and how to do delete and modify part.
Thanks in advance.