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?

Answers

Answer 1

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 statementIF-ELSE statementNested 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


Related Questions

Other Questions
what should you most consider when deciding Can you explain why number 3 is correct?Q19. Which waves would be found at the surface at regions A and C?1. P waves2. S waves3. No wavesThe correct answer was number 3. But I dont know why :( Type the correct answer in the box. Use numerals instead of words. If necessary, use / for the fraction bar. is parallel to . and are perpendicular to and .The ratio of the lengths of and is : . Consider the following reversible reaction:2 CO(g) + O(g) 2CO(g). Describe a way to increase the CO concentration at equilibrium. Explain your reasoning. Jane earns an annual salary of 14000 and receivesa pay rise of 1.8%Lenny earns an annual salary of 11000 andreceives a pay rise of 1.7%Find which of the employees has the larger payincrease and by how much. An explosion casualty suffering from crush injuries of the lower extremities has been admitted to your hospital with severe hyperkalemia. What emergency treatment is NOT recommended the price of a barrel oil currently cost $120. Next month if the price increases by 1/12 of the current cost, then what would be the new cost of a barrel of oil? What is the value of the expression below? 2[3(16+)]-8 Morrison is thinking about becoming a therapist. He is a social learner. Is this a good career for him?A. No, therapists generally work alone.B. No, social learning does not mean good social skills.C. Yes, social learners are good at seeing other perspectives.D. Yes, there is a lot of math and science in becoming a therapist. What kind of sentence is "Although Mary isn't fond of reading, she read Harper Lee's To Kill A Mockingbird, and she found out fascinating" is it a simple sentence, complex sentence, compound sentence, or compound-complex sentence the parable of the rainbow colors The cumulative work of many scientists can often be summarized as theories or laws. how is a theory different from a law ( the average power transferred to the solar panels by sunlight is 26 000 w calculate the average energy transferred to the solar panels in 30 seconde use the equations energy transferred = power time What is the first step in summarizing a plot? Calculate 5.1234 + 0.033 1.650 and report to the correct number of significant figures. The band is raising money for their spring trip. they need to raise at least $1500. earlier in the year, they made $575 on their annual fruit sale. now they are selling donuts, where they will earn $5 profit for each box of donuts they sell. which inequality could be used in order to determine the number of boxes of donuts, d, they must sell in order to meet their goal? In the statement 10 +(-10) =0, how would you describe the -10? What kind of mimicry is it when houseflies try to imitate wasps?When houseflies try to imitate wasps, it is an example of mimicry. In the photoPls pls pls pls pls pls pls pls pls pls Select the correct answer.Teams with varying numbers of players are playing a group card game. The time taken by each team to complete one game is given in the table.What is the relationship between the two variables?Number of Players on the Team Time Taken to Complete Game (in minutes)322263201458OA positive linear associationOB. exponential relationshipOC. negative linear associationO D.no relationship