closed
Goto Top

SourceCode für Dining Philosophers Problem in C

Hallo,

benötige SourceCode in C (nicht C++, Java oder sonst wasface-smile ), der das Dining Philosophers Problem implementiert hat. Kann im Web nur Code für C++, Java oder sonst was finden. Brauche es aber dringend für eine Evaluierung in C und ein Umschreiben wäre die letzte Lösung. Das dauert bei meinen C-Kenntnissen zu lange face-smile

Vielen Dank

Content-Key: 135341

Url: https://administrator.de/contentid/135341

Printed on: April 20, 2024 at 03:04 o'clock

Member: H41mSh1C0R
H41mSh1C0R Feb 06, 2010 at 13:43:17 (UTC)
Goto Top
also ich brauche dringend eine Implementation eines USB Treibers inkl. Firmwarepaket zur Evaluierung =). Aber bitte dringend. xD xD Dauert bei meinen C-Kenntnissen auch zulange. xD

Aber glaube mir gibt das keiner, also schalte ich mal *Brain.exe* ein und schreibe mir bestehende Pakete um und stelle dann meine Fragen wenn ich wo nicht weiterkomme aber schon Ansätze vorweisen kann. xD
Member: Closed
Closed Feb 06, 2010 at 14:28:46 (UTC)
Goto Top
sorry, aber blöd rumflamen kann man sich auch sparen. ohne witz, solche posts bringen rein gar nix und nerven einfach nur.
Member: H41mSh1C0R
H41mSh1C0R Feb 06, 2010 at 15:10:23 (UTC)
Goto Top
dito dito dito

aber Bettelposts a la "bitte macht meine Hausaufgaben" ebenso. xD
Member: Closed
Closed Feb 06, 2010 at 16:56:43 (UTC)
Goto Top
Das ist doch kein Bettelpost und mit Hausaufgaben hat das wenig zu tun... dafür bin ich dann mittlerweile doch zu alt ;)

Ein Forum ist dafür da, dass man Usern hilft. Es ist ja nicht so, dass ich keine Anstrengungen in die Suche investiert habe. Und wenn es jemanden gibt, der das gleiche Problem schonmal gehabt hat, dann erspart man sich mit einem einfachen Post in einem Forum einfach eine Menge Arbeit.

Ich sehe eh, wenn nichts bei rum kommt und ich keine Antworten erhalte. Dann wird mir nichts anderes übrig bleiben, als es selbst zu coden, allerdings sollte der Versuch nicht unter Verachtung gestellt werden.

Stelle meinen Code auch immer wieder gerne in Foren rein und denke, dass dies den meisten Usern weiterhilft. Dabei verfolge ich grundsätzlich den Gedanken des Webs, dass man eine große Community ist, die einem weiterhilft, auch wenn man mal eine nicht allzu herausfordernde Frage stellt...
Member: Closed
Closed Feb 06, 2010 at 17:35:06 (UTC)
Goto Top
Sodalle,

für alle die dasselbe Problem haben, hier nun die Lösung:

1. pthread.h, sched.h und semaphore.h müssen von einer Quelle downgeloaded werden
2. Downloaden von pthreadVC1.lib und und pthredVC1.dll und in Windows einbinden
3. Source Code:

//Quelle: http://people.eecs.ku.edu/~mjantz/678labs/lab08/lab08.tar.gz
//Authoren:  Vergleiche (Source von Quelle wurde umgeschrieben) Michael Jantz und Prasad Kulkarni 
#include <stdio.h>
#include "C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\DiningPhilosophers\DiningPhilosophers\pthread.h" 
#include "pthread.h" 
#include <windows.h>
//#include <unistd.h>
#include <math.h>
#include <string.h>

#define NUM_PHILS 5
#define MAX_BUF 256
#define NUM_CHOPS NUM_PHILS

typedef struct {
  int can_grab_left;
  int can_grab_right;
  pthread_t thread;
  pthread_cond_t can_eat; 
  int prog;
  int prog_total;
  int id;
} philosopher;

static pthread_mutex_t chopstick[NUM_CHOPS];
static pthread_mutex_t waiter;
static philosopher diners[NUM_PHILS];
static int stop=0;

/*
 * Helper functions for grabbing chopsticks, referencing neighbors
 */
philosopher *left_phil (philosopher *p)
{
  return &diners[(p->id == 0 ? NUM_PHILS-1 : (p->id)-1)];
}

philosopher *right_phil (philosopher *p)
{
  return &diners[(p->id == NUM_PHILS ? 0 : (p->id)+1)];
}

pthread_mutex_t *left_chop (philosopher *p)
{
  return &chopstick[(p->id == 0 ? NUM_CHOPS-1 : (p->id)-1)];
}

pthread_mutex_t *right_chop (philosopher *p)
{
  return &chopstick[p->id];
}

/*
 * Philosopher code
 */
static void *dp_thread(void *arg)
{
      int iter=0, id = (int)arg;
  int think_rnd, eat_rnd, i;
  philosopher *me;

  me = (philosopher *) arg;

      while (!stop) {
    think_rnd = (rand() % 10000);
    eat_rnd = (rand() % 10000);

            /*
             * Think for some random time
             */         
            for (i = 0; i < think_rnd; i++){}

    /*
     * Grab both chopsticks
     */
    pthread_mutex_lock(left_chop(me));
    pthread_mutex_lock(right_chop(me));

    /*
     * Eat for some random time
     */
    for (i = 0; i < eat_rnd; i++){}

    /*
     * Release both chopsticks
     */
    pthread_mutex_unlock(left_chop(me));
    pthread_mutex_unlock(right_chop(me));

    /* Update my progress */
    me->prog++;
    me->prog_total++;
  }

  return NULL;
}

void init_diners()
{
  int i;
      for (i = 0; i < NUM_PHILS; i++) {
    diners[i].can_grab_left = 1;
    diners[i].can_grab_right = 1;
    diners[i].prog = 0;
    diners[i].prog_total = 0;
    diners[i].id = i;
  }

      for (i = 0; i < NUM_PHILS; i++) {
            pthread_create(&(diners[i].thread), NULL, dp_thread, &diners[i]);
  }
}

void print_progress()
{
  int i,j;
  char buf[MAX_BUF];

  for (i = 0; i < NUM_PHILS;) {
    for (j = 0; j < 5; j++) {
      if (i == NUM_PHILS) {
        printf("\n");  
        break;
      }
      sprintf(buf, "p%d=%d/%d", i, diners[i].prog,  
          diners[i].prog_total);
      if (strlen(buf) < 16)
        printf("%s\t\t", buf);  
      else 
        printf("%s\t", buf);  
      i++;
    }
    if (i == NUM_PHILS) {
      printf("\n");  
      break;
    }
    printf("p%d=%d/%d\n", i, diners[i].prog,  
        diners[i].prog_total); 
    i++;
  }

  printf("\n");  
}

int main(int argc, char **argv)
{
      int i, deadlock, iter=0;

  srand(time(NULL));

      for (i = 0; i < NUM_CHOPS; i++)
            pthread_mutex_init(&chopstick[i], NULL);

  pthread_mutex_init(&waiter, NULL);
  init_diners();
      
      do {
    /*
     * Reset the philosophers eating progress to 0. If the philosopher makes
     * progress, the philosopher will increment it.
     */
            for (i = 0; i < NUM_PHILS; i++)
                  diners[i].prog = 0;

    /*
     * Let the philosophers do some thinking and eating
     */
            Sleep(5);

    /*
     * Check for deadlock (i.e. none of the philosophers are making progress)
     */
            deadlock = 1;
            for (i = 0; i < NUM_PHILS; i++)
                  if (diners[i].prog)
                        deadlock = 0;

    /*
     * Print out the philosophers progress
     */
    print_progress();
      } while (!deadlock);

      stop = 1;
  printf ("Reached deadlock\n");  

  /*
   * Release all locks so philosophers can exit
   */
      for (i = 0; i < NUM_CHOPS; i++)
            pthread_mutex_unlock(&chopstick[i]);

  /*
   * Wait for philosophers to finish
   */
      for (i = 0; i < NUM_PHILS; i++)
            pthread_join(diners[i].thread, NULL);

      system("pause");  

      return 0;


}
Member: H41mSh1C0R
H41mSh1C0R Feb 08, 2010 at 07:49:38 (UTC)
Goto Top
Sry, aber der Ausgangspost las sich aber so.
Das Alter eines Fragestellenden kann man bei recht frischen Accs und spärlicher Frage auch nicht einschätzen.
Der Satzanfang "Benötige das und das ..... Brauche es aber dringend" verbessert bei dem potentiell helfenden ebenfalls NICHT die Hilfbereitschaft, wirkt dem eher entgegen oder verursacht wie in meinem Fall eine flappsige Antwort.
Member: Closed
Closed Feb 08, 2010 at 18:03:12 (UTC)
Goto Top
naja past ja eh, lassen wirs einfach so stehen.

PS: bin auch schon ein paar Jahre nun mit dabei ;)