Question

Exercise 4 – Passing an argument to a new thread and reporting on execution times================...

Exercise 4 – Passing an argument to a new thread and reporting on execution times=====================================================================

Here are some miscellaneous items on threads and resource usage times. This is a very short exercise to learn how to pass an argument to a thread’s start function.

Write a program called threadArgA.c that simply does the following:

  1. Pass a simple integer to a thread’s start function at thread creation time.
  2. ii) The thread function will assign this argument as variable int i (just like the example in the notes) and use the variable to iterate something for i times, such as the following trivial code:

while ( i > 0) { printf ("Integer i has the value %d \n", i ); i--; }

  1. The thread then exits

Now, for a final step. Before your main() function finishes it will find out how much CPU processing time was used in user space and kernel space while threadArgA was running. To achieve this use relevant code from the example cputime.c below. Now, suggest and write a fragment of code that is more interesting than the while() statement above to demonstrate use of processing time etc.

// cputime.c
// Small demo to show usage of getrusage() to measure CPU time
// DH 19/Sept/2012 ver 0.0.2
//////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<sys/resource.h>
#include <unistd.h>
// Declare ru as a struct rusage;
struct rusage ru;
int main(void) {
//////////////////////////////////////////////////////////////////
// Just some silly code to use up a few seconds of CPU time
printf ("\n A dummy program is running .... PLEASE WAIT!!! \n\n");
float y;
int x,v;
for (v=1;v<500;v++){
for (x=1;x<1000000;x++){
y++;
}
}
//////////////////////////////////////////////////////////////////
getrusage(RUSAGE_SELF, &ru);
printf ("Usage results for this program are:\n\n");
printf("User space CPU time used: %6lu %6lu\n", ru.ru_utime.tv_sec, ru.ru_utime.tv_usec );
printf("Kernel CPU time used: %6lu %6lu\n", ru.ru_stime.tv_sec, ru.ru_stime.tv_usec );
return 0;
}

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

threadArgA.c

************************

#include<stdio.h>

#include<stdlib.h>

#include<sys/time.h>

#include<sys/resource.h>

#include<unistd.h>

#include<pthread.h>

struct rusage ru;

//The Function to be executed by thread

void *myThread(void *vargp)

{

//Store the value argument passed to this thread

int *myid = (int *)vargp;

//Print the argument

printf("Thread ID :%d\n", *myid);

}

int main(void)

{

int i;

pthread_t tid;

for(i = 0; i < 1; i++)

{

//Create the thread

pthread_create(&tid, NULL, myThread, (void *)&tid);

}

while( i > 0)

{

printf(" Integer i has the value %d\n", i);

i--;

}

getrusage(RUSAGE_SELF, &ru);

printf(" Usage results for this program are : \n\n");

printf(" User space CPU time used : %6lu %6lu\n", ru.ru_utime.tv_sec, ru.ru_utime.tv_usec);

printf("Kernel CPU time used : %6lu %6lu\n", ru.ru_stime.tv_sec, ru.ru_stime.tv_usec);

pthread_exit(NULL);

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Exercise 4 – Passing an argument to a new thread and reporting on execution times================...
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
  • Please change this 2 thread program to a 4 thread program. /*compile as "gcc thread.c -pthread"...

    Please change this 2 thread program to a 4 thread program. /*compile as "gcc thread.c -pthread" */ #include <pthread.h> #include <stdio.h> #include <time.h> #define SIZE 100000000 //100 M //#define SIZE 10000 #define NUMBER_OF_TIMES 100 float a[SIZE]; typedef struct {        int start;        int end;        double sum;        int pid;    } range; void *thread_function(void *arg) //define a function that will be executed from //within a thread {    range *incoming = (range *) arg;...

  • /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from...

    /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from various connections * in real time. * * Student: */ #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <sys/socket.h> #include <sys/un.h> #include <stdlib.h> #include <pthread.h> #include "message-lib.h" // forward declarations int usage( char name[] ); // a function to be executed by each thread void * recv_log_msgs( void * arg ); // globals int log_fd; // opened by main() but accessible...

  • IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two...

    IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two arguments (IP add & port no. of the concurrent Server with thread - conServThread.c). Similarly, modify the Server (conServThread.c) program to accept an argument which is the port number of the server to bind and listen to. Try these two updated programs (server and client) with a port number (e.g., hhmm6) with current time where hh is hours in 24-hour format and mm is...

  • I am supposed to write documentation and report for the code below but I am new...

    I am supposed to write documentation and report for the code below but I am new to operating system concepts I will appreciate if someone can help make a detailed comment on each line of code for better understanding. Thanks #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <ctype.h> #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) struct thread_info...

  • In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer...

    In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer to the standard output. The Producer Accepts multiple consumer requests and processes each request by creating the following four threads. The reader thread will read an input file, one line at a time. It will pass each line of input to the character thread through a queue...

  • #include <stdio.h> #include <sys/time.h> #define DEBUG 1 //Constant is defined for true int main() { struct timeval start,end; //to store time of starting and ending of program //if state...

    #include <stdio.h> #include <sys/time.h> #define DEBUG 1 //Constant is defined for true int main() { struct timeval start,end; //to store time of starting and ending of program //if statement is added if(DEBUG){ gettimeofday(&start,0); } printf("Time in microsecond for every second: \n"); //output for(int i=0;i<100;i++) //outer loop to run 100 times { struct timeval loopstart,loopend; //to store time of starting and ending of loop //if statement added if(DEBUG){ gettimeofday(&loopstart,0); //measuring time at start of loop } for(int j=0;j<1000000;j++); //outer loop to...

  • program in C - Starter code below //In this assignment, we practice call by reference. //Below...

    program in C - Starter code below //In this assignment, we practice call by reference. //Below description of call by reference is from the following link //https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm //The call by reference method of passing arguments to a function copies //the address of an argument into the formal parameter. Inside the function, //the address is used to access the actual argument used in the call. //It means the changes made to the parameter affect the passed argument. //We use an example...

  • 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...

  • //In this assignment, we use multiple threads to calculate the frequencies of the first digits //of...

    //In this assignment, we use multiple threads to calculate the frequencies of the first digits //of the numbers in an array of long integers #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <assert.h> //#define NUM_THREADS 2 #define DIGITS 10 #define MAX 100000000 unsigned long a[MAX]; struct thread_data { int thread_num; int i, j;                           //the staring and ending index unsigned long freq[DIGITS];           // results }; //initialize the array void init_array(unsigned...

  • 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...

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