Friday, May 15, 2015

Add Listen ports to apache in ubuntu

http://www.cyberciti.biz/faq/linux-apache2-change-default-port-ipbinding/

Question: How do I change Apache 2 default port under Debian / Ubuntu Linux? I've couple of public IPv4 address assigned by my ISP, how do I force Apache2 to listen to a specific IP address? How do I change Apache2 IP address binding?
Answer: You can easily change the port and other settings using following directives:


Task: Change Apache port

Open /etc/apache2/ports.conf file, enter:
# vi /etc/apache2/ports.conf
OR
$ sudo vi /etc/apache2/ports.conf
To make the server accept connections on both port 8010, enter:
Listen 8010
To make the server accept connections on both port 80 and port 8010, use:
Listen 80
Listen 8010

Task: Accept connections on specific IP / interface port

You need to use network interfaces IP address. For example, to make the server accept connections on IP 202.54.1.2 and port 80, enter:
Listen 202.54.1.2:80
To make the server accept connections on two specified interfaces and port numbers, enter:
Listen 202.54.2.1:80
Listen 202.54.2.5:8010

Save and close the file. Restart Apache 2 webserver:
# /etc/init.d/apache2 restart
OR
$ sudo /etc/init.d/apache2 restart

How do I verify port and IP binding working ?

Use netstat command to find out if Apache is listening on a specific port or not, use:
# netstat -tulpn
# netstat -tulpn | grep :80
# netstat -tulpn | grep :8010
# netstat -tulpn| grep 202.54.1.2:80

Sample output:
tcp        0      0 202.54.1.2:80            0.0.0.0:*               LISTEN  


Friday, May 1, 2015

Problem of Thread examples

Example Thread Problems
pthread create - create a new thread
int pthread create(pthread t * thread, pthread attr t * attr, void * (*start routine)(void
*), void * arg);
pthread self - return identifier of current thread
pthread t pthread self(void);
pthread exit - terminate the calling thread
void pthread exit(void *retval);
pthread join - wait for termination of another thread
int pthread join(pthread t th, void **thread return);
pthread detach - put a running thread in the detached state
int pthread detach(pthread t th);
pthread cancel - thread cancellation
int pthread cancel(pthread t thread);
Semaphores
int sem init(sem t *sem, int pshared, unsigned int value);
int sem wait(sem t * sem); /* P */
int sem post(sem t * sem); /* V */
Problem 1:
Consider the following programs which uses threads. Assume that all system calls return normally. Circle
the programs which are guaranteed to print out 42.
Program 1
void *thread(void *vargp)
{
pthread_exit((void*)42);
}
int main()
{
int i;
pthread_t tid;
pthread_create(&tid, NULL, thread, NULL);
pthread_join(tid, (void **)&i);
printf("%d\n",i);
}
Page 1 of 6
Program 2
void *thread(void *vargp)
{
exit(42);
}
int main()
{
int i;
pthread_t tid;
pthread_create(&tid, NULL, thread, NULL);
pthread_join(tid, (void **)&i);
printf("%d\n",i);
}
Program 3
void *thread(void *vargp)
{
int *ptr = (int*)vargp;
pthread_exit((void*)*ptr);
}
void *thread2(void *vargp)
{
int *ptr = (int*)vargp;
*ptr = 0;
pthread_exit((void*)31);
}
int main()
{
int i = 42;
pthread_t tid, tid2;
pthread_create(&tid, NULL, thread, (void*)&i);
pthread_create(&tid2, NULL, thread2, (void*)&i);
pthread_join(tid, (void**)&i);
pthread_join(tid2, NULL);
printf("%d\n",i);
}
Page 2 of 6
Program 4
void *thread(void *vargp)
{
pthread_detach(pthread_self());
pthread_exit((void*)42);
}
int main()
{
int i = 0;
pthread_t tid;
pthread_create(&tid, NULL, thread, (void*)&i);
pthread_join(tid, (void**)&i);
printf("%d\n",i);
}
Program 5
int i = 42;
void *thread(void *vargp)
{
printf("%d\n",i);
}
void *thread2(void *vargp)
{
i = 31;
}
int main()
{
pthread_t tid, tid2;
pthread_create(&tid2, NULL, thread2, (void*)&i);
pthread_create(&tid, NULL, thread, (void*)&i);
pthread_join(tid, (void**)&i);
pthread_join(tid2, NULL);
}
Page 3 of 6
Problem 2:
This problem tests your understanding of race conditions in concurrent programs.
Consider a simple concurrent program with the following specification: The main thread creates two peer
threads, passing each peer thread a unique integer thread ID (either 0 or 1), and then waits for each thread
to terminate. Each peer thread prints its thread ID and then terminates.
Each of the following programs attempts to implement this specification. However, some are incorrect
because they contain a race on the value of myid that makes it possible for one or more peer threads to print
an incorrect thread ID. Except for the race, each program is otherwise correct.
You are to indicate whether or not each of the following programs contains such a race on the value of
myid.
A. Does the following program contain a race on the value of myid? Yes No
void *foo(void *vargp) {
int myid;
myid = *((int *)vargp);
Free(vargp);
printf("Thread %d\n", myid);
}
int main() {
pthread_t tid[2];
int i, *ptr;
for (i = 0; i < 2; i++) {
ptr = Malloc(sizeof(int));
*ptr = i;
Pthread_create(&tid[i], 0, foo, ptr);
}
Pthread_join(tid[0], 0);
Pthread_join(tid[1], 0);
}
Page 4 of 6
B. Does the following program contain a race on the value of myid? Yes No
void *foo(void *vargp) {
int id;
id = *((int *)vargp);
printf("Thread %d\n", id);
}
int main() {
pthread_t tid[2];
int i;
for (i = 0; i < 2; i++)
Pthread_create(&tid[i], NULL, foo, &i);
Pthread_join(tid[0], NULL);
Pthread_join(tid[1], NULL);
}
C. Does the following program contain a race on the value of myid? Yes No
void *foo(void *vargp) {
int id;
id = (int)vargp;
printf("Thread %d\n", id);
}
int main() {
pthread_t tid[2];
int i;
for (i = 0; i < 2; i++)
Pthread_create(&tid[i], 0, foo, i);
Pthread_join(tid[0], 0);
Pthread_join(tid[1], 0);
}
Page 5 of 6
D. Does the following program contain a race on the value of myid? Yes No
sem_t s; /* semaphore s */
void *foo(void *vargp) {
int id;
id = *((int *)vargp);
V(&s);
printf("Thread %d\n", id);
}
int main() {
pthread_t tid[2];
int i;
sem_init(&s, 0, 0); /* S=0 INITIALLY */
for (i = 0; i < 2; i++) {
Pthread_create(&tid[i], 0, foo, &i);
P(&s);
}
Pthread_join(tid[0], 0);
Pthread_join(tid[1], 0);
}
E. Does the following program contain a race on the value of myid? Yes No
sem_t s; /* semaphore s */
void *foo(void *vargp) {
int id;
P(&s);
id = *((int *)vargp);
V(&s);
printf("Thread %d\n", id);
}
int main() {
pthread_t tid[2];
int i;
sem_init(&s, 0, 1); /* S=1 INITIALLY */
for (i = 0; i < 2; i++) {
Pthread_create(&tid[i], 0, foo, &i);
}
Pthread_join(tid[0], 0);
Pthread_join(tid[1], 0);
}
Page 6 of 6