c程序不会读取用户输入

时间:2015-10-21 01:16:26

标签: c user-input scanf

我正在编写一个客户端/服务器,在我完成从他们关闭的客户端获取信息之后。现在我试图询问用户(服务器端)是否要继续,但我的程序直接跳到程序的末尾。

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>


main (void)
{
  int to_server;    // to read from client
  int from_server;  // to write to client
  int finish;   // lets me know that client is done
  int i,j,k,m,l,p;  // because C needs this defined as int
  int numClient;// number of clients
  char temp[14];
  int page_size = 128;
  int pages_left;
  int max_frames=10;
  int used_frames = 0;
  int frameUpdate=0;
  int freeframe[10] = {3,0,9,8,7,5,1,4,2,6};
  int numpage=0;
  int frames;
  int client1[5];
  int client2[5];
  int client3[5];
  int check;
  int option;
  int byte;
  int search;
  int getPage;
  int getOffset;
  int physical_Addr;
  int offset;
  int req[3];
  int dummy = 0;
  int again;


  struct values{
      char privFifo[14];
      int memoryreq;
  }cinput;


  /* Create the fifos and open them  */
  if ((mkfifo("FIFO1",0666)<0 && errno != EEXIST))
    {
    perror("cant create FIFO1");
    exit(-1);
    }
  if((to_server=open("FIFO1", O_RDONLY))<0){
       printf("cant open fifo to write");
  }

  //get number of clients
  printf("\nHow many clients?\n");
  scanf("%d", &numClient);

  for(j =1; j <= numClient; j++){
      read(to_server, &cinput, sizeof(cinput));
      printf("\n\nFifo_%d \nMemory request %d", cinput.privFifo, cinput.memoryreq);
      req[j-1] = cinput.memoryreq;
      if(cinput.memoryreq > 1280){
          printf("\nERROR! Insufficient memory for request!");
          exit(-1);
      }
        if((mkfifo(cinput.privFifo,0666)<0 && errno != EEXIST))
           {
            perror("cant create privFifo");
            exit(-1);
          }

        if((from_server=open(cinput.privFifo, O_WRONLY)<0)){
              printf("cant open fifo to write");
      }

      // find number of pages need for request
      numpage = cinput.memoryreq/page_size;

      if((numpage * page_size) < cinput.memoryreq){
          numpage++;
      }

      sleep(1);
      printf("\nPages needed %d", numpage);
      write(from_server, &numpage, sizeof(numpage));

      printf("\n******Main Memory******");
      for(m = 0; m < numpage; m++){
          if(j==1){
          printf("\n* client: %d\tframe: %d", j, freeframe[m]);
          frames = freeframe[m];
          client1[m] = frames;
         // write(from_server, &frames, sizeof(frames));
          used_frames++;
          }
          else if (j == 2){
              printf("\n* client: %d\tframe: %d", j, freeframe[dummy]);
              frames = freeframe[dummy];
              write(from_server, &frames, sizeof(frames));
              used_frames++;
              dummy++;
          }
          else if (j == 3){
              printf("\n* client: %d\tframe: %d", j, freeframe[dummy]);
              frames = freeframe[dummy];
              write(from_server, &frames, sizeof(frames));
              used_frames++;
              dummy++;
          }
      }

      dummy = used_frames;
      used_frames = max_frames - used_frames;

      if(j==1){
          pages_left = max_frames - numpage;
      }
      else {
          pages_left = dummy;
      }

      printf("\n Frames available:");
      for(l = pages_left; l!= 0; l--){
            check = max_frames - l;
            printf(" %d", freeframe[check]);
        }
        pages_left = check;
      close(from_server);
      unlink(cinput.privFifo); 
  }

  printf("\nDONE!!!"); 


  close(to_server);
  unlink("FIFO1");
}

这是我的问题区域(抱歉我的代码很乱):

  printf("press 1 to find Physical address of a byte?");
  scanf("%d\n", &again);
  while(again == 1){
   printf("\nWhich client are we accessing? ");
      scanf("%d", &option);
      printf("\nWhat byte would you like to find? ");
      scanf("%d", &byte);
      if(option == 1){
          for(p = 0; p < sizeof(client1); p++){
              search = p*page_size;
              if(search <= byte)
                 getPage= client1[p];
          }
          getOffset=byte%page_size;
          physical_Addr = (getPage * page_size) + offset;
          printf("Physical Address is %d", physical_Addr);
      }
      else if(option == 2){
          for(p = 0; p < sizeof(client1); p++){
              search = p*page_size;
              if(search <= byte)
                 getPage= client1[p];
          }
          getOffset=byte%page_size;
          physical_Addr = (getPage * page_size) + offset;
          printf("Physical Address is %d", physical_Addr);

      }
      else if(option == 3){
          for(p = 0; p < sizeof(client1); p++){
              search = p*page_size;
              if(search <= byte)
                 getPage= client1[p];
          }
          getOffset=byte%page_size;
          physical_Addr = (getPage * page_size) + offset;
          printf("Physical Address is %d", physical_Addr);

      }

      else{
          printf("ERROR!");
      }

      printf("Press 1 to choose another client.");
      scanf("%d", &again);
  } 

0 个答案:

没有答案