Pthreads, Operating System

Assignment Help:

A thread is defined as an independent stream of instructions that can be scheduled to run as such by the operating system. Each program contains a number of threads which can be running in parallel.

The program  intro.c is a demonstration of using  pthread_create(thread,attr,start_routine,arg) function to create a new thread. The parameter thread is used as a way to pass variable between the main programs to the thread. The  attr parameter is used to set the thread attributes, which is usually set to NULL. The start_routine parameter is the C routine being executed when the thread starts. The arg parameter is the argument passed to the routine.

A thread is terminated using the function pthread_exit(NULL).
 
In the beginning of the main program a  thread_id of 1 is assigned to the first thread via the thread_data structure that is declared at the beginning of the program. This is used to identify which process is being run by the processor. Then the thread is created using the pthread_create() function with the rc variable which is used to identified the problem if the thread fail to be created. The first thread will execute the PrintProcess1 routine when the thread is created. After the first thread is created we create the second thread with a thread_id of 2 using the same procedure, but this time the PrintProcess2 routine will be used for the second thread.

In each of the PrintProcess routine the thread_id will be passed onto a process_id variable via the threadid data structure. Then the process will print a message saying the process is running, then delay by the number of seconds that is decided by the process_id, and display a message saying the process is finished. The pthread_exit(NULL) function will be used to terminate the current thread.


Related Discussions:- Pthreads

How program execute in operating system, Q. How Program execute in operatin...

Q. How Program execute in operating system? Program execution: Operating system loads a program in memory and executes the program. The program should be able to end its exec

Rf, whta is an operating system ? what sorts services are provided by an op...

whta is an operating system ? what sorts services are provided by an operating system ?

Data races, Research the phenomenon of data races. Give an example of how...

Research the phenomenon of data races. Give an example of how an unprotected data race can give rise to data inconsistency. How do OpenMP and Cilk resolve this problem? Data

Buffer, What is bounded buffer

What is bounded buffer

Deadlock resoloution technique, justify the role played by the operating sy...

justify the role played by the operating system in managing the process to avoid deadlock

Dual mode operation, WHAT ARE THE ADVANTAGES AND DISADVANTAGES OF DUAL MODE...

WHAT ARE THE ADVANTAGES AND DISADVANTAGES OF DUAL MODE OPERATION OF OPERATING SYSTEM>?

Explain segmentation and paging, Why are segmentation and paging sometimes ...

Why are segmentation and paging sometimes combined into one scheme? Segmentation and paging are often combined in order to improve upon each other. Segmented paging is helpful

Explain contiguous allocation, Contiguous allocation The contiguous-all...

Contiguous allocation The contiguous-allocation method needs each file to occupy a set of contiguous blocks of the disk. Disk addresses explain a linear ordering on the disk. T

Explain the advantages and drawback of multiple processes, Explain the Adva...

Explain the Advantages and Drawback of Multiple Processes Application architected to take advantage of multiple processes will certainly see performance gains. Functions and Fe

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd