Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume that the compiler performs no optimization and that all code runs on the

ID: 3734086 • Letter: A

Question

Assume that the compiler performs no optimization and that all code runs on the processor exactly as written. Also assume that no library calls will fail. #include 0001: 0002: #include 0003: #de fine NUM THREADS 4 0004: char array [NUM THREADS+21: 0005: int pos =0; 0006: char outs [9] # 0011 : 0012: 0013: 0014 char outs[index]: 0015 : 0016: return NULL 0017: 0018: int main() 0019: void * work (void* id) int index (* ( (int*) id)) *2; = array [pos++] = c; 0020: char three'z 0021: int i; 0022: pthread t tid [NUM THREADS] 0023 : for (i 0; í

Explanation / Answer

Program:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 4
char array[NUM_THREADS+2];
int pos = 0;
char outs[9] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
void * work(void* id)
{
int index = (*((int*)id)*2);
char c = outs[index];
array[pos++] = c;
return NULL;
}
int main()
{
char three = 'z';
int i;
pthread_t tid[NUM_THREADS];
for(i =0;i<NUM_THREADS;i++)
pthread_create(&(tid[i]), NULL, work, (void*)(&i));
for(i=0;i<NUM_THREADS;i++)
pthread_join(tid[i], NULL);
array[pos++] = three;
array[pos]='';
printf(" %d ",strlen(array));
exit(0);
}

Output: The above program doesnot print any value

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote