0% found this document useful (0 votes)
192 views15 pages

Labprog Vtu

The document describes 7 programs related to multithreading and inter-process communication in C on Linux: 1. A multithreading program that creates 9 child threads to print messages. 2. A program demonstrating the fork() system call to create a parent and child process. 3. A program implementing an anonymous pipe for data sharing between parent and child processes. 4. A multithreaded program that creates two threads with different priorities. 5. A program that creates two threads, with one changing the priority of the other. 6. A program creating a main thread and child thread with default stack sizes. 7. A program creating threads with user-defined stack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views15 pages

Labprog Vtu

The document describes 7 programs related to multithreading and inter-process communication in C on Linux: 1. A multithreading program that creates 9 child threads to print messages. 2. A program demonstrating the fork() system call to create a parent and child process. 3. A program implementing an anonymous pipe for data sharing between parent and child processes. 4. A multithreaded program that creates two threads with different priorities. 5. A program that creates two threads, with one changing the priority of the other. 6. A program creating a main thread and child thread with default stack sizes. 7. A program creating threads with user-defined stack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

PROGRAM-01

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

void *Chlidthread(void *arg)

printf("I am in thread number #%ld\n",(long)arg);

usleep(50000);

pthread_exit(NULL);

int main()

longi;

pthread_tmy_thread[N];

for(i = 1; i<= N; i++) {


int ret = pthread_create(&my_thread[i], NULL,

&Chlidthread, (void*)i);

if(ret != 0) {

printf("Error: pthread_create() failed\n");

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)

printf("error in creating a thread");

else if(ret_val == 0)

printf ("pid of child is %d \n",getpid());

printf("pid of parent is %d \n",getppid());

printf("I am child process \n");

else if (ret_val> 0)

printf ("pid of parent is %d \n",getpid());


printf("pid of its parent is %d \n",getppid());

printf("I am parent process \n");

wait();

return 0;

RESULT:

Pid of its parents is 2534

I am parent process

Pid of child is 2575

Pid of parents 2574

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;

ret=pipe(myPipe); // create the pipe

if(ret==-1)

perror("pipe");

exit(1);

pid = fork();

/* child process */

if(pid == 0)

close(myPipe[0]); // close unused read end

write(myPipe[1], " Welcome to RTOS LAB ",sizeof("Welcome to RTOS


LAB"));
printf("Child process sent ' Welcome to RTOS LAB '\n");

close(myPipe[1]);

/* parent process */

else

char buffer[512];

close(myPipe[1]); // close unused write end

int length = read(myPipe[0], buffer, 512);

buffer[length] = '\0';

printf("Parent process received '%s'\n", buffer);

close(myPipe[0]);

return 0;

RESULT:

“welcome to RTOS lab”


PROGRAM-04

4. Implement the multithread application satisfying the following: To create two


child threads with different priority and prints its respective priority.

#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;

void * testThread(void *arg)

printf("Inside thread 1\n");

printf ("Priority of Thread1--- %d \n", (long) arg);

usleep(50000);

pthread_exit(NULL);

void * testThread1(void *arg)

printf("Inside thread 2 \n");

printf ("Priority of Thread2--- %d \n", (long) arg);


usleep(50000);

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, &param);

pthread_create(&thread1, &attr,&testThread,(int *)param.sched_priority);

pthread_setschedprio(thread1,i);

param.sched_priority =22;

pthread_create(&thread2,&attr,&testThread1,(int *)param.sched_priority);

pthread_join( thread1, NULL);

pthread_exit(NULL);

RESULT:

Inside thread 1

Inside thread 2

Priority of Thread1--- 20

Priority of Thread2--- 22
PROGRAM-05

5. Implement the multithread application satisfying the following:

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;

void * testThread(void *arg)

printf("Inside thread 1\n");

printf ("Priority of Thread1--- %d \n", (long) arg);

usleep(50000);

pthread_exit(NULL);

void * testThread1(void *arg)

{
printf("Inside thread 2 \n");

printf ("Priority of Thread2--- %d \n", (long) arg);

printf ("Priority of Thread1--- %d \n",i);

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, &param);

pthread_create(&thread1, &attr,&testThread,(int *)param.sched_priority);

pthread_setschedprio(thread1,i);

param.sched_priority =22;

pthread_create(&thread2,&attr,&testThread1,(int *)param.sched_priority);

pthread_join( thread1, NULL);

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;

void * func(int id)

printf("THIS is Hello Thread.. \n");

printf("THREADID:%ld\n",pthread_self());

pthread_attr_init(&tattr);

pthread_attr_setstacksize(&tattr,stacksize1);

pthread_attr_getstacksize(&tattr, &stacksize1);

printf("child_stack =%zd\n", 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

THIS is Hello Thread..

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>

size_t stacksize1 = 0x5500 ;

pthread_attr_ttattr;

void * func(int id)

printf("THIS is Hello Thread.. \n");

printf("THREADID:%ld\n",pthread_self());

pthread_attr_init(&tattr);

pthread_attr_setstacksize(&tattr,stacksize1);

pthread_attr_getstacksize(&tattr, &stacksize1);

printf("child_stack =%zd\n", 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

THIS is Hello Thread..

THREADID:139666768594688

child_stack =21760

You might also like