subject

Consider the following code using the POSIX Pthreads API: thread2.c
#include
#include
#include
#include
int myglobal;
void *thread_function(void *arg) {
int i, j;
for ( i=0; i<20; i++ ) {
j=myglobal;
j=j+1;
printf(".");
fflush(stdout);
sleep(1);
myglobal=j;
}
return NULL;
}
int main(void) {
pthread_t mythread;
int i;
if ( pthread_create( &mythread, NULL, thread_function,
NULL) ) {
printf(ldquo;error creating thread.");
abort();
}
for ( i=0; i<20; i++) {
myglobal=myglobal+1;
printf("o");
fflush(stdout);
sleep(1);
}
if ( pthread_join ( mythread, NULL ) ) {
printf("error joining thread.");
abort();
}
printf("\nmyglobal equals %d\n",myglobal);
exit(0);
}
In main() we first declare a variable called mythread, which has a type of pthread_t. This is essentially an ID for a thread. Next, the if statement creates a thread associated with mythread. The call pthread_create() returns zero on success and a nonzero value on failure. The third argument of pthread_create() is the name of a function that the new thread will execute when it starts. When this thread_function() returns, the thread terminates. Meanwhile, the main program itself defines a thread, so that there are two threads executing. The pthread_join function enables the main thread to wait until the new thread completes.
a. What does this program accomplish?
b. Here is the output from the executed program:
$ ./thread2
..o. o.o. o.oo. o.o. o.o. o.o. o.o. o..o. o.o. o.o
myglobal equals 21
Is this the output you would expect? If not, what has gone wrong?

ansver
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 24.06.2019 00:30, bsonicx
The best definition of an idiom is a. a word or phrase that describes a noun b. a word or phrase describing a verb c. a phrase containing figurative language in which the word expresses a different idea from its exact meaning d. a phrase that compares two unlike objects or ideas
Answers: 2
image
Computers and Technology, 24.06.2019 15:30, jhony70
What type of forensic evidence was recovered during the bomb set off at the new mexico facility on the video that was similar to the evidence obtained at the boston bombings and how did the evidence allow the researchers to connect other pieces of evidence to the same bomb?
Answers: 2
image
Computers and Technology, 24.06.2019 15:30, sonyfan
What is the total number of time zones that can be configured to show by default in a calendar in outlook 2016?
Answers: 1
image
Computers and Technology, 24.06.2019 22:30, juicyx39
In writing a paper for his english class, gavin quoted an author of the book. what should he include in his paper to credit the source? citation caption header entry
Answers: 1
You know the right answer?
Consider the following code using the POSIX Pthreads API: thread2.c
#include
#include

Questions in other subjects:

Konu
English, 04.02.2021 02:30
Konu
Mathematics, 04.02.2021 02:30