Language C
(multithreaded version)
| Date: | 05/11/05 |
| Author: | Stefan Scheler |
| URL: | http://sts.synflood.de/ |
| Comments: | 5 |
| Info: | http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html |
| Score: |
/*
* C (multithreaded) using POSIX Thread Library (pthread)
* by Stefan Scheler <sts[at]synflood[dot]de>
* Ilmenau, Germany - May 2005
*
* compile with: gcc -pthread -o 99bottles 99bottles.c
* disable debug output with: ./99bottles 2>/dev/null
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define THREADS 10
#define PLURALS (bottles>1) ? "s" : ""
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int bottles = 99;
void *takeonedownandpassitaround(void *ptr) {
struct timespec sleeptime;
int *thread_id = (int *)ptr;
while (1) {
pthread_mutex_lock(&mutex);
if (bottles>0) {
fprintf(stderr, "Thread %d: ", *thread_id);
printf("%d bottle%s of beer on the wall, %d bottle%s of beer.\n", bottles, PLURALS, \
bottles, PLURALS);
fprintf(stderr, "Thread %d: ", *thread_id);
printf("Take one down and pass it around, ");
if (bottles==1)
printf("no more bottles of beer on the wall.\n");
else
printf("%d bottle%s of beer on the wall.\n", bottles-1, PLURALS);
} else if (bottles==0) {
fprintf(stderr, "Thread %d: ", *thread_id);
printf("No more bottles of beer on the wall, no more bottles of beer.\n");
fprintf(stderr, "Thread %d: ", *thread_id);
printf("Go to the store and buy some more, 99 bottles of beer on the wall.\n");
} else {
pthread_mutex_unlock(&mutex);
break;
}
bottles--;
pthread_mutex_unlock(&mutex);
sleeptime.tv_nsec = 0;
sleeptime.tv_sec = rand() % 10;
fprintf(stderr, "Thread %d: sleeps for %d ms\n", *thread_id, sleeptime.tv_sec);
nanosleep(&sleeptime, NULL);
}
}
int main(void) {
pthread_t thread[THREADS];
int threadid[THREADS];
int i;
srand(time(NULL));
for (i=0; i<THREADS; i++) {
threadid[i] = i;
pthread_create(&thread[i], NULL, takeonedownandpassitaround, &threadid[i]);
}
for (i=0; i<THREADS; i++)
pthread_join(thread[i], NULL);
exit(EXIT_SUCCESS);
}
Download Source | Write Comment
Alternative Versions
| Version | Author | Date | Comments | Rate |
|---|---|---|---|---|
| Linux kernel module | Stefan Scheler | 08/02/05 | 9 | |
| actually produces correct lyrics :P | Dustshine | 08/20/05 | 0 | |
| standard version | Bill Wein | 04/20/05 | 3 | |
| poor Style | Matteo Casati | 09/01/05 | 7 |
Download Source | Write Comment
Add Comment
Please provide a value for the fields Name,
Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.
Please don't post large portions of code here! Use the form to submit new examples or updates instead!
Comments
/* * Uhh... Here it is..... */ #include <stdio.h> #define NAMELEN 128 int main(void) { char *you; you = malloc(NAMELEN); printf("Hello :)\n"); printf("Your name, Sir> "); fgets(you, NAMELEN, stdin); printf("Have a nice day, %s!\n", you); return 0; }