Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Prosti brojevi na Suncu: Upomoc C Programming Problem

[es] :: C/C++ programiranje :: Prosti brojevi na Suncu: Upomoc C Programming Problem

[ Pregleda: 3220 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

miros21k

Član broj: 22204
Poruke: 21
*.dsl.pipex.com.



Profil

icon Prosti brojevi na Suncu: Upomoc C Programming Problem19.11.2004. u 18:05 - pre 236 meseci
E cao drustvo, na faksu radimo nesto pa mi zapelo pa da pitam ovde strucnjake.

Ovako nekako:

Koristimo SUNs workstations za ove projekte (samo da napomenem).

Dobijemo 3 fajla, primes.x; primes_client.c; primes_server.c (oni su dole na kraju)
Kazu nam da prvo uradimo: rpcgen primes.x, to kad odradi onda dobijemo nove fajlove pored prva 3 fajla: primes.h; primes_clnt.c; primes_svc.c; primes_xdr.c (njih nisam dodao, ali ako treba recite)

Onda nam kaze da "compile" primes_client.c i primes_server.c koristeci ove dve komande:
gcc -o client primes_client.c primes_clnt.c primes_xdr.c -lnsl
i
gcc -o server primes_server.c primes_svc.c primes_xdr.c -lnsl

Onda kaze da otvorimo dva CMD-a jedan za server i jedan za klijent, server pokrenemo sa ./server
a na klijent strani uradimo ovo ./client localhost 1 99
i onda nam to IZBACI "PRIME" BROJEVE OD 1 DO 99 (ne rugam se, al ako neko ne zna prime number je onaj koji je deljiv samo sa 1 i sa samim sobom, znaci 1..3..5..7...11, itd itd itd)
Znaci klijent trazi od servera primes numbers from 1 to 99 i onda se dobije lista na klijentovoj strani.


E sad oni oce od mene da ja PROMENIM MALO primes.x "so it takes int pos; kao treci argument" (prva dva su int min; int max;), i da taj POS vraca samo jedan broj a ne kao na pocetku ceo "array".
Isto naravno oce da promenim primes_server.c i primes_client.c tako da kad ukucam naprimer:
./client localhost 1 99 4
ono da mi pokaze 4-ti prime number a to je broj 7, znaci da mi vrati jedan broj a ne celu listu.
A ako bi stavio naprimer
./client 1 20 10
znaci minimum=1.. maximum=20, a ja trazim 10-ti prime number, kojeg nema izmedju 1 i 20 da mi vrati 0 kao rezultat.

Znaci oce da izbaci se "array" kao rezultat, i da rezultat bude samo jedna cifra.

Ako mozes neko bilo kako da mi pomogne bio bi jako zahvalan
Thanx.

Nastavnik nam je nesto napomenio da moramo na SERVER strani da umesto array stavimo kao neki "counter" koji se povecava svaki puta kada FOR LOOP nadje prime number dok chekira brojeve od minimum do maximuma, i kada taj "counter" bude bio jednak sa "requested position" da samo jednostavno return that number.


PRIMES.X
Code:


/* Max size of array of primes we can return */
const MAXPRIMES = 1024;

/* Argument: a min-max range */
struct prime_request {
  int min;
  int max;
};

/* Return value: a variable length array */
struct prime_result {
  int array<MAXPRIMES>;
};

/* Define program */
program PRIME_PROG {
  version PRIME_VERS {
    prime_result FIND_PRIMES(prime_request) = 2;  /* proc num */
  } = 1;         /* vers num */
} = 0x2a3b4c5d;  /* prog num */




[Ovu poruku je menjao filmil dana 27.11.2004. u 02:25 GMT+1]
 
Odgovor na temu

miros21k

Član broj: 22204
Poruke: 21
*.dsl.pipex.com.



Profil

icon Re: Upomoc C Programming Problem19.11.2004. u 18:06 - pre 236 meseci
Evo druga dva

PRIMES_SERVER.C
Code:


#include "primes.h"

int isprime(int);
char *strtime(struct tm *);

/**
 * The server procedure is defined as
 *   ret_type <func name>_<vers num>_svc(req_type, struct svc_req *)
**/
prime_result *
find_primes_1_svc(prime_request *request, struct svc_req *rqstp)
{
  /**
   * Reserve stroage for the results. This MUST be static,
   * otherwise it will be deallocated from the stack when the
   * procedure terminates. By the time the xdr_prime_result
   * filter gets to serialise the results, it is no longer valid.
  **/
  static prime_result result;
  static int prime_array[MAXPRIMES];
  int i, count=0;
  time_t curr_time;

  /* Log requests. */
  curr_time = time(NULL);
  printf("%s find_primes_1_svc(): range = [%d,%d]\n",
    strtime(localtime(&curr_time)), request->min, request->max);

  /* Loop over the range limits in the request "packet". */
  for (i=request->min; count<MAXPRIMES && i<=request->max; i++)
    if (isprime(i))
      prime_array[count++] = i;

  /**
   * Assemble the reply packet. Note that the variable length
   * array we are returning is really a struct with an
   * element count and a pointer to the first element.
  **/
  result.array.array_len = count;
  result.array.array_val = prime_array;
  return &result;
}

/**
 * This is a 'private' support function and is not directly
 * accessible via RPC. Returns 1 if n is prime and 0 otherwise.
**/
int isprime(int n)
{
  int i;

  if (n < 2)
    return 0;

  for (i=2; i*i<=n; i++)
    if ((n % i) == 0)
      return 0;
  return 1;
}

/**
 * Private function to stringify time structure.
**/
char *strtime(struct tm *timeptr)
{
  static char tm_str[] = "MMM dd hh:mm:ss";
  char *tm_mon;

  switch (timeptr->tm_mon) {
    case 0: tm_mon = "Jan"; break;
    case 1: tm_mon = "Feb"; break;
    case 2: tm_mon = "Mar"; break;
    case 3: tm_mon = "Apr"; break;
    case 4: tm_mon = "May"; break;
    case 5: tm_mon = "Jun"; break;
    case 6: tm_mon = "Jul"; break;
    case 7: tm_mon = "Aug"; break;
    case 8: tm_mon = "Sep"; break;
    case 9: tm_mon = "Oct"; break;
    case 10: tm_mon = "Nov"; break;
    case 11: tm_mon = "Dec"; break;
    default: return NULL;
  }

  sprintf(tm_str, "%3s %2d %02d:%02d:%02d", tm_mon, timeptr->tm_mday,
      timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec);
  return tm_str;
}




PRIMES_CLIENT.C
Code:


#include "primes.h"

int main(int argc, char *argv[])
{
  CLIENT *cl;
  prime_request request;
  prime_result *result;
  int i;

  if (argc != 4) {
    fprintf(stderr, "usage: %s host min max\n", argv[0]);
    exit(1);
  }

  /* Connect to the client on the host specified in argv[1] */
  cl = clnt_create(argv[1], PRIME_PROG, PRIME_VERS, "tcp");
  if (cl == NULL) {
    clnt_pcreateerror(argv[1]);
    exit(2);
  }

  /* Form a request: taking the min and max values from the
     command line. */
  request.min = atoi(argv[2]);
  request.max = atoi(argv[3]);

  /* Call the remote procedure */
  result = find_primes_1(&request, cl);
  if (result == NULL) {
    clnt_perror(cl, argv[1]);
    exit(3);
  }

  /* Print the result */
  for (i=0; i<result->array.array_len; i++)
    printf("%d is prime\n", result->array.array_val[i]);
  printf("count of primes found = %d\n", result->array.array_len);

  /* Clean up */
  clnt_destroy(cl);
  return 0;
}

 
Odgovor na temu

Marko Stankovic

Član broj: 11
Poruke: 306
*.rvkds.net.



Profil

icon Re: Upomoc C Programming Problem24.11.2004. u 14:48 - pre 236 meseci
Ako sam dobro razumeo problem u primes.x jedino sto bi trebalo da se promeni jeste da dodas u okviru strukture prime_request jos jednu promenljivu a to je ta promenljiva "pos" i izmenis vrednost funkcije FIND_PRIMES, znaci sve to bi trebalo da izgleda ovako:

Code:

/* Max size of array of primes we can return */
const MAXPRIMES = 1024;

/* Argument: a min-max range */
struct prime_request {
  int min;
  int max;
  int pos;
};

/*OVU STRUKUTURU SAM OSTAVIO JER SE KORISTI U PRIME_SERVER.C
 BICE TI JASNO KASNIJE ZASTO JE OSTALA LAKSE JE KADA JE ONA TU MADA NAM
I NIJE NESTO MNOGO POTREBNA */
/* Return value: a variable length array */
struct prime_result {
  int array<MAXPRIMES>;
};

/* Define program */
program PRIME_PROG {
  version PRIME_VERS {
    int FIND_PRIMES(prime_request) = 2;  /* proc num */
  } = 1;         /* vers num */
} = 0x2a3b4c5d;  /* prog num */


e sada moramo malo da izmenimo client da uzme u obzir i argument POS i da malo izmenimo ispis, nesto otprilike ovako(komentare cu da pisem velikim slovima da bi bili uocljivi):

Code:

#include "primes.h"

int main(int argc, char *argv[])
{
  CLIENT *cl;
  prime_request request;
  /*OVDE SMO PROMENILI TIP result JER SADA VRACAMO SAMO JEDAN BROJ */
  int result;
  int i;

/* SADA IMAMO 5 ARGUMENTA A NE 4*/
  if (argc != 5) {
  
  /*OVDE SAM UBACIO DA JE PRAVILNA UPOTREBA SA POS NA KRAJU*/
    fprintf(stderr, "usage: %s host min max pos\n", argv[0]);
    
    exit(1);
  }

  /* Connect to the client on the host specified in argv[1] */
  cl = clnt_create(argv[1], PRIME_PROG, PRIME_VERS, "tcp");
  if (cl == NULL) {
    clnt_pcreateerror(argv[1]);
    exit(2);
  }

  /* Form a request: taking the min and max values from the
     command line. */
  request.min = atoi(argv[2]);
  request.max = atoi(argv[3]);
  /*OVDE CITAMO I ARGUMENT POS I SMESTAMO GA U STRUKTURU 
   NARAVNO TI MOZES DA VRSIS I PROVERU DA UNESENI BROJ NIJE SLUCAJNO 
   NEGATIVAN JA NECU TIME DA SE OPTERECUJEM */
  request.pos=atoi(argv[4]);


  /* Call the remote procedure */
  result = find_primes_1(&request, cl);
  /*OVDE SAM STAVIO NULU JER JE RESULT INT A CINI MI SE DA STANDARD NE GARANTUJE
  DA JE NULL==0 */
  if (result == 0) {
    clnt_perror(cl, argv[1]);
    exit(3);
  }

/* SADA MENJAMO ISPIS JER VISE NE POSTOJI NIZ PROSTIH BROJEVA VEC JE SAMO JEDAN 
   NARAVNO JA CU ISPIS PO SRPSKI A TI PROMENI KAKO TI ODGOVARA :) */
  printf("%d. prosti broj po redu je %d\n", request.pos, result);

  /* Clean up */
  clnt_destroy(cl);
  return 0;
}



to bi trebalo da bude to bar sto se tice klijenta, e sada jos samo da promenimo server :


Code:



#include "primes.h"

int isprime(int);
char *strtime(struct tm *);

/**
 * The server procedure is defined as
 *   ret_type <func name>_<vers num>_svc(req_type, struct svc_req *)
**/

/*OVDE MENJAM TIP KOJI VRACA FUNKCIJA */
int
find_primes_1_svc(prime_request *request, struct svc_req *rqstp)
{
  /**
   * Reserve stroage for the results. This MUST be static,
   * otherwise it will be deallocated from the stack when the
   * procedure terminates. By the time the xdr_prime_result
   * filter gets to serialise the results, it is no longer valid.
  **/
  
  /*CINI MI SE DA VISE NEMA POTREBE DA BUDU STATIC OVI ELEMENTI JER
  VRACAMO SAMO INT PO VREDNOSTI ALI JA CU IH OSTAVITI A TI RADI KAKO HOCES */
  static prime_result result;
  static int prime_array[MAXPRIMES];
  int i, count=0;
  time_t curr_time;

  /* Log requests. */
  curr_time = time(NULL);
  printf("%s find_primes_1_svc(): range = [%d,%d]\n",
    strtime(localtime(&curr_time)), request->min, request->max);

  /* Loop over the range limits in the request "packet". */
  for (i=request->min; count<MAXPRIMES && i<=request->max; i++)
    if (isprime(i))
      prime_array[count++] = i;

  /**
   * Assemble the reply packet. Note that the variable length
   * array we are returning is really a struct with an
   * element count and a pointer to the first element.
  **/
  result.array.array_len = count;
  result.array.array_val = prime_array;
  
  /*E POSTO JE DO OVOG KORAKA ON PRONASAO SVE PROSTE BROJEVE MI CEMO SAMO 
  UZETI BROJ KOJI JE NA ONOM MESTU KOJE NAMA TREBA. 
  DA SAM IZ PRIMES.X IZBRISAO STRUKTURU primes_result MORAO BIH DA MENJAM DOBAR DEO OVOG
  KODA A OVAKO SAMO DODAJEM PAR REDA, JESTE DA BI SE ZADATAK BRZE IZVRSAVAO BEZ NJE ALI JE 
  I OVAKO NADAM SE OK*/
  
  /* SADA TREBA DA PITAMO DA LI JE POS VECE OD BROJA PRONADJENIH BROJEVA */
  if (request->pos>result.array.array_len)
       return 0; /*ZNACI AKO JESTE VECE ONDA NEMAMO ELEMENT KOJI TRAZIMO I VRACAMO NULU */
  else return prime_array[request->pos-1]; /*A AKO NIJE ONDA VRACAMO TAJ ELEMENT
                                            OVDE STAVLJAMO -1 ZA POZICIJU JER INDEXI 
                                            KRECU OD 0 */
}

/**
 * This is a 'private' support function and is not directly
 * accessible via RPC. Returns 1 if n is prime and 0 otherwise.
**/
int isprime(int n)
{
  int i;

  if (n < 2)
    return 0;

  for (i=2; i*i<=n; i++)
    if ((n % i) == 0)
      return 0;
  return 1;
}

/**
 * Private function to stringify time structure.
**/
char *strtime(struct tm *timeptr)
{
  static char tm_str[] = "MMM dd hh:mm:ss";
  char *tm_mon;

  switch (timeptr->tm_mon) {
    case 0: tm_mon = "Jan"; break;
    case 1: tm_mon = "Feb"; break;
    case 2: tm_mon = "Mar"; break;
    case 3: tm_mon = "Apr"; break;
    case 4: tm_mon = "May"; break;
    case 5: tm_mon = "Jun"; break;
    case 6: tm_mon = "Jul"; break;
    case 7: tm_mon = "Aug"; break;
    case 8: tm_mon = "Sep"; break;
    case 9: tm_mon = "Oct"; break;
    case 10: tm_mon = "Nov"; break;
    case 11: tm_mon = "Dec"; break;
    default: return NULL;
  }

  sprintf(tm_str, "%3s %2d %02d:%02d:%02d", tm_mon, timeptr->tm_mday,
      timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec);
  return tm_str;
}



To bi trebalo da bude to, nisam isprobao program pa necu da garantujem da radi, ali ti probaj pa ako nesto negde zeza ostavi poruku, naravno javi se i ako treba neko dodatno objasnjenje oko stvari koje sam menjao.
poz

I drink to make other people interesting.
 
Odgovor na temu

miros21k

Član broj: 22204
Poruke: 21
*.dsl.pipex.com.



Profil

icon Re: Upomoc C Programming Problem25.11.2004. u 00:57 - pre 236 meseci
Hvala Marko na odgovoru, ali imam problem nazalost :(


gcc -o client primes_client.c primes_clnt.c primes_xdr.c -lnsl (komanda za "compile" klijenta)
greske:
primes_client.c: In function `main':
primes_client.c:29: warning: assignment makes integer from pointer without a cast



gcc -o server primes_server.c primes_svc.c primes_xdr.c -lnsl (komanda za "compile" servera)
greske:
primes_server.c:12: conflicting types for `find_primes_1_svc'
primes.h:38: previous declaration of `find_primes_1_svc'


Evo i primes.h mozda ti on nesto kaze (njega dobijem kad uradim rpcgen primes.x)

Code:

/*
 * Please do not edit this file.
 * It was generated using rpcgen.
 */

#ifndef _PRIMES_H_RPCGEN
#define _PRIMES_H_RPCGEN

#include <rpc/rpc.h>

#ifdef __cplusplus
extern "C" {
#endif

#define MAXPRIMES 1024

struct prime_request {
        int min;
        int max;
        int pos;
};
typedef struct prime_request prime_request;

struct prime_result {
        struct {
                u_int array_len;
                int *array_val;
        } array;
};
typedef struct prime_result prime_result;

#define PRIME_PROG ((unsigned long)(0x2a3b4c5d))
#define PRIME_VERS ((unsigned long)(1))

#if defined(__STDC__) || defined(__cplusplus)
#define FIND_PRIMES ((unsigned long)(2))
extern  prime_result * find_primes_1(prime_request *, CLIENT *);
extern  prime_result * find_primes_1_svc(prime_request *, struct svc_req *);
extern int prime_prog_1_freeresult(SVCXPRT *, xdrproc_t, caddr_t);

#else /* K&R C */
#define FIND_PRIMES ((unsigned long)(2))
extern  prime_result * find_primes_1();
extern  prime_result * find_primes_1_svc();
extern int prime_prog_1_freeresult();
#endif /* K&R C */

/* the xdr functions */

#if defined(__STDC__) || defined(__cplusplus)
extern  bool_t xdr_prime_request(XDR *, prime_request*);
extern  bool_t xdr_prime_result(XDR *, prime_result*);

#else /* K&R C */
extern bool_t xdr_prime_request();
extern bool_t xdr_prime_result();

#endif /* K&R C */

#ifdef __cplusplus
}
#endif

#endif /* !_PRIMES_H_RPCGEN */



Ne kuzim zasto izbacuje te greske ?

 
Odgovor na temu

Marko Stankovic

Član broj: 11
Poruke: 306
*.rvkds.net.



Profil

icon Re: Upomoc C Programming Problem26.11.2004. u 18:02 - pre 236 meseci
Koliko ja vidim problem je u tome sto je rpcgen u primes.h definisao funkcije find_primes1 i find_primes1_svc da vracaju vrednost tipa *prime_result a trebalo je da ih definise da vracaju int, pa se zato ne slaze header sa ovim kodom koji sam ti ostavio. Malo mi je cudno sto se to desilo jer smo u primes.x lepo stavili da je vrednost od find_primes int. Iskreno ja nemam iskustva sa rpcgen-om tako da nemam pojma na koji nacin on generise primes.h, tako da ti ne mogu pomoci oko toga, koliko vidim kod je sasvim dobar tako da je jedino problem sa njim. Valjda ce se javiti neko ko je vec radio sa tim da kaze sta je jos potrebno navesti u primes.x da bi rpcgen ispravno generisao zaglavlje.
U svakom slucaju da bi isprobao da li program radi mozes sam rucno prepraviti primes.h i da prepravis povratne vrednosti funkcije find_primes1 i find_primes1_svc u tip int. To bi valjda omogucilo da se kod iskompajlira cisto da vidimo da li kod radi ono sto bi trebalo.
I drink to make other people interesting.
 
Odgovor na temu

leka
Dejan Lekić
senior software engineer, 3Developers
Ltd.
London, UK

Član broj: 234
Poruke: 2534
..31.216.81.gus.vf.siwnet.net.

Sajt: dejan.lekic.org


+2 Profil

icon Re: Prosti brojevi na Suncu: Upomoc C Programming Problem27.11.2004. u 12:51 - pre 236 meseci
Koliko ja vidim ovo je vise SUN RPC problem nego C/C++-programiranje problem ...
Dejan Lekic
software engineer, MySQL/PgSQL DBA, sysadmin
 
Odgovor na temu

Goran Arandjelovic
Beograd

Član broj: 29116
Poruke: 387
*.neobee.net.



+9 Profil

icon Re: Prosti brojevi na Suncu: Upomoc C Programming Problem17.12.2004. u 00:38 - pre 235 meseci
Hm... 1 nije prost broj. :) Prost broj mora da ima dva delioca... 1 je kvalifikator ili kako se vec zove... ;))
 
Odgovor na temu

[es] :: C/C++ programiranje :: Prosti brojevi na Suncu: Upomoc C Programming Problem

[ Pregleda: 3220 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.