Labprog Vtu
Labprog Vtu
1. Create „n‟ number of child threads. Each thread prints the message “I’m in
thread number ...” and sleeps for 50 ms and then quits. The main thread waits
for complete execution of all the child threads and then quits. Compile and
execute in Linux.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define N 9
usleep(50000);
pthread_exit(NULL);
int main()
longi;
pthread_tmy_thread[N];
&Chlidthread, (void*)i);
if(ret != 0) {
exit(EXIT_FAILURE);
pthread_exit(NULL);
RESULT:
I m in thread number #1
I m in thread number #5
I m in thread number #3
I m in thread number #2
I m in thread number #7
I m in thread number #4
I m in thread number #6
I m in thread number #8
I m in thread number #9
PROGRAM-02
2.Demonstrate fork( ) system call. Let the parent process display itspid, ppid
and a message ‘I’m the parent’. Also let the child display itspid, ppid and a
message ‘I’m the child’.
#include<stdio.h>
#include<unistd.h>
int main()
intret_val;
ret_val = fork();
if(ret_val< 0)
else if(ret_val == 0)
else if (ret_val> 0)
wait();
return 0;
RESULT:
I am parent process
I am chid process
PROGRAM-03
3. Implement the usage of anonymous pipe with 512 bytes for data sharing
between parent and child processes using handle inheritance mechanism.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
pid_tpid;
intmyPipe[2];
int ret;
if(ret==-1)
perror("pipe");
exit(1);
pid = fork();
/* child process */
if(pid == 0)
close(myPipe[1]);
/* parent process */
else
char buffer[512];
buffer[length] = '\0';
close(myPipe[0]);
return 0;
RESULT:
#include<pthread.h>
#include<stdio.h>
#include<unistd.h>
#include<stdint.h>
#include<error.h>
inti;
pthread_attr_tattr;
pthread_t thread1;
pthread_t thread2;
structsched_paramparam;
usleep(50000);
pthread_exit(NULL);
pthread_exit(NULL);
int main ()
pthread_attr_init(&attr);
param.sched_priority =20;
i=param.sched_priority;
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
pthread_attr_setschedparam(&attr, ¶m);
pthread_setschedprio(thread1,i);
param.sched_priority =22;
pthread_create(&thread2,&attr,&testThread1,(int *)param.sched_priority);
pthread_exit(NULL);
RESULT:
Inside thread 1
Inside thread 2
Priority of Thread1--- 20
Priority of Thread2--- 22
PROGRAM-05
Two child threads are created, thread 2 prints the priority of the thread 1 and
rises its priority to above normal and retrieves the new priority of thread 1,
prints it and then quits.
#include<pthread.h>
#include<stdio.h>
#include<unistd.h>
#include<stdint.h>
#include<error.h>
inti;
pthread_attr_tattr;
pthread_t thread1;
pthread_t thread2;
structsched_paramparam;
usleep(50000);
pthread_exit(NULL);
{
printf("Inside thread 2 \n");
param.sched_priority=i+10;
pthread_setschedprio(thread1,param.sched_priority);
printf("Priority of Thread1-----%d\n",param.sched_priority);
pthread_exit(NULL);
int main ()
pthread_attr_init(&attr);
param.sched_priority =20;
i=param.sched_priority;
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
pthread_attr_setschedparam(&attr, ¶m);
pthread_setschedprio(thread1,i);
param.sched_priority =22;
pthread_create(&thread2,&attr,&testThread1,(int *)param.sched_priority);
pthread_exit(NULL);
}
RESULT
Priorityof thread1----------20
Inside thread2
Priority of thread2------------22
Priority of thread1------------20
Priority of thread1------------30
PROGRAM-06
6. WAP to create main thread and child thread with default stack size.\
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
size_t stacksize1;
pthread_attr_ttattr;
printf("THREADID:%ld\n",pthread_self());
pthread_attr_init(&tattr);
pthread_attr_setstacksize(&tattr,stacksize1);
pthread_attr_getstacksize(&tattr, &stacksize1);
pthread_attr_destroy(&tattr);
int main()
pthread_ttid;
int ret;
size_tstacksize;
pthread_attr_init(&tattr);
pthread_attr_setstacksize(&tattr,stacksize);
pthread_attr_getstacksize(&tattr, &stacksize);
printf("parent_stack=%zd\n", stacksize);
pthread_create(&tid, &tattr,&func,NULL);
sleep(1);
pthread_attr_destroy(&tattr);
return 0;
RESULT:
parent_stack=4196592
THREADID:140628164354016
child_stack =10485760
PROGRAM-07
7. WAP to create main thread and child thread with user defined stack size.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
pthread_attr_ttattr;
printf("THREADID:%ld\n",pthread_self());
pthread_attr_init(&tattr);
pthread_attr_setstacksize(&tattr,stacksize1);
pthread_attr_getstacksize(&tattr, &stacksize1);
pthread_attr_destroy(&tattr);
int main()
pthread_ttid;
int ret;
size_tstacksize = 0x4000;
pthread_attr_init(&tattr);
pthread_attr_setstacksize(&tattr,stacksize);
pthread_attr_getstacksize(&tattr, &stacksize);
printf("parent_stack=%zd\n", stacksize);
pthread_create(&tid, &tattr,&func,NULL);
sleep(1);
pthread_attr_destroy(&tattr);
return 0;
RESULT:
parent_stack=16384
THREADID:139666768594688
child_stack =21760