… 10│ struct myIO {FILE *in; FILE *out;}; 11│ void* threadFunc(void *num); … 17│ struct { 18│ pthread_mutex_t mtx; 19│ char text[256]; 20│ } item[5]; 21│ 22│ int main(int argc, char **argv) { … 28│ for (intptr_t i = 1; i < 5; ++i) { 29│ pthread_t tid; 30│ pthread_create(&tid, NULL, threadFunc, (void *)i); 31│ pthread_detach(tid); 32│ } 33│ threadFunc(0); 34│ } 35│ 36│ void *threadFunc(void* numRaw) { 37│ intptr_t num = (intptr_t) numRaw; 38│ struct myIO win = openWin(num); … 48│ while (1) { 49│ fprintf(win.out, "> "); 50│ 51│ char line[1024]; 52│ fgets(line, 1024, win.in); 53│ line[strlen(line)-1] = 0; 54│ 55│ int argOne = atoi(line); … 61│ char *argTwoTxt = strchr(line, ' '); 62│ 63│ if (!argTwoTxt) { 65│ pthread_mutex_lock(&item[argOne].mtx); 66│ fprintf(win.out, "T#%ld reads %d as: %s\n", num, argOne, item[argOne].text); 67│ pthread_mutex_unlock(&item[argOne].mtx); 68│ continue; 69│ } 70│ 71│ argTwoTxt++; 72│ char *e; 73│ int argTwo = strtol(argTwoTxt, &e, 10); 74│ 75│ if (!*e && argTwo < 5 && argTwo >= 0 && argOne != argTwo) { 77│ pthread_mutex_lock(&item[argOne].mtx); 78│ pthread_mutex_lock(&item[argTwo].mtx); 79│ fprintf(win.out, "T#%ld copies %d to %d\n", num, argTwo, argOne); 80│ memcpy(item[argOne].text, item[argTwo].text, sizeof(item[argOne].text)); 81│ pthread_mutex_unlock(&item[argTwo].mtx); 82│ pthread_mutex_unlock(&item[argOne].mtx); 83│ } else { 85│ pthread_mutex_lock(&item[argOne].mtx); 86│ fprintf(win.out, "T#%ld assigns to %d the value: %s\n", num, argOne, argTwoTxt); 87│ memset(item[argOne].text, 0, sizeof(item[argOne].text)); 88│ strncpy(item[argOne].text, argTwoTxt, sizeof(item[argOne].text) - 1); 89│ pthread_mutex_unlock(&item[argOne].mtx); 90│ } 91│ } 92│ } …