Question

Modify the program q1.c by adding calls to uthread_join (and making no other changes) so that...

Modify the program q1.c by adding calls to uthread_join (and making no other changes) so that it always prints the lines “zero” to “three” in order; i.e., its output must always be the following.

q1.c code as below:

#include <stdlib.h>
#include <stdio.h>
#include "uthread.h"

uthread_t t0, t1, t2;

void randomStall() {
int i, r = random() >> 16;
while (i++<r);
}

void* p0(void* v) {
randomStall();
printf("zero\n");
return NULL;
}

void* p1(void* v) {
randomStall();
printf("one\n");
return NULL;
}

void* p2(void* v) {
randomStall();
printf("two\n");
return NULL;
}

int main(int arg, char** arv) {
uthread_init(4);
t0 = uthread_create(p0, NULL);
t1 = uthread_create(p1, NULL);
t2 = uthread_create(p2, NULL);
randomStall();
printf("three\n");
printf("------\n");
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Concept of Thread Join

a calling thread i.e the one which is executing the current statement, will wait until the called thread finish executing its function

e.g:

thread2.start(); // do more stuff here thread2.join(); System.out.println("Done");

in the above code the current thread i.e which will be executing these statements will for thread2 to finish its work and that will print Done

Modified Source Code

#include <stdlib.h>

#include <stdio.h>

#include "uthread.h"

uthread_t t0, t1, t2;

void randomStall() {

int i, r = random() >> 16;

while (i++<r);

}

void* p0(void* v) {

randomStall();

printf("zero\n");

return NULL;

}

void* p1(void* v) {

randomStall();

printf("one\n");

return NULL;

}

void* p2(void* v) {

randomStall();

printf("two\n");

return NULL;

}

int main(int arg, char** arv) {

uthread_init(4);

t0 = uthread_create(p0, NULL);

//make main thread wait for t0 to finish its work

t0.uthread_join();

t1 = uthread_create(p1, NULL);

//make main thread wait for t1 to finish its work

t1.uthread_join();

t2 = uthread_create(p2, NULL);

//make main thread wait for t2 to finish its work

t2.uthread_join();

randomStall();

//main thread print three after t0, t1, t2 has already done printing their text

printf("three\n");

printf("------\n");

}

Add a comment
Know the answer?
Add Answer to:
Modify the program q1.c by adding calls to uthread_join (and making no other changes) so that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the program q4.c to use a mutex and one or more condition variables to order the threads to produce the same output as your solution to the first question, where you ordered threads using uthread_join. Your solution must use condition variables to

    #include <stdlib.h>#include <stdio.h>#include "uthread.h"#include "uthread_mutex_cond.h"uthread_t t0, t1, t2;void randomStall() {int i, r = random() >> 16;while (i++<r);}void* p0(void* v) {randomStall();printf("zero\n");return NULL;}void* p1(void* v) {randomStall();printf("one\n");return NULL;}void* p2(void* v) {randomStall();printf("two\n");return NULL;}int main(int arg, char** arv) {uthread_init(4);t0 = uthread_create(p0, NULL);t1 = uthread_create(p1, NULL);t2 = uthread_create(p2, NULL);randomStall();uthread_join (t0, NULL);uthread_join (t1, NULL);uthread_join (t2, NULL);printf("three\n");printf("------\n");}

  • 1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer...

    1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • If you already answer this question, please skip, thanks C Programming. Fill in ... This program...

    If you already answer this question, please skip, thanks C Programming. Fill in ... This program will be called with one command line argument that contains a string followed by an asterisk and an integer. Print out the string as many time as indicated by the integer. For example, when called as prog Hi*3, you print HiHiHi. Hint: Look for the '*' starting from the back of the string. len = strlen(arg) gives you the string length. When you have...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • Read given code RaceOrNot3.c and write all possible outputs of the program. Assume there will be...

    Read given code RaceOrNot3.c and write all possible outputs of the program. Assume there will be no thread creation or joining failures or semaphore failures. If you believe there is only one possible output, you just need to write that output. #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <errno.h> sem_t s1; int c=0,x=0; void *UpdateC1(void *arg) {    int i;    for(i=0;i<2000000;i++)   {        sem_wait(&s1);        c++;   x++;        sem_post(&s1);    } } void *UpdateC2(void *arg) {    int i,x=0;    for(i=0;i<2999999;i++)   {        sem_wait(&s1);        c++;   x++;       ...

  • Read given code RaceOrNot1.c and write all possible outputs of the program. Assume there will be...

    Read given code RaceOrNot1.c and write all possible outputs of the program. Assume there will be no thread creation or joining failures or mutex failures. If you believe there is only one possible output, you just need to write that output. #include <stdio.h> #include <stdlib.h> #include <pthread.h> pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t count_mutex3 = PTHREAD_MUTEX_INITIALIZER; int c[2] = {1,0}; void *UpdateC1(void *arg) {    int i;    for(i=0;i<1000000;i++)    {        pthread_mutex_lock(&count_mutex);        c[0]=(c[0]+1)%2;        c[1]=(c[1]+1)%2;        pthread_mutex_unlock(&count_mutex);    }    return NULL; } void *UpdateC2(void *arg) {    int...

  • Compile and run the deadlockFree.c program below and report if this program does go into deadlock...

    Compile and run the deadlockFree.c program below and report if this program does go into deadlock or not. Modify this deadlockFree.c program so that you change the lock/unlock order of the mutexes. Experiment with the order of locks (and maybe extend the number of loop iterations) until you find a scenario where you can demonstrate deadlock. //Program: deadlockFree.c A simple threaded program to demonstrate the deadlock condition.Two threads are used to wait on two MUTEXES. Careful ordering of locks should...

  • a multi-threaded producer / consumer program without any locking or signaling. It therefore has synchronization problems....

    a multi-threaded producer / consumer program without any locking or signaling. It therefore has synchronization problems. Add locks and signals so that it works correctly. /* Homework 5.X */ /* Robin Ehrlich */ /* compile: gcc Homework5.c -lpthread */ #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> struct class {    struct class *next;    int id;    int grade; }; #define SLEEP_TIME 1 #define MAX_PRODUCE 10 static struct class *classHead = NULL; static struct class *classTail = NULL; static...

  • ANSWER ASAP PLEASE IN C Duplicate the program below to a new Program 2, and modify...

    ANSWER ASAP PLEASE IN C Duplicate the program below to a new Program 2, and modify Program 2 such that it generates n random virtual addresses between 0 and 232-1 and computes the page number and offset for each address. Here, do not write to the console. Instead, run your program with n = 1000000 random virtual addresses and compute the total CPU time of the task. Report your findings, as well as how you ran your program. Provide commentary...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT