Advertisement

03.29.2003 at 02:29PM PST, ID: 20567654
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.0

HARD question: Getting Segmentation Fault

Asked by sinua in Linux Programming

Tags: ,

Please help.
Getting Segmentation Fault when I run server.c in the background and client.c in the forground.

Oh, and reading from database where first line is name, second line is address and third line is phonenumber. And so on.

/* client.c */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>

#define FIFO_NAME "Port80"
#define PRIVATE "private"

main(){                 // Will need to receive signal from server.

      int fd[2], i, port;
      char *pipename, name[80], query[80], *result;

      // create named pipe
      if (access(PRIVATE, F_OK) == -1) {
             port = mkfifo(PRIVATE, 0777);
              if (port != 0) {
                        fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME);
                        exit(0);
              }
          }
      pipename = PRIVATE;
      printf("pipename contains: %s\n", pipename);

      // Read from user
            // 1. person to search.
            // 2. queiry type.
      printf("Input name: ");
      scanf("%s", &name);
      printf("Search options: 'a' for address and 'p' for phone number.\n");

      for(i =0; i<10; i++){
            printf("Input search type: ");
            scanf("%s", &query);
            if(strcmp(query,"a")==0 || strcmp(query,"p")==0){
                  printf("Thank you. Search underway.\n");
                  break;
            }
            printf("Search options are 'a' for address. 'p' for phone number.\n");
      }

      printf("Name is %s with search type -%s. \n", name, query);

      // Write to well known pipe(FIFO)
            // 1. pipe number
            // 2. person to search.
            // 3. queiry type.
      if (access(FIFO_NAME, F_OK) == -1) {
              port = mkfifo(FIFO_NAME, 0777);
              if (port != 0) {
                        fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME);
                        exit(0);
              }
          }
      port = open(FIFO_NAME, O_WRONLY);
      write(port, pipename, 80);
      write(port, name, 80);
      write(port, query, 2);
      close(port);

      // Read from private pipe
      port = open(pipename, O_RDONLY);
      i = read(port, result, 80);
      close(port);
      printf("result contains: %s\n", result);
      printf("i contains: %i\n", i);

      if(strcmp(result,"Person not found")==0 ){
            printf("Person was not found!\n");
      }
      else if(strcmp(query, "a")==0){
            printf("%s's address is: %s", name, result);
      }
      else if(strcmp(query, "p")==0){
            printf("%s's phone number is: %s", name, result);
      }
      printf("ending\n");
      exit(0);
}

/* Server.c */

// Mahad Ali
// 011088242
// Section 2

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>

#define FIFO_NAME "Port80"
#define FALSE 0
#define TRUE 1

void agent(char*, char*, char*); //do I need arguments or is void fine?
void searcher(char*, char*, char*);        // Will also need to send a signal to client

main(){
      char pipename[3], person_name[80], query[2], answer_to_query[80];
      int port, i;

      if (access(FIFO_NAME, F_OK) == -1) {
              port = mkfifo(FIFO_NAME, 0777);
              if (port != 0) {
                        fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME);
                        exit(0);
              }
          }
      while(1){


            // Read from Well known Pipe(FIFO)
            port= open(FIFO_NAME, O_RDONLY);

            system("clear");
            //Will recieve 3 things
            read(port, pipename, 80);      //1. Name of named pipe(pipe)
            read(port, person_name, 80);      //2. Name of person
            read(port, query, 2);            //3. Type of query
            close(port);
                        //i. 'a' for address
                        //ii. 'p' for phone number
            printf("pipename contains: %s\n", pipename);
            printf("person_name contains: %s\n", person_name);
            printf("query contains: %s\n", query);
                  // When type of queiry is received
                        // call function agent.
                        // let it fork and take care of everything
                        // loop to see if there are anymore requests in port
            agent(pipename, person_name, query);
      }
}

void agent(char *pipename, char *person, char *query){

      int status;

      status = fork();      // Fork.
      if(status == -1){
            fprintf(stderr, "Error forking.\n");
            exit(1);
      }
      if(status ==0){            // In child


                  // search database for person. call searcher.
            searcher(pipename, person, query);
                  // searcher does the following:
                  // When found
                        //1. return to client address if query is 'a'
                        //2. return to client phone num if query is 'p'

                  // Not found
                        // return to client "Did not find person"
            exit(0);            //End child
      }
}
 // receives info and where to send result
void searcher(char *pipename, char *person, char *query)
{
      FILE *ifp;
      char name[80];
      char address[80];
      char phone[80];
      int recordFound = FALSE;
      int i, port;

      // open file
        ifp = fopen("database.txt", "r");

      // error opening file
        if (ifp == NULL)
        {
                fprintf(stderr, "Error opening file");
                _exit(1);
        }

      port = open(pipename, O_WRONLY);

      // search for user inputted name
      while (fgets (name, 80, ifp) != NULL)
      {
              fgets(address, 80, ifp);   // Gets address
              fgets(phone, 80, ifp);             // Gets phone

                if (strncmp(person, name,strlen(name)-1) == 0)
                {
                  recordFound = TRUE;
                  printf("found name in database\n");
                  // if name found then write appropriate result
                  if(strcmp(query,"a")==0){
                          i = write(port, address, 80);
                        printf("i contains: %i\n", i);
                        if(i==-1){
                              printf("Error writing address to client\n");
                        }
                  }
                  else if(strcmp(query,"p")==0){
                        i = write(port, phone, 80);
                        printf("i contains: %i\n", i);
                        if(i==-1){
                              printf("Error writing number to client\n");
                        }
                  }
                }
        }

      // Write "not found" to client
      if (recordFound == FALSE)
      {
            i=write(port, "Person not found", 80);
            if(i==-1){
                  printf("Error writing to client\n");
            }
            printf("Did NOT find name in database\n"); // remove later
      }
      close(port);

      recordFound = FALSE;

      // close file
      fclose(ifp);

}
Start Free Trial
 
 
[+][-]03.29.2003 at 06:39PM PST, ID: 8232710

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.31.2003 at 02:47AM PST, ID: 8237652

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Linux Programming
Tags: fault, segmentation
Sign Up Now!
Solution Provided By: mjmanikandan
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20081112-EE-VQP-42