Threading Programming
ą¹ąøŖąøąøąøąø±ąø§ąøąø¢ą¹ąø²ąøąøąø²ąø£ąøŖąø£ą¹ąø²ąø thread ąøąø¢ą¹ąø²ąøąøą¹ąø²ąø¢ą¹ąøąø¢ąøąø²ąø£ą¹ąø£ąøµąø¢ąøą¹ąøą¹ąøąø±ąøąøą¹ąøąø±ąø pthread_create()
// thread1.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* print_message_function(void *ptr) {
char *message;
message = (char*) ptr;
printf("[Thread started] %s \n", message);
}
main() {
pthread_t thread;
const char *message = "Hello World...";
int iret;
iret = pthread_create(&thread, NULL, print_message_function,
(void*) message);
/* Wait till thread is complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and thread before the thread has completed. */
pthread_join(thread, NULL);
printf("Thread returns: %d\n", iret);
exit(0);
}
ąøąø³ąøąø²ąø£ąøąøąø”ą¹ąøąø„ą¹ ą¹ąø„ąø°ąøąøąøŖąøąøąø£ąø±ąøą¹ąøąø£ą¹ąøąø£ąø”
$ gcc -o thread1 thread1.c -lpthread
$ ./thread1
[Thread started] Hello world...
Thread returns: 0
ą¹ąøŖąøąøąøąø±ąø§ąøąø¢ą¹ąø²ąøąøąø²ąø£ąøŖąø£ą¹ąø²ąø thread ąøąø¶ą¹ąøąø”ąø² 4 ąøąø±ąø§ąøąø²ąøąøąø±ąøąøą¹ąøąø±ąø *printme(void *ip)
ą¹ąøąøµąø¢ąø§ąøąø±ąø ą¹ąøąø¢ą¹ąøą¹ąø„ąø° thread ąøą¹ąøąø°ą¹ąøŖąøąøąøą¹ąøąøąø§ąø²ąø”ą¹ąøąøµąø¢ąøą¹ąøą¹ąøąø§ą¹ąø²ąøąø±ąø§ą¹ąøąøą¹ąøą¹ąø thread ąøąø±ąø§ąøąøµą¹ą¹ąøą¹ąø²ą¹ąø«ąø£ą¹ (0-3
) ąøąø¶ą¹ąøąøą¹ąø²ą¹ąøąø£ą¹ąøąø£ąø”ąøąø³ąøąø²ąøąøąø¢ąø¹ą¹ąøąøą¹ąøąø£ąø·ą¹ąøąøąøąøąø”ąøąø“ąø§ą¹ąøąøąø£ą¹ąøąøµą¹ąø”ąøµąø«ąøą¹ąø§ąø¢ąøąø£ąø°ąø”ąø§ąø„ąøąø„ą¹ąøą¹ąøą¹ąøąøąø«ąø„ąø²ąø¢ąøąø±ąø§ (multiprocessors) ąøą¹ąøąø°ąøąø³ą¹ąø«ą¹ąøąø£ąø°ąøŖąø“ąøąøąø“ąø ąø²ąøąøąø²ąø£ąøąø³ąøąø²ąøą¹ąøąøąø²ąø£ąøąø£ąø°ąø”ąø§ąø„ąøąø„ąø£ąø§ąøą¹ąø£ą¹ąø§ąø¢ąø“ą¹ąøąøąø¶ą¹ąø ą¹ąøąø£ąø²ąø°ą¹ąøą¹ąø„ąø°ąøąø±ąø§ąøąø°ą¹ąø¢ąøą¹ąøą¹ąøąø²ąøą¹ąøą¹ąøą¹ąø„ąø°ąøąø±ąø§ąø«ąøą¹ąø§ąø¢ąøąø£ąø°ąø”ąø§ąø„ąøąø„
// thread2.c
// forks off four threads that print their ids
#include <pthread.h>
#include <stdio.h>
void* printme(void *ip) {
int *i;
i = (int*) ip;
printf("Hi. I'm thread %d\n", *i);
return NULL;
}
main() {
int i, vals[4];
pthread_t tids[4];
void *retval;
for (i = 0; i < 4; i++) {
vals[i] = i;
pthread_create(tids + i, NULL, printme, vals + i);
}
for (i = 0; i < 4; i++) {
printf("Trying to join with tid %d\n", i);
pthread_join(tids[i], &retval);
printf("Joined with tid %d\n", i);
}
}
ąøąø³ąøąø²ąø£ąøąøąø”ą¹ąøąø„ą¹ ą¹ąø„ąø°ąøąøąøŖąøąøąø£ąø±ąøą¹ąøąø£ą¹ąøąø£ąø”
$ gcc -o thread2 thread2.c -lpthread
$ ./thread2
Trying to join with tid 0
Hi. I'm thread 1
Hi. I'm thread 2
Hi. I'm thread 3
Hi. I'm thread 0
Joined with tid 0
Trying to join with tid 1
Joined with tid 1
Trying to join with tid 2
Joined with tid 2
Trying to join with tid 3
Joined with tid 3
ąøąø²ąøąøąø„ąø„ąø±ąøąøą¹ąøąø°ąøŖąø±ąøą¹ąøąøą¹ąø«ą¹ąøąø§ą¹ąø²ą¹ąø”ąø·ą¹ąøą¹ąø£ąø“ą¹ąø”ą¹ąø£ąøµąø¢ąøąøąø±ąøąøą¹ąøąø±ąø pthread_join()
ąøąø±ąøąøąø±ą¹ąøąøąø±ąø§ thread ąøąø±ąø§ąøąøµą¹ 1 (ąøąø²ąøąøąø°ą¹ąø”ą¹ą¹ąøą¹ą¹ąø£ąøµąø¢ąøąøąø²ąø”ąø„ąø³ąøąø±ąøąø«ąø”ąø²ąø¢ą¹ąø„ąøąøąøøąøąøąø£ąø±ą¹ąøą¹ąø ą¹ąøąø·ą¹ąøąøąøąø²ąøąøąø¶ą¹ąøąøąø¢ąø¹ą¹ąøąø±ąøąø§ą¹ąø²ąøąø±ąø§ thread ą¹ąøą¹ąøą¹ąø²ąøąø£ąøąøąøąø£ąøąøąø«ąøą¹ąø§ąø¢ąøąø£ąø°ąø”ąø§ąø„ąøąø„ą¹ąøą¹ąøąøąøą¹ąø£ąø) ąøą¹ąøąø°ą¹ąø£ąø“ą¹ąø”ą¹ąøą¹ąø²ą¹ąøą¹ąøąø²ąøąø«ąøą¹ąø§ąø¢ąøąø£ąø°ąø”ąø§ąø„ąøąø„ ą¹ąø„ą¹ąø§ą¹ąøŖąøąøąøą¹ąøąøąø§ąø²ąø”ąø§ą¹ąø² āHi. I'm thread xā ą¹ąø”ąø·ą¹ąøąøąø²ąø£ąøąø³ąøąø²ąøą¹ąøŖąø£ą¹ąøąøŖąø“ą¹ąø ąøą¹ąøąø°ą¹ąøą¹ąøąøąø“ąø§ąøąøąøąøąø±ąø§ thread ąøąø±ąøą¹ą¹ąøąøąøąøąø£ąøąøąø±ą¹ąø 4 ąøąø±ąø§ ąøą¹ąøąø°ąøąø„ąø±ąøąø”ąø²ąø¢ąø±ąøąøąø±ąøąøą¹ąøąø±ąøąø«ąø„ัภ(main
) ą¹ąøąø·ą¹ąøąøŖąø“ą¹ąøąøŖąøøąøąøąø²ąø£ąøąø³ąøąø²ąøąøąøąøą¹ąøąø£ą¹ąøąø£ąø”
ą¹ąøŖąøąøąøąø±ąø§ąøąø¢ą¹ąø²ąøąøąø²ąø£ąøŖą¹ąøąøą¹ąø²ąøąøą¹ąøąøąø§ąø²ąø” (char *
) ą¹ąø«ą¹ąøąø±ąøą¹ąøąø£ąø ąøąø¶ą¹ąøąøąø±ąø§ąøąø¢ą¹ąø²ąøąøą¹ąøąøąø«ąøą¹ąø²ąøąøµą¹ą¹ąøą¹ąøąøąø²ąø£ąøŖą¹ąøąøą¹ąøąø”ąø¹ąø„ąøąøµą¹ą¹ąøą¹ąøąøąø±ąø§ą¹ąøąø£ąøąø³ąøąø§ąøą¹ąøą¹ąø”ąøąø±ą¹ąø§ą¹ąø
// thread3.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* print_message_function(void *ptr);
main() {
pthread_t thread1, thread2;
const char *message1 = "Thread 1ās Message";
const char *message2 = "Thread 2ās Message";
int iret1, iret2;
/* Create independent threads each of which will execute function */
iret1 = pthread_create(&thread1, NULL, print_message_function,
(void*) message1);
iret2 = pthread_create(&thread2, NULL, print_message_function,
(void*) message2);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("Thread 1 returns: %d\n", iret1);
printf("Thread 2 returns: %d\n", iret2);
exit(0);
}
void* print_message_function(void *ptr) {
char *message;
message = (char*) ptr;
printf("%s \n", message);
}
ąøąø³ąøąø²ąø£ąøąøąø”ą¹ąøąø„ą¹ ą¹ąø„ąø°ąøąøąøŖąøąøąø£ąø±ąøą¹ąøąø£ą¹ąøąø£ąø”
$ gcc -o thread3 thread3.c ālpthread
$ ./thread3
Thread 2ās Message
Thread 1ās Message
Thread 1 returns: 0
Thread 2 returns: 0
ą¹ąøŖąøąøąøąø±ąø§ąøąø¢ą¹ąø²ąøąøąø²ąø£ą¹ąøą¹ pthread_exit()
ą¹ąøąø·ą¹ąøąøąø³ą¹ąø«ą¹ thread ąøŖąø“ą¹ąøąøŖąøøąøąøąø²ąø£ąøąø³ąøąø²ąø ą¹ąøą¹ąøąø°ąø¢ąø±ąøąøąøąø£ąø±ąøąø©ąø²ą¹ąø«ą¹ąøąø²ąø (task) ąø¢ąø±ąøąøąø³ąøąø²ąøąøąø¢ąø¹ą¹ ąøą¹ąø² thread ąøąø±ą¹ąøąø«ąø”ąøąøŖąø“ą¹ąøąøŖąøøąøąø„ąøą¹ąø„ą¹ąø§ąøąø²ąø (task) ąøą¹ąøąø°ąøŖąø“ą¹ąøąøŖąøøąøąø„ąøą¹ąøą¹ąøąøąø±ąø
// thread4.c
#include <pthread.h>
#include <stdio.h>
void* printme(void *v) {
int *i;
i = (int*) v;
printf("Hi. I'm thread %d\n", *i);
pthread_exit(NULL);
}
main() {
int i, vals[4];
pthread_t tids[4];
void *retval;
for (i = 0; i < 4; i++) {
vals[i] = i;
pthread_create(tids + i, NULL, printme, (void*) (vals + i));
}
for (i = 0; i < 4; i++) {
printf("Trying to join with tid %d\n", i);
pthread_join(tids[i], &retval);
printf("Joined with tid %d\n", i);
}
pthread_exit(NULL);
}
ąøąø³ąøąø²ąø£ąøąøąø”ą¹ąøąø„ą¹ ą¹ąø„ąø°ąøąøąøŖąøąøąø£ąø±ąøą¹ąøąø£ą¹ąøąø£ąø”
$ gcc -o thread4 thread4.c -lpthread
$ ./thread4
Trying to join with tid 0
Hi. I'm thread 3
Hi. I'm thread 2
Hi. I'm thread 1
Hi. I'm thread 0
Joined with tid 0
Trying to join with tid 1
Joined with tid 1
Trying to join with tid 2
Joined with tid 2
Trying to join with tid 3
Joined with tid 3
ąøąø„ąø„ąø±ąøąøą¹ąøąø²ąøąøąø²ąø£ąø£ąø±ąøą¹ąøąø£ą¹ąøąø£ąø” thread4
ąøąø°ą¹ąøą¹ąøąø„ą¹ąøą¹ąøą¹ąøąøµąø¢ąø§ąøąø±ąøą¹ąøąø£ą¹ąøąø£ąø” thread2
ąøąø²ąøąøąø±ąø§ąøąø¢ą¹ąø²ąøąøąøµą¹ 2
ą¹ąøŖąøąøąøąø±ąø§ąøąø¢ą¹ąø²ąøąøąø²ąø£ą¹ąø£ąøµąø¢ąø exit()
ą¹ąøąøąø±ąøąøą¹ąøąø±ąøąøąøąø thread ą¹ąøąøąøąøµą¹ąøąø°ą¹ąø£ąøµąø¢ąø pthread_exit()
// thread5.c
#include <pthread.h>
#include <stdio.h>
void* printme(i)
int *i; {
printf("Hi. I'm thread %d\n", *i);
exit(NULL);
}
main() {
int i, vals[4];
pthread_t tids[4];
void *retval;
for (i = 0; i < 4; i++) {
vals[i] = i;
pthread_create(tids + i, NULL, printme, vals + i);
}
for (i = 0; i < 4; i++) {
printf("Trying to join with tid %d\n", i);
pthread_join(tids[i], &retval);
printf("Joined with tid %d\n", i);
}
}
ąøąø³ąøąø²ąø£ąøąøąø”ą¹ąøąø„ą¹ ą¹ąø„ąø°ąøąøąøŖąøąøąø£ąø±ąøą¹ąøąø£ą¹ąøąø£ąø”
$ gcc -o thread5 thread5.c -lpthread
$ ./thread5
Trying to join with tid 0
Hi. I'm thread 3
ąøąø„ąø„ąø±ąøąøą¹ąøąø²ąøąøąø²ąø£ąø£ąø±ąøą¹ąøąø£ą¹ąøąø£ąø” thread5 ąøąø°ąøŖąø±ąøą¹ąøąøą¹ąø«ą¹ąøąø§ą¹ąø²ąø”ąøµąøąø²ąø£ą¹ąø£ąøµąø¢ąøąøąø±ąøąøą¹ąøąø±ąø exit() ąø ąø²ąø¢ą¹ąøąøąø±ąøąøą¹ąøąø±ąø thread
ąøąø³ą¹ąø«ą¹ą¹ąø”ąø·ą¹ąøąø”ąøµ thread ąøąø±ąø§ą¹ąøąøąø±ąø§ąø«ąøąø¶ą¹ąøą¹ąø£ąø“ą¹ąø”ą¹ąøą¹ąø²ąøąø£ąøąøąøąø£ąøąøąø«ąøą¹ąø§ąø¢ąøąø£ąø°ąø”ąø§ąø„ąøąø„ąøąø„ąø²ąø ą¹ąø”ąø·ą¹ąøąøąø³ąøąø²ąøą¹ąøŖąø£ą¹ąø (ą¹ąøŖąøąøąøą¹ąøąøąø§ąø²ąø”) ąøą¹ąøąø°ą¹ąø£ąøµąø¢ąø exit()
ą¹ąøą¹ąøąøąø„ą¹ąø«ą¹ąøŖąø“ą¹ąøąøŖąøøąøąøąø²ąø£ąøąø³ąøąø²ąøąøąøąøą¹ąøąø£ą¹ąøąø£ąø”ąø«ąø„ąø±ąø ąø£ąø§ąø”ąøąø±ą¹ąø thread ąø„ąø¹ąøąøą¹ąø²ąøą¹ąøąø±ąøąøąøµ
ą¹ąøŖąøąøąøąø±ąø§ąøąø¢ą¹ąø²ąøąøąø²ąø£ąøąø³ąøąø²ąøąø£ąø°ąø«ąø§ą¹ąø²ąø 2 ą¹ąøąø£ąø ąøąøµą¹ąøąø¢ąø²ąø¢ąø²ąø”ą¹ąø¢ą¹ąøąøąø±ąøą¹ąøŖąøąøąøąø„ąøą¹ąøąøąø§ąø²ąø”ąøąøąøąøąø²ąøąø«ąøą¹ąø²ąøąø
// thread6.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void* thread1() {
while (1) {
printf("Hello!!\n");
}
}
void* thread2() {
while (1) {
printf("How are you?\n");
}
}
int main() {
int status;
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, thread1, NULL);
pthread_create(&tid2, NULL, thread2, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
ąøąø³ąøąø²ąø£ąøąøąø”ą¹ąøąø„ą¹ ą¹ąø„ąø°ąøąøąøŖąøąøąø£ąø±ąøą¹ąøąø£ą¹ąøąø£ąø”
$ gcc -o thread6 thread6.c -lpthread
$ ./thread6
How are you?
How are you?
How are you?
How are you?
How are you?
How are you?
Hello!!
Hello!!
Hello!!
Hello!!
Hello!!
How are you?
How are you?
Hello!!
How are you?
...
ąøąø²ąøąøąø„ąø„ąø±ąøąøą¹ąøąø³ąøŖąø±ą¹ąøąøą¹ąø²ąøąøą¹ąøąøąø°ąøŖąø±ąøą¹ąøąøą¹ąø«ą¹ąøąøą¹ąøąøąø§ąø²ąø”ąøąø±ą¹ąøąøŖąøąøąøąøµą¹ą¹ąøąø“ąøąøąø²ąøą¹ąøą¹ąø„ąø°ą¹ąøąø£ąøąøąø°ą¹ąøŖąøąøą¹ąøąø£ąøąøąø±ąøą¹ąøąø”ąø²ą¹ąø”ą¹ąø”ąøµąø£ąø¹ąøą¹ąøąøą¹ąøą¹ąøąøąø ąøąø¶ą¹ąøąøąø¢ąø¹ą¹ąøąø±ąøąø§ą¹ąø²ą¹ąøąø£ąøąøąø±ąø§ą¹ąø ą¹ąøą¹ąø²ąøąø£ąøąøąøąø£ąøąøąø«ąøą¹ąø§ąø¢ąøąø£ąø°ąø”ąø§ąø„ąøąø„ąøą¹ąøąøąøąø±ąø
ą¹ąøŖąøąøąøąø±ąø§ąøąø¢ą¹ąø²ąøąøąø²ąø£ą¹ąøą¹ąøąø±ąøąø«ąø²ąø„ąø³ąøąø±ąøąøąø²ąø£ą¹ąøŖąøąøąøąø„ ą¹ąøąø¢ą¹ąø«ą¹ą¹ąøąø£ąøą¹ąø£ąøą¹ąøą¹ą¹ąøą¹ąø²ąøąø¶ąøąøąø£ąø±ąøąø¢ąø²ąøąø£ąøąø„ąø²ąøąøą¹ąøąø ą¹ąø„ą¹ąø§ą¹ąø«ą¹ąøąøµąøą¹ąøąø£ąøąøąø¶ąøą¹ąøąøą¹ąø²ąøąøą¹ąø²ą¹ąøąøąø£ąø±ąøąø¢ąø²ąøąø£ąøąø„ąø²ąøą¹ąøą¹ąøąø„ąø³ąøąø±ąøąøąø±ąøą¹ąø ąøąø±ąøąøąø±ąø§ąøąø¢ą¹ąø²ąøąøą¹ąø²ąøąø„ą¹ąø²ąøąøąøµą¹
// thread7.c
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
char msg[1024];
sem_t len;
void* thread_read() {
while (1) {
printf("Enter a string: ");
scanf("%s", msg);
sem_post(&len);
}
}
void* thread_write() {
while (1) {
sem_wait(&len);
printf("The string entered is : ");
printf("==== %s\n", msg);
}
}
int main() {
int status;
pthread_t tr, tw;
pthread_create(&tr, NULL, thread_read, NULL);
pthread_create(&tw, NULL, thread_write, NULL);
pthread_join(tr, NULL);
pthread_join(tw, NULL);
return 0;
}
ąøąø³ąøąø²ąø£ąøąøąø”ą¹ąøąø„ą¹ ą¹ąø„ąø°ąøąøąøŖąøąøąø£ąø±ąøą¹ąøąø£ą¹ąøąø£ąø”
$ gcc -o thread7 thread7.c -lpthread
$ ./thread7
Enter a string: Hello
Enter a string: The string entered is : ==== Hello
Fine?
Enter a string: The string entered is : ==== Fine?
Yes
Enter a string: The string entered is : ==== Yes
ąøąø²ąøąøąø„ąø„ąø±ąøąøą¹ąøąø³ąøŖąø±ą¹ąøąøą¹ąø²ąøąøą¹ąø ą¹ąø”ąø·ą¹ąøąø”ąøµąøąø²ąø£ą¹ąøą¹ąøąø²ąø£ąøąø±ąøąøąø²ąø£ąøąø±ąøąø«ąø§ąø°ąøąø²ąø£ąøąø³ąøąø²ąøą¹ąøąø·ą¹ąøąøąø§ąøąøąøøąø”ą¹ąø«ą¹ą¹ąøą¹ą¹ąøąø£ąøąøąø³ąøąø²ąøą¹ąøą¹ąøąø„ąø³ąøąø±ąø ąøą¹ąøąø°ąøąø³ą¹ąø«ą¹ąøąø„ąøąø²ąø£ąøąø³ąøąø²ąøąøąøąøąøąø¹ąøąøą¹ąøąø ąøąø„ą¹ąø²ąø§ąøąø·ąøą¹ąø”ąø·ą¹ąøą¹ąøąø£ąø thread_read()
ąøąø³ąøąø²ąø£ąøą¹ąø²ąøąøą¹ąø²ąøąø²ąøąøąø¹ą¹ą¹ąøą¹ą¹ąø„ą¹ąø§ąøąø³ą¹ąøą¹ąøą¹ąøą¹ąøąøąø±ąø§ą¹ąøąø£ msg
ą¹ąøąø¢ąøąøµą¹ą¹ąøąø£ąøąøąøµąøąøąø±ąø§ thread_write()
ąøąø°ąøą¹ąøąøąø£ąøą¹ąø«ą¹ąøąø±ąø§ą¹ąø£ąøąøąø³ąøąø²ąø£ą¹ąøą¹ąøąøą¹ąø²ą¹ąø«ą¹ą¹ąøŖąø£ą¹ąøą¹ąø£ąøµąø¢ąøąø£ą¹ąøąø¢ą¹ąøŖąøµąø¢ąøą¹ąøąø ąøąø¶ąøąøąø°ąøŖąø²ąø”ąø²ąø£ąøą¹ąøą¹ąø²ą¹ąøąøą¹ąø²ąøąøą¹ąø²ą¹ąøąøąø±ąø§ą¹ąøąø£ msg ą¹ąøąø·ą¹ąøąø”ąø²ą¹ąøŖąøąøąøąø„ą¹ąøą¹
ą¹ąøŖąøąøąøąø²ąø£ąøąø£ąø°ąø¢ąøøąøąøą¹ąøąø²ąø£ąøŖąø·ą¹ąøąøŖąø²ąø£ąøą¹ąø²ąøąø£ąø°ąøąøą¹ąøąø£ąø·ąøąøą¹ąø²ąø¢ąøą¹ąø§ąø¢ąø§ąø“ąøąøµąøąø²ąø£ socket programming ą¹ąøąø¢ą¹ąø«ą¹ą¹ąøąø£ąø·ą¹ąøąøą¹ąø”ą¹ąøą¹ąø²ąø¢ąøąøµą¹ą¹ąøąø“ąøąøąøąø£ą¹ąøąø£ąø ąøŖąø²ąø”ąø²ąø£ąøąø£ąøąøąø£ąø±ąøąøąø²ąø£ą¹ąøąø·ą¹ąøąø”ąøą¹ąøąøąø²ąøąøąø±ąø§ąø„ąø¹ąøąøą¹ąø²ąø¢ą¹ąøą¹ąø«ąø„ąø²ąø¢ą¹ąøąø£ąø·ą¹ąøąøąøąø£ą¹ąøąø”ąøąø±ąø ą¹ąøąø¢ąøąø²ąø£ą¹ąøą¹ pthread
ąøąø±ąøąøąø±ąø§ąøąø¢ą¹ąø²ąøąøŖą¹ąø§ąøąøąøąøą¹ąøąø£ąø·ą¹ąøąøą¹ąø”ą¹ąøą¹ąø²ąø¢ąøą¹ąø²ąøąø„ą¹ąø²ąø
/*
server.c
C socket server example, handles multiple clients using threads
*/
#include<stdio.h>
#include<string.h> //strlen
#include<stdlib.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
#include<pthread.h> //for threading , link with lpthread
//the thread function
void* connection_handler(void*);
int main(int argc, char *argv[]) {
int socket_desc, client_sock, c;
struct sockaddr_in server, client;
//Create socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);
//Bind
if (bind(socket_desc, (struct sockaddr*) &server, sizeof(server)) < 0) {
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc, 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
pthread_t thread_id;
while ((client_sock = accept(socket_desc, (struct sockaddr*) &client,
(socklen_t*) &c))) {
puts("Connection accepted");
if (pthread_create(&thread_id, NULL, connection_handler,
(void*) &client_sock) < 0) {
perror("could not create thread");
return 1;
}
//Now join the thread , so that we dont terminate before the thread
//pthread_join( thread_id , NULL);
puts("Handler assigned");
}
if (client_sock < 0) {
perror("accept failed");
return 1;
}
return 0;
}
/*
* This will handle connection for each client
* */
void* connection_handler(void *socket_desc) {
//Get the socket descriptor
int sock = *(int*) socket_desc;
int read_size;
char *message, client_message[2000];
//Send some messages to the client
message = "Greetings! I am your connection handler\n";
write(sock, message, strlen(message));
message = "Now type something and i shall repeat what you type \n";
write(sock, message, strlen(message));
//Receive a message from client
while ((read_size = recv(sock, client_message, 2000, 0)) > 0) {
//end of string marker
client_message[read_size] = '\0';
//Send the message back to client
write(sock, client_message, strlen(client_message));
//clear the message buffer
memset(client_message, 0, 2000);
}
if (read_size == 0) {
puts("Client disconnected");
fflush(stdout);
} else if (read_size == -1) {
perror("recv failed");
}
return 0;
}
ąøŖąø³ąø«ąø£ąø±ąøą¹ąøąø£ą¹ąøąø£ąø”ąø„ąø¹ąøąøą¹ąø²ąø¢ąøąø±ąøą¹ąøŖąøąøąøą¹ąø²ąøąø„ą¹ąø²ąøąøąøµą¹
/*
client.c
Client was fixed port 8888 for connection to server.
*/
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
int print_usage() {
printf("\n");
printf("Usage : client HOST_ADDRESS\n");
printf("Connect to host by using port 8888 for communication\n");
printf("\n");
return 0;
}
int main(int argc, char *argv[]) {
struct sockaddr_in serv_addr, cli_addr;
struct hostent *server;
int serv_fd, cli_fd;
char msg_buffer[256];
if (argc != 2) {
print_usage();
return -1;
}
system("clear");
printf("********** CLIENT **********\n");
// 1. create socket
if ((serv_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Error : creating socket !!!\n");
} else {
printf("1. Creating socket -> OK\n");
}
// 2. connect to host
server = gethostbyname(argv[1]);
bzero((char*) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8888);
bcopy((char*) server->h_addr, (char*) &serv_addr.sin_addr.s_addr,
server->h_length);
if (connect(serv_fd, (struct sockaddr*) &serv_addr, sizeof(serv_addr))
< 0) {
printf("Error : connecting to server !!!\n");
return -1;
} else {
printf("2. Connecting to server -> OK\n");
}
// 3. communication
printf("********* TALKING **********\n");
// 3.1 receive ready message from server
bzero(msg_buffer, 256);
recv(serv_fd, msg_buffer, 256, 0);
printf("[server] : %s\n", msg_buffer);
// 3.2 answer to server
bzero(msg_buffer, 256);
printf("[client] : ");
scanf("%s", msg_buffer);
send(serv_fd, msg_buffer, strlen(msg_buffer), 0);
// 3.3 ok communication
bzero(msg_buffer, 256);
recv(serv_fd, msg_buffer, 256, 0);
printf("[server] : %s\n", msg_buffer);
return 0;
}
ąøąøąø”ą¹ąøąø„ą¹ą¹ąø„ąø°ąø£ąø±ąøą¹ąøąø£ą¹ąøąø£ąø”ąøąø±ą¹ąøąøŖąøąøąøąø±ąøąøąøµą¹
$ gcc -o server server.c -lpthread
$ ./server
Socket created
bind done
Waiting for incoming connections...
Waiting for incoming connections...
Last updated
Was this helpful?