Westonci.ca offers quick and accurate answers to your questions. Join our community and get the insights you need today. Get quick and reliable solutions to your questions from a community of experienced experts on our platform. Get immediate and reliable solutions to your questions from a community of experienced professionals on our platform.

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?

Sagot :

The thing which the given program accomplishes is that it creates a method, declares variables, and executes commands if they meet the conditions in the code.

What is a Conditional Statement?

This is a type of statement that executes a line of code if a condition is not met.

Some types of conditional statements are:

  • IF statement
  • IF-ELSE statement
  • Nested If-else statement.
  • If-Else If ladder.
  • Switch statement.

No, that is not the desired output because the integer should be less than 20.

Read more about conditional statements here:

https://brainly.com/question/11073037

#SPJ1