rite a program that tests whether the threads of a process are executed concurrently or not. The created threads should call a function that contains an infinite loop that prints out the ID of the thread and the ID of the process to which the thread belongs to.
#include <pthread.h>
#include <stdio.h>
void* thFunction(void* arg)
{
int* val = (int*) arg;
int id = pthread_self();
int i;
for (i =0; i<5; i++)
{
printf(”Thread with id %d\n”, id);
sleep(2);
}
}
int main()
{
pthread_t th1;
pthread_t th2;
pthread_t th3;
int arg1 = 1;
int arg2 = 1;
int arg3 = 1;
int id = pthread_self();
printf(”Main thread with id %d\n”, id);
pthread_create(&th1, NULL, thFunction, &arg1);
pthread_create(&th2, NULL, thFunction, &arg2);
pthread_create(&th3, NULL, thFunction, &arg3);
pthread_join(th1, NULL);
pthread_join(th3, NULL);
pthread_join(th2, NULL);
return 0;
}
Using the program written for the previous problem, test the two different reactions of a thread to a cancel request coming from another thread through the call of the pthread_cancel function.
#include <pthread.h>
#include <stdio.h>
void* thFunction(void* arg)
{
int* val = (int*) arg;
int id = pthread_self();
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
int i;
for (i =0; i<5; i++)
{
printf(”Thread with id %d\n”, *val);
sleep(1);
}
}
int main()
{
pthread_t th1;
pthread_t th2;
pthread_t th3;
int arg1 = 1;
int arg2 = 2;
int arg3 = 3;
int id = pthread_self();
printf(”Main thread with id %d\n”, id);
pthread_create(&th1, NULL, thFunction, &arg1);
pthread_create(&th2, NULL, thFunction, &arg2);
pthread_create(&th3, NULL, thFunction, &arg3);
pthread_cancel(th1);
pthread_join(th1, NULL);
pthread_join(th3, NULL);
pthread_join(th2, NULL);
return 0;
}
Write a program that specifies the maximum number of threads that can simultaneously exist in the same process. To avoid overloading the processor, the created threads will call the function sleep inside an infinite loop. To find the wanted number, the value returned by the pthread_create function is tested, so to detect the moment when no more threads can be created.
#include <pthread.h>
#include <stdio.h>
void* thFunction(int arg)
{
printf(”%d\n”,arg);
sleep(100);
}
int main()
{
int count =0;
pthread_t th1;
int arg1 = 1;
while (1) //thread successfuly created
{
arg1++;
printf(”-%d\n”,arg1);
if (pthread_create(&th1, NULL, thFunction, arg1) == 0)
count++;
else
{
printf(”the maximum number is %d\n”, count);
return 0;
}
}
return 0;
}
Write a server type C program that periodically creates threads that simulate the handling of some requests from clients. The server threads display a message, wait for a while (using sleep function) and then terminate. In the same time, the server also accepts commands introduced from keyboard. Implement the stop command of the server, like when a special key is pressed (for example ‘x’): the process should not create new threads and end. But ending the process should be done in a clean state, only after the termination of the existing threads at that time.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int a = 1;
void* test()
{
char ch;
scanf(”%c”,&ch);
if(ch==’\n’)
a = 0;
else
a = 1;
}
void* thFunction(void* arg)
{
int i;
printf(”new client request\n”);
pthread_t th;
a = 1;
for ( i= 0; i<3; i++)
{
printf(”the thread is at the second %d\n”, i);
pthread_create(&th, NULL, test, &i);
sleep(1);
pthread_cancel(th);
if(a==0)
return 0;
}
}
int main()
{
int count =0;
pthread_t th1[50];
int arg1 = 1;
char ch;
int* state[30];
int i=0;
while (1)
{
printf(”enter x for terminating the process.”);
scanf(”%c”, &ch);
if (ch != ‘x’)
{
scanf(”%c”, &ch);
arg1++;
if (pthread_create(&th1[i], NULL, thFunction, &arg1) != 0) //thread unsuccessfuly created
return 0;
pthread_join(th1[i], NULL);
i++;
}
else
{
printf(”\nchar =%c\n”, ch);
exit(0);
}
}
return 0;
}
Modify Problem 4, such that the threads that simulate the handling of the client requests to execute in an infinite loop intensive computational operations (for example calculating the first N prime numbers or just doing “nothing”). When a key is pressed, the server has to display the number of threads created till that moment. Follow how the computational threads influence the reaction time of the thread that reads from the keyboard (the interactive one).
#include <pthread.h>
#include <stdio.h>
int count = 0;
int a;
char ch = ‘a’;
void* thFunction(void* arg)
{
sleep(10);
//count–;
}
void* test()
{
char ch1;
scanf(”%c”,&ch);
if(ch==’x')
a = 0;
else
a = 1;
}
int main()
{
pthread_t th1;
int arg1 = 1;
printf(”enter x for terminating the process.\n”);
while (1) //thread successfuly created
{
a = 1;
if( pthread_create(&th1, NULL, test, &a) !=0 )
{
printf(”stack full ( at %d ), cannot read key\n”,count);
a = 0;
}
else
{
sleep(1);
pthread_cancel(th1);
}
if(ch == ‘l’)
printf(”the number is %d\n”, count);
if (a)
{
arg1++;
int i;
for(i=1;i<100;i++)
if (pthread_create(&th1, NULL, thFunction, &arg1) == 0)
count++;
}
else
return 0;
}
return 0;
}
Test the behavior of different threads of the same process that simultaneously access functions that read from the keyboard.
#include <pthread.h>
#include <stdio.h>
void* thFunction(void* arg)
{
int number;
int id = pthread_self();
int i;
printf(”Thread with id %d\n”, id);
printf(”input a number for this thread\n”);
scanf(”%d”, &number);
sleep(2);
printf(”the thread with number %d”, number);
}
int main()
{
pthread_t th1;
pthread_t th2;
pthread_t th3;
int number;
int arg1 = 1;
int arg2 = 1;
int arg3 = 1;
char ch;
int id = pthread_self();
printf(”Main thread with id %d\n”, id);
printf(”\n”, id);
pthread_create(&th1, NULL, thFunction, &arg1);
pthread_create(&th2, NULL, thFunction, &arg2);
pthread_create(&th3, NULL, thFunction, &arg3);
printf(”input a number for the main thread\n”);
scanf(”%d”, &number);
printf(”the theard with number %d\n”, number);
sleep(12);
// pthread_join(th1, NULL);
// pthread_join(th3, NULL);
// pthread_join(th2, NULL);
return 0;
}
Test the effect of the pthread_join function called by the only active thread of a child process, in order to wait till the end of a thread in the parent process, existing there when function fork is called, but not active in the child process. The question is if the active thread will be blocked in the pthread_join or not; if not, what does that function
returns?
#include <pthread.h>
#include <stdio.h>
void* thFunction(void* arg)
{
int* val = (int*) arg;
int id = pthread_self();
printf(”Thread with id %d\n”, id);
sleep(100);
}
int main()
{
int pid;
int arg1=1;
pthread_t th1;
int id = pthread_self();
pthread_create(&th1, NULL, thFunction, &arg1);
printf(”thread created\n”);
pid=fork();
if (pid == 0) //we are in the child process
{
printf(”in the child\n”);
pthread_join(th1, NULL);
printf(”after join\n”);
}
else
if (pid >0) // we are in the parent
{
int j=0;
while(j<5)
{
printf(”in the parent\n”);
j++;
}
exit(0);
}
}
EXAMPLE CANCEL
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#define MAXSTEPS 10
char name1[]=”T1″;
char name2[]=”T2″;
void* execThread1(void* threadName)
{
int i;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE);
for (i=1; i<=MAXSTEPS; i++){
sleep(1);
printf(”Thread %s at step %d\n”, threadName, i);
}
return number;
}
void* execThread1(void* threadName)
{
int i;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE);
for (i=1; i<=MAXSTEPS; i++){
sleep(1);
printf(”Thread %s at step %d\n”, threadName, i);
}
return number;
}
main()
{
pthread_t t1, t2;
printf(”Starting main thread [%d] with pid=%d\n”, pthread_self(), getpid());
if (pthread_create(&t1, NULL, execThread, name1)<0) {
perror(”Error creating a new thread”);
exit(1);
}
if (pthread_create(&t2, NULL, execThread, name2)<0) {
perror(”Error creating a new thread”);
exit(1);
}
printf(”Main thread created threads [%d, %d]\n”, t1, t2);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf(”Thread %s[%d] terminated with state %d\n”, name1, t1, *(state[0]));
printf(”Thread %s[%d] terminated with state %d\n”, name2, t2, *(state[1]));
}
EXAMPLE CLEAN PUSH
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
typedef struct m {
int size;
void* pMem;
} MEM;
void allocate_mem(void* arg) {
MEM* p = (MEM*) arg;
p->pMem = malloc(p->size);
if (p->Mem == NULL)
p->size = 0;
printf(”Thread %d has allocated % bytes of memory.\n”, pthread_self(), p->size);
}
void release_mem(void* arg) {
MEM* p = (MEM*) arg;
if (p->pMem)
free(p->pMem);
printf(”Thread %d has released % bytes of memory.\n”, pthread_self(), p->size);
}
void* thFunction(void* arg){
int oldType, i;
MEM thMem;
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldType);
thMem.size = 10 * sizeof(int);
thMem.pMem = NULL;
pthread_cleanup_push(release_mem, (void *) &thMem);
allocate_mem(&thMem);
// do some work with the memory
if (thMem != NULL)
for (i=0; i<10; i++) {
thMem.pMem[i] = i;
sleep(1);
}
pthread_cleanup_pop(1);
pthread_setcanceltype(oldType, NULL);
}
main()
{
int i;
pthread_t th[5];
for (i=1; i<=5; i++)
pthread_create(&th[i], NULL, thFunction, NULL);
// cancel the threads
for (i=1; i<=5; i++) {
usleep(100);
pthread_cancel(th[i]);
}
// wait the threads to cancel (finish)
for (i=1; i<=5; i++)
pthread_join(th[i], NULL);
}
EXAMPLE CREATE THREAD
#include <pthread.h>
#include <stdio.h>
void* thFunction(void* arg) {
int* val = (int*) arg;
printf(”Thread with argument %d\n”, *val);
}
main() {
pthread_t th1;
int arg1 = 1;
pthread_create(&th1, NULL, thFunction, &arg1);
pthread_join(th1, NULL);
}
EXAMPLE CREATE JOIN
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#define MAXSTEPS 10
char name1[]=”T1″;
char name2[]=”T2″;
void* execThread(void* threadName)
{
int i;
int* number = new int;
char* tmp;
printf(”Starting thread %s [%d] with pid=%d\n”, threadName, pthread_self(), getpid());
for (i=1; i<=MAXSTEPS; i++){
sleep(1);
printf(”Thread %s at step %d\n”, threadName, i);
}
tmp = & ((char*)threadName)[strlen((char*)threadName)-1];
*number = atoi(tmp);
return number;
}
main()
{
pthread_t t1, t2;
int* state[2];
printf(”Starting main thread [%d] with pid=%d\n”, pthread_self(), getpid());
if (pthread_create(&t1, NULL, execThread, name1)<0) {
perror(”Error creating a new thread”);
exit(1);
}
if (pthread_create(&t2, NULL, execThread, name2)<0) {
perror(”Error creating a new thread”);
exit(1);
}
printf(”Main thread created threads [%d, %d]\n”, t1, t2);
pthread_join(t1, (void**) &state[0]);
pthread_join(t2, (void**) &state[1]);
printf(”Thread %s[%d] terminated with state %d\n”, name1, t1, *(state[0]));
printf(”Thread %s[%d] terminated with state %d\n”, name2, t2, *(state[1]));
}
EXAMPLE GETPID GETTHREAD
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
void* execThread(void *arg)
{
printf(”Starting child thread [tid=%d] with [pid=%d]\n”, pthread_self(), getpid());
sleep(2);
}
int main()
{
pthread_t t1;
printf(”Starting process [pid=%d]\n”, getpid());
printf(”Starting main thread [tid=%d] with [pid=%d]\n”, pthread_self(), getpid());
pthread_create(&t1, NULL, execThread, NULL);
printf(”Running main thread [tid=%d] with [pid=%d]\n”, pthread_self(), getpid());
sleep(2);
}