Threading Programming

ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąø—ąøµą¹ˆ 1

ą¹ąøŖąø”ąø‡ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąøąø²ąø£ąøŖąø£ą¹‰ąø²ąø‡ 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

ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąø—ąøµą¹ˆ 2

ą¹ąøŖąø”ąø‡ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąøąø²ąø£ąøŖąø£ą¹‰ąø²ąø‡ 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) ą¹€ąøžąø·ą¹ˆąø­ąøŖąø“ą¹‰ąø™ąøŖąøøąø”ąøąø²ąø£ąø—ąø³ąø‡ąø²ąø™ąø‚ąø­ąø‡ą¹‚ąø›ąø£ą¹ąøąø£ąø”

ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąø—ąøµą¹ˆ 3

ą¹ąøŖąø”ąø‡ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąøąø²ąø£ąøŖą¹ˆąø‡ąøœą¹ˆąø²ąø™ąø‚ą¹‰ąø­ąø„ąø§ąø²ąø” (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

ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąø—ąøµą¹ˆ 4

ą¹ąøŖąø”ąø‡ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąøąø²ąø£ą¹ƒąøŠą¹‰ 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

ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąø—ąøµą¹ˆ 5

ą¹ąøŖąø”ąø‡ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąøąø²ąø£ą¹€ąø£ąøµąø¢ąø 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 ąø„ąø¹ąøąø•ą¹ˆąø²ąø‡ą¹†ąø—ąø±ąø™ąø—ąøµ

ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąø—ąøµą¹ˆ 6

ą¹ąøŖąø”ąø‡ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąøąø²ąø£ąø—ąø³ąø‡ąø²ąø™ąø£ąø°ąø«ąø§ą¹ˆąø²ąø‡ 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?
...

ąøˆąø²ąøąøœąø„ąø„ąø±ąøžąø˜ą¹Œąø„ąø³ąøŖąø±ą¹ˆąø‡ąø‚ą¹‰ąø²ąø‡ąø•ą¹‰ąø™ąøˆąø°ąøŖąø±ąø‡ą¹€ąøąø•ą¹€ąø«ą¹‡ąø™ąø‚ą¹‰ąø­ąø„ąø§ąø²ąø”ąø—ąø±ą¹‰ąø‡ąøŖąø­ąø‡ąø—ąøµą¹ˆą¹€ąøąø“ąø”ąøˆąø²ąøą¹ąø•ą¹ˆąø„ąø°ą¹€ąø—ąø£ąø”ąøˆąø°ą¹ąøŖąø”ąø‡ą¹ąø—ąø£ąøąøąø±ąø™ą¹„ąø›ąø”ąø²ą¹„ąø”ą¹ˆąø”ąøµąø£ąø¹ąø›ą¹ąøšąøšą¹ąø™ą¹ˆąø™ąø­ąø™ ąø‚ąø¶ą¹‰ąø™ąø­ąø¢ąø¹ą¹ˆąøąø±ąøšąø§ą¹ˆąø²ą¹€ąø—ąø£ąø”ąø•ąø±ąø§ą¹ƒąø” ą¹€ąø‚ą¹‰ąø²ąø„ąø£ąø­ąøšąø„ąø£ąø­ąø‡ąø«ąø™ą¹ˆąø§ąø¢ąø›ąø£ąø°ąø”ąø§ąø„ąøœąø„ąøą¹ˆąø­ąø™ąøąø±ąø™

ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąø—ąøµą¹ˆ 7

ą¹ąøŖąø”ąø‡ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąøąø²ąø£ą¹ąøą¹‰ąø›ąø±ąøąø«ąø²ąø„ąø³ąø”ąø±ąøšąøąø²ąø£ą¹ąøŖąø”ąø‡ąøœąø„ ą¹‚ąø”ąø¢ą¹ƒąø«ą¹‰ą¹€ąø—ąø£ąø”ą¹ąø£ąøą¹„ąø”ą¹‰ą¹€ąø‚ą¹‰ąø²ąø–ąø¶ąø‡ąø—ąø£ąø±ąøžąø¢ąø²ąøąø£ąøąø„ąø²ąø‡ąøą¹ˆąø­ąø™ ą¹ąø„ą¹‰ąø§ą¹ƒąø«ą¹‰ąø­ąøµąøą¹€ąø—ąø£ąø”ąø–ąø¶ąø‡ą¹„ąø›ąø­ą¹ˆąø²ąø™ąø„ą¹ˆąø²ą¹ƒąø™ąø—ąø£ąø±ąøžąø¢ąø²ąøąø£ąøąø„ąø²ąø‡ą¹€ąø›ą¹‡ąø™ąø„ąø³ąø”ąø±ąøšąø–ąø±ąø”ą¹„ąø› ąø”ąø±ąø‡ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąø‚ą¹‰ąø²ąø‡ąø„ą¹ˆąø²ąø‡ąø™ąøµą¹‰

// 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 ą¹€ąøžąø·ą¹ˆąø­ąø”ąø²ą¹ąøŖąø”ąø‡ąøœąø„ą¹„ąø”ą¹‰

ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąø—ąøµą¹ˆ 8

ą¹ąøŖąø”ąø‡ąøąø²ąø£ąø›ąø£ąø°ąø¢ąøøąøąø•ą¹Œąøąø²ąø£ąøŖąø·ą¹ˆąø­ąøŖąø²ąø£ąøœą¹ˆąø²ąø™ąø£ąø°ąøšąøšą¹€ąø„ąø£ąø·ąø­ąø‚ą¹ˆąø²ąø¢ąø”ą¹‰ąø§ąø¢ąø§ąø“ąø˜ąøµąøąø²ąø£ 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?