C ![]() |
similar languages: | C# C++ C-Talk D Cilk Java Objective-C Pike TOM | |
Calculate PI | Michael Neumann |
/* * Calculating PI * * Author: Michael Neumann (mneumann@fantasy-coders.de) * Date: 03.12.2002 * * Requires GNU MP (tested with version 4.1) * (http://www.swox.com/gmp/) * * To compile: * cc pi.c -o pi -I/usr/local/include -L/usr/local/lib -lgmp */ #include "gmp.h" #include <stdio.h> int main(int argc, char **argv) { mpz_t k, a, b, a1, b1, p, q, d, d1; mpz_t t1, t2, t3, t4; /* temporary */ /* Initialize and assign integers */ mpz_init(p); mpz_init(q); mpz_init(d); mpz_init(d1); mpz_init(t1); mpz_init(t2); mpz_init(t3); mpz_init(t4); mpz_init_set_ui(k, 2); mpz_init_set_ui(a, 4); mpz_init_set_ui(b, 1); mpz_init_set_ui(a1, 12); mpz_init_set_ui(b1, 4); for(;;) { /* Next approximation */ mpz_mul(p, k, k); /* p = k * k */ mpz_mul_ui(q, k, 2); /* q = k * 2 */ mpz_add_ui(q, q, 1); /* q = q + 1 */ mpz_add_ui(k, k, 1); /* k = k + 1 */ mpz_set(t1, a1); /* t1 = a1 */ mpz_set(t2, b1); /* t2 = b1 */ /* a1 = p*a + q*a1 */ mpz_mul(t3, p, a); /* t3 = p * a */ mpz_mul(t4, q, a1); /* t4 = q * a1 */ mpz_add(a1, t3, t4); /* a1 = t3 + t4 */ /* b1 = p*b + q*b1 */ mpz_mul(t3, p, b); /* t3 = p * b */ mpz_mul(t4, q, b1); /* t4 = q * b1 */ mpz_add(b1, t3, t4); /* b1 = t3 + t4 */ mpz_set(a, t1); mpz_set(b, t2); /* Print common digits */ mpz_tdiv_q(d, a, b); /* d = a / b */ mpz_tdiv_q(d1, a1, b1); /* d1 = a1 / b1 */ while (mpz_cmp(d, d1) == 0) { /* d == d1 */ mpz_out_str(stdout, 10, d1); fflush(stdout); /* a = 10*(a mod b) */ mpz_tdiv_r(t1, a, b); /* t1 = a mod b */ mpz_mul_si(a, t1, 10); /* a = t1 * 10 */ /* a1 = 10*(a1 mod b1) */ mpz_tdiv_r(t1, a1, b1); /* t1 = a1 mod b1 */ mpz_mul_si(a1, t1, 10); /* a = t1 * 10 */ mpz_tdiv_q(d, a, b); /* d = a / b */ mpz_tdiv_q(d1, a1, b1); /* d1 = a1 / b1 */ } } return 0; } |
Calculating PI |
Hello World | Michael Neumann |
#include <stdio.h> main() { printf("Hello World\n"); } |
Prints "Hello World" onto the
screen. |
Multi-Threaded (forking) Echo-Server | Michael Neumann |
/* * $Id: echo-mt.c,v 1.1 2002/01/09 14:28:43 michael Exp $ * * Copyright (c) 2001, 2002 by Michael Neumann * * Multiple Threaded (forking) Echo-Server. * * Platform: NetBSD (should run on other Unices, too). * */ #include <sys/types.h> /* htons() ... */ #include <sys/socket.h> /* socket(), bind(), listen(), accept() */ #include <netinet/in.h> /* sockaddr_in */ #include <sys/param.h> /* MAXHOSTNAMELEN */ #include <netdb.h> /* gethostbyname() */ #include <signal.h> /* signal() */ #include <sys/wait.h> /* waitpid() */ #include <unistd.h> /* close(), gethostname(), read() ... */ #include <stdio.h> /* perror() */ #include <strings.h> /* bzero(), bcopy() */ #define PORT_NR 8000 /* port number to listen on */ /** * Signal handler for SIGCHLD (child process changes state). * * Queries return status of terminated child processes, * otherwise zombie processes will occure. */ void child_sig_handler(void) { while (waitpid(-1, NULL, WNOHANG) > 0) ; } int main(int argc, char** argv) { int s, t; /* socket handles */ struct sockaddr_in sa; /* socket address */ char myhostname[MAXHOSTNAMELEN+1]; /* local host name */ struct hostent *hp; /* host informations */ int pid; /* proccess id of forked child process */ int count; char buffer[100]; /* get host informations */ gethostname(myhostname, MAXHOSTNAMELEN); if ( (hp = gethostbyname(myhostname)) == NULL ) { perror("Couldn't get informations about host name."); return -1; } /* initialize sockaddr_in struct */ bzero(&sa, sizeof (struct sockaddr_in)); bcopy(hp->h_addr, &sa.sin_addr, hp->h_length); sa.sin_family = hp->h_addrtype; sa.sin_port = htons(PORT_NR); /* create socket */ if ( (s = socket(PF_INET, SOCK_STREAM, 0)) == -1 ) { perror("Couldn't create socket."); return -1; } /* bind socket `s' to specific address (local host) */ if ( bind(s, (struct sockaddr*) &sa, sizeof (struct sockaddr)) == -1 ) { perror("Coudn't bind socket."); close(s); return -1; } /* allow incoming requests; queue up to 3 of them */ listen(s, 3); /* install signal handler */ signal(SIGCHLD, child_sig_handler); while (1) { /* accept a connection */ if ( (t = accept(s, NULL, NULL)) == -1 ) { perror("Couldn't accept socket."); close(s); return -1; } /* fork new subprocess, which handles the new connection */ switch (pid = fork()) { case -1: /* error */ perror("Could not fork!"); close(s); close(t); return -1; case 0: /* child process */ /* close handle `s' as it is a duplicate */ close(s); /* echo data */ while ( (count = read(t, buffer, sizeof (buffer)-1)) > 0 ) { write(t, buffer, count); } /* close connection */ close(t); return 0; default: /* parent process */ /* close handle `t' as it is a duplicate */ close(t); } } close(s); return 0; } |
Implements a multi-threaded (forking)
echo-server using BSD Sockets. Only tested on NetBSD! |
Single-Threaded (non-forking) Echo-Server | Michael Neumann |
/* * $Id: echo-st.c,v 1.2 2002/01/09 14:29:03 michael Exp $ * * Copyright (c) 2001, 2002 by Michael Neumann * * Single Threaded (non-forking) Echo-Server. * * Platform: NetBSD (should run on other Unices, too). * */ #include <sys/types.h> /* htons() ... */ #include <sys/socket.h> /* socket(), bind(), listen(), accept() */ #include <netinet/in.h> /* sockaddr_in */ #include <sys/param.h> /* MAXHOSTNAMELEN */ #include <netdb.h> /* gethostbyname() */ #include <unistd.h> /* close(), gethostname(), read() ... */ #include <stdio.h> /* perror() */ #include <strings.h> /* bzero(), bcopy() */ #define PORT_NR 8000 /* port number to listen on */ int main(int argc, char** argv) { int s, t; /* socket handles */ struct sockaddr_in sa; /* socket address */ char myhostname[MAXHOSTNAMELEN+1]; /* local host name */ struct hostent *hp; /* host informations */ int count; char buffer[100]; /* get host informations */ gethostname(myhostname, MAXHOSTNAMELEN); if ( (hp = gethostbyname(myhostname)) == NULL ) { perror("Couldn't get informations about host name."); return -1; } /* initialize sockaddr_in struct */ bzero(&sa, sizeof (struct sockaddr_in)); bcopy(hp->h_addr, &sa.sin_addr, hp->h_length); sa.sin_family = hp->h_addrtype; sa.sin_port = htons(PORT_NR); /* create socket */ if ( (s = socket(PF_INET, SOCK_STREAM, 0)) == -1 ) { perror("Couldn't create socket."); return -1; } /* bind socket `s' to specific address (local host) */ if ( bind(s, (struct sockaddr*) &sa, sizeof (struct sockaddr)) == -1 ) { perror("Coudn't bind socket."); close(s); return -1; } /* allow incoming requests; queue up to 3 of them */ listen(s, 3); while (1) { /* accept a connection */ if ( (t = accept(s, NULL, NULL)) == -1 ) { perror("Couldn't accept socket."); close(s); return -1; } /* echo data */ while ( (count = read(t, buffer, sizeof (buffer)-1)) > 0 ) { write(t, buffer, count); } /* close connection */ close(t); } close(s); return 0; } |
Implements a single-threaded
(non-forking) echo-server using BSD Sockets. Only tested on
NetBSD! |