#include < pthread.h > #include < stdio.h > #include "socketfun.h" typedef struct { int fd; FILE *fout; int *nchars; pthread_mutex_t *lock; } TStruct; int read1000(int fd, char *chars) { int so_far, i; so_far = 0; while(so_far < 1000) { i = read(fd, chars+so_far, 1000-so_far); if (i == 0) return 0; so_far += i; } return 1; } void *service_thread(void *arg) { TStruct *t; char chars[1000]; char output[20]; t = (TStruct *) arg; if (read1000(t->fd, chars) == 1) { pthread_mutex_lock(t->lock); fwrite(chars, 1, 1000, t->fout); *t->nchars += 1000; sprintf(output, "%d\n", *t->nchars); pthread_mutex_unlock(t->lock); write(t->fd, output, strlen(output)); } close(t->fd); free(t); return NULL; } |
main() { int sock; int fd; FILE *fout; int nchars; pthread_t tid; TStruct *t; pthread_mutex_t lock; sock = serve_socket("cetus3a", 15000); if (sock < 0) { perror("serve_socket"); exit(1); } fout = fopen("outfile", "w"); if (fout == NULL) { perror("fopen(outfile)"); exit(1); } nchars = 0; pthread_mutex_init(&lock, NULL); while (1) { fd = accept_connection(sock); t = (TStruct *) malloc(sizeof(TStruct)); t->fd = fd; t->fout = fout; t->nchars = &nchars; t->lock = &lock; pthread_create(&tid, NULL, service_thread, (void *)t); } } |
Nothing fancy. The original code works and is in q4.c, and the answer is in q4a.c. Try them out.
Part 2: The threaded code will improve performance under two conditions (and both conditions must be present):
Part 2 You got two point for saying that it could service multiple simultaneous connections, but you needed to mention one of the other two issues (bad network or non-instantaneous sending of the 1000 bytes) to get the final point.